|
| 1 | +extern crate itertools; |
| 2 | + |
| 3 | +#[macro_use] |
| 4 | +extern crate quickcheck; |
| 5 | + |
| 6 | +use itertools::{EitherOrBoth, Itertools}; |
| 7 | + |
| 8 | +use std::fmt::Debug; |
| 9 | +use std::ops::BitXor; |
| 10 | + |
| 11 | +struct Unspecialized<I>(I); |
| 12 | +impl<I> Iterator for Unspecialized<I> |
| 13 | +where |
| 14 | + I: Iterator, |
| 15 | +{ |
| 16 | + type Item = I::Item; |
| 17 | + |
| 18 | + #[inline(always)] |
| 19 | + fn next(&mut self) -> Option<I::Item> { |
| 20 | + self.0.next() |
| 21 | + } |
| 22 | + |
| 23 | + #[inline(always)] |
| 24 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 25 | + self.0.size_hint() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +fn check_specialized<'a, V, IterItem, Iter, F>(iterator: &Iter, mapper: F) |
| 30 | +where |
| 31 | + V: Eq + Debug, |
| 32 | + IterItem: 'a, |
| 33 | + Iter: Iterator<Item = IterItem> + Clone + 'a, |
| 34 | + F: Fn(Box<Iterator<Item = IterItem> + 'a>) -> V, |
| 35 | +{ |
| 36 | + assert_eq!( |
| 37 | + mapper(Box::new(Unspecialized(iterator.clone()))), |
| 38 | + mapper(Box::new(iterator.clone())) |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +fn check_specialized_count_last_nth_sizeh<'a, IterItem, Iter>( |
| 43 | + it: &Iter, |
| 44 | + known_expected_size: Option<usize>, |
| 45 | +) where |
| 46 | + IterItem: 'a + Eq + Debug, |
| 47 | + Iter: Iterator<Item = IterItem> + Clone + 'a, |
| 48 | +{ |
| 49 | + let size = it.clone().count(); |
| 50 | + if let Some(expected_size) = known_expected_size { |
| 51 | + assert_eq!(size, expected_size); |
| 52 | + } |
| 53 | + check_specialized(it, |i| i.count()); |
| 54 | + check_specialized(it, |i| i.last()); |
| 55 | + for n in 0..size + 2 { |
| 56 | + check_specialized(it, |mut i| i.nth(n)); |
| 57 | + } |
| 58 | + let mut it_sh = it.clone(); |
| 59 | + for n in 0..size + 2 { |
| 60 | + let len = it_sh.clone().count(); |
| 61 | + let (min, max) = it_sh.size_hint(); |
| 62 | + assert_eq!((size - n.min(size)), len); |
| 63 | + assert!(min <= len); |
| 64 | + if let Some(max) = max { |
| 65 | + assert!(len <= max); |
| 66 | + } |
| 67 | + it_sh.next(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn check_specialized_fold_xor<'a, IterItem, Iter>(it: &Iter) |
| 72 | +where |
| 73 | + IterItem: 'a |
| 74 | + + BitXor |
| 75 | + + Eq |
| 76 | + + Debug |
| 77 | + + BitXor<<IterItem as BitXor>::Output, Output = <IterItem as BitXor>::Output> |
| 78 | + + Clone, |
| 79 | + <IterItem as BitXor>::Output: |
| 80 | + BitXor<Output = <IterItem as BitXor>::Output> + Eq + Debug + Clone, |
| 81 | + Iter: Iterator<Item = IterItem> + Clone + 'a, |
| 82 | +{ |
| 83 | + check_specialized(it, |mut i| { |
| 84 | + let first = i.next().map(|f| f.clone() ^ (f.clone() ^ f)); |
| 85 | + i.fold(first, |acc, v: IterItem| acc.map(move |a| v ^ a)) |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +fn put_back_test(test_vec: Vec<i32>, known_expected_size: Option<usize>) { |
| 90 | + { |
| 91 | + // Lexical lifetimes support |
| 92 | + let pb = itertools::put_back(test_vec.iter()); |
| 93 | + check_specialized_count_last_nth_sizeh(&pb, known_expected_size); |
| 94 | + check_specialized_fold_xor(&pb); |
| 95 | + } |
| 96 | + |
| 97 | + let mut pb = itertools::put_back(test_vec.into_iter()); |
| 98 | + pb.put_back(1); |
| 99 | + check_specialized_count_last_nth_sizeh(&pb, known_expected_size.map(|x| x + 1)); |
| 100 | + check_specialized_fold_xor(&pb) |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn put_back() { |
| 105 | + put_back_test(vec![7, 4, 1], Some(3)); |
| 106 | +} |
| 107 | + |
| 108 | +quickcheck! { |
| 109 | + fn put_back_qc(test_vec: Vec<i32>) -> () { |
| 110 | + put_back_test(test_vec, None) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +fn merge_join_by_test(i1: Vec<usize>, i2: Vec<usize>, known_expected_size: Option<usize>) { |
| 115 | + let i1 = i1.into_iter(); |
| 116 | + let i2 = i2.into_iter(); |
| 117 | + let mjb = i1.clone().merge_join_by(i2.clone(), std::cmp::Ord::cmp); |
| 118 | + check_specialized_count_last_nth_sizeh(&mjb, known_expected_size); |
| 119 | + // Rust 1.24 compatibility: |
| 120 | + fn eob_left_z(eob: EitherOrBoth<usize, usize>) -> usize { |
| 121 | + eob.left().unwrap_or(0) |
| 122 | + } |
| 123 | + fn eob_right_z(eob: EitherOrBoth<usize, usize>) -> usize { |
| 124 | + eob.left().unwrap_or(0) |
| 125 | + } |
| 126 | + fn eob_both_z(eob: EitherOrBoth<usize, usize>) -> usize { |
| 127 | + let (a, b) = eob.both().unwrap_or((0, 0)); |
| 128 | + assert_eq!(a, b); |
| 129 | + a |
| 130 | + } |
| 131 | + check_specialized_fold_xor(&mjb.clone().map(eob_left_z)); |
| 132 | + check_specialized_fold_xor(&mjb.clone().map(eob_right_z)); |
| 133 | + check_specialized_fold_xor(&mjb.clone().map(eob_both_z)); |
| 134 | + |
| 135 | + // And the other way around |
| 136 | + let mjb = i2.merge_join_by(i1, std::cmp::Ord::cmp); |
| 137 | + check_specialized_count_last_nth_sizeh(&mjb, known_expected_size); |
| 138 | + check_specialized_fold_xor(&mjb.clone().map(eob_left_z)); |
| 139 | + check_specialized_fold_xor(&mjb.clone().map(eob_right_z)); |
| 140 | + check_specialized_fold_xor(&mjb.clone().map(eob_both_z)); |
| 141 | +} |
| 142 | + |
| 143 | +#[test] |
| 144 | +fn merge_join_by() { |
| 145 | + let i1 = vec![1, 3, 5, 7, 8, 9]; |
| 146 | + let i2 = vec![0, 3, 4, 5]; |
| 147 | + merge_join_by_test(i1, i2, Some(8)); |
| 148 | +} |
| 149 | + |
| 150 | +quickcheck! { |
| 151 | + fn merge_join_by_qc(i1: Vec<usize>, i2: Vec<usize>) -> () { |
| 152 | + merge_join_by_test(i1, i2, None) |
| 153 | + } |
| 154 | +} |
0 commit comments