Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 1 | use itertools::Itertools; |
| 2 | |
| 3 | struct PanickingCounter { |
| 4 | curr: usize, |
| 5 | max: usize, |
| 6 | } |
| 7 | |
| 8 | impl Iterator for PanickingCounter { |
| 9 | type Item = (); |
| 10 | |
| 11 | fn next(&mut self) -> Option<Self::Item> { |
| 12 | self.curr += 1; |
| 13 | |
| 14 | if self.curr == self.max { |
| 15 | panic!( |
| 16 | "Input iterator reached maximum of {} suggesting collection by adaptor", |
| 17 | self.max |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | Some(()) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn no_collect_test<A, T>(to_adaptor: T) |
| 26 | where A: Iterator, T: Fn(PanickingCounter) -> A |
| 27 | { |
| 28 | let counter = PanickingCounter { curr: 0, max: 10_000 }; |
| 29 | let adaptor = to_adaptor(counter); |
| 30 | |
| 31 | for _ in adaptor.take(5) {} |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn permutations_no_collect() { |
| 36 | no_collect_test(|iter| iter.permutations(5)) |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn combinations_no_collect() { |
| 41 | no_collect_test(|iter| iter.combinations(5)) |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn combinations_with_replacement_no_collect() { |
| 46 | no_collect_test(|iter| iter.combinations_with_replacement(5)) |
| 47 | } |