Skip to content

Commit 36a1b80

Browse files
authored
Rollup merge of #114111 - allaboutevemirolive:add-test-case-string, r=Mark-Simulacrum
Improve test case for experimental API remove_matches ## Add Test Cases for `remove_matches` Function ### Motivation After reading the discussion in [this GitHub thread](rust-lang/rust#71780), I'm trying to redesign the current API to use less memory when working with `String` and to make it simpler. I've discovered that some test cases are very helpful in ensuring that the new API behaves as intended. I'm still in the process of redesigning the current API, and these test cases have proven to be very useful. ### Testing The current test has been tested with the command `./x test --stage 0 library/alloc`. ### Overview This pull request adds several new test cases for the `remove_matches` function to make sure it works correctly in different situations. The `remove_matches` function is used to get rid of all instances of a specific pattern from a given text. These test cases thoroughly check how the function behaves in various scenarios. ### Test Cases 1. **Single Pattern Occurrence** (`test_single_pattern_occurrence`): - Description: Tests the removal of a single pattern occurrence from the text. - Input: Text: "abc", Pattern: 'b' - Expected Output: "ac" 2. **Repeat Test Single Pattern Occurrence** (`repeat_test_single_pattern_occurrence`): - Description: Repeats the previous test case to ensure consecutive removal of the same pattern. - Input: Text: "ac", Pattern: 'b' - Expected Output: "ac" 3. **Single Character Pattern** (`test_single_character_pattern`): - Description: Tests the removal of a single character pattern. - Input: Text: "abcb", Pattern: 'b' - Expected Output: "ac" 4. **Pattern with Special Characters** (`test_pattern_with_special_characters`): - Description: Tests the removal of a pattern containing special characters. - Input: Text: "ศไทย中华Việt Nam; foobarศ", Pattern: 'ศ' - Expected Output: "ไทย中华Việt Nam; foobar" 5. **Pattern Empty Text and Pattern** (`test_pattern_empty_text_and_pattern`): - Description: Tests the removal of an empty pattern from an empty text. - Input: Text: "", Pattern: "" - Expected Output: "" 6. **Pattern Empty Text** (`test_pattern_empty_text`): - Description: Tests the removal of a pattern from an empty text. - Input: Text: "", Pattern: "something" - Expected Output: "" 7. **Empty Pattern** (`test_empty_pattern`): - Description: Tests the behavior of removing an empty pattern from the text. - Input: Text: "Testing with empty pattern.", Pattern: "" - Expected Output: "Testing with empty pattern." 8. **Multiple Consecutive Patterns 1** (`test_multiple_consecutive_patterns_1`): - Description: Tests the removal of multiple consecutive occurrences of a pattern. - Input: Text: "aaaaa", Pattern: 'a' - Expected Output: "" 9. **Multiple Consecutive Patterns 2** (`test_multiple_consecutive_patterns_2`): - Description: Tests the removal of a longer pattern that occurs consecutively. - Input: Text: "Hello **world****today!**", Pattern: "**" - Expected Output: "Hello worldtoday!" 10. **Case Insensitive Pattern** (`test_case_insensitive_pattern`): - Description: Tests the removal of a case-insensitive pattern from the text. - Input: Text: "CASE ** SeNsItIvE ** PaTtErN.", Pattern: "sEnSiTiVe" - Expected Output: "CASE ** SeNsItIvE ** PaTtErN."
2 parents 4851dcb + f95e144 commit 36a1b80

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

alloc/tests/string.rs

+46-2
Original file line numberDiff line numberDiff line change
@@ -368,29 +368,73 @@ fn remove_bad() {
368368

369369
#[test]
370370
fn test_remove_matches() {
371+
// test_single_pattern_occurrence
371372
let mut s = "abc".to_string();
372-
373373
s.remove_matches('b');
374374
assert_eq!(s, "ac");
375+
// repeat_test_single_pattern_occurrence
375376
s.remove_matches('b');
376377
assert_eq!(s, "ac");
377378

379+
// test_single_character_pattern
378380
let mut s = "abcb".to_string();
379-
380381
s.remove_matches('b');
381382
assert_eq!(s, "ac");
382383

384+
// test_pattern_with_special_characters
383385
let mut s = "ศไทย中华Việt Nam; foobarศ".to_string();
384386
s.remove_matches('ศ');
385387
assert_eq!(s, "ไทย中华Việt Nam; foobar");
386388

389+
// test_pattern_empty_text_and_pattern
387390
let mut s = "".to_string();
388391
s.remove_matches("");
389392
assert_eq!(s, "");
390393

394+
// test_pattern_empty_text
395+
let mut s = "".to_string();
396+
s.remove_matches("something");
397+
assert_eq!(s, "");
398+
399+
// test_empty_pattern
400+
let mut s = "Testing with empty pattern.".to_string();
401+
s.remove_matches("");
402+
assert_eq!(s, "Testing with empty pattern.");
403+
404+
// test_multiple_consecutive_patterns_1
391405
let mut s = "aaaaa".to_string();
392406
s.remove_matches('a');
393407
assert_eq!(s, "");
408+
409+
// test_multiple_consecutive_patterns_2
410+
let mut s = "Hello **world****today!**".to_string();
411+
s.remove_matches("**");
412+
assert_eq!(s, "Hello worldtoday!");
413+
414+
// test_case_insensitive_pattern
415+
let mut s = "CASE ** SeNsItIvE ** PaTtErN.".to_string();
416+
s.remove_matches("sEnSiTiVe");
417+
assert_eq!(s, "CASE ** SeNsItIvE ** PaTtErN.");
418+
419+
// test_pattern_with_digits
420+
let mut s = "123 ** 456 ** 789".to_string();
421+
s.remove_matches("**");
422+
assert_eq!(s, "123 456 789");
423+
424+
// test_pattern_occurs_after_empty_string
425+
let mut s = "abc X defXghi".to_string();
426+
s.remove_matches("X");
427+
assert_eq!(s, "abc defghi");
428+
429+
// test_large_pattern
430+
let mut s = "aaaXbbbXcccXdddXeee".to_string();
431+
s.remove_matches("X");
432+
assert_eq!(s, "aaabbbcccdddeee");
433+
434+
// test_pattern_at_multiple_positions
435+
let mut s = "Pattern ** found ** multiple ** times ** in ** text.".to_string();
436+
s.remove_matches("**");
437+
assert_eq!(s, "Pattern found multiple times in text.");
394438
}
395439

396440
#[test]

0 commit comments

Comments
 (0)