summaryrefslogtreecommitdiffstats
path: root/tokio-buf/tests/collect.rs
blob: f9bb401cc57cf48b3ee8b98367cecb6c2ff6dba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![cfg(feature = "util")]

use bytes::Bytes;
use futures::Future;
use tokio_buf::BufStreamExt;

mod support;
use support::*;

macro_rules! test_collect_impl {
    ($t:ty $(, $capacity:ident)*) => {
        // While unfortunate, this test makes some assumptions on vec's resizing
        // behavior.
        //
        // Collect one
        //
        let bs = one("hello world");

        let vec: $t = bs.collect().wait().unwrap();

        assert_eq!(vec, &b"hello world"[..]);
        $( assert_eq!(vec.$capacity(), 64); )*

        // Collect one, with size hint
        //
        let mut bs = one("hello world");
        bs.size_hint.set_lower(11);

        let vec: $t = bs.collect().wait().unwrap();

        assert_eq!(vec, &b"hello world"[..]);
        $( assert_eq!(vec.$capacity(), 64); )*

        // Collect one, with size hint
        //
        let mut bs = one("hello world");
        bs.size_hint.set_lower(10);

        let vec: $t = bs.collect().wait().unwrap();

        assert_eq!(vec, &b"hello world"[..]);
        $( assert_eq!(vec.$capacity(), 64); )*

        // Collect many
        //
        let bs = list(&["hello", " ", "world", ", one two three"]);

        let vec: $t = bs.collect().wait().unwrap();

        assert_eq!(vec, &b"hello world, one two three"[..]);
    }
}

#[test]
fn collect_vec() {
    test_collect_impl!(Vec<u8>, capacity);
}

#[test]
fn collect_bytes() {
    test_collect_impl!(Bytes);
}