]> git.mdlowis.com Git - projs/libcds.git/commitdiff
Implemented concat and insert and added unit tests
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 13 Aug 2014 02:23:16 +0000 (22:23 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 13 Aug 2014 02:23:16 +0000 (22:23 -0400)
source/string/str.c
tests/main.c
tests/test_str.c [new file with mode: 0644]

index 5d58ac3d7a7451a752fac1cfa98124e5f7f612ec..576f68ac5806abf726128f5d87485c47b739565d 100644 (file)
@@ -67,17 +67,32 @@ str_t* str_set(str_t* p_str, size_t index, char val)
 
 str_t* str_concat(str_t* p_str1, str_t* p_str2)
 {
-    (void)p_str1;
-    (void)p_str2;
-    return NULL;
+    str_t* p_newstr = NULL;
+    size_t newsize;
+    assert(NULL != p_str1);
+    assert(NULL != p_str2);
+    newsize = sizeof(str_t) + p_str1->size + p_str2->size + 1;
+    p_newstr = (str_t*)mem_allocate(newsize, NULL);
+    memcpy(&(p_newstr->data[0]), p_str1->data, p_str1->size);
+    memcpy(&(p_newstr->data[p_str1->size]), p_str2->data, p_str2->size);
+    return p_newstr;
 }
 
 str_t* str_insert(str_t* p_str1, size_t index, str_t* p_str2)
 {
-    (void)p_str1;
-    (void)index;
-    (void)p_str2;
-    return NULL;
+    str_t* p_newstr = NULL;
+    assert(NULL != p_str1);
+    assert(NULL != p_str2);
+    if (index <= p_str1->size)
+    {
+        size_t newsize = sizeof(str_t) + p_str1->size + p_str2->size + 1;
+        p_newstr = (str_t*)mem_allocate(newsize, NULL);
+        memcpy(&(p_newstr->data[0]), p_str1->data, index);
+        memcpy(&(p_newstr->data[index]), p_str2->data, p_str2->size);
+        memcpy(&(p_newstr->data[index+p_str2->size]), &(p_str1->data[index]), p_str1->size-index);
+        p_newstr->data[p_str1->size + p_str2->size] = '\0';
+    }
+    return p_newstr;
 }
 
 str_t* str_erase(str_t* p_str, size_t start, size_t end)
index 0e15d6b458c47f4792fdc9de9c215d8692e19545..8e12161cc46b35454addd5dd8fc7264c099a1cfa 100644 (file)
@@ -7,5 +7,6 @@ int main(int argc, char** argv)
     RUN_TEST_SUITE(Vector);
     RUN_TEST_SUITE(List);
     RUN_TEST_SUITE(Buffer);
+    RUN_TEST_SUITE(String);
     return PRINT_TEST_RESULTS();
 }
diff --git a/tests/test_str.c b/tests/test_str.c
new file mode 100644 (file)
index 0000000..cf39687
--- /dev/null
@@ -0,0 +1,191 @@
+// Unit Test Framework Includes
+#include "test.h"
+
+// File To Test
+#include "str.h"
+
+static void test_setup(void) { }
+
+//-----------------------------------------------------------------------------
+// Begin Unit Tests
+//-----------------------------------------------------------------------------
+TEST_SUITE(String) {
+    //-------------------------------------------------------------------------
+    // Test str_new function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_new_returns_a_new_string)
+    {
+        str_t* p_str = str_new("foo");
+        CHECK(0 == strcmp(str_cstr(p_str), "foo"));
+        mem_release(p_str);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_size function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_size_returns_the_size_of_the_string)
+    {
+        str_t* p_str = str_new("foo");
+        CHECK(3 == str_size(p_str));
+        mem_release(p_str);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_copy function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_copy_should_make_an_exact_copy_of_the_string)
+    {
+        str_t* p_str1 = str_new("foo");
+        str_t* p_str2 = str_copy(p_str1);
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1), str_cstr(p_str2)));
+        mem_release(p_str1);
+        mem_release(p_str2);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_at function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_at_should_return_a_char_from_the_string)
+    {
+        str_t* p_str = str_new("abc");
+        CHECK('c' == str_at(p_str,2));
+        mem_release(p_str);
+    }
+
+    TEST(Verify_str_at_should_return_null_char_if_index_out_of_range)
+    {
+        str_t* p_str = str_new("abc");
+        CHECK('\0' == str_at(p_str,3));
+        mem_release(p_str);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_set function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_set_should_return_null_if_index_out_of_range)
+    {
+        str_t* p_str = str_new("abc");
+        CHECK(NULL == str_set(p_str,3,'d'));
+        mem_release(p_str);
+    }
+
+    TEST(Verify_str_set_should_return_a_new_string_with_the_char_at_the_specified_index)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_set(p_str1, 2, 'd');
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1),"abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2),"abd"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_concat function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_concat_should_concat_the_two_strings_and_return_a_new_string)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_new("def");
+        str_t* p_str3 = str_concat(p_str1, p_str2);
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2), "def"));
+        CHECK(0 == strcmp(str_cstr(p_str3), "abcdef"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+        mem_release(p_str3);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_insert function
+    //-------------------------------------------------------------------------
+    TEST(Verify_str_insert_should_insert_at_index_0)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_new("def");
+        str_t* p_str3 = str_insert(p_str1, 0, p_str2);
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2), "def"));
+        CHECK(0 == strcmp(str_cstr(p_str3), "defabc"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+        mem_release(p_str3);
+    }
+
+    TEST(Verify_str_insert_should_insert_at_index_1)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_new("def");
+        str_t* p_str3 = str_insert(p_str1, 1, p_str2);
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2), "def"));
+        CHECK(0 == strcmp(str_cstr(p_str3), "adefbc"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+        mem_release(p_str3);
+    }
+
+    TEST(Verify_str_insert_should_insert_at_index_2)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_new("def");
+        str_t* p_str3 = str_insert(p_str1, 2, p_str2);
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2), "def"));
+        CHECK(0 == strcmp(str_cstr(p_str3), "abdefc"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+        mem_release(p_str3);
+    }
+
+    TEST(Verify_str_insert_should_insert_at_index_3)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_new("def");
+        str_t* p_str3 = str_insert(p_str1, 3, p_str2);
+        CHECK(p_str1 != p_str2);
+        CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2), "def"));
+        CHECK(0 == strcmp(str_cstr(p_str3), "abcdef"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+        mem_release(p_str3);
+    }
+
+    TEST(Verify_str_insert_should_return_NULL_if_index_out_of_bound)
+    {
+        str_t* p_str1 = str_new("abc");
+        str_t* p_str2 = str_new("def");
+        CHECK(p_str1 != p_str2);
+        CHECK(NULL == str_insert(p_str1, 4, p_str2));
+        CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
+        CHECK(0 == strcmp(str_cstr(p_str2), "def"));
+        mem_release(p_str1);
+        mem_release(p_str2);
+    }
+
+    //-------------------------------------------------------------------------
+    // Test str_ function
+    //-------------------------------------------------------------------------
+
+    //-------------------------------------------------------------------------
+    // Test str_ function
+    //-------------------------------------------------------------------------
+
+    //-------------------------------------------------------------------------
+    // Test str_ function
+    //-------------------------------------------------------------------------
+
+    //-------------------------------------------------------------------------
+    // Test str_ function
+    //-------------------------------------------------------------------------
+
+    //-------------------------------------------------------------------------
+    // Test str_ function
+    //-------------------------------------------------------------------------
+}