From 368f27d299c185f1dfda8f38208c5c2e99b81498 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 12 Aug 2014 22:23:16 -0400 Subject: [PATCH] Implemented concat and insert and added unit tests --- source/string/str.c | 29 +++++-- tests/main.c | 1 + tests/test_str.c | 191 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 7 deletions(-) create mode 100644 tests/test_str.c diff --git a/source/string/str.c b/source/string/str.c index 5d58ac3..576f68a 100644 --- a/source/string/str.c +++ b/source/string/str.c @@ -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) diff --git a/tests/main.c b/tests/main.c index 0e15d6b..8e12161 100644 --- a/tests/main.c +++ b/tests/main.c @@ -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 index 0000000..cf39687 --- /dev/null +++ b/tests/test_str.c @@ -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 + //------------------------------------------------------------------------- +} -- 2.55.0