about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-04-19 22:57:22 +0100
committerJames Booth <boothj5@gmail.com>2012-04-19 22:57:22 +0100
commit4f454b6a64c864f6fa2c1b8e45262238783c188e (patch)
tree07838d6787139c56e8dabb35f9abdbb6d885dd6e
parent366eecc19569f14a6081e1acb6efd72a372982da (diff)
downloadprofani-tty-4f454b6a64c864f6fa2c1b8e45262238783c188e.tar.gz
Added more tests to str_replace
Handles NULLs and empty strings better
-rw-r--r--test_util.c96
-rw-r--r--util.c7
2 files changed, 102 insertions, 1 deletions
diff --git a/test_util.c b/test_util.c
index e2327be5..ae936d9a 100644
--- a/test_util.c
+++ b/test_util.c
@@ -57,6 +57,94 @@ void replace_char(void)
     assert_string_equals("some &amp; a thing &amp; something else", result);
 }
 
+void replace_when_none(void)
+{
+    char *string = "its another string";
+    char *sub = "haha";
+    char *new = "replaced";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("its another string", result);
+}
+
+void replace_when_match(void)
+{
+    char *string = "hello";
+    char *sub = "hello";
+    char *new = "goodbye";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("goodbye", result);
+}
+
+void replace_when_string_empty(void)
+{
+    char *string = "";
+    char *sub = "hello";
+    char *new = "goodbye";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("", result);
+}
+
+void replace_when_string_null(void)
+{
+    char *string = NULL;
+    char *sub = "hello";
+    char *new = "goodbye";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_is_null(result);
+}
+
+void replace_when_sub_empty(void)
+{
+    char *string = "hello";
+    char *sub = "";
+    char *new = "goodbye";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("hello", result);
+}
+
+void replace_when_sub_null(void)
+{
+    char *string = "hello";
+    char *sub = NULL;
+    char *new = "goodbye";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("hello", result);
+}
+
+void replace_when_new_empty(void)
+{
+    char *string = "hello";
+    char *sub = "hello";
+    char *new = "";
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("", result);
+}
+
+void replace_when_new_null(void)
+{
+    char *string = "hello";
+    char *sub = "hello";
+    char *new = NULL;
+
+    char *result = str_replace(string, sub, new);
+
+    assert_string_equals("hello", result);
+}
+
 void register_util_tests(void)
 {
     TEST_MODULE("util tests");
@@ -65,4 +153,12 @@ void register_util_tests(void)
     TEST(replace_one_substr_end);
     TEST(replace_two_substr);
     TEST(replace_char);
+    TEST(replace_when_none);
+    TEST(replace_when_match);
+    TEST(replace_when_string_empty);
+    TEST(replace_when_string_null);
+    TEST(replace_when_sub_empty);
+    TEST(replace_when_sub_null);
+    TEST(replace_when_new_empty);
+    TEST(replace_when_new_null);
 }
diff --git a/util.c b/util.c
index 1452d357..bfe21263 100644
--- a/util.c
+++ b/util.c
@@ -62,7 +62,12 @@ char * str_replace (const char *string, const char *substr,
     char *oldstr = NULL;
     char *head = NULL;
  
-    if ( substr == NULL || replacement == NULL ) 
+    if (string == NULL)
+        return NULL;
+
+    if ( substr == NULL || 
+         replacement == NULL || 
+         (strcmp(substr, "") == 0)) 
         return strdup (string);
 
     newstr = strdup (string);