1 | use itertools::EitherOrBoth; |
2 | use itertools::free::merge_join_by; |
3 | |
4 | #[test] |
5 | fn empty() { |
6 | let left: Vec<u32> = vec![]; |
7 | let right: Vec<u32> = vec![]; |
8 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![]; |
9 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
10 | .collect::<Vec<_>>(); |
11 | assert_eq!(expected_result, actual_result); |
12 | } |
13 | |
14 | #[test] |
15 | fn left_only() { |
16 | let left: Vec<u32> = vec![1,2,3]; |
17 | let right: Vec<u32> = vec![]; |
18 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![ |
19 | EitherOrBoth::Left(1), |
20 | EitherOrBoth::Left(2), |
21 | EitherOrBoth::Left(3) |
22 | ]; |
23 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
24 | .collect::<Vec<_>>(); |
25 | assert_eq!(expected_result, actual_result); |
26 | } |
27 | |
28 | #[test] |
29 | fn right_only() { |
30 | let left: Vec<u32> = vec![]; |
31 | let right: Vec<u32> = vec![1,2,3]; |
32 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![ |
33 | EitherOrBoth::Right(1), |
34 | EitherOrBoth::Right(2), |
35 | EitherOrBoth::Right(3) |
36 | ]; |
37 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
38 | .collect::<Vec<_>>(); |
39 | assert_eq!(expected_result, actual_result); |
40 | } |
41 | |
42 | #[test] |
43 | fn first_left_then_right() { |
44 | let left: Vec<u32> = vec![1,2,3]; |
45 | let right: Vec<u32> = vec![4,5,6]; |
46 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![ |
47 | EitherOrBoth::Left(1), |
48 | EitherOrBoth::Left(2), |
49 | EitherOrBoth::Left(3), |
50 | EitherOrBoth::Right(4), |
51 | EitherOrBoth::Right(5), |
52 | EitherOrBoth::Right(6) |
53 | ]; |
54 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
55 | .collect::<Vec<_>>(); |
56 | assert_eq!(expected_result, actual_result); |
57 | } |
58 | |
59 | #[test] |
60 | fn first_right_then_left() { |
61 | let left: Vec<u32> = vec![4,5,6]; |
62 | let right: Vec<u32> = vec![1,2,3]; |
63 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![ |
64 | EitherOrBoth::Right(1), |
65 | EitherOrBoth::Right(2), |
66 | EitherOrBoth::Right(3), |
67 | EitherOrBoth::Left(4), |
68 | EitherOrBoth::Left(5), |
69 | EitherOrBoth::Left(6) |
70 | ]; |
71 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
72 | .collect::<Vec<_>>(); |
73 | assert_eq!(expected_result, actual_result); |
74 | } |
75 | |
76 | #[test] |
77 | fn interspersed_left_and_right() { |
78 | let left: Vec<u32> = vec![1,3,5]; |
79 | let right: Vec<u32> = vec![2,4,6]; |
80 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![ |
81 | EitherOrBoth::Left(1), |
82 | EitherOrBoth::Right(2), |
83 | EitherOrBoth::Left(3), |
84 | EitherOrBoth::Right(4), |
85 | EitherOrBoth::Left(5), |
86 | EitherOrBoth::Right(6) |
87 | ]; |
88 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
89 | .collect::<Vec<_>>(); |
90 | assert_eq!(expected_result, actual_result); |
91 | } |
92 | |
93 | #[test] |
94 | fn overlapping_left_and_right() { |
95 | let left: Vec<u32> = vec![1,3,4,6]; |
96 | let right: Vec<u32> = vec![2,3,4,5]; |
97 | let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![ |
98 | EitherOrBoth::Left(1), |
99 | EitherOrBoth::Right(2), |
100 | EitherOrBoth::Both(3, 3), |
101 | EitherOrBoth::Both(4, 4), |
102 | EitherOrBoth::Right(5), |
103 | EitherOrBoth::Left(6) |
104 | ]; |
105 | let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)) |
106 | .collect::<Vec<_>>(); |
107 | assert_eq!(expected_result, actual_result); |
108 | } |
109 | |