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