-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve str_replace and add tests for it (#158)
- Loading branch information
Showing
3 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include "pac_utils.h" | ||
|
||
#define STREQ(s1, s2) (strcmp((s1), (s2)) == 0) | ||
|
||
// Unit tests | ||
int main() { | ||
// Test case 1: Single replacement | ||
assert(STREQ("Hello, universe!", str_replace("Hello, world!", "world", "universe"))); | ||
|
||
// Test case 2: Multiple replacements | ||
assert(STREQ("one cat, two cat, red cat, blue cat", | ||
str_replace("one fish, two fish, red fish, blue fish", "fish", "cat"))); | ||
// Expected output: "one cat, two cat, red cat, blue cat" | ||
|
||
// Test case 3: No replacements | ||
assert(STREQ("AI is amazing", str_replace("AI is amazing", "robot", "AI"))); | ||
|
||
// Test case 4: Empty original string | ||
assert(STREQ("", str_replace("", "hello", "world"))); | ||
|
||
// Test case 5: Empty replacement string | ||
assert(STREQ("Hello, world!", str_replace("Hello, world!", "", "universe"))); | ||
|
||
// Test case 6: Empty "with" string | ||
assert(STREQ("Hello, !", str_replace("Hello, world!", "world", ""))); | ||
|
||
// Test case 7: Complex replacements | ||
assert(STREQ("abcdeXYZcba", str_replace("abcdeedcba", "ed", "XYZ"))); | ||
|
||
// Test case 8: Null inputs | ||
assert(str_replace(NULL, "hello", "world") == NULL); | ||
assert(str_replace("Hello hello", NULL, "world") == NULL); | ||
assert(str_replace("Hello hello", "hello", NULL) == NULL); | ||
|
||
return 0; | ||
} |