quick.rs 52 KB

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