|
| 1 | +#![allow(unstable_name_collisions, deprecated)] |
| 2 | + |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::collections::BTreeMap; |
| 5 | + |
| 6 | +use itertools::Itertools; |
| 7 | + |
| 8 | +#[test] |
| 9 | +// Force to explicit `let _ = ...;` to help find missing `must_use` attributes manually. |
| 10 | +// Only helpful if you don't add it before seeing if there is an error or not. |
| 11 | +#[deny(unused_must_use)] |
| 12 | +fn laziness() { |
| 13 | + let used_ids = RefCell::<BTreeMap<_, usize>>::default(); |
| 14 | + let not_lazy = |s: &'static str| -> u8 { |
| 15 | + *used_ids.borrow_mut().entry(s).or_default() += 1; |
| 16 | + 0 |
| 17 | + }; |
| 18 | + let it = |s: &'static str| (0..20).map(move |_| not_lazy(s)); |
| 19 | + // Itertools trait: |
| 20 | + let _ = it("interleave-1").interleave(it("interleave-2")); |
| 21 | + let _ = it("interleave_shortest-1").interleave_shortest(it("interleave_shortest-2")); |
| 22 | + let _ = it("intersperse").intersperse(0); |
| 23 | + let _ = it("intersperse_with").intersperse_with(|| 0); |
| 24 | + let _ = it("zip_longest-1").zip_longest(it("zip_longest-2")); |
| 25 | + let _ = it("zip_eq-1").zip_eq(it("zip_eq-2")); |
| 26 | + let _ = it("batching").batching(Iterator::next); |
| 27 | + let _ = it("group_by").group_by(|x| *x); |
| 28 | + let _ = it("chunks-1").chunks(1); |
| 29 | + let _ = it("chunks-2").chunks(2); |
| 30 | + let _ = it("tuple_windows::<(_,)>").tuple_windows::<(_,)>(); |
| 31 | + let _ = it("tuple_windows::<(_,_)>").tuple_windows::<(_, _)>(); |
| 32 | + let _ = it("tuple_windows::<(_,_,_)>").tuple_windows::<(_, _, _)>(); |
| 33 | + let _ = it("circular_tuple_windows::<(_,)>").circular_tuple_windows::<(_,)>(); |
| 34 | + let _ = it("circular_tuple_windows::<(_,_)>").circular_tuple_windows::<(_, _)>(); |
| 35 | + let _ = it("circular_tuple_windows::<(_,_,_)>").circular_tuple_windows::<(_, _, _)>(); |
| 36 | + let _ = it("tuples::<(_,)>").tuples::<(_,)>(); |
| 37 | + let _ = it("tuples::<(_,_)>").tuples::<(_, _)>(); |
| 38 | + let _ = it("tuples::<(_,_,_)>").tuples::<(_, _, _)>(); |
| 39 | + let (_, _) = it("tee").tee(); |
| 40 | + let _ = it("step").step(2); |
| 41 | + let _ = it("map_into").map_into::<u16>(); |
| 42 | + let _ = it("map_ok").map(Ok::<u8, ()>).map_ok(|x| x + 1); |
| 43 | + let _ = it("filter_ok").map(Ok::<u8, ()>).filter_ok(|x| x % 2 == 0); |
| 44 | + let _ = it("filter_map_ok").map(Ok::<u8, ()>).filter_map_ok(|x| { |
| 45 | + if x % 2 == 0 { |
| 46 | + Some(x + 1) |
| 47 | + } else { |
| 48 | + None |
| 49 | + } |
| 50 | + }); |
| 51 | + let _ = it("flatten_ok").map(|x| Ok::<_, ()>([x])).flatten_ok(); |
| 52 | + let _ = it("merge-1").merge(it("merge-2")); |
| 53 | + let _ = it("merge_by-1").merge_by(it("merge_by-2"), |_, _| true); |
| 54 | + let _ = it("merge_join_by:bool-1").merge_join_by(it("merge_join_by:bool-2"), |_, _| true); |
| 55 | + let _ = it("merge_join_by:Ordering-1").merge_join_by(it("merge_join_by:Ordering-2"), Ord::cmp); |
| 56 | + let _ = it("kmerge").map(|_| it("kmerge-nested")).kmerge(); |
| 57 | + let _ = it("kmerge_by") |
| 58 | + .map(|_| it("kmerge_by-nested")) |
| 59 | + .kmerge_by(|_, _| true); |
| 60 | + let _ = it("cartesian_product-1").cartesian_product(it("cartesian_product-2")); |
| 61 | + let _ = vec![ |
| 62 | + it("multi_cartesian_product-1"), |
| 63 | + it("multi_cartesian_product-2"), |
| 64 | + it("multi_cartesian_product-3"), |
| 65 | + ] |
| 66 | + .into_iter() |
| 67 | + .multi_cartesian_product(); |
| 68 | + let _ = it("coalesce").coalesce(|x, y| if x == y { Ok(x) } else { Err((x, y)) }); |
| 69 | + let _ = it("dedup").dedup(); |
| 70 | + let _ = it("dedup_by").dedup_by(|_, _| true); |
| 71 | + let _ = it("dedup_with_count").dedup_with_count(); |
| 72 | + let _ = it("dedup_by_with_count").dedup_by_with_count(|_, _| true); |
| 73 | + let _ = it("duplicates").duplicates(); |
| 74 | + let _ = it("duplicates_by").duplicates_by(|x| *x); |
| 75 | + let _ = it("unique").unique(); |
| 76 | + let _ = it("unique_by").unique_by(|x| *x); |
| 77 | + let _ = it("peeking_take_while") |
| 78 | + .peekable() |
| 79 | + .peeking_take_while(|x| x % 2 == 0); |
| 80 | + let _ = it("take_while_ref").take_while_ref(|x| x % 2 == 0); |
| 81 | + let _ = it("take_while_inclusive").take_while_inclusive(|x| x % 2 == 0); |
| 82 | + let _ = it("while_some").map(Some).while_some(); |
| 83 | + let _ = it("tuple_combinations::<(_,)>").tuple_combinations::<(_,)>(); |
| 84 | + let _ = it("tuple_combinations::<(_,_)>").tuple_combinations::<(_, _)>(); |
| 85 | + let _ = it("tuple_combinations::<(_,_,_)>").tuple_combinations::<(_, _, _)>(); |
| 86 | + let _ = it("combinations(0)").combinations(0); |
| 87 | + let _ = it("combinations(1)").combinations(1); |
| 88 | + let _ = it("combinations(2)").combinations(2); |
| 89 | + let _ = it("combinations(3)").combinations(3); |
| 90 | + it("combinations_with_replacement(0)").combinations_with_replacement(0); |
| 91 | + it("combinations_with_replacement(1)").combinations_with_replacement(1); |
| 92 | + it("combinations_with_replacement(2)").combinations_with_replacement(2); |
| 93 | + it("combinations_with_replacement(3)").combinations_with_replacement(3); |
| 94 | + let _ = it("permutations(0)").permutations(0); |
| 95 | + let _ = it("permutations(1)").permutations(1); |
| 96 | + let _ = it("permutations(2)").permutations(2); |
| 97 | + let _ = it("permutations(3)").permutations(3); |
| 98 | + let _ = it("powerset").powerset(); |
| 99 | + let _ = it("pad_using").pad_using(25, |_| 10); |
| 100 | + let _ = it("with_position").with_position(); |
| 101 | + let _ = it("positions").positions(|v| v % 2 == 0); |
| 102 | + let _ = it("update").update(|n| *n += 1); |
| 103 | + it("multipeek").multipeek(); |
| 104 | + // Not iterator themselves but still lazy. |
| 105 | + let _ = it("into_grouping_map") |
| 106 | + .map(|x| (x, x + 1)) |
| 107 | + .into_grouping_map(); |
| 108 | + let _ = it("into_grouping_map_by").into_grouping_map_by(|x| *x); |
| 109 | + // Macros: |
| 110 | + let _ = itertools::iproduct!(it("iproduct-1")); |
| 111 | + let _ = itertools::iproduct!(it("iproduct-2"), it("iproduct-3")); |
| 112 | + let _ = itertools::iproduct!(it("iproduct-4"), it("iproduct-5"), it("iproduct-6")); |
| 113 | + let _ = itertools::izip!(it("izip-1")); |
| 114 | + let _ = itertools::izip!(it("izip-2"), it("izip-3")); |
| 115 | + let _ = itertools::izip!(it("izip-4"), it("izip-5"), it("izip-6")); |
| 116 | + let _ = itertools::chain!(it("chain-1")); |
| 117 | + let _ = itertools::chain!(it("chain-2"), it("chain-3")); |
| 118 | + let _ = itertools::chain!(it("chain-4"), it("chain-5"), it("chain-6")); |
| 119 | + // Free functions: |
| 120 | + let _ = itertools::multizip((it("multizip-1"), it("multizip-2"))); |
| 121 | + itertools::put_back(it("put_back")); |
| 122 | + itertools::put_back(it("put_back")).with_value(15); |
| 123 | + itertools::peek_nth(it("peek_nth")); |
| 124 | + itertools::put_back_n(it("put_back_n")); |
| 125 | + itertools::rciter(it("rciter")); |
| 126 | + assert!(used_ids.borrow().is_empty(), "{:#?}", used_ids.borrow()); |
| 127 | +} |
0 commit comments