blob: e5bee1713791503178bb23bc78a3d72e94dfaea0 [file] [log] [blame]
Jakub Kotura425e552020-12-21 17:28:15 +01001//! The purpose of these tests is to cover corner cases of iterators
2//! and adaptors.
3//!
4//! In particular we test the tedious size_hint and exact size correctness.
5
6use quickcheck as qc;
7use std::default::Default;
Joel Galenson6f798712021-04-01 17:03:06 -07008use std::num::Wrapping;
Jakub Kotura425e552020-12-21 17:28:15 +01009use std::ops::Range;
10use std::cmp::{max, min, Ordering};
Joel Galenson6f798712021-04-01 17:03:06 -070011use std::collections::{HashMap, HashSet};
Jakub Kotura425e552020-12-21 17:28:15 +010012use itertools::Itertools;
13use itertools::{
14 multizip,
15 EitherOrBoth,
16 iproduct,
17 izip,
18};
19use itertools::free::{
20 cloned,
21 enumerate,
22 multipeek,
Joel Galenson6f798712021-04-01 17:03:06 -070023 peek_nth,
Jakub Kotura425e552020-12-21 17:28:15 +010024 put_back,
25 put_back_n,
26 rciter,
27 zip,
28 zip_eq,
29};
30
31use rand::Rng;
32use rand::seq::SliceRandom;
33use quickcheck::TestResult;
34
35/// Trait for size hint modifier types
36trait HintKind: Copy + Send + qc::Arbitrary {
37 fn loosen_bounds(&self, org_hint: (usize, Option<usize>)) -> (usize, Option<usize>);
38}
39
40/// Exact size hint variant that leaves hints unchanged
41#[derive(Clone, Copy, Debug)]
42struct Exact {}
43
44impl HintKind for Exact {
45 fn loosen_bounds(&self, org_hint: (usize, Option<usize>)) -> (usize, Option<usize>) {
46 org_hint
47 }
48}
49
50impl qc::Arbitrary for Exact {
51 fn arbitrary<G: qc::Gen>(_: &mut G) -> Self {
52 Exact {}
53 }
54}
55
56/// Inexact size hint variant to simulate imprecise (but valid) size hints
57///
58/// Will always decrease the lower bound and increase the upper bound
59/// of the size hint by set amounts.
60#[derive(Clone, Copy, Debug)]
61struct Inexact {
62 underestimate: usize,
63 overestimate: usize,
64}
65
66impl HintKind for Inexact {
67 fn loosen_bounds(&self, org_hint: (usize, Option<usize>)) -> (usize, Option<usize>) {
68 let (org_lower, org_upper) = org_hint;
69 (org_lower.saturating_sub(self.underestimate),
70 org_upper.and_then(move |x| x.checked_add(self.overestimate)))
71 }
72}
73
74impl qc::Arbitrary for Inexact {
75 fn arbitrary<G: qc::Gen>(g: &mut G) -> Self {
76 let ue_value = usize::arbitrary(g);
77 let oe_value = usize::arbitrary(g);
78 // Compensate for quickcheck using extreme values too rarely
79 let ue_choices = &[0, ue_value, usize::max_value()];
80 let oe_choices = &[0, oe_value, usize::max_value()];
81 Inexact {
82 underestimate: *ue_choices.choose(g).unwrap(),
83 overestimate: *oe_choices.choose(g).unwrap(),
84 }
85 }
86
87 fn shrink(&self) -> Box<dyn Iterator<Item=Self>> {
88 let underestimate_value = self.underestimate;
89 let overestimate_value = self.overestimate;
90 Box::new(
91 underestimate_value.shrink().flat_map(move |ue_value|
92 overestimate_value.shrink().map(move |oe_value|
93 Inexact {
94 underestimate: ue_value,
95 overestimate: oe_value,
96 }
97 )
98 )
99 )
100 }
101}
102
103/// Our base iterator that we can impl Arbitrary for
104///
105/// By default we'll return inexact bounds estimates for size_hint
106/// to make tests harder to pass.
107///
108/// NOTE: Iter is tricky and is not fused, to help catch bugs.
109/// At the end it will return None once, then return Some(0),
110/// then return None again.
111#[derive(Clone, Debug)]
112struct Iter<T, SK: HintKind = Inexact> {
113 iterator: Range<T>,
114 // fuse/done flag
115 fuse_flag: i32,
116 hint_kind: SK,
117}
118
119impl<T, HK> Iter<T, HK> where HK: HintKind
120{
121 fn new(it: Range<T>, hint_kind: HK) -> Self {
122 Iter {
123 iterator: it,
124 fuse_flag: 0,
125 hint_kind,
126 }
127 }
128}
129
130impl<T, HK> Iterator for Iter<T, HK>
131 where Range<T>: Iterator,
132 <Range<T> as Iterator>::Item: Default,
133 HK: HintKind,
134{
135 type Item = <Range<T> as Iterator>::Item;
136
137 fn next(&mut self) -> Option<Self::Item>
138 {
139 let elt = self.iterator.next();
140 if elt.is_none() {
141 self.fuse_flag += 1;
142 // check fuse flag
143 if self.fuse_flag == 2 {
144 return Some(Default::default())
145 }
146 }
147 elt
148 }
149
150 fn size_hint(&self) -> (usize, Option<usize>)
151 {
152 let org_hint = self.iterator.size_hint();
153 self.hint_kind.loosen_bounds(org_hint)
154 }
155}
156
157impl<T, HK> DoubleEndedIterator for Iter<T, HK>
158 where Range<T>: DoubleEndedIterator,
159 <Range<T> as Iterator>::Item: Default,
160 HK: HintKind
161{
162 fn next_back(&mut self) -> Option<Self::Item> { self.iterator.next_back() }
163}
164
165impl<T> ExactSizeIterator for Iter<T, Exact> where Range<T>: ExactSizeIterator,
166 <Range<T> as Iterator>::Item: Default,
167{ }
168
169impl<T, HK> qc::Arbitrary for Iter<T, HK>
170 where T: qc::Arbitrary,
171 HK: HintKind,
172{
173 fn arbitrary<G: qc::Gen>(g: &mut G) -> Self
174 {
175 Iter::new(T::arbitrary(g)..T::arbitrary(g), HK::arbitrary(g))
176 }
177
178 fn shrink(&self) -> Box<dyn Iterator<Item=Iter<T, HK>>>
179 {
180 let r = self.iterator.clone();
181 let hint_kind = self.hint_kind;
182 Box::new(
183 r.start.shrink().flat_map(move |a|
184 r.end.shrink().map(move |b|
185 Iter::new(a.clone()..b, hint_kind)
186 )
187 )
188 )
189 }
190}
191
192/// A meta-iterator which yields `Iter<i32>`s whose start/endpoints are
193/// increased or decreased linearly on each iteration.
194#[derive(Clone, Debug)]
195struct ShiftRange<HK = Inexact> {
196 range_start: i32,
197 range_end: i32,
198 start_step: i32,
199 end_step: i32,
200 iter_count: u32,
201 hint_kind: HK,
202}
203
204impl<HK> Iterator for ShiftRange<HK> where HK: HintKind {
205 type Item = Iter<i32, HK>;
206
207 fn next(&mut self) -> Option<Self::Item> {
208 if self.iter_count == 0 {
209 return None;
210 }
211
212 let iter = Iter::new(self.range_start..self.range_end, self.hint_kind);
213
214 self.range_start += self.start_step;
215 self.range_end += self.end_step;
216 self.iter_count -= 1;
217
218 Some(iter)
219 }
220}
221
222impl ExactSizeIterator for ShiftRange<Exact> { }
223
224impl<HK> qc::Arbitrary for ShiftRange<HK>
225 where HK: HintKind
226{
227 fn arbitrary<G: qc::Gen>(g: &mut G) -> Self {
228 const MAX_STARTING_RANGE_DIFF: i32 = 32;
229 const MAX_STEP_MODULO: i32 = 8;
230 const MAX_ITER_COUNT: u32 = 3;
231
232 let range_start = qc::Arbitrary::arbitrary(g);
233 let range_end = range_start + g.gen_range(0, MAX_STARTING_RANGE_DIFF + 1);
234 let start_step = g.gen_range(-MAX_STEP_MODULO, MAX_STEP_MODULO + 1);
235 let end_step = g.gen_range(-MAX_STEP_MODULO, MAX_STEP_MODULO + 1);
236 let iter_count = g.gen_range(0, MAX_ITER_COUNT + 1);
237 let hint_kind = qc::Arbitrary::arbitrary(g);
238
239 ShiftRange {
240 range_start,
241 range_end,
242 start_step,
243 end_step,
244 iter_count,
245 hint_kind,
246 }
247 }
248}
249
250fn correct_count<I, F>(get_it: F) -> bool
251where
252 I: Iterator,
253 F: Fn() -> I
254{
255 let mut counts = vec![get_it().count()];
256
257 'outer: loop {
258 let mut it = get_it();
259
260 for _ in 0..(counts.len() - 1) {
261 if let None = it.next() {
262 panic!("Iterator shouldn't be finished, may not be deterministic");
263 }
264 }
265
266 if let None = it.next() {
267 break 'outer;
268 }
269
270 counts.push(it.count());
271 }
272
273 let total_actual_count = counts.len() - 1;
274
275 for (i, returned_count) in counts.into_iter().enumerate() {
276 let actual_count = total_actual_count - i;
277 if actual_count != returned_count {
278 println!("Total iterations: {} True count: {} returned count: {}", i, actual_count, returned_count);
279
280 return false;
281 }
282 }
283
284 true
285}
286
287fn correct_size_hint<I: Iterator>(mut it: I) -> bool {
288 // record size hint at each iteration
289 let initial_hint = it.size_hint();
290 let mut hints = Vec::with_capacity(initial_hint.0 + 1);
291 hints.push(initial_hint);
292 while let Some(_) = it.next() {
293 hints.push(it.size_hint())
294 }
295
296 let mut true_count = hints.len(); // start off +1 too much
297
298 // check all the size hints
299 for &(low, hi) in &hints {
300 true_count -= 1;
301 if low > true_count ||
302 (hi.is_some() && hi.unwrap() < true_count)
303 {
304 println!("True size: {:?}, size hint: {:?}", true_count, (low, hi));
305 //println!("All hints: {:?}", hints);
306 return false
307 }
308 }
309 true
310}
311
312fn exact_size<I: ExactSizeIterator>(mut it: I) -> bool {
313 // check every iteration
314 let (mut low, mut hi) = it.size_hint();
315 if Some(low) != hi { return false; }
316 while let Some(_) = it.next() {
317 let (xlow, xhi) = it.size_hint();
318 if low != xlow + 1 { return false; }
319 low = xlow;
320 hi = xhi;
321 if Some(low) != hi { return false; }
322 }
323 let (low, hi) = it.size_hint();
324 low == 0 && hi == Some(0)
325}
326
327// Exact size for this case, without ExactSizeIterator
328fn exact_size_for_this<I: Iterator>(mut it: I) -> bool {
329 // check every iteration
330 let (mut low, mut hi) = it.size_hint();
331 if Some(low) != hi { return false; }
332 while let Some(_) = it.next() {
333 let (xlow, xhi) = it.size_hint();
334 if low != xlow + 1 { return false; }
335 low = xlow;
336 hi = xhi;
337 if Some(low) != hi { return false; }
338 }
339 let (low, hi) = it.size_hint();
340 low == 0 && hi == Some(0)
341}
342
343/*
344 * NOTE: Range<i8> is broken!
345 * (all signed ranges are)
346#[quickcheck]
347fn size_range_i8(a: Iter<i8>) -> bool {
348 exact_size(a)
349}
350
351#[quickcheck]
352fn size_range_i16(a: Iter<i16>) -> bool {
353 exact_size(a)
354}
355
356#[quickcheck]
357fn size_range_u8(a: Iter<u8>) -> bool {
358 exact_size(a)
359}
360 */
361
362macro_rules! quickcheck {
363 // accept several property function definitions
364 // The property functions can use pattern matching and `mut` as usual
365 // in the function arguments, but the functions can not be generic.
366 {$($(#$attr:tt)* fn $fn_name:ident($($arg:tt)*) -> $ret:ty { $($code:tt)* })*} => (
367 $(
368 #[test]
369 $(#$attr)*
370 fn $fn_name() {
371 fn prop($($arg)*) -> $ret {
372 $($code)*
373 }
374 ::quickcheck::quickcheck(quickcheck!(@fn prop [] $($arg)*));
375 }
376 )*
377 );
378 // parse argument list (with patterns allowed) into prop as fn(_, _) -> _
379 (@fn $f:ident [$($t:tt)*]) => {
380 $f as fn($($t),*) -> _
381 };
382 (@fn $f:ident [$($p:tt)*] : $($tail:tt)*) => {
383 quickcheck!(@fn $f [$($p)* _] $($tail)*)
384 };
385 (@fn $f:ident [$($p:tt)*] $t:tt $($tail:tt)*) => {
386 quickcheck!(@fn $f [$($p)*] $($tail)*)
387 };
388}
389
390quickcheck! {
391
392 fn size_product(a: Iter<u16>, b: Iter<u16>) -> bool {
393 correct_size_hint(a.cartesian_product(b))
394 }
395 fn size_product3(a: Iter<u16>, b: Iter<u16>, c: Iter<u16>) -> bool {
396 correct_size_hint(iproduct!(a, b, c))
397 }
398
399 fn correct_cartesian_product3(a: Iter<u16>, b: Iter<u16>, c: Iter<u16>,
400 take_manual: usize) -> ()
401 {
402 // test correctness of iproduct through regular iteration (take)
403 // and through fold.
404 let ac = a.clone();
405 let br = &b.clone();
406 let cr = &c.clone();
407 let answer: Vec<_> = ac.flat_map(move |ea| br.clone().flat_map(move |eb| cr.clone().map(move |ec| (ea, eb, ec)))).collect();
408 let mut product_iter = iproduct!(a, b, c);
409 let mut actual = Vec::new();
410
411 actual.extend((&mut product_iter).take(take_manual));
412 if actual.len() == take_manual {
413 product_iter.fold((), |(), elt| actual.push(elt));
414 }
415 assert_eq!(answer, actual);
416 }
417
418 fn size_multi_product(a: ShiftRange) -> bool {
419 correct_size_hint(a.multi_cartesian_product())
420 }
421 fn correct_multi_product3(a: ShiftRange, take_manual: usize) -> () {
422 // Fix no. of iterators at 3
423 let a = ShiftRange { iter_count: 3, ..a };
424
425 // test correctness of MultiProduct through regular iteration (take)
426 // and through fold.
427 let mut iters = a.clone();
428 let i0 = iters.next().unwrap();
429 let i1r = &iters.next().unwrap();
430 let i2r = &iters.next().unwrap();
431 let answer: Vec<_> = i0.flat_map(move |ei0| i1r.clone().flat_map(move |ei1| i2r.clone().map(move |ei2| vec![ei0, ei1, ei2]))).collect();
432 let mut multi_product = a.clone().multi_cartesian_product();
433 let mut actual = Vec::new();
434
435 actual.extend((&mut multi_product).take(take_manual));
436 if actual.len() == take_manual {
437 multi_product.fold((), |(), elt| actual.push(elt));
438 }
439 assert_eq!(answer, actual);
440
441 assert_eq!(answer.into_iter().last(), a.clone().multi_cartesian_product().last());
442 }
443
444 #[allow(deprecated)]
445 fn size_step(a: Iter<i16, Exact>, s: usize) -> bool {
446 let mut s = s;
447 if s == 0 {
448 s += 1; // never zero
449 }
450 let filt = a.clone().dedup();
451 correct_size_hint(filt.step(s)) &&
452 exact_size(a.step(s))
453 }
454
455 #[allow(deprecated)]
456 fn equal_step(a: Iter<i16>, s: usize) -> bool {
457 let mut s = s;
458 if s == 0 {
459 s += 1; // never zero
460 }
461 let mut i = 0;
462 itertools::equal(a.clone().step(s), a.filter(|_| {
463 let keep = i % s == 0;
464 i += 1;
465 keep
466 }))
467 }
468
469 #[allow(deprecated)]
470 fn equal_step_vec(a: Vec<i16>, s: usize) -> bool {
471 let mut s = s;
472 if s == 0 {
473 s += 1; // never zero
474 }
475 let mut i = 0;
476 itertools::equal(a.iter().step(s), a.iter().filter(|_| {
477 let keep = i % s == 0;
478 i += 1;
479 keep
480 }))
481 }
482
483 fn size_multipeek(a: Iter<u16, Exact>, s: u8) -> bool {
484 let mut it = multipeek(a);
485 // peek a few times
486 for _ in 0..s {
487 it.peek();
488 }
489 exact_size(it)
490 }
491
Joel Galenson6f798712021-04-01 17:03:06 -0700492 fn size_peek_nth(a: Iter<u16, Exact>, s: u8) -> bool {
493 let mut it = peek_nth(a);
494 // peek a few times
495 for n in 0..s {
496 it.peek_nth(n as usize);
497 }
498 exact_size(it)
499 }
500
Jakub Kotura425e552020-12-21 17:28:15 +0100501 fn equal_merge(a: Vec<i16>, b: Vec<i16>) -> bool {
502 let mut sa = a.clone();
503 let mut sb = b.clone();
504 sa.sort();
505 sb.sort();
506 let mut merged = sa.clone();
507 merged.extend(sb.iter().cloned());
508 merged.sort();
509 itertools::equal(&merged, sa.iter().merge(&sb))
510 }
511 fn size_merge(a: Iter<u16>, b: Iter<u16>) -> bool {
512 correct_size_hint(a.merge(b))
513 }
514 fn size_zip(a: Iter<i16, Exact>, b: Iter<i16, Exact>, c: Iter<i16, Exact>) -> bool {
515 let filt = a.clone().dedup();
516 correct_size_hint(multizip((filt, b.clone(), c.clone()))) &&
517 exact_size(multizip((a, b, c)))
518 }
519 fn size_zip_rc(a: Iter<i16>, b: Iter<i16>) -> bool {
520 let rc = rciter(a.clone());
521 correct_size_hint(multizip((&rc, &rc, b)))
522 }
523
524 fn size_zip_macro(a: Iter<i16, Exact>, b: Iter<i16, Exact>, c: Iter<i16, Exact>) -> bool {
525 let filt = a.clone().dedup();
526 correct_size_hint(izip!(filt, b.clone(), c.clone())) &&
527 exact_size(izip!(a, b, c))
528 }
529 fn equal_kmerge(a: Vec<i16>, b: Vec<i16>, c: Vec<i16>) -> bool {
530 use itertools::free::kmerge;
531 let mut sa = a.clone();
532 let mut sb = b.clone();
533 let mut sc = c.clone();
534 sa.sort();
535 sb.sort();
536 sc.sort();
537 let mut merged = sa.clone();
538 merged.extend(sb.iter().cloned());
539 merged.extend(sc.iter().cloned());
540 merged.sort();
541 itertools::equal(merged.into_iter(), kmerge(vec![sa, sb, sc]))
542 }
543
544 // Any number of input iterators
545 fn equal_kmerge_2(mut inputs: Vec<Vec<i16>>) -> bool {
546 use itertools::free::kmerge;
547 // sort the inputs
548 for input in &mut inputs {
549 input.sort();
550 }
551 let mut merged = inputs.concat();
552 merged.sort();
553 itertools::equal(merged.into_iter(), kmerge(inputs))
554 }
555
556 // Any number of input iterators
557 fn equal_kmerge_by_ge(mut inputs: Vec<Vec<i16>>) -> bool {
558 // sort the inputs
559 for input in &mut inputs {
560 input.sort();
561 input.reverse();
562 }
563 let mut merged = inputs.concat();
564 merged.sort();
565 merged.reverse();
566 itertools::equal(merged.into_iter(),
567 inputs.into_iter().kmerge_by(|x, y| x >= y))
568 }
569
570 // Any number of input iterators
571 fn equal_kmerge_by_lt(mut inputs: Vec<Vec<i16>>) -> bool {
572 // sort the inputs
573 for input in &mut inputs {
574 input.sort();
575 }
576 let mut merged = inputs.concat();
577 merged.sort();
578 itertools::equal(merged.into_iter(),
579 inputs.into_iter().kmerge_by(|x, y| x < y))
580 }
581
582 // Any number of input iterators
583 fn equal_kmerge_by_le(mut inputs: Vec<Vec<i16>>) -> bool {
584 // sort the inputs
585 for input in &mut inputs {
586 input.sort();
587 }
588 let mut merged = inputs.concat();
589 merged.sort();
590 itertools::equal(merged.into_iter(),
591 inputs.into_iter().kmerge_by(|x, y| x <= y))
592 }
593 fn size_kmerge(a: Iter<i16>, b: Iter<i16>, c: Iter<i16>) -> bool {
594 use itertools::free::kmerge;
595 correct_size_hint(kmerge(vec![a, b, c]))
596 }
597 fn equal_zip_eq(a: Vec<i32>, b: Vec<i32>) -> bool {
598 let len = std::cmp::min(a.len(), b.len());
599 let a = &a[..len];
600 let b = &b[..len];
601 itertools::equal(zip_eq(a, b), zip(a, b))
602 }
603 fn size_zip_longest(a: Iter<i16, Exact>, b: Iter<i16, Exact>) -> bool {
604 let filt = a.clone().dedup();
605 let filt2 = b.clone().dedup();
606 correct_size_hint(filt.zip_longest(b.clone())) &&
607 correct_size_hint(a.clone().zip_longest(filt2)) &&
608 exact_size(a.zip_longest(b))
609 }
610 fn size_2_zip_longest(a: Iter<i16>, b: Iter<i16>) -> bool {
611 let it = a.clone().zip_longest(b.clone());
612 let jt = a.clone().zip_longest(b.clone());
613 itertools::equal(a.clone(),
614 it.filter_map(|elt| match elt {
615 EitherOrBoth::Both(x, _) => Some(x),
616 EitherOrBoth::Left(x) => Some(x),
617 _ => None,
618 }
619 ))
620 &&
621 itertools::equal(b.clone(),
622 jt.filter_map(|elt| match elt {
623 EitherOrBoth::Both(_, y) => Some(y),
624 EitherOrBoth::Right(y) => Some(y),
625 _ => None,
626 }
627 ))
628 }
629 fn size_interleave(a: Iter<i16>, b: Iter<i16>) -> bool {
630 correct_size_hint(a.interleave(b))
631 }
632 fn exact_interleave(a: Iter<i16, Exact>, b: Iter<i16, Exact>) -> bool {
633 exact_size_for_this(a.interleave(b))
634 }
635 fn size_interleave_shortest(a: Iter<i16>, b: Iter<i16>) -> bool {
636 correct_size_hint(a.interleave_shortest(b))
637 }
638 fn exact_interleave_shortest(a: Vec<()>, b: Vec<()>) -> bool {
639 exact_size_for_this(a.iter().interleave_shortest(&b))
640 }
641 fn size_intersperse(a: Iter<i16>, x: i16) -> bool {
642 correct_size_hint(a.intersperse(x))
643 }
644 fn equal_intersperse(a: Vec<i32>, x: i32) -> bool {
645 let mut inter = false;
646 let mut i = 0;
647 for elt in a.iter().cloned().intersperse(x) {
648 if inter {
649 if elt != x { return false }
650 } else {
651 if elt != a[i] { return false }
652 i += 1;
653 }
654 inter = !inter;
655 }
656 true
657 }
658
659 fn equal_combinations_2(a: Vec<u8>) -> bool {
660 let mut v = Vec::new();
661 for (i, x) in enumerate(&a) {
662 for y in &a[i + 1..] {
663 v.push((x, y));
664 }
665 }
666 itertools::equal(a.iter().tuple_combinations::<(_, _)>(), v)
667 }
668
669 fn collect_tuple_matches_size(a: Iter<i16>) -> bool {
670 let size = a.clone().count();
671 a.collect_tuple::<(_, _, _)>().is_some() == (size == 3)
672 }
673
674 fn correct_permutations(vals: HashSet<i32>, k: usize) -> () {
675 // Test permutations only on iterators of distinct integers, to prevent
676 // false positives.
677
678 const MAX_N: usize = 5;
679
680 let n = min(vals.len(), MAX_N);
681 let vals: HashSet<i32> = vals.into_iter().take(n).collect();
682
683 let perms = vals.iter().permutations(k);
684
685 let mut actual = HashSet::new();
686
687 for perm in perms {
688 assert_eq!(perm.len(), k);
689
690 let all_items_valid = perm.iter().all(|p| vals.contains(p));
691 assert!(all_items_valid, "perm contains value not from input: {:?}", perm);
692
693 // Check that all perm items are distinct
694 let distinct_len = {
695 let perm_set: HashSet<_> = perm.iter().collect();
696 perm_set.len()
697 };
698 assert_eq!(perm.len(), distinct_len);
699
700 // Check that the perm is new
701 assert!(actual.insert(perm.clone()), "perm already encountered: {:?}", perm);
702 }
703 }
704
705 fn permutations_lexic_order(a: usize, b: usize) -> () {
706 let a = a % 6;
707 let b = b % 6;
708
709 let n = max(a, b);
710 let k = min (a, b);
711
712 let expected_first: Vec<usize> = (0..k).collect();
713 let expected_last: Vec<usize> = ((n - k)..n).rev().collect();
714
715 let mut perms = (0..n).permutations(k);
716
717 let mut curr_perm = match perms.next() {
718 Some(p) => p,
719 None => { return; }
720 };
721
722 assert_eq!(expected_first, curr_perm);
723
724 while let Some(next_perm) = perms.next() {
725 assert!(
726 next_perm > curr_perm,
727 "next perm isn't greater-than current; next_perm={:?} curr_perm={:?} n={}",
728 next_perm, curr_perm, n
729 );
730
731 curr_perm = next_perm;
732 }
733
734 assert_eq!(expected_last, curr_perm);
735
736 }
737
738 fn permutations_count(n: usize, k: usize) -> bool {
739 let n = n % 6;
740
741 correct_count(|| (0..n).permutations(k))
742 }
743
744 fn permutations_size(a: Iter<i32>, k: usize) -> bool {
745 correct_size_hint(a.take(5).permutations(k))
746 }
747
748 fn permutations_k0_yields_once(n: usize) -> () {
749 let k = 0;
750 let expected: Vec<Vec<usize>> = vec![vec![]];
751 let actual = (0..n).permutations(k).collect_vec();
752
753 assert_eq!(expected, actual);
754 }
755}
756
757quickcheck! {
Joel Galenson6f798712021-04-01 17:03:06 -0700758 fn dedup_via_coalesce(a: Vec<i32>) -> bool {
759 let mut b = a.clone();
760 b.dedup();
761 itertools::equal(
762 &b,
763 a
764 .iter()
765 .coalesce(|x, y| {
766 if x==y {
767 Ok(x)
768 } else {
769 Err((x, y))
770 }
771 })
772 .fold(vec![], |mut v, n| {
773 v.push(n);
774 v
775 })
776 )
777 }
778}
779
780quickcheck! {
Jakub Kotura425e552020-12-21 17:28:15 +0100781 fn equal_dedup(a: Vec<i32>) -> bool {
782 let mut b = a.clone();
783 b.dedup();
784 itertools::equal(&b, a.iter().dedup())
785 }
786}
787
788quickcheck! {
789 fn equal_dedup_by(a: Vec<(i32, i32)>) -> bool {
790 let mut b = a.clone();
791 b.dedup_by(|x, y| x.0==y.0);
792 itertools::equal(&b, a.iter().dedup_by(|x, y| x.0==y.0))
793 }
794}
795
796quickcheck! {
797 fn size_dedup(a: Vec<i32>) -> bool {
798 correct_size_hint(a.iter().dedup())
799 }
800}
801
802quickcheck! {
803 fn size_dedup_by(a: Vec<(i32, i32)>) -> bool {
804 correct_size_hint(a.iter().dedup_by(|x, y| x.0==y.0))
805 }
806}
807
808quickcheck! {
809 fn exact_repeatn((n, x): (usize, i32)) -> bool {
810 let it = itertools::repeat_n(x, n);
811 exact_size(it)
812 }
813}
814
815quickcheck! {
816 fn size_put_back(a: Vec<u8>, x: Option<u8>) -> bool {
817 let mut it = put_back(a.into_iter());
818 match x {
819 Some(t) => it.put_back(t),
820 None => {}
821 }
822 correct_size_hint(it)
823 }
824}
825
826quickcheck! {
827 fn size_put_backn(a: Vec<u8>, b: Vec<u8>) -> bool {
828 let mut it = put_back_n(a.into_iter());
829 for elt in b {
830 it.put_back(elt)
831 }
832 correct_size_hint(it)
833 }
834}
835
836quickcheck! {
837 fn size_tee(a: Vec<u8>) -> bool {
838 let (mut t1, mut t2) = a.iter().tee();
839 t1.next();
840 t1.next();
841 t2.next();
842 exact_size(t1) && exact_size(t2)
843 }
844}
845
846quickcheck! {
847 fn size_tee_2(a: Vec<u8>) -> bool {
848 let (mut t1, mut t2) = a.iter().dedup().tee();
849 t1.next();
850 t1.next();
851 t2.next();
852 correct_size_hint(t1) && correct_size_hint(t2)
853 }
854}
855
856quickcheck! {
857 fn size_take_while_ref(a: Vec<u8>, stop: u8) -> bool {
858 correct_size_hint(a.iter().take_while_ref(|x| **x != stop))
859 }
860}
861
862quickcheck! {
863 fn equal_partition(a: Vec<i32>) -> bool {
864 let mut a = a;
865 let mut ap = a.clone();
866 let split_index = itertools::partition(&mut ap, |x| *x >= 0);
867 let parted = (0..split_index).all(|i| ap[i] >= 0) &&
868 (split_index..a.len()).all(|i| ap[i] < 0);
869
870 a.sort();
871 ap.sort();
872 parted && (a == ap)
873 }
874}
875
876quickcheck! {
877 fn size_combinations(it: Iter<i16>) -> bool {
878 correct_size_hint(it.tuple_combinations::<(_, _)>())
879 }
880}
881
882quickcheck! {
883 fn equal_combinations(it: Iter<i16>) -> bool {
884 let values = it.clone().collect_vec();
885 let mut cmb = it.tuple_combinations();
886 for i in 0..values.len() {
887 for j in i+1..values.len() {
888 let pair = (values[i], values[j]);
889 if pair != cmb.next().unwrap() {
890 return false;
891 }
892 }
893 }
894 cmb.next() == None
895 }
896}
897
898quickcheck! {
899 fn size_pad_tail(it: Iter<i8>, pad: u8) -> bool {
900 correct_size_hint(it.clone().pad_using(pad as usize, |_| 0)) &&
901 correct_size_hint(it.dropping(1).rev().pad_using(pad as usize, |_| 0))
902 }
903}
904
905quickcheck! {
906 fn size_pad_tail2(it: Iter<i8, Exact>, pad: u8) -> bool {
907 exact_size(it.pad_using(pad as usize, |_| 0))
908 }
909}
910
911quickcheck! {
Joel Galenson6f798712021-04-01 17:03:06 -0700912 fn size_powerset(it: Iter<u8, Exact>) -> bool {
913 // Powerset cardinality gets large very quickly, limit input to keep test fast.
914 correct_size_hint(it.take(12).powerset())
915 }
916}
917
918quickcheck! {
Jakub Kotura425e552020-12-21 17:28:15 +0100919 fn size_unique(it: Iter<i8>) -> bool {
920 correct_size_hint(it.unique())
921 }
922
923 fn count_unique(it: Vec<i8>, take_first: u8) -> () {
924 let answer = {
925 let mut v = it.clone();
926 v.sort(); v.dedup();
927 v.len()
928 };
929 let mut iter = cloned(&it).unique();
930 let first_count = (&mut iter).take(take_first as usize).count();
931 let rest_count = iter.count();
932 assert_eq!(answer, first_count + rest_count);
933 }
934}
935
936quickcheck! {
937 fn fuzz_group_by_lazy_1(it: Iter<u8>) -> bool {
938 let jt = it.clone();
939 let groups = it.group_by(|k| *k);
940 let res = itertools::equal(jt, groups.into_iter().flat_map(|(_, x)| x));
941 res
942 }
943}
944
945quickcheck! {
946 fn fuzz_group_by_lazy_2(data: Vec<u8>) -> bool {
947 let groups = data.iter().group_by(|k| *k / 10);
948 let res = itertools::equal(data.iter(), groups.into_iter().flat_map(|(_, x)| x));
949 res
950 }
951}
952
953quickcheck! {
954 fn fuzz_group_by_lazy_3(data: Vec<u8>) -> bool {
955 let grouper = data.iter().group_by(|k| *k / 10);
956 let groups = grouper.into_iter().collect_vec();
957 let res = itertools::equal(data.iter(), groups.into_iter().flat_map(|(_, x)| x));
958 res
959 }
960}
961
962quickcheck! {
963 fn fuzz_group_by_lazy_duo(data: Vec<u8>, order: Vec<(bool, bool)>) -> bool {
964 let grouper = data.iter().group_by(|k| *k / 3);
965 let mut groups1 = grouper.into_iter();
966 let mut groups2 = grouper.into_iter();
967 let mut elts = Vec::<&u8>::new();
968 let mut old_groups = Vec::new();
969
970 let tup1 = |(_, b)| b;
971 for &(ord, consume_now) in &order {
972 let iter = &mut [&mut groups1, &mut groups2][ord as usize];
973 match iter.next() {
974 Some((_, gr)) => if consume_now {
975 for og in old_groups.drain(..) {
976 elts.extend(og);
977 }
978 elts.extend(gr);
979 } else {
980 old_groups.push(gr);
981 },
982 None => break,
983 }
984 }
985 for og in old_groups.drain(..) {
986 elts.extend(og);
987 }
988 for gr in groups1.map(&tup1) { elts.extend(gr); }
989 for gr in groups2.map(&tup1) { elts.extend(gr); }
990 itertools::assert_equal(&data, elts);
991 true
992 }
993}
994
995quickcheck! {
996 fn equal_chunks_lazy(a: Vec<u8>, size: u8) -> bool {
997 let mut size = size;
998 if size == 0 {
999 size += 1;
1000 }
1001 let chunks = a.iter().chunks(size as usize);
1002 let it = a.chunks(size as usize);
1003 for (a, b) in chunks.into_iter().zip(it) {
1004 if !itertools::equal(a, b) {
1005 return false;
1006 }
1007 }
1008 true
1009 }
1010}
1011
1012quickcheck! {
1013 fn equal_tuple_windows_1(a: Vec<u8>) -> bool {
1014 let x = a.windows(1).map(|s| (&s[0], ));
1015 let y = a.iter().tuple_windows::<(_,)>();
1016 itertools::equal(x, y)
1017 }
1018
1019 fn equal_tuple_windows_2(a: Vec<u8>) -> bool {
1020 let x = a.windows(2).map(|s| (&s[0], &s[1]));
1021 let y = a.iter().tuple_windows::<(_, _)>();
1022 itertools::equal(x, y)
1023 }
1024
1025 fn equal_tuple_windows_3(a: Vec<u8>) -> bool {
1026 let x = a.windows(3).map(|s| (&s[0], &s[1], &s[2]));
1027 let y = a.iter().tuple_windows::<(_, _, _)>();
1028 itertools::equal(x, y)
1029 }
1030
1031 fn equal_tuple_windows_4(a: Vec<u8>) -> bool {
1032 let x = a.windows(4).map(|s| (&s[0], &s[1], &s[2], &s[3]));
1033 let y = a.iter().tuple_windows::<(_, _, _, _)>();
1034 itertools::equal(x, y)
1035 }
1036
1037 fn equal_tuples_1(a: Vec<u8>) -> bool {
1038 let x = a.chunks(1).map(|s| (&s[0], ));
1039 let y = a.iter().tuples::<(_,)>();
1040 itertools::equal(x, y)
1041 }
1042
1043 fn equal_tuples_2(a: Vec<u8>) -> bool {
1044 let x = a.chunks(2).filter(|s| s.len() == 2).map(|s| (&s[0], &s[1]));
1045 let y = a.iter().tuples::<(_, _)>();
1046 itertools::equal(x, y)
1047 }
1048
1049 fn equal_tuples_3(a: Vec<u8>) -> bool {
1050 let x = a.chunks(3).filter(|s| s.len() == 3).map(|s| (&s[0], &s[1], &s[2]));
1051 let y = a.iter().tuples::<(_, _, _)>();
1052 itertools::equal(x, y)
1053 }
1054
1055 fn equal_tuples_4(a: Vec<u8>) -> bool {
1056 let x = a.chunks(4).filter(|s| s.len() == 4).map(|s| (&s[0], &s[1], &s[2], &s[3]));
1057 let y = a.iter().tuples::<(_, _, _, _)>();
1058 itertools::equal(x, y)
1059 }
1060
1061 fn exact_tuple_buffer(a: Vec<u8>) -> bool {
1062 let mut iter = a.iter().tuples::<(_, _, _, _)>();
1063 (&mut iter).last();
1064 let buffer = iter.into_buffer();
1065 assert_eq!(buffer.len(), a.len() % 4);
1066 exact_size(buffer)
1067 }
1068}
1069
1070// with_position
1071quickcheck! {
1072 fn with_position_exact_size_1(a: Vec<u8>) -> bool {
1073 exact_size_for_this(a.iter().with_position())
1074 }
1075 fn with_position_exact_size_2(a: Iter<u8, Exact>) -> bool {
1076 exact_size_for_this(a.with_position())
1077 }
1078}
1079
1080quickcheck! {
1081 fn correct_group_map_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1082 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1083 let count = a.len();
1084 let lookup = a.into_iter().map(|i| (i % modulo, i)).into_group_map();
1085
1086 assert_eq!(lookup.values().flat_map(|vals| vals.iter()).count(), count);
1087
1088 for (&key, vals) in lookup.iter() {
1089 assert!(vals.iter().all(|&val| val % modulo == key));
1090 }
1091 }
1092}
1093
1094/// A peculiar type: Equality compares both tuple items, but ordering only the
1095/// first item. This is so we can check the stability property easily.
1096#[derive(Clone, Debug, PartialEq, Eq)]
1097struct Val(u32, u32);
1098
1099impl PartialOrd<Val> for Val {
1100 fn partial_cmp(&self, other: &Val) -> Option<Ordering> {
1101 self.0.partial_cmp(&other.0)
1102 }
1103}
1104
1105impl Ord for Val {
1106 fn cmp(&self, other: &Val) -> Ordering {
1107 self.0.cmp(&other.0)
1108 }
1109}
1110
1111impl qc::Arbitrary for Val {
1112 fn arbitrary<G: qc::Gen>(g: &mut G) -> Self {
1113 let (x, y) = <(u32, u32)>::arbitrary(g);
1114 Val(x, y)
1115 }
1116 fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
1117 Box::new((self.0, self.1).shrink().map(|(x, y)| Val(x, y)))
1118 }
1119}
1120
1121quickcheck! {
1122 fn minmax(a: Vec<Val>) -> bool {
1123 use itertools::MinMaxResult;
1124
1125
1126 let minmax = a.iter().minmax();
1127 let expected = match a.len() {
1128 0 => MinMaxResult::NoElements,
1129 1 => MinMaxResult::OneElement(&a[0]),
1130 _ => MinMaxResult::MinMax(a.iter().min().unwrap(),
1131 a.iter().max().unwrap()),
1132 };
1133 minmax == expected
1134 }
1135}
1136
1137quickcheck! {
1138 fn minmax_f64(a: Vec<f64>) -> TestResult {
1139 use itertools::MinMaxResult;
1140
1141 if a.iter().any(|x| x.is_nan()) {
1142 return TestResult::discard();
1143 }
1144
1145 let min = cloned(&a).fold1(f64::min);
1146 let max = cloned(&a).fold1(f64::max);
1147
1148 let minmax = cloned(&a).minmax();
1149 let expected = match a.len() {
1150 0 => MinMaxResult::NoElements,
1151 1 => MinMaxResult::OneElement(min.unwrap()),
1152 _ => MinMaxResult::MinMax(min.unwrap(), max.unwrap()),
1153 };
1154 TestResult::from_bool(minmax == expected)
1155 }
1156}
1157
1158quickcheck! {
1159 #[allow(deprecated)]
1160 fn tree_fold1_f64(mut a: Vec<f64>) -> TestResult {
1161 fn collapse_adjacent<F>(x: Vec<f64>, mut f: F) -> Vec<f64>
1162 where F: FnMut(f64, f64) -> f64
1163 {
1164 let mut out = Vec::new();
1165 for i in (0..x.len()).step(2) {
1166 if i == x.len()-1 {
1167 out.push(x[i])
1168 } else {
1169 out.push(f(x[i], x[i+1]));
1170 }
1171 }
1172 out
1173 }
1174
1175 if a.iter().any(|x| x.is_nan()) {
1176 return TestResult::discard();
1177 }
1178
1179 let actual = a.iter().cloned().tree_fold1(f64::atan2);
1180
1181 while a.len() > 1 {
1182 a = collapse_adjacent(a, f64::atan2);
1183 }
1184 let expected = a.pop();
1185
1186 TestResult::from_bool(actual == expected)
1187 }
1188}
1189
1190quickcheck! {
1191 fn exactly_one_i32(a: Vec<i32>) -> TestResult {
1192 let ret = a.iter().cloned().exactly_one();
1193 match a.len() {
1194 1 => TestResult::from_bool(ret.unwrap() == a[0]),
1195 _ => TestResult::from_bool(ret.unwrap_err().eq(a.iter().cloned())),
1196 }
1197 }
1198}
Joel Galenson6f798712021-04-01 17:03:06 -07001199
1200quickcheck! {
1201 fn consistent_grouping_map_with_by(a: Vec<u8>, modulo: u8) -> () {
1202 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1203
1204 let lookup_grouping_map = a.iter().copied().map(|i| (i % modulo, i)).into_grouping_map().collect::<Vec<_>>();
1205 let lookup_grouping_map_by = a.iter().copied().into_grouping_map_by(|i| i % modulo).collect::<Vec<_>>();
1206
1207 assert_eq!(lookup_grouping_map, lookup_grouping_map_by);
1208 }
1209
1210 fn correct_grouping_map_by_aggregate_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1211 let modulo = if modulo < 2 { 2 } else { modulo } as u64; // Avoid `% 0`
1212 let lookup = a.iter()
1213 .map(|&b| b as u64) // Avoid overflows
1214 .into_grouping_map_by(|i| i % modulo)
1215 .aggregate(|acc, &key, val| {
1216 assert!(val % modulo == key);
1217 if val % (modulo - 1) == 0 {
1218 None
1219 } else {
1220 Some(acc.unwrap_or(0) + val)
1221 }
1222 });
1223
1224 let group_map_lookup = a.iter()
1225 .map(|&b| b as u64)
1226 .map(|i| (i % modulo, i))
1227 .into_group_map()
1228 .into_iter()
1229 .filter_map(|(key, vals)| {
1230 vals.into_iter().fold(None, |acc, val| {
1231 if val % (modulo - 1) == 0 {
1232 None
1233 } else {
1234 Some(acc.unwrap_or(0) + val)
1235 }
1236 }).map(|new_val| (key, new_val))
1237 })
1238 .collect::<HashMap<_,_>>();
1239 assert_eq!(lookup, group_map_lookup);
1240
1241 for m in 0..modulo {
1242 assert_eq!(
1243 lookup.get(&m).copied(),
1244 a.iter()
1245 .map(|&b| b as u64)
1246 .filter(|&val| val % modulo == m)
1247 .fold(None, |acc, val| {
1248 if val % (modulo - 1) == 0 {
1249 None
1250 } else {
1251 Some(acc.unwrap_or(0) + val)
1252 }
1253 })
1254 );
1255 }
1256 }
1257
1258 fn correct_grouping_map_by_fold_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1259 let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
1260 let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
1261 .into_grouping_map_by(|i| i % modulo)
1262 .fold(0u64, |acc, &key, val| {
1263 assert!(val % modulo == key);
1264 acc + val
1265 });
1266
1267 let group_map_lookup = a.iter()
1268 .map(|&b| b as u64)
1269 .map(|i| (i % modulo, i))
1270 .into_group_map()
1271 .into_iter()
1272 .map(|(key, vals)| (key, vals.into_iter().fold(0u64, |acc, val| acc + val)))
1273 .collect::<HashMap<_,_>>();
1274 assert_eq!(lookup, group_map_lookup);
1275
1276 for (&key, &sum) in lookup.iter() {
1277 assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
1278 }
1279 }
1280
1281 fn correct_grouping_map_by_fold_first_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1282 let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
1283 let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
1284 .into_grouping_map_by(|i| i % modulo)
1285 .fold_first(|acc, &key, val| {
1286 assert!(val % modulo == key);
1287 acc + val
1288 });
1289
1290 // TODO: Swap `fold1` with stdlib's `fold_first` when it's stabilized
1291 let group_map_lookup = a.iter()
1292 .map(|&b| b as u64)
1293 .map(|i| (i % modulo, i))
1294 .into_group_map()
1295 .into_iter()
1296 .map(|(key, vals)| (key, vals.into_iter().fold1(|acc, val| acc + val).unwrap()))
1297 .collect::<HashMap<_,_>>();
1298 assert_eq!(lookup, group_map_lookup);
1299
1300 for (&key, &sum) in lookup.iter() {
1301 assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
1302 }
1303 }
1304
1305 fn correct_grouping_map_by_collect_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1306 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1307 let lookup_grouping_map = a.iter().copied().into_grouping_map_by(|i| i % modulo).collect::<Vec<_>>();
1308 let lookup_group_map = a.iter().copied().map(|i| (i % modulo, i)).into_group_map();
1309
1310 assert_eq!(lookup_grouping_map, lookup_group_map);
1311 }
1312
1313 fn correct_grouping_map_by_max_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1314 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1315 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).max();
1316
1317 let group_map_lookup = a.iter().copied()
1318 .map(|i| (i % modulo, i))
1319 .into_group_map()
1320 .into_iter()
1321 .map(|(key, vals)| (key, vals.into_iter().max().unwrap()))
1322 .collect::<HashMap<_,_>>();
1323 assert_eq!(lookup, group_map_lookup);
1324
1325 for (&key, &max) in lookup.iter() {
1326 assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max());
1327 }
1328 }
1329
1330 fn correct_grouping_map_by_max_by_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1331 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1332 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).max_by(|_, v1, v2| v1.cmp(v2));
1333
1334 let group_map_lookup = a.iter().copied()
1335 .map(|i| (i % modulo, i))
1336 .into_group_map()
1337 .into_iter()
1338 .map(|(key, vals)| (key, vals.into_iter().max_by(|v1, v2| v1.cmp(v2)).unwrap()))
1339 .collect::<HashMap<_,_>>();
1340 assert_eq!(lookup, group_map_lookup);
1341
1342 for (&key, &max) in lookup.iter() {
1343 assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max_by(|v1, v2| v1.cmp(v2)));
1344 }
1345 }
1346
1347 fn correct_grouping_map_by_max_by_key_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1348 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1349 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).max_by_key(|_, &val| val);
1350
1351 let group_map_lookup = a.iter().copied()
1352 .map(|i| (i % modulo, i))
1353 .into_group_map()
1354 .into_iter()
1355 .map(|(key, vals)| (key, vals.into_iter().max_by_key(|&val| val).unwrap()))
1356 .collect::<HashMap<_,_>>();
1357 assert_eq!(lookup, group_map_lookup);
1358
1359 for (&key, &max) in lookup.iter() {
1360 assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max_by_key(|&val| val));
1361 }
1362 }
1363
1364 fn correct_grouping_map_by_min_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1365 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1366 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min();
1367
1368 let group_map_lookup = a.iter().copied()
1369 .map(|i| (i % modulo, i))
1370 .into_group_map()
1371 .into_iter()
1372 .map(|(key, vals)| (key, vals.into_iter().min().unwrap()))
1373 .collect::<HashMap<_,_>>();
1374 assert_eq!(lookup, group_map_lookup);
1375
1376 for (&key, &min) in lookup.iter() {
1377 assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min());
1378 }
1379 }
1380
1381 fn correct_grouping_map_by_min_by_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1382 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1383 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min_by(|_, v1, v2| v1.cmp(v2));
1384
1385 let group_map_lookup = a.iter().copied()
1386 .map(|i| (i % modulo, i))
1387 .into_group_map()
1388 .into_iter()
1389 .map(|(key, vals)| (key, vals.into_iter().min_by(|v1, v2| v1.cmp(v2)).unwrap()))
1390 .collect::<HashMap<_,_>>();
1391 assert_eq!(lookup, group_map_lookup);
1392
1393 for (&key, &min) in lookup.iter() {
1394 assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min_by(|v1, v2| v1.cmp(v2)));
1395 }
1396 }
1397
1398 fn correct_grouping_map_by_min_by_key_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1399 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1400 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min_by_key(|_, &val| val);
1401
1402 let group_map_lookup = a.iter().copied()
1403 .map(|i| (i % modulo, i))
1404 .into_group_map()
1405 .into_iter()
1406 .map(|(key, vals)| (key, vals.into_iter().min_by_key(|&val| val).unwrap()))
1407 .collect::<HashMap<_,_>>();
1408 assert_eq!(lookup, group_map_lookup);
1409
1410 for (&key, &min) in lookup.iter() {
1411 assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min_by_key(|&val| val));
1412 }
1413 }
1414
1415 fn correct_grouping_map_by_minmax_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1416 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1417 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax();
1418
1419 let group_map_lookup = a.iter().copied()
1420 .map(|i| (i % modulo, i))
1421 .into_group_map()
1422 .into_iter()
1423 .map(|(key, vals)| (key, vals.into_iter().minmax()))
1424 .collect::<HashMap<_,_>>();
1425 assert_eq!(lookup, group_map_lookup);
1426
1427 for (&key, &minmax) in lookup.iter() {
1428 assert_eq!(minmax, a.iter().copied().filter(|&val| val % modulo == key).minmax());
1429 }
1430 }
1431
1432 fn correct_grouping_map_by_minmax_by_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1433 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1434 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax_by(|_, v1, v2| v1.cmp(v2));
1435
1436 let group_map_lookup = a.iter().copied()
1437 .map(|i| (i % modulo, i))
1438 .into_group_map()
1439 .into_iter()
1440 .map(|(key, vals)| (key, vals.into_iter().minmax_by(|v1, v2| v1.cmp(v2))))
1441 .collect::<HashMap<_,_>>();
1442 assert_eq!(lookup, group_map_lookup);
1443
1444 for (&key, &minmax) in lookup.iter() {
1445 assert_eq!(minmax, a.iter().copied().filter(|&val| val % modulo == key).minmax_by(|v1, v2| v1.cmp(v2)));
1446 }
1447 }
1448
1449 fn correct_grouping_map_by_minmax_by_key_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1450 let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
1451 let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax_by_key(|_, &val| val);
1452
1453 let group_map_lookup = a.iter().copied()
1454 .map(|i| (i % modulo, i))
1455 .into_group_map()
1456 .into_iter()
1457 .map(|(key, vals)| (key, vals.into_iter().minmax_by_key(|&val| val)))
1458 .collect::<HashMap<_,_>>();
1459 assert_eq!(lookup, group_map_lookup);
1460
1461 for (&key, &minmax) in lookup.iter() {
1462 assert_eq!(minmax, a.iter().copied().filter(|&val| val % modulo == key).minmax_by_key(|&val| val));
1463 }
1464 }
1465
1466 fn correct_grouping_map_by_sum_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1467 let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
1468 let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
1469 .into_grouping_map_by(|i| i % modulo)
1470 .sum();
1471
1472 let group_map_lookup = a.iter().map(|&b| b as u64)
1473 .map(|i| (i % modulo, i))
1474 .into_group_map()
1475 .into_iter()
1476 .map(|(key, vals)| (key, vals.into_iter().sum()))
1477 .collect::<HashMap<_,_>>();
1478 assert_eq!(lookup, group_map_lookup);
1479
1480 for (&key, &sum) in lookup.iter() {
1481 assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
1482 }
1483 }
1484
1485 fn correct_grouping_map_by_product_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1486 let modulo = Wrapping(if modulo == 0 { 1 } else { modulo } as u64); // Avoid `% 0`
1487 let lookup = a.iter().map(|&b| Wrapping(b as u64)) // Avoid overflows
1488 .into_grouping_map_by(|i| i % modulo)
1489 .product();
1490
1491 let group_map_lookup = a.iter().map(|&b| Wrapping(b as u64))
1492 .map(|i| (i % modulo, i))
1493 .into_group_map()
1494 .into_iter()
1495 .map(|(key, vals)| (key, vals.into_iter().product::<Wrapping<u64>>()))
1496 .collect::<HashMap<_,_>>();
1497 assert_eq!(lookup, group_map_lookup);
1498
1499 for (&key, &prod) in lookup.iter() {
1500 assert_eq!(
1501 prod,
1502 a.iter()
1503 .map(|&b| Wrapping(b as u64))
1504 .filter(|&val| val % modulo == key)
1505 .product::<Wrapping<u64>>()
1506 );
1507 }
1508 }
1509
1510 // This should check that if multiple elements are equally minimum or maximum
1511 // then `max`, `min` and `minmax` pick the first minimum and the last maximum.
1512 // This is to be consistent with `std::iter::max` and `std::iter::min`.
1513 fn correct_grouping_map_by_min_max_minmax_order_modulo_key() -> () {
1514 use itertools::MinMaxResult;
1515
1516 let lookup = (0..=10)
1517 .into_grouping_map_by(|_| 0)
1518 .max_by(|_, _, _| Ordering::Equal);
1519
1520 assert_eq!(lookup[&0], 10);
1521
1522 let lookup = (0..=10)
1523 .into_grouping_map_by(|_| 0)
1524 .min_by(|_, _, _| Ordering::Equal);
1525
1526 assert_eq!(lookup[&0], 0);
1527
1528 let lookup = (0..=10)
1529 .into_grouping_map_by(|_| 0)
1530 .minmax_by(|_, _, _| Ordering::Equal);
1531
1532 assert_eq!(lookup[&0], MinMaxResult::MinMax(0, 10));
1533 }
1534}
1535
1536quickcheck! {
1537 #[test]
1538 fn counts(nums: Vec<isize>) -> TestResult {
1539 let counts = nums.iter().counts();
1540 for (&item, &count) in counts.iter() {
1541 if count <= 0 {
1542 return TestResult::failed();
1543 }
1544 if count != nums.iter().filter(|&x| x == item).count() {
1545 return TestResult::failed();
1546 }
1547 }
1548 for item in nums.iter() {
1549 if !counts.contains_key(item) {
1550 return TestResult::failed();
1551 }
1552 }
1553 TestResult::passed()
1554 }
1555}
1556
1557quickcheck! {
1558 fn test_double_ended_zip_2(a: Vec<u8>, b: Vec<u8>) -> TestResult {
1559 let mut x =
1560 multizip((a.clone().into_iter(), b.clone().into_iter()))
1561 .collect_vec();
1562 x.reverse();
1563
1564 let y =
1565 multizip((a.into_iter(), b.into_iter()))
1566 .rfold(Vec::new(), |mut vec, e| { vec.push(e); vec });
1567
1568 TestResult::from_bool(itertools::equal(x, y))
1569 }
1570
1571 fn test_double_ended_zip_3(a: Vec<u8>, b: Vec<u8>, c: Vec<u8>) -> TestResult {
1572 let mut x =
1573 multizip((a.clone().into_iter(), b.clone().into_iter(), c.clone().into_iter()))
1574 .collect_vec();
1575 x.reverse();
1576
1577 let y =
1578 multizip((a.into_iter(), b.into_iter(), c.into_iter()))
1579 .rfold(Vec::new(), |mut vec, e| { vec.push(e); vec });
1580
1581 TestResult::from_bool(itertools::equal(x, y))
1582 }
1583}