Skip to content

Commit c5a1d8c

Browse files
committed
StrSearcher: Add tests for rfind(&str)
Add tests for .rfind(&str), using the reverse searcher case for substring search.
1 parent 832e5a0 commit c5a1d8c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcollectionstest/str.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ fn test_find_str() {
115115
assert_eq!(data[43..86].find("ย中"), Some(67 - 43));
116116
assert_eq!(data[43..86].find("iệt"), Some(77 - 43));
117117
assert_eq!(data[43..86].find("Nam"), Some(83 - 43));
118+
119+
// find every substring -- assert that it finds it, or an earlier occurence.
120+
let string = "Việt Namacbaabcaabaaba";
121+
for (i, ci) in string.char_indices() {
122+
let ip = i + ci.len_utf8();
123+
for j in string[ip..].char_indices()
124+
.map(|(i, _)| i)
125+
.chain(Some(string.len() - ip))
126+
{
127+
let pat = &string[i..ip + j];
128+
assert!(match string.find(pat) {
129+
None => false,
130+
Some(x) => x <= i,
131+
});
132+
assert!(match string.rfind(pat) {
133+
None => false,
134+
Some(x) => x >= i,
135+
});
136+
}
137+
}
118138
}
119139

120140
#[test]

0 commit comments

Comments
 (0)