-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d188e8e
commit 922ca9f
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#![allow(unstable_name_collisions, deprecated)] | ||
|
||
use std::cell::RefCell; | ||
use std::collections::BTreeMap; | ||
|
||
use itertools::Itertools; | ||
|
||
#[test] | ||
// Force to explicit `let _ = ...;` to help find missing `must_use` attributes manually. | ||
// Only helpful if you don't add it before seeing if there is an error or not. | ||
#[deny(unused_must_use)] | ||
fn laziness() { | ||
let used_ids = RefCell::<BTreeMap<_, usize>>::default(); | ||
let not_lazy = |s: &'static str| -> u8 { | ||
*used_ids.borrow_mut().entry(s).or_default() += 1; | ||
0 | ||
}; | ||
let it = |s: &'static str| (0..20).map(move |_| not_lazy(s)); | ||
// Itertools trait: | ||
let _ = it("interleave-1").interleave(it("interleave-2")); | ||
let _ = it("interleave_shortest-1").interleave_shortest(it("interleave_shortest-2")); | ||
let _ = it("intersperse").intersperse(0); | ||
let _ = it("intersperse_with").intersperse_with(|| 0); | ||
let _ = it("zip_longest-1").zip_longest(it("zip_longest-2")); | ||
let _ = it("zip_eq-1").zip_eq(it("zip_eq-2")); | ||
let _ = it("batching").batching(Iterator::next); | ||
let _ = it("group_by").group_by(|x| *x); | ||
let _ = it("chunks-1").chunks(1); | ||
let _ = it("chunks-2").chunks(2); | ||
let _ = it("tuple_windows::<(_,)>").tuple_windows::<(_,)>(); | ||
let _ = it("tuple_windows::<(_,_)>").tuple_windows::<(_, _)>(); | ||
let _ = it("tuple_windows::<(_,_,_)>").tuple_windows::<(_, _, _)>(); | ||
let _ = it("circular_tuple_windows::<(_,)>").circular_tuple_windows::<(_,)>(); | ||
let _ = it("circular_tuple_windows::<(_,_)>").circular_tuple_windows::<(_, _)>(); | ||
let _ = it("circular_tuple_windows::<(_,_,_)>").circular_tuple_windows::<(_, _, _)>(); | ||
let _ = it("tuples::<(_,)>").tuples::<(_,)>(); | ||
let _ = it("tuples::<(_,_)>").tuples::<(_, _)>(); | ||
let _ = it("tuples::<(_,_,_)>").tuples::<(_, _, _)>(); | ||
let (_, _) = it("tee").tee(); | ||
let _ = it("step").step(2); | ||
let _ = it("map_into").map_into::<u16>(); | ||
let _ = it("map_ok").map(Ok::<u8, ()>).map_ok(|x| x + 1); | ||
let _ = it("filter_ok").map(Ok::<u8, ()>).filter_ok(|x| x % 2 == 0); | ||
let _ = it("filter_map_ok").map(Ok::<u8, ()>).filter_map_ok(|x| { | ||
if x % 2 == 0 { | ||
Some(x + 1) | ||
} else { | ||
None | ||
} | ||
}); | ||
let _ = it("flatten_ok").map(|x| Ok::<_, ()>([x])).flatten_ok(); | ||
let _ = it("merge-1").merge(it("merge-2")); | ||
let _ = it("merge_by-1").merge_by(it("merge_by-2"), |_, _| true); | ||
let _ = it("merge_join_by:bool-1").merge_join_by(it("merge_join_by:bool-2"), |_, _| true); | ||
let _ = it("merge_join_by:Ordering-1").merge_join_by(it("merge_join_by:Ordering-2"), Ord::cmp); | ||
let _ = it("kmerge").map(|_| it("kmerge-nested")).kmerge(); | ||
let _ = it("kmerge_by") | ||
.map(|_| it("kmerge_by-nested")) | ||
.kmerge_by(|_, _| true); | ||
let _ = it("cartesian_product-1").cartesian_product(it("cartesian_product-2")); | ||
let _ = vec![ | ||
it("multi_cartesian_product-1"), | ||
it("multi_cartesian_product-2"), | ||
it("multi_cartesian_product-3"), | ||
] | ||
.into_iter() | ||
.multi_cartesian_product(); | ||
let _ = it("coalesce").coalesce(|x, y| if x == y { Ok(x) } else { Err((x, y)) }); | ||
let _ = it("dedup").dedup(); | ||
let _ = it("dedup_by").dedup_by(|_, _| true); | ||
let _ = it("dedup_with_count").dedup_with_count(); | ||
let _ = it("dedup_by_with_count").dedup_by_with_count(|_, _| true); | ||
let _ = it("duplicates").duplicates(); | ||
let _ = it("duplicates_by").duplicates_by(|x| *x); | ||
let _ = it("unique").unique(); | ||
let _ = it("unique_by").unique_by(|x| *x); | ||
let _ = it("peeking_take_while") | ||
.peekable() | ||
.peeking_take_while(|x| x % 2 == 0); | ||
let _ = it("take_while_ref").take_while_ref(|x| x % 2 == 0); | ||
let _ = it("take_while_inclusive").take_while_inclusive(|x| x % 2 == 0); | ||
let _ = it("while_some").map(Some).while_some(); | ||
let _ = it("tuple_combinations::<(_,)>").tuple_combinations::<(_,)>(); | ||
let _ = it("tuple_combinations::<(_,_)>").tuple_combinations::<(_, _)>(); | ||
let _ = it("tuple_combinations::<(_,_,_)>").tuple_combinations::<(_, _, _)>(); | ||
let _ = it("combinations(0)").combinations(0); | ||
let _ = it("combinations(1)").combinations(1); | ||
let _ = it("combinations(2)").combinations(2); | ||
let _ = it("combinations(3)").combinations(3); | ||
it("combinations_with_replacement(0)").combinations_with_replacement(0); | ||
it("combinations_with_replacement(1)").combinations_with_replacement(1); | ||
it("combinations_with_replacement(2)").combinations_with_replacement(2); | ||
it("combinations_with_replacement(3)").combinations_with_replacement(3); | ||
let _ = it("permutations(0)").permutations(0); | ||
let _ = it("permutations(1)").permutations(1); | ||
let _ = it("permutations(2)").permutations(2); | ||
let _ = it("permutations(3)").permutations(3); | ||
let _ = it("powerset").powerset(); | ||
let _ = it("pad_using").pad_using(25, |_| 10); | ||
let _ = it("with_position").with_position(); | ||
let _ = it("positions").positions(|v| v % 2 == 0); | ||
let _ = it("update").update(|n| *n += 1); | ||
it("multipeek").multipeek(); | ||
it("format").format(","); | ||
it("format_with").format_with(", ", |elt, f| f(&elt)); | ||
// Not iterator themselves but still lazy. | ||
let _ = it("into_grouping_map") | ||
.map(|x| (x, x + 1)) | ||
.into_grouping_map(); | ||
let _ = it("into_grouping_map_by").into_grouping_map_by(|x| *x); | ||
// Macros: | ||
let _ = itertools::iproduct!(it("iproduct-1")); | ||
let _ = itertools::iproduct!(it("iproduct-2"), it("iproduct-3")); | ||
let _ = itertools::iproduct!(it("iproduct-4"), it("iproduct-5"), it("iproduct-6")); | ||
let _ = itertools::izip!(it("izip-1")); | ||
let _ = itertools::izip!(it("izip-2"), it("izip-3")); | ||
let _ = itertools::izip!(it("izip-4"), it("izip-5"), it("izip-6")); | ||
let _ = itertools::chain!(it("chain-1")); | ||
let _ = itertools::chain!(it("chain-2"), it("chain-3")); | ||
let _ = itertools::chain!(it("chain-4"), it("chain-5"), it("chain-6")); | ||
// Free functions: | ||
let _ = itertools::multizip((it("multizip-1"), it("multizip-2"))); | ||
itertools::put_back(it("put_back")); | ||
itertools::put_back(it("put_back")).with_value(15); | ||
itertools::peek_nth(it("peek_nth")); | ||
itertools::put_back_n(it("put_back_n")); | ||
itertools::rciter(it("rciter")); | ||
assert!(used_ids.borrow().is_empty(), "{:#?}", used_ids.borrow()); | ||
} |