1 | use itertools::Itertools; |
2 | |
3 | #[test] |
4 | fn tuples() { |
5 | let v = [1, 2, 3, 4, 5]; |
6 | let mut iter = v.iter().cloned().tuples(); |
7 | assert_eq!(Some((1,)), iter.next()); |
8 | assert_eq!(Some((2,)), iter.next()); |
9 | assert_eq!(Some((3,)), iter.next()); |
10 | assert_eq!(Some((4,)), iter.next()); |
11 | assert_eq!(Some((5,)), iter.next()); |
12 | assert_eq!(None, iter.next()); |
13 | assert_eq!(None, iter.into_buffer().next()); |
14 | |
15 | let mut iter = v.iter().cloned().tuples(); |
16 | assert_eq!(Some((1, 2)), iter.next()); |
17 | assert_eq!(Some((3, 4)), iter.next()); |
18 | assert_eq!(None, iter.next()); |
19 | itertools::assert_equal(vec![5], iter.into_buffer()); |
20 | |
21 | let mut iter = v.iter().cloned().tuples(); |
22 | assert_eq!(Some((1, 2, 3)), iter.next()); |
23 | assert_eq!(None, iter.next()); |
24 | itertools::assert_equal(vec![4, 5], iter.into_buffer()); |
25 | |
26 | let mut iter = v.iter().cloned().tuples(); |
27 | assert_eq!(Some((1, 2, 3, 4)), iter.next()); |
28 | assert_eq!(None, iter.next()); |
29 | itertools::assert_equal(vec![5], iter.into_buffer()); |
30 | } |
31 | |
32 | #[test] |
33 | fn tuple_windows() { |
34 | let v = [1, 2, 3, 4, 5]; |
35 | |
36 | let mut iter = v.iter().cloned().tuple_windows(); |
37 | assert_eq!(Some((1,)), iter.next()); |
38 | assert_eq!(Some((2,)), iter.next()); |
39 | assert_eq!(Some((3,)), iter.next()); |
40 | |
41 | let mut iter = v.iter().cloned().tuple_windows(); |
42 | assert_eq!(Some((1, 2)), iter.next()); |
43 | assert_eq!(Some((2, 3)), iter.next()); |
44 | assert_eq!(Some((3, 4)), iter.next()); |
45 | assert_eq!(Some((4, 5)), iter.next()); |
46 | assert_eq!(None, iter.next()); |
47 | |
48 | let mut iter = v.iter().cloned().tuple_windows(); |
49 | assert_eq!(Some((1, 2, 3)), iter.next()); |
50 | assert_eq!(Some((2, 3, 4)), iter.next()); |
51 | assert_eq!(Some((3, 4, 5)), iter.next()); |
52 | assert_eq!(None, iter.next()); |
53 | |
54 | let mut iter = v.iter().cloned().tuple_windows(); |
55 | assert_eq!(Some((1, 2, 3, 4)), iter.next()); |
56 | assert_eq!(Some((2, 3, 4, 5)), iter.next()); |
57 | assert_eq!(None, iter.next()); |
58 | |
59 | let v = [1, 2, 3]; |
60 | let mut iter = v.iter().cloned().tuple_windows::<(_, _, _, _)>(); |
61 | assert_eq!(None, iter.next()); |
62 | } |
63 | |
64 | #[test] |
65 | fn next_tuple() { |
66 | let v = [1, 2, 3, 4, 5]; |
67 | let mut iter = v.iter(); |
68 | assert_eq!(iter.next_tuple().map(|(&x, &y)| (x, y)), Some((1, 2))); |
69 | assert_eq!(iter.next_tuple().map(|(&x, &y)| (x, y)), Some((3, 4))); |
70 | assert_eq!(iter.next_tuple::<(_, _)>(), None); |
71 | } |
72 | |
73 | #[test] |
74 | fn collect_tuple() { |
75 | let v = [1, 2]; |
76 | let iter = v.iter().cloned(); |
77 | assert_eq!(iter.collect_tuple(), Some((1, 2))); |
78 | |
79 | let v = [1]; |
80 | let iter = v.iter().cloned(); |
81 | assert_eq!(iter.collect_tuple::<(_, _)>(), None); |
82 | |
83 | let v = [1, 2, 3]; |
84 | let iter = v.iter().cloned(); |
85 | assert_eq!(iter.collect_tuple::<(_, _)>(), None); |
86 | } |
87 | |