#include #include #include #include "scufcont.h" #include "DString.h" void test_SCDString_alloc(void** state) { (void) state; const char* c_str = "Hello"; const size_t c_str_len = strlen(c_str); SCDString dstr; assert_true(SCDString_alloc(&dstr, c_str, c_str_len) == SCUFCONT_SUCCESS); assert_memory_equal(dstr.data, c_str, c_str_len + 1); assert_true(dstr.len == c_str_len); assert_true(dstr.capacity == c_str_len + 1); free(dstr.data); } void test_SCDString_free(void** state) { (void) state; SCDString dstr; assert_true(SCDString_alloc(&dstr, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); SCDString_free(&dstr); assert_null(dstr.data); assert_true(dstr.len == 0); assert_true(dstr.capacity == 0); } void test_SCDString_equal(void** state) { (void) state; SCDString s1; assert_true(SCDString_alloc(&s1, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); SCDString s2; assert_true(SCDString_alloc(&s2, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); assert_true(SCDString_equal(&s1, &s2)); SCDString_free(&s1); SCDString_free(&s2); assert_true(SCDString_alloc(&s1, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); assert_true(SCDString_alloc(&s2, "Hell0", strlen("Hell0")) == SCUFCONT_SUCCESS); assert_false(SCDString_equal(&s1, &s2)); SCDString_free(&s1); SCDString_free(&s2); assert_true(SCDString_alloc(&s1, "Hello World", strlen("Hello World")) == SCUFCONT_SUCCESS); assert_true(SCDString_alloc(&s2, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); assert_false(SCDString_equal(&s1, &s2)); SCDString_free(&s1); SCDString_free(&s2); } void test_SCDString_equal_c_str(void** state) { (void) state; SCDString s1; assert_true(SCDString_alloc(&s1,"Hello", strlen("Hello")) == SCUFCONT_SUCCESS); char* s2 = "Hello"; assert_true(SCDString_equal_c_str(&s1, s2)); SCDString_free(&s1); assert_true(SCDString_alloc(&s1, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); s2 = "Hell0"; assert_false(SCDString_equal_c_str(&s1, s2)); SCDString_free(&s1); assert_true(SCDString_alloc(&s1, "Hello World", strlen("Hello World")) == SCUFCONT_SUCCESS); s2 = "Hello"; assert_false(SCDString_equal_c_str(&s1, s2)); SCDString_free(&s1); } void test_SCDString_equal_next_chars_c_str(void** state) { (void) state; SCDString dstr; assert_true(SCDString_alloc(&dstr, "Hello", strlen("Hello")) == SCUFCONT_SUCCESS); char* str = "ll"; size_t str_len = strlen(str); assert_true(SCDString_equal_next_chars_c_str(&dstr, str, str_len, 2)); assert_false(SCDString_equal_next_chars_c_str(&dstr, str, str_len, 1)); SCDString_free(&dstr); } void test_SCDString_concat_c_str(void** state) { (void) state; const char* dstr_c_str = "Hello"; const char* c_str = " World"; const size_t combined_length = strlen(dstr_c_str) + strlen(c_str); SCDString dstr; assert_true(SCDString_alloc(&dstr, dstr_c_str, strlen("Hello")) == SCUFCONT_SUCCESS); SCDString_concat_c_str(&dstr, c_str, strlen(c_str)); assert_memory_equal(dstr.data, (void*) "Hello World\0", strlen("Hello World") + 1); assert_true(dstr.len == combined_length); assert_true(dstr.capacity == combined_length + 1); SCDString_free(&dstr); } void test_SCDString_c_str(void** state) { (void) state; const char* c_str = "Hello"; SCDString dstr; assert_true(SCDString_alloc(&dstr, c_str, strlen(c_str)) == SCUFCONT_SUCCESS); assert_string_equal(c_str, SCDString_c_str(&dstr)); SCDString_concat_c_str(&dstr, " World", strlen(" World")); assert_string_equal("Hello World", SCDString_c_str(&dstr)); } void test_SCDString_split(void** state) { (void) state; SCDString dstr; assert_true(SCDString_alloc(&dstr, "Hello World", strlen("Hello World")) == SCUFCONT_SUCCESS); size_t arr_len = 0; SCDString* arr = NULL; ScufContResult res = SCDString_split(&dstr, " ", &arr, &arr_len); assert_true(res == SCUFCONT_SUCCESS); assert_true(arr_len == 2); assert_string_equal(SCDString_c_str(arr), "Hello"); assert_string_equal(SCDString_c_str(arr + 1), "World"); SCDString_free(&dstr); SCDString_split_arr_free(&arr, arr_len); assert_true(arr == NULL); assert_true(SCDString_alloc(&dstr, " Hello World", strlen(" Hello World")) == SCUFCONT_SUCCESS); arr_len = 0; res = SCDString_split(&dstr, " ", &arr, &arr_len); assert_true(arr_len == 3); assert_string_equal(SCDString_c_str(arr), ""); assert_string_equal(SCDString_c_str(arr + 1), "Hello"); assert_string_equal(SCDString_c_str(arr + 2), "World"); SCDString_free(&dstr); SCDString_split_arr_free(&arr, arr_len); assert_true(arr == NULL); } int DString_tests_run(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_SCDString_alloc), cmocka_unit_test(test_SCDString_free), cmocka_unit_test(test_SCDString_equal), cmocka_unit_test(test_SCDString_equal_c_str), cmocka_unit_test(test_SCDString_equal_next_chars_c_str), cmocka_unit_test(test_SCDString_concat_c_str), cmocka_unit_test(test_SCDString_c_str), cmocka_unit_test(test_SCDString_split), }; return cmocka_run_group_tests_name("DString", tests, NULL, NULL); }