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