Skip to content

Commit c37e8fa

Browse files
committed
generalize str.contains() tests to a range of haystack sizes
The Big-O is cubic, but this is only called with ~70 chars so it's still fast enough
1 parent 3d4a848 commit c37e8fa

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

library/alloc/tests/str.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1590,11 +1590,27 @@ fn test_bool_from_str() {
15901590
assert_eq!("not even a boolean".parse::<bool>().ok(), None);
15911591
}
15921592

1593-
fn check_contains_all_substrings(s: &str) {
1594-
assert!(s.contains(""));
1595-
for i in 0..s.len() {
1596-
for j in i + 1..=s.len() {
1597-
assert!(s.contains(&s[i..j]));
1593+
fn check_contains_all_substrings(haystack: &str) {
1594+
let mut modified_needle = String::new();
1595+
1596+
for i in 0..haystack.len() {
1597+
// check different haystack lengths since we special-case short haystacks.
1598+
let haystack = &haystack[0..i];
1599+
assert!(haystack.contains(""));
1600+
for j in 0..haystack.len() {
1601+
for k in j + 1..=haystack.len() {
1602+
let needle = &haystack[j..k];
1603+
assert!(haystack.contains(needle));
1604+
modified_needle.clear();
1605+
modified_needle.push_str(needle);
1606+
modified_needle.replace_range(0..1, "\0");
1607+
assert!(!haystack.contains(&modified_needle));
1608+
1609+
modified_needle.clear();
1610+
modified_needle.push_str(needle);
1611+
modified_needle.replace_range(needle.len() - 1..needle.len(), "\0");
1612+
assert!(!haystack.contains(&modified_needle));
1613+
}
15981614
}
15991615
}
16001616
}

0 commit comments

Comments
 (0)