blob: a7b7449d39d3896ceec8341d979728a779465dcf [file] [log] [blame]
Jakub Kotura425e552020-12-21 17:28:15 +01001//! Licensed under the Apache License, Version 2.0
Joel Galensonb593e252021-06-21 13:15:57 -07002//! https://www.apache.org/licenses/LICENSE-2.0 or the MIT license
3//! https://opensource.org/licenses/MIT, at your
Jakub Kotura425e552020-12-21 17:28:15 +01004//! option. This file may not be copied, modified, or distributed
5//! except according to those terms.
6#![no_std]
7
8use core::iter;
9use itertools as it;
10use crate::it::Itertools;
11use crate::it::interleave;
David LeGareb72e9052022-03-02 16:21:08 +000012use crate::it::intersperse;
13use crate::it::intersperse_with;
Jakub Kotura425e552020-12-21 17:28:15 +010014use crate::it::multizip;
15use crate::it::free::put_back;
16use crate::it::iproduct;
17use crate::it::izip;
Joel Galensonb593e252021-06-21 13:15:57 -070018use crate::it::chain;
Jakub Kotura425e552020-12-21 17:28:15 +010019
20#[test]
21fn 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]
33fn 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]
45fn 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]
62fn izip2() {
63 let _zip1: iter::Zip<_, _> = izip!(1.., 2..);
64 let _zip2: iter::Zip<_, _> = izip!(1.., 2.., );
65}
66
67#[test]
68fn 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]
77fn 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]
Joel Galensonb593e252021-06-21 13:15:57 -070094fn 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]
110fn chain2() {
111 let _ = chain!(1.., 2..);
112 let _ = chain!(1.., 2.., );
113}
114
115#[test]
Jakub Kotura425e552020-12-21 17:28:15 +0100116fn write_to() {
117 let xs = [7, 9, 8];
118 let mut ys = [0; 5];
119 let cnt = ys.iter_mut().set_from(xs.iter().map(|x| *x));
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]
129fn 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
David LeGareb72e9052022-03-02 16:21:08 +0000141#[test]
142fn 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]
150fn 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
Jakub Kotura425e552020-12-21 17:28:15 +0100158#[allow(deprecated)]
159#[test]
160fn 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]
168fn 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]
178fn 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.iter().cloned().batching(|it| {
184 match it.next() {
185 None => None,
186 Some(x) => match it.next() {
187 None => None,
188 Some(y) => Some((x, y)),
189 }
190 }
191 });
192 it::assert_equal(pit, ys.iter().cloned());
193}
194
195#[test]
196fn test_put_back() {
197 let xs = [0, 1, 1, 1, 2, 1, 3, 3];
198 let mut pb = put_back(xs.iter().cloned());
199 pb.next();
200 pb.put_back(1);
201 pb.put_back(0);
202 it::assert_equal(pb, xs.iter().cloned());
203}
204
205#[allow(deprecated)]
206#[test]
207fn step() {
208 it::assert_equal((0..10).step(1), 0..10);
209 it::assert_equal((0..10).step(2), (0..10).filter(|x: &i32| *x % 2 == 0));
210 it::assert_equal((0..10).step(10), 0..1);
211}
212
213#[allow(deprecated)]
214#[test]
215fn merge() {
216 it::assert_equal((0..10).step(2).merge((1..10).step(2)), 0..10);
217}
218
219
220#[test]
221fn repeatn() {
222 let s = "α";
223 let mut it = it::repeat_n(s, 3);
224 assert_eq!(it.len(), 3);
225 assert_eq!(it.next(), Some(s));
226 assert_eq!(it.next(), Some(s));
227 assert_eq!(it.next(), Some(s));
228 assert_eq!(it.next(), None);
229 assert_eq!(it.next(), None);
230}
231
232#[test]
233fn count_clones() {
234 // Check that RepeatN only clones N - 1 times.
235
236 use core::cell::Cell;
237 #[derive(PartialEq, Debug)]
238 struct Foo {
239 n: Cell<usize>
240 }
241
242 impl Clone for Foo
243 {
244 fn clone(&self) -> Self
245 {
246 let n = self.n.get();
247 self.n.set(n + 1);
248 Foo { n: Cell::new(n + 1) }
249 }
250 }
251
252
253 for n in 0..10 {
254 let f = Foo{n: Cell::new(0)};
255 let it = it::repeat_n(f, n);
256 // drain it
257 let last = it.last();
258 if n == 0 {
259 assert_eq!(last, None);
260 } else {
261 assert_eq!(last, Some(Foo{n: Cell::new(n - 1)}));
262 }
263 }
264}
265
266#[test]
267fn part() {
268 let mut data = [7, 1, 1, 9, 1, 1, 3];
269 let i = it::partition(&mut data, |elt| *elt >= 3);
270 assert_eq!(i, 3);
271 assert_eq!(data, [7, 3, 9, 1, 1, 1, 1]);
272
273 let i = it::partition(&mut data, |elt| *elt == 1);
274 assert_eq!(i, 4);
275 assert_eq!(data, [1, 1, 1, 1, 9, 3, 7]);
276
277 let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
278 let i = it::partition(&mut data, |elt| *elt % 3 == 0);
279 assert_eq!(i, 3);
280 assert_eq!(data, [9, 6, 3, 4, 5, 2, 7, 8, 1]);
281}
282
283#[test]
284fn tree_fold1() {
285 for i in 0..100 {
286 assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y));
287 }
288}
289
290#[test]
291fn exactly_one() {
292 assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
293 assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
294 assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
295 assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
296}
297
298#[test]
Joel Galensonb593e252021-06-21 13:15:57 -0700299fn at_most_one() {
300 assert_eq!((0..10).filter(|&x| x == 2).at_most_one().unwrap(), Some(2));
301 assert!((0..10).filter(|&x| x > 1 && x < 4).at_most_one().unwrap_err().eq(2..4));
302 assert!((0..10).filter(|&x| x > 1 && x < 5).at_most_one().unwrap_err().eq(2..5));
303 assert_eq!((0..10).filter(|&_| false).at_most_one().unwrap(), None);
304}
305
306#[test]
Jakub Kotura425e552020-12-21 17:28:15 +0100307fn sum1() {
308 let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
309 assert_eq!(v[..0].iter().cloned().sum1::<i32>(), None);
310 assert_eq!(v[1..2].iter().cloned().sum1::<i32>(), Some(1));
311 assert_eq!(v[1..3].iter().cloned().sum1::<i32>(), Some(3));
312 assert_eq!(v.iter().cloned().sum1::<i32>(), Some(55));
313}
314
315#[test]
316fn product1() {
317 let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
318 assert_eq!(v[..0].iter().cloned().product1::<i32>(), None);
319 assert_eq!(v[..1].iter().cloned().product1::<i32>(), Some(0));
320 assert_eq!(v[1..3].iter().cloned().product1::<i32>(), Some(2));
321 assert_eq!(v[1..5].iter().cloned().product1::<i32>(), Some(24));
322}