Debian Codegraph @

tests/tests.rs

raw symbols SCIP
1 // Copyright (c) The buf-list Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 use buf_list::BufList;
5 use bytes::{Buf, Bytes};
6 use std::{io::IoSlice, ops::Deref};
7
8 #[test]
9 fn test_basic() {
10 let mut buf_list = vec![&b"hello"[..], &b"world"[..]]
11 .into_iter()
12 .collect::<BufList>();
13 println!("{:?}", buf_list);
14 assert_eq!(buf_list.num_bytes(), 10);
15 assert_eq!(buf_list.num_chunks(), 2);
16
17 let chunk = buf_list.push_chunk(&b"foo"[..]);
18 assert_eq!(buf_list.num_bytes(), 13);
19 assert_eq!(buf_list.num_chunks(), 3);
20 assert_eq!(chunk, &b"foo"[..]);
21
22 // Try inserting a zero-length chunk. This should not count as a chunk.
23 buf_list.push_chunk(&[] as &[u8]);
24 assert_eq!(buf_list.num_bytes(), 13);
25 assert_eq!(buf_list.num_chunks(), 3);
26
27 {
28 let mut buf_list = buf_list.clone();
29
30 assert_eq!(buf_list.chunk(), &b"hello"[..]);
31
32 // Advance by 2. This won't consume the first chunk.
33 buf_list.advance(2);
34 assert_eq!(buf_list.num_bytes(), 11);
35 assert_eq!(buf_list.num_chunks(), 3);
36 assert_eq!(buf_list.chunk(), &b"llo"[..]);
37
38 // Advance by 3. This will consume the "hello" chunk exactly.
39 buf_list.advance(3);
40 assert_eq!(buf_list.num_bytes(), 8);
41 assert_eq!(buf_list.num_chunks(), 2);
42 assert_eq!(buf_list.chunk(), &b"world"[..]);
43
44 // Advance by 6. This will consume the "world" chunk + the first byte of "foo".
45 buf_list.advance(6);
46 assert_eq!(buf_list.num_bytes(), 2);
47 assert_eq!(buf_list.num_chunks(), 1);
48 assert_eq!(buf_list.chunk(), &b"oo"[..]);
49
50 // Advance by 2. This will consume the "foo" chunk exactly.
51 buf_list.advance(2);
52 assert_eq!(buf_list.num_bytes(), 0);
53 assert_eq!(buf_list.num_chunks(), 0);
54 assert_eq!(buf_list.chunk(), &[] as &[u8]);
55 }
56
57 {
58 // Test chunks_vectored.
59 let mut chunks_vectored = vec![IoSlice::new(&[]); 5];
60 let ret = buf_list.chunks_vectored(&mut chunks_vectored);
61 assert_eq!(ret, 3);
62 assert_eq!(chunks_vectored[0].deref(), &b"hello"[..]);
63 assert_eq!(chunks_vectored[1].deref(), &b"world"[..]);
64 assert_eq!(chunks_vectored[2].deref(), &b"foo"[..]);
65 assert_eq!(chunks_vectored[3].deref(), &[] as &[u8]);
66
67 // Test chunks_vectored with a smaller buffer.
68 let mut chunks_vectored = vec![IoSlice::new(&[]); 2];
69 let ret = buf_list.chunks_vectored(&mut chunks_vectored);
70 assert_eq!(ret, 2);
71 assert_eq!(chunks_vectored[0].deref(), &b"hello"[..]);
72 assert_eq!(chunks_vectored[1].deref(), &b"world"[..]);
73
74 // Test with an empty buffer.
75 let mut chunks_vectored = vec![];
76 let ret = buf_list.chunks_vectored(&mut chunks_vectored);
77 assert_eq!(ret, 0);
78 }
79
80 {
81 // Test copy_to_bytes.
82 let mut buf_list = buf_list.clone();
83
84 // Copy the first two bytes -- this should just be a refcount.
85 let bytes = buf_list.copy_to_bytes(2);
86 assert_eq!(bytes, &b"he"[..]);
87 assert_eq!(buf_list.num_bytes(), 11);
88 assert_eq!(buf_list.num_chunks(), 3);
89
90 // Copy the next 3 bytes. This should consume the buffer.
91 let bytes = buf_list.copy_to_bytes(3);
92 assert_eq!(bytes, &b"llo"[..]);
93 assert_eq!(buf_list.num_bytes(), 8);
94 assert_eq!(buf_list.num_chunks(), 2);
95
96 // Copy 6 bytes. This should consume the next buffer.
97 let bytes = buf_list.copy_to_bytes(6);
98 assert_eq!(bytes, &b"worldf"[..]);
99 assert_eq!(buf_list.num_bytes(), 2);
100 assert_eq!(buf_list.num_chunks(), 1);
101
102 // Copy the last 2 bytes + this next buffer -- ensure that all buffers are consumed.
103 buf_list.push_chunk(&b"bar"[..]);
104 let bytes = buf_list.copy_to_bytes(5);
105 assert_eq!(bytes, &b"oobar"[..]);
106 assert_eq!(buf_list.num_bytes(), 0);
107 assert_eq!(buf_list.num_chunks(), 0);
108 }
109 }
110
111 #[test]
112 fn test_iter() {
113 let buf_list = vec![&b"hello"[..], &b"world"[..], &b"foo"[..], &b"bar"[..]]
114 .into_iter()
115 .collect::<BufList>();
116
117 // Test iteration over the buf_list.
118 let mut iter = (&buf_list).into_iter();
119 println!("{:?}", iter);
120 assert_eq!(iter.next(), Some(&Bytes::from_static(&b"hello"[..])));
121 assert_eq!(iter.size_hint(), (3, Some(3)));
122 assert_eq!(iter.len(), 3);
123
124 {
125 let mut iter = iter.clone();
126 #[allow(clippy::iter_nth_zero)]
127 {
128 assert_eq!(iter.nth(0), Some(&Bytes::from_static(&b"world"[..])));
129 }
130 assert_eq!(iter.last(), Some(&Bytes::from_static(&b"bar"[..])));
131 }
132
133 {
134 let iter = iter.clone();
135 let v = iter.fold(vec![], |mut v, item| {
136 v.push(item);
137 v
138 });
139 assert_eq!(
140 v,
141 vec![
142 &Bytes::from_static(&b"world"[..]),
143 &Bytes::from_static(&b"foo"[..]),
144 &Bytes::from_static(&b"bar"[..]),
145 ]
146 );
147 }
148
149 {
150 let mut iter = iter.clone();
151 assert_eq!(iter.next_back(), Some(&Bytes::from_static(&b"bar"[..])));
152 assert_eq!(
153 iter.rfold(vec![], |mut v, item| {
154 v.push(item);
155 v
156 }),
157 vec![
158 &Bytes::from_static(&b"foo"[..]),
159 &Bytes::from_static(&b"world"[..]),
160 ]
161 );
162 }
163 }
164
165 #[test]
166 fn test_into_iter() {
167 let buf_list = vec![&b"hello"[..], &b"world"[..], &b"foo"[..], &b"bar"[..]]
168 .into_iter()
169 .collect::<BufList>();
170
171 let into_iter = buf_list.into_iter();
172 println!("{:?}", into_iter);
173 #[allow(clippy::redundant_clone)]
174 let mut into_iter = into_iter.clone();
175 assert_eq!(into_iter.next(), Some(Bytes::from_static(&b"hello"[..])));
176 assert_eq!(into_iter.size_hint(), (3, Some(3)));
177 assert_eq!(into_iter.len(), 3);
178 assert_eq!(into_iter.next_back(), Some(Bytes::from_static(&b"bar"[..])));
179 }
180
181 #[test]
182 #[should_panic = "`len` (12) greater than remaining (10)"]
183 fn test_copy_to_bytes_panic() {
184 let mut buf_list = vec![&b"hello"[..], &b"world"[..]]
185 .into_iter()
186 .collect::<BufList>();
187 buf_list.copy_to_bytes(12);
188 }