1 | #![warn (rust_2018_idioms)] |
2 | |
3 | use bytes::{Buf, BufMut, Bytes}; |
4 | #[cfg (feature = "std" )] |
5 | use std::io::IoSlice; |
6 | |
7 | #[test] |
8 | fn collect_two_bufs() { |
9 | let a = Bytes::from(&b"hello" [..]); |
10 | let b = Bytes::from(&b"world" [..]); |
11 | |
12 | let res = a.chain(b).copy_to_bytes(10); |
13 | assert_eq!(res, &b"helloworld" [..]); |
14 | } |
15 | |
16 | #[test] |
17 | fn writing_chained() { |
18 | let mut a = [0u8; 64]; |
19 | let mut b = [0u8; 64]; |
20 | |
21 | { |
22 | let mut buf = (&mut a[..]).chain_mut(&mut b[..]); |
23 | |
24 | for i in 0u8..128 { |
25 | buf.put_u8(i); |
26 | } |
27 | } |
28 | |
29 | for i in 0..64 { |
30 | let expect = i as u8; |
31 | assert_eq!(expect, a[i]); |
32 | assert_eq!(expect + 64, b[i]); |
33 | } |
34 | } |
35 | |
36 | #[test] |
37 | fn iterating_two_bufs() { |
38 | let a = Bytes::from(&b"hello" [..]); |
39 | let b = Bytes::from(&b"world" [..]); |
40 | |
41 | let res: Vec<u8> = a.chain(b).into_iter().collect(); |
42 | assert_eq!(res, &b"helloworld" [..]); |
43 | } |
44 | |
45 | #[cfg (feature = "std" )] |
46 | #[test] |
47 | fn vectored_read() { |
48 | let a = Bytes::from(&b"hello" [..]); |
49 | let b = Bytes::from(&b"world" [..]); |
50 | |
51 | let mut buf = a.chain(b); |
52 | |
53 | { |
54 | let b1: &[u8] = &mut []; |
55 | let b2: &[u8] = &mut []; |
56 | let b3: &[u8] = &mut []; |
57 | let b4: &[u8] = &mut []; |
58 | let mut iovecs = [ |
59 | IoSlice::new(b1), |
60 | IoSlice::new(b2), |
61 | IoSlice::new(b3), |
62 | IoSlice::new(b4), |
63 | ]; |
64 | |
65 | assert_eq!(2, buf.chunks_vectored(&mut iovecs)); |
66 | assert_eq!(iovecs[0][..], b"hello" [..]); |
67 | assert_eq!(iovecs[1][..], b"world" [..]); |
68 | assert_eq!(iovecs[2][..], b"" [..]); |
69 | assert_eq!(iovecs[3][..], b"" [..]); |
70 | } |
71 | |
72 | buf.advance(2); |
73 | |
74 | { |
75 | let b1: &[u8] = &mut []; |
76 | let b2: &[u8] = &mut []; |
77 | let b3: &[u8] = &mut []; |
78 | let b4: &[u8] = &mut []; |
79 | let mut iovecs = [ |
80 | IoSlice::new(b1), |
81 | IoSlice::new(b2), |
82 | IoSlice::new(b3), |
83 | IoSlice::new(b4), |
84 | ]; |
85 | |
86 | assert_eq!(2, buf.chunks_vectored(&mut iovecs)); |
87 | assert_eq!(iovecs[0][..], b"llo" [..]); |
88 | assert_eq!(iovecs[1][..], b"world" [..]); |
89 | assert_eq!(iovecs[2][..], b"" [..]); |
90 | assert_eq!(iovecs[3][..], b"" [..]); |
91 | } |
92 | |
93 | buf.advance(3); |
94 | |
95 | { |
96 | let b1: &[u8] = &mut []; |
97 | let b2: &[u8] = &mut []; |
98 | let b3: &[u8] = &mut []; |
99 | let b4: &[u8] = &mut []; |
100 | let mut iovecs = [ |
101 | IoSlice::new(b1), |
102 | IoSlice::new(b2), |
103 | IoSlice::new(b3), |
104 | IoSlice::new(b4), |
105 | ]; |
106 | |
107 | assert_eq!(1, buf.chunks_vectored(&mut iovecs)); |
108 | assert_eq!(iovecs[0][..], b"world" [..]); |
109 | assert_eq!(iovecs[1][..], b"" [..]); |
110 | assert_eq!(iovecs[2][..], b"" [..]); |
111 | assert_eq!(iovecs[3][..], b"" [..]); |
112 | } |
113 | |
114 | buf.advance(3); |
115 | |
116 | { |
117 | let b1: &[u8] = &mut []; |
118 | let b2: &[u8] = &mut []; |
119 | let b3: &[u8] = &mut []; |
120 | let b4: &[u8] = &mut []; |
121 | let mut iovecs = [ |
122 | IoSlice::new(b1), |
123 | IoSlice::new(b2), |
124 | IoSlice::new(b3), |
125 | IoSlice::new(b4), |
126 | ]; |
127 | |
128 | assert_eq!(1, buf.chunks_vectored(&mut iovecs)); |
129 | assert_eq!(iovecs[0][..], b"ld" [..]); |
130 | assert_eq!(iovecs[1][..], b"" [..]); |
131 | assert_eq!(iovecs[2][..], b"" [..]); |
132 | assert_eq!(iovecs[3][..], b"" [..]); |
133 | } |
134 | } |
135 | |
136 | #[test] |
137 | fn chain_growing_buffer() { |
138 | let mut buff = [' ' as u8; 10]; |
139 | let mut vec = b"wassup" .to_vec(); |
140 | |
141 | let mut chained = (&mut buff[..]).chain_mut(&mut vec).chain_mut(Vec::new()); // Required for potential overflow because remaining_mut for Vec is isize::MAX - vec.len(), but for chain_mut is usize::MAX |
142 | |
143 | chained.put_slice(b"hey there123123" ); |
144 | |
145 | assert_eq!(&buff, b"hey there1" ); |
146 | assert_eq!(&vec, b"wassup23123" ); |
147 | } |
148 | |
149 | #[test] |
150 | fn chain_overflow_remaining_mut() { |
151 | let mut chained = Vec::<u8>::new().chain_mut(Vec::new()).chain_mut(Vec::new()); |
152 | |
153 | assert_eq!(chained.remaining_mut(), usize::MAX); |
154 | chained.put_slice(&[0; 256]); |
155 | assert_eq!(chained.remaining_mut(), usize::MAX); |
156 | } |
157 | |
158 | #[test] |
159 | fn chain_get_bytes() { |
160 | let mut ab = Bytes::copy_from_slice(b"ab" ); |
161 | let mut cd = Bytes::copy_from_slice(b"cd" ); |
162 | let ab_ptr = ab.as_ptr(); |
163 | let cd_ptr = cd.as_ptr(); |
164 | let mut chain = (&mut ab).chain(&mut cd); |
165 | let a = chain.copy_to_bytes(1); |
166 | let bc = chain.copy_to_bytes(2); |
167 | let d = chain.copy_to_bytes(1); |
168 | |
169 | assert_eq!(Bytes::copy_from_slice(b"a" ), a); |
170 | assert_eq!(Bytes::copy_from_slice(b"bc" ), bc); |
171 | assert_eq!(Bytes::copy_from_slice(b"d" ), d); |
172 | |
173 | // assert `get_bytes` did not allocate |
174 | assert_eq!(ab_ptr, a.as_ptr()); |
175 | // assert `get_bytes` did not allocate |
176 | assert_eq!(cd_ptr.wrapping_offset(1), d.as_ptr()); |
177 | } |
178 | |