1 extern crate bytes;
2 extern crate iovec;
3 
4 use bytes::{Buf, BufMut, Bytes, BytesMut};
5 use bytes::buf::Chain;
6 use iovec::IoVec;
7 use std::io::Cursor;
8 
9 #[test]
collect_two_bufs()10 fn collect_two_bufs() {
11     let a = Cursor::new(Bytes::from(&b"hello"[..]));
12     let b = Cursor::new(Bytes::from(&b"world"[..]));
13 
14     let res: Vec<u8> = a.chain(b).collect();
15     assert_eq!(res, &b"helloworld"[..]);
16 }
17 
18 #[test]
writing_chained()19 fn writing_chained() {
20     let mut a = BytesMut::with_capacity(64);
21     let mut b = BytesMut::with_capacity(64);
22 
23     {
24         let mut buf = Chain::new(&mut a, &mut b);
25 
26         for i in 0..128 {
27             buf.put(i as u8);
28         }
29     }
30 
31     assert_eq!(64, a.len());
32     assert_eq!(64, b.len());
33 
34     for i in 0..64 {
35         let expect = i as u8;
36         assert_eq!(expect, a[i]);
37         assert_eq!(expect + 64, b[i]);
38     }
39 }
40 
41 #[test]
iterating_two_bufs()42 fn iterating_two_bufs() {
43     let a = Cursor::new(Bytes::from(&b"hello"[..]));
44     let b = Cursor::new(Bytes::from(&b"world"[..]));
45 
46     let res: Vec<u8> = a.chain(b).iter().collect();
47     assert_eq!(res, &b"helloworld"[..]);
48 }
49 
50 #[test]
vectored_read()51 fn vectored_read() {
52     let a = Cursor::new(Bytes::from(&b"hello"[..]));
53     let b = Cursor::new(Bytes::from(&b"world"[..]));
54 
55     let mut buf = a.chain(b);
56 
57     {
58         let b1: &[u8] = &mut [0];
59         let b2: &[u8] = &mut [0];
60         let b3: &[u8] = &mut [0];
61         let b4: &[u8] = &mut [0];
62         let mut iovecs: [&IoVec; 4] =
63             [b1.into(), b2.into(), b3.into(), b4.into()];
64 
65         assert_eq!(2, buf.bytes_vec(&mut iovecs));
66         assert_eq!(iovecs[0][..], b"hello"[..]);
67         assert_eq!(iovecs[1][..], b"world"[..]);
68         assert_eq!(iovecs[2][..], b"\0"[..]);
69         assert_eq!(iovecs[3][..], b"\0"[..]);
70     }
71 
72     buf.advance(2);
73 
74     {
75         let b1: &[u8] = &mut [0];
76         let b2: &[u8] = &mut [0];
77         let b3: &[u8] = &mut [0];
78         let b4: &[u8] = &mut [0];
79         let mut iovecs: [&IoVec; 4] =
80             [b1.into(), b2.into(), b3.into(), b4.into()];
81 
82         assert_eq!(2, buf.bytes_vec(&mut iovecs));
83         assert_eq!(iovecs[0][..], b"llo"[..]);
84         assert_eq!(iovecs[1][..], b"world"[..]);
85         assert_eq!(iovecs[2][..], b"\0"[..]);
86         assert_eq!(iovecs[3][..], b"\0"[..]);
87     }
88 
89     buf.advance(3);
90 
91     {
92         let b1: &[u8] = &mut [0];
93         let b2: &[u8] = &mut [0];
94         let b3: &[u8] = &mut [0];
95         let b4: &[u8] = &mut [0];
96         let mut iovecs: [&IoVec; 4] =
97             [b1.into(), b2.into(), b3.into(), b4.into()];
98 
99         assert_eq!(1, buf.bytes_vec(&mut iovecs));
100         assert_eq!(iovecs[0][..], b"world"[..]);
101         assert_eq!(iovecs[1][..], b"\0"[..]);
102         assert_eq!(iovecs[2][..], b"\0"[..]);
103         assert_eq!(iovecs[3][..], b"\0"[..]);
104     }
105 
106     buf.advance(3);
107 
108     {
109         let b1: &[u8] = &mut [0];
110         let b2: &[u8] = &mut [0];
111         let b3: &[u8] = &mut [0];
112         let b4: &[u8] = &mut [0];
113         let mut iovecs: [&IoVec; 4] =
114             [b1.into(), b2.into(), b3.into(), b4.into()];
115 
116         assert_eq!(1, buf.bytes_vec(&mut iovecs));
117         assert_eq!(iovecs[0][..], b"ld"[..]);
118         assert_eq!(iovecs[1][..], b"\0"[..]);
119         assert_eq!(iovecs[2][..], b"\0"[..]);
120         assert_eq!(iovecs[3][..], b"\0"[..]);
121     }
122 }
123