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