Skip to content

Commit

Permalink
Rollup merge of #114111 - allaboutevemirolive:add-test-case-string, r…
Browse files Browse the repository at this point in the history
…=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](#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."
  • Loading branch information
matthiaskrgr committed Jul 31, 2023
2 parents 2de51cc + adb36cb commit e981db0
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions library/alloc/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,29 +368,73 @@ fn remove_bad() {

#[test]
fn test_remove_matches() {
// test_single_pattern_occurrence
let mut s = "abc".to_string();

s.remove_matches('b');
assert_eq!(s, "ac");
// repeat_test_single_pattern_occurrence
s.remove_matches('b');
assert_eq!(s, "ac");

// test_single_character_pattern
let mut s = "abcb".to_string();

s.remove_matches('b');
assert_eq!(s, "ac");

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

// test_pattern_empty_text_and_pattern
let mut s = "".to_string();
s.remove_matches("");
assert_eq!(s, "");

// test_pattern_empty_text
let mut s = "".to_string();
s.remove_matches("something");
assert_eq!(s, "");

// test_empty_pattern
let mut s = "Testing with empty pattern.".to_string();
s.remove_matches("");
assert_eq!(s, "Testing with empty pattern.");

// test_multiple_consecutive_patterns_1
let mut s = "aaaaa".to_string();
s.remove_matches('a');
assert_eq!(s, "");

// test_multiple_consecutive_patterns_2
let mut s = "Hello **world****today!**".to_string();
s.remove_matches("**");
assert_eq!(s, "Hello worldtoday!");

// test_case_insensitive_pattern
let mut s = "CASE ** SeNsItIvE ** PaTtErN.".to_string();
s.remove_matches("sEnSiTiVe");
assert_eq!(s, "CASE ** SeNsItIvE ** PaTtErN.");

// test_pattern_with_digits
let mut s = "123 ** 456 ** 789".to_string();
s.remove_matches("**");
assert_eq!(s, "123 456 789");

// test_pattern_occurs_after_empty_string
let mut s = "abc X defXghi".to_string();
s.remove_matches("X");
assert_eq!(s, "abc defghi");

// test_large_pattern
let mut s = "aaaXbbbXcccXdddXeee".to_string();
s.remove_matches("X");
assert_eq!(s, "aaabbbcccdddeee");

// test_pattern_at_multiple_positions
let mut s = "Pattern ** found ** multiple ** times ** in ** text.".to_string();
s.remove_matches("**");
assert_eq!(s, "Pattern found multiple times in text.");
}

#[test]
Expand Down

0 comments on commit e981db0

Please sign in to comment.