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
pub fn overlap<'a>(left: &str, right: &'a str) -> &'a str {
let mut substring = right;
while !substring.is_empty() {
if left.ends_with(&substring) {
break;
}
substring = &substring[0..(substring.len() - substring.chars().last().unwrap().len_utf8())];
}
substring
}
#[cfg(test)]
mod tests {
use crate::overlap;
#[test]
fn test_partial_overlap() {
assert_eq!(overlap("abc", "bcd"), "bc");
}
#[test]
fn test_full_overlap() {
assert_eq!(overlap("abc", "abc"), "abc");
}
#[test]
fn test_no_overlap() {
assert_eq!(overlap("abc", "def"), "");
}
#[test]
fn test_right_substring_of_left() {
assert_eq!(overlap("abcd", "bcd"), "bcd");
}
#[test]
fn test_left_substring_of_right() {
assert_eq!(overlap("abc", "abcd"), "abc");
}
#[test]
fn test_only_checks_overlap_one_way() {
assert_eq!(overlap("bcd", "abc"), "");
}
#[test]
fn test_left_empty() {
assert_eq!(overlap("", "abc"), "");
}
#[test]
fn test_right_empty() {
assert_eq!(overlap("abc", ""), "");
}
#[test]
fn test_both_empty() {
assert_eq!(overlap("", ""), "");
}
}