1 | //! Licensed under the Apache License, Version 2.0 |
2 | //! https://www.apache.org/licenses/LICENSE-2.0 or the MIT license |
3 | //! https://opensource.org/licenses/MIT, at your |
4 | //! option. This file may not be copied, modified, or distributed |
5 | //! except according to those terms. |
6 | #![no_std ] |
7 | |
8 | use core::iter; |
9 | use itertools as it; |
10 | use crate::it::Itertools; |
11 | use crate::it::interleave; |
12 | use crate::it::intersperse; |
13 | use crate::it::intersperse_with; |
14 | use crate::it::multizip; |
15 | use crate::it::free::put_back; |
16 | use crate::it::iproduct; |
17 | use crate::it::izip; |
18 | use crate::it::chain; |
19 | |
20 | #[test] |
21 | fn product2() { |
22 | let s = "αβ" ; |
23 | |
24 | let mut prod = iproduct!(s.chars(), 0..2); |
25 | assert!(prod.next() == Some(('α' , 0))); |
26 | assert!(prod.next() == Some(('α' , 1))); |
27 | assert!(prod.next() == Some(('β' , 0))); |
28 | assert!(prod.next() == Some(('β' , 1))); |
29 | assert!(prod.next() == None); |
30 | } |
31 | |
32 | #[test] |
33 | fn product_temporary() { |
34 | for (_x, _y, _z) in iproduct!( |
35 | [0, 1, 2].iter().cloned(), |
36 | [0, 1, 2].iter().cloned(), |
37 | [0, 1, 2].iter().cloned()) |
38 | { |
39 | // ok |
40 | } |
41 | } |
42 | |
43 | |
44 | #[test] |
45 | fn izip_macro() { |
46 | let mut zip = izip!(2..3); |
47 | assert!(zip.next() == Some(2)); |
48 | assert!(zip.next().is_none()); |
49 | |
50 | let mut zip = izip!(0..3, 0..2, 0..2i8); |
51 | for i in 0..2 { |
52 | assert!((i as usize, i, i as i8) == zip.next().unwrap()); |
53 | } |
54 | assert!(zip.next().is_none()); |
55 | |
56 | let xs: [isize; 0] = []; |
57 | let mut zip = izip!(0..3, 0..2, 0..2i8, &xs); |
58 | assert!(zip.next().is_none()); |
59 | } |
60 | |
61 | #[test] |
62 | fn izip2() { |
63 | let _zip1: iter::Zip<_, _> = izip!(1.., 2..); |
64 | let _zip2: iter::Zip<_, _> = izip!(1.., 2.., ); |
65 | } |
66 | |
67 | #[test] |
68 | fn izip3() { |
69 | let mut zip: iter::Map<iter::Zip<_, _>, _> = izip!(0..3, 0..2, 0..2i8); |
70 | for i in 0..2 { |
71 | assert!((i as usize, i, i as i8) == zip.next().unwrap()); |
72 | } |
73 | assert!(zip.next().is_none()); |
74 | } |
75 | |
76 | #[test] |
77 | fn multizip3() { |
78 | let mut zip = multizip((0..3, 0..2, 0..2i8)); |
79 | for i in 0..2 { |
80 | assert!((i as usize, i, i as i8) == zip.next().unwrap()); |
81 | } |
82 | assert!(zip.next().is_none()); |
83 | |
84 | let xs: [isize; 0] = []; |
85 | let mut zip = multizip((0..3, 0..2, 0..2i8, xs.iter())); |
86 | assert!(zip.next().is_none()); |
87 | |
88 | for (_, _, _, _, _) in multizip((0..3, 0..2, xs.iter(), &xs, xs.to_vec())) { |
89 | /* test compiles */ |
90 | } |
91 | } |
92 | |
93 | #[test] |
94 | fn chain_macro() { |
95 | let mut chain = chain!(2..3); |
96 | assert!(chain.next() == Some(2)); |
97 | assert!(chain.next().is_none()); |
98 | |
99 | let mut chain = chain!(0..2, 2..3, 3..5i8); |
100 | for i in 0..5i8 { |
101 | assert_eq!(Some(i), chain.next()); |
102 | } |
103 | assert!(chain.next().is_none()); |
104 | |
105 | let mut chain = chain!(); |
106 | assert_eq!(chain.next(), Option::<()>::None); |
107 | } |
108 | |
109 | #[test] |
110 | fn chain2() { |
111 | let _ = chain!(1.., 2..); |
112 | let _ = chain!(1.., 2.., ); |
113 | } |
114 | |
115 | #[test] |
116 | fn write_to() { |
117 | let xs = [7, 9, 8]; |
118 | let mut ys = [0; 5]; |
119 | let cnt = ys.iter_mut().set_from(xs.iter().copied()); |
120 | assert!(cnt == xs.len()); |
121 | assert!(ys == [7, 9, 8, 0, 0]); |
122 | |
123 | let cnt = ys.iter_mut().set_from(0..10); |
124 | assert!(cnt == ys.len()); |
125 | assert!(ys == [0, 1, 2, 3, 4]); |
126 | } |
127 | |
128 | #[test] |
129 | fn test_interleave() { |
130 | let xs: [u8; 0] = []; |
131 | let ys = [7u8, 9, 8, 10]; |
132 | let zs = [2u8, 77]; |
133 | let it = interleave(xs.iter(), ys.iter()); |
134 | it::assert_equal(it, ys.iter()); |
135 | |
136 | let rs = [7u8, 2, 9, 77, 8, 10]; |
137 | let it = interleave(ys.iter(), zs.iter()); |
138 | it::assert_equal(it, rs.iter()); |
139 | } |
140 | |
141 | #[test] |
142 | fn test_intersperse() { |
143 | let xs = [1u8, 2, 3]; |
144 | let ys = [1u8, 0, 2, 0, 3]; |
145 | let it = intersperse(&xs, &0); |
146 | it::assert_equal(it, ys.iter()); |
147 | } |
148 | |
149 | #[test] |
150 | fn test_intersperse_with() { |
151 | let xs = [1u8, 2, 3]; |
152 | let ys = [1u8, 10, 2, 10, 3]; |
153 | let i = 10; |
154 | let it = intersperse_with(&xs, || &i); |
155 | it::assert_equal(it, ys.iter()); |
156 | } |
157 | |
158 | #[allow (deprecated)] |
159 | #[test] |
160 | fn foreach() { |
161 | let xs = [1i32, 2, 3]; |
162 | let mut sum = 0; |
163 | xs.iter().foreach(|elt| sum += *elt); |
164 | assert!(sum == 6); |
165 | } |
166 | |
167 | #[test] |
168 | fn dropping() { |
169 | let xs = [1, 2, 3]; |
170 | let mut it = xs.iter().dropping(2); |
171 | assert_eq!(it.next(), Some(&3)); |
172 | assert!(it.next().is_none()); |
173 | let mut it = xs.iter().dropping(5); |
174 | assert!(it.next().is_none()); |
175 | } |
176 | |
177 | #[test] |
178 | fn batching() { |
179 | let xs = [0, 1, 2, 1, 3]; |
180 | let ys = [(0, 1), (2, 1)]; |
181 | |
182 | // An iterator that gathers elements up in pairs |
183 | let pit = xs |
184 | .iter() |
185 | .cloned() |
186 | .batching(|it| it.next().and_then(|x| it.next().map(|y| (x, y)))); |
187 | it::assert_equal(pit, ys.iter().cloned()); |
188 | } |
189 | |
190 | #[test] |
191 | fn test_put_back() { |
192 | let xs = [0, 1, 1, 1, 2, 1, 3, 3]; |
193 | let mut pb = put_back(xs.iter().cloned()); |
194 | pb.next(); |
195 | pb.put_back(1); |
196 | pb.put_back(0); |
197 | it::assert_equal(pb, xs.iter().cloned()); |
198 | } |
199 | |
200 | #[allow (deprecated)] |
201 | #[test] |
202 | fn step() { |
203 | it::assert_equal((0..10).step(1), 0..10); |
204 | it::assert_equal((0..10).step(2), (0..10).filter(|x: &i32| *x % 2 == 0)); |
205 | it::assert_equal((0..10).step(10), 0..1); |
206 | } |
207 | |
208 | #[allow (deprecated)] |
209 | #[test] |
210 | fn merge() { |
211 | it::assert_equal((0..10).step(2).merge((1..10).step(2)), 0..10); |
212 | } |
213 | |
214 | |
215 | #[test] |
216 | fn repeatn() { |
217 | let s = "α" ; |
218 | let mut it = it::repeat_n(s, 3); |
219 | assert_eq!(it.len(), 3); |
220 | assert_eq!(it.next(), Some(s)); |
221 | assert_eq!(it.next(), Some(s)); |
222 | assert_eq!(it.next(), Some(s)); |
223 | assert_eq!(it.next(), None); |
224 | assert_eq!(it.next(), None); |
225 | } |
226 | |
227 | #[test] |
228 | fn count_clones() { |
229 | // Check that RepeatN only clones N - 1 times. |
230 | |
231 | use core::cell::Cell; |
232 | #[derive(PartialEq, Debug)] |
233 | struct Foo { |
234 | n: Cell<usize> |
235 | } |
236 | |
237 | impl Clone for Foo |
238 | { |
239 | fn clone(&self) -> Self |
240 | { |
241 | let n = self.n.get(); |
242 | self.n.set(n + 1); |
243 | Foo { n: Cell::new(n + 1) } |
244 | } |
245 | } |
246 | |
247 | |
248 | for n in 0..10 { |
249 | let f = Foo{n: Cell::new(0)}; |
250 | let it = it::repeat_n(f, n); |
251 | // drain it |
252 | let last = it.last(); |
253 | if n == 0 { |
254 | assert_eq!(last, None); |
255 | } else { |
256 | assert_eq!(last, Some(Foo{n: Cell::new(n - 1)})); |
257 | } |
258 | } |
259 | } |
260 | |
261 | #[test] |
262 | fn part() { |
263 | let mut data = [7, 1, 1, 9, 1, 1, 3]; |
264 | let i = it::partition(&mut data, |elt| *elt >= 3); |
265 | assert_eq!(i, 3); |
266 | assert_eq!(data, [7, 3, 9, 1, 1, 1, 1]); |
267 | |
268 | let i = it::partition(&mut data, |elt| *elt == 1); |
269 | assert_eq!(i, 4); |
270 | assert_eq!(data, [1, 1, 1, 1, 9, 3, 7]); |
271 | |
272 | let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9]; |
273 | let i = it::partition(&mut data, |elt| *elt % 3 == 0); |
274 | assert_eq!(i, 3); |
275 | assert_eq!(data, [9, 6, 3, 4, 5, 2, 7, 8, 1]); |
276 | } |
277 | |
278 | #[test] |
279 | fn tree_fold1() { |
280 | for i in 0..100 { |
281 | assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y)); |
282 | } |
283 | } |
284 | |
285 | #[test] |
286 | fn exactly_one() { |
287 | assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2); |
288 | assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4)); |
289 | assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5)); |
290 | assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0)); |
291 | } |
292 | |
293 | #[test] |
294 | fn at_most_one() { |
295 | assert_eq!((0..10).filter(|&x| x == 2).at_most_one().unwrap(), Some(2)); |
296 | assert!((0..10).filter(|&x| x > 1 && x < 4).at_most_one().unwrap_err().eq(2..4)); |
297 | assert!((0..10).filter(|&x| x > 1 && x < 5).at_most_one().unwrap_err().eq(2..5)); |
298 | assert_eq!((0..10).filter(|&_| false).at_most_one().unwrap(), None); |
299 | } |
300 | |
301 | #[test] |
302 | fn sum1() { |
303 | let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
304 | assert_eq!(v[..0].iter().cloned().sum1::<i32>(), None); |
305 | assert_eq!(v[1..2].iter().cloned().sum1::<i32>(), Some(1)); |
306 | assert_eq!(v[1..3].iter().cloned().sum1::<i32>(), Some(3)); |
307 | assert_eq!(v.iter().cloned().sum1::<i32>(), Some(55)); |
308 | } |
309 | |
310 | #[test] |
311 | fn product1() { |
312 | let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
313 | assert_eq!(v[..0].iter().cloned().product1::<i32>(), None); |
314 | assert_eq!(v[..1].iter().cloned().product1::<i32>(), Some(0)); |
315 | assert_eq!(v[1..3].iter().cloned().product1::<i32>(), Some(2)); |
316 | assert_eq!(v[1..5].iter().cloned().product1::<i32>(), Some(24)); |
317 | } |
318 | |