diff --git a/meson.build b/meson.build index fd99b11..c940d63 100644 --- a/meson.build +++ b/meson.build @@ -1,75 +1,80 @@ project( - 'testlib', - 'c', - version: '0.2', - default_options: ['c_std=c18', 'warning_level=3'], + 'testlib', + 'c', + version: '0.3', + default_options: ['c_std=c18', 'warning_level=3'], ) -cc = meson.get_compiler('c') - # Add global posix feature macro + add_global_arguments('-D_POSIX_C_SOURCE=200112L', language: 'c') # Sources without test files -lib_src = files( - 'hashmap/myhashmap.c', - 'queue/myqueue.c', - 'set/myset.c', - 'stack/mystack.c', - 'string/mystring.c', - 'vector/myvector.c', -) -# Test files -test_src = files( - 'test/hashmap/hm1.c', - 'test/queue/queue1.c', - 'test/set/set1.c', - 'test/stack/stack1.c', - 'test/string/str1.c', - 'test/string/str2.c', - 'test/string/str3.c', - 'test/test.c', - 'test/vector/vec1.c', +lib_src = files( + 'hashmap/myhashmap.c', + 'queue/myqueue.c', + 'set/myset.c', + 'stack/mystack.c', + 'string/mystring.c', + 'vector/myvector.c', ) # Include directories + inc_dir = include_directories('string', 'queue', 'hashmap', 'vector', 'stack', 'set') if host_machine.system() == 'windows' - win_inc_dir = include_directories('c:/include/') + win_inc_dir = include_directories('c:/include/') else - win_inc_dir = [] + win_inc_dir = [] endif # Static library + myclib_lib = static_library( - 'myclib', - lib_src, - include_directories: inc_dir, - dependencies: winsock_dep, - install: true, + 'myclib', + lib_src, + include_directories: inc_dir, + install: true, ) # Install headers + install_headers( - [ - 'hashmap/myhashmap.h', - 'queue/myqueue.h', - 'string/mystring.h', - 'vector/myvector.h', - 'set/myset.h', - 'stack/mystack.h', - ], - subdir: 'myclib', + [ + 'hashmap/myhashmap.h', + 'queue/myqueue.h', + 'string/mystring.h', + 'vector/myvector.h', + 'set/myset.h', + 'stack/mystack.h', + ], + subdir: 'myclib', ) -# Test executable -testlib_exe = executable( - 'testlib', - test_src, - include_directories: [inc_dir, win_inc_dir], - dependencies: winsock_dep, - link_with: myclib_lib, -) +# Per-file test registrations -test('testlib', testlib_exe) +test_cases = [ + ['hashmap_hm1', 'test/hashmap/hm1.c'], + ['queue_queue1', 'test/queue/queue1.c'], + ['set_set1', 'test/set/set1.c'], + ['stack_stack1', 'test/stack/stack1.c'], + ['string_str1', 'test/string/str1.c'], + ['string_str2', 'test/string/str2.c'], + ['string_str3', 'test/string/str3.c'], + ['vector_vec1', 'test/vector/vec1.c'], +] + +foreach tc : test_cases + test_name = tc[0] + test_source = tc[1] + + test_exe = executable( + 'test_' + test_name, + test_source, + include_directories: [inc_dir, win_inc_dir], + link_with: myclib_lib, + ) + + test(test_name, test_exe) +endforeach \ No newline at end of file diff --git a/test/hashmap/hm1.c b/test/hashmap/hm1.c index cd4a664..e11bd8f 100644 --- a/test/hashmap/hm1.c +++ b/test/hashmap/hm1.c @@ -48,7 +48,7 @@ void my_free_value(void *value) { free(mct); } -void test_hm1(void) { +int main(void) { /* Allocate a new hashmap */ /* Pass your custom hash, equal and free functions */ /* This hashmap will contain names as keys and a custom type as value */ diff --git a/test/queue/queue1.c b/test/queue/queue1.c index ca69f28..d1cb043 100644 --- a/test/queue/queue1.c +++ b/test/queue/queue1.c @@ -1,7 +1,7 @@ #include "../queue/myqueue.h" #include -void test_queue1(void) { +int main(void) { /* Allocate a new queue */ queue_s *queue = queue_new(3, sizeof(int)); assert(queue != NULL); diff --git a/test/set/set1.c b/test/set/set1.c index 54e0b79..e125093 100644 --- a/test/set/set1.c +++ b/test/set/set1.c @@ -2,7 +2,7 @@ #include -void test_set1(void) { +int main(void) { set_s *set = set_new(sizeof(int)); assert(set != NULL); diff --git a/test/stack/stack1.c b/test/stack/stack1.c index 53c7a90..f772080 100644 --- a/test/stack/stack1.c +++ b/test/stack/stack1.c @@ -2,7 +2,7 @@ #include #include -void test_stack1(void) { +int main(void) { stack_s *stack = stack_new(32, sizeof(int)); int num = 10; stack_push(stack, &num); diff --git a/test/string/str1.c b/test/string/str1.c index e23bcba..95621ba 100644 --- a/test/string/str1.c +++ b/test/string/str1.c @@ -4,7 +4,7 @@ #include #include -void test_str1(void) { +int main(void) { size_t length; size_t capacity; char *c_str; diff --git a/test/string/str2.c b/test/string/str2.c index e6969c1..e06b2f6 100644 --- a/test/string/str2.c +++ b/test/string/str2.c @@ -2,7 +2,7 @@ #include #include -void test_str2(void) { +int main(void) { string_s *s1 = string_new("Hello, world!", 0); assert(s1 != NULL); string_s *s2 = string_new("Hello, world!", 0); @@ -27,6 +27,21 @@ void test_str2(void) { assert(string_len(s1) == 50); assert(string_cap(s1) == 64); + /* Regression: when need == capacity, extend still needs room for '\0'. */ + string_s *tight_dst = string_new("abc", 4); + string_s *tight_src = string_new("d", 0); + assert(tight_dst != NULL); + assert(tight_src != NULL); + assert(string_extend(tight_dst, tight_src) == 0); + assert(strcmp(string_cstr(tight_dst), "abcd") == 0); + assert(string_cap(tight_dst) >= 5); + + /* Self-extend should be safe and deterministic. */ + string_s *self = string_new("xy", 0); + assert(self != NULL); + assert(string_extend(self, self) == 0); + assert(strcmp(string_cstr(self), "xyxy") == 0); + /* Find substring in string */ int pos = string_find(s1, " is "); assert(pos == 11); @@ -34,4 +49,7 @@ void test_str2(void) { string_free(s1); string_free(s2); string_free(extend_me); + string_free(tight_dst); + string_free(tight_src); + string_free(self); } diff --git a/test/string/str3.c b/test/string/str3.c index 7ffce3a..2b068d3 100644 --- a/test/string/str3.c +++ b/test/string/str3.c @@ -2,7 +2,7 @@ #include #include -void test_str3(void) { +int main(void) { /* Make a new string from format */ string_s *s = string_format("My name is %s (%d)", "John", 21); assert(strcmp(s->data, "My name is John (21)") == 0); @@ -16,6 +16,16 @@ void test_str3(void) { /* Test delete */ string_remove(ms, 5, 5); assert(strcmp(ms->data, "Hello!") == 0); + assert(string_remove(ms, string_len(ms), 1) == -1); + + string_s *remove_mid = string_new("abcdef", 0); + assert(remove_mid != NULL); + assert(string_remove(remove_mid, 2, 2) == 0); + assert(strcmp(remove_mid->data, "abef") == 0); + assert(string_remove(remove_mid, 2, 2) == 0); + assert(strcmp(remove_mid->data, "ab") == 0); + assert(string_remove(remove_mid, 0, 2) == 0); + assert(strcmp(remove_mid->data, "") == 0); /* Test replace */ string_s *replace_me = string_new("My car doesn't bark!", 0); @@ -25,5 +35,6 @@ void test_str3(void) { string_free(ms); string_free(s); + string_free(remove_mid); string_free(replace_me); } diff --git a/test/test.c b/test/test.c deleted file mode 100644 index c26fc65..0000000 --- a/test/test.c +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Ignore this file - */ - -#include -#include - -void test_hm1(void); - -void test_queue1(void); - -void test_str1(void); -void test_str2(void); -void test_str3(void); - -void test_vec1(void); - -void test_stack1(void); - -void test_set1(void); - -int main(void) { - puts("==== [Running Hashmap tests] ===="); - test_hm1(); - puts("OK!"); - puts(""); - - puts("==== [Running Queue tests] ===="); - test_queue1(); - puts("OK!"); - puts(""); - - puts("==== [Running String tests] ===="); - test_str1(); - test_str2(); - test_str3(); - puts("OK!"); - puts(""); - - puts("==== [Running Vector tests] ===="); - test_vec1(); - puts("OK!"); - puts(""); - - puts("==== [Running Stack tests] ===="); - test_stack1(); - puts("OK!"); - puts(""); - - puts("==== [Running Set tests] ===="); - test_set1(); - puts("OK!"); - puts(""); - - return 0; -} diff --git a/test/vector/vec1.c b/test/vector/vec1.c index d305a36..43c0baa 100644 --- a/test/vector/vec1.c +++ b/test/vector/vec1.c @@ -30,7 +30,7 @@ int my_cmp(const void *a, const void *b) { return ma->age - mb->age; } -void test_vec1(void) { +int main(void) { /* Allocate a new vector */ size_t elem_size = sizeof(my_elem_s); vec_s *v = vec_new(10, elem_size);