From 0914c26b8d57fe96035b7151319685152bc7f356 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 11 Apr 2024 01:00:06 +0200 Subject: [PATCH] Update test suite testing more functions in exception scenario --- tests/except-array.c | 3 +++ tests/except-list.c | 60 ++++++++++++++++++++++++++++++++++++++--- tests/test-obj-except.h | 24 ++++++++--------- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/tests/except-array.c b/tests/except-array.c index 78933f22..38c81338 100644 --- a/tests/except-array.c +++ b/tests/except-array.c @@ -60,6 +60,7 @@ static void test1(unsigned n) bool b = array_obj_parse_str(array, string_get_cstr(str), NULL); assert(b); } + array_obj_insert_v(tmp, 12, 3); } } M_CATCH(test1, 0) { // Nothing to do @@ -77,6 +78,7 @@ static void test1(unsigned n) } M_CATCH(test1, 0) { // Nothing to do } + fclose(f); } static void do_test1(void) @@ -84,6 +86,7 @@ static void do_test1(void) // Run once to get the number of exceptions point existing in the test service test_obj_except__trigger_exception = 0; test1(10); + test_obj_except__final_check(); int count = -test_obj_except__trigger_exception; assert(count > 0); // Run once again the test service, and for each registered exception point, throw an exception diff --git a/tests/except-list.c b/tests/except-list.c index d5cbaa3b..dcc4bf5a 100644 --- a/tests/except-list.c +++ b/tests/except-list.c @@ -31,6 +31,9 @@ M_TRY_DEF_ONCE() LIST_DEF(list_obj, test_obj_except__t, TEST_OBJ_EXCEPT_OPLIST) #define M_OPL_list_obj_t() LIST_OPLIST(list_obj, TEST_OBJ_EXCEPT_OPLIST) +LIST_DUAL_PUSH_DEF(list2_obj, test_obj_except__t, TEST_OBJ_EXCEPT_OPLIST) +#define M_OPL_list2_obj_t() LIST_OPLIST(list2_obj, TEST_OBJ_EXCEPT_OPLIST) + static void test1(unsigned n) { FILE *f = m_core_fopen ("a-elist.dat", "wt"); @@ -53,6 +56,8 @@ static void test1(unsigned n) bool b = list_obj_parse_str(list, string_get_cstr(str), NULL); assert(b); } + list_obj_it_t it; list_obj_it(it, tmp); list_obj_next(it); + list_obj_insert(tmp, it, obj); } } M_CATCH(test1, 0) { // Nothing to do @@ -70,19 +75,65 @@ static void test1(unsigned n) } M_CATCH(test1, 0) { // Nothing to do } + fclose(f); } -static void do_test1(void) +static void test2(unsigned n) +{ + FILE *f = m_core_fopen ("a-elist.dat", "wt"); + assert(f != NULL); + M_TRY(test1) { + M_LET(obj, test_obj_except__t) + M_LET(list, tmp, list2_obj_t) { + for(unsigned i = 0; i < n; i++) { + test_obj_except__set_ui(obj, i); + list2_obj_push_back(list, obj); + } + list2_obj_set(tmp, list); + M_LET( (tmp2, tmp), list2_obj_t) { + list2_obj_out_str(f, tmp2); + } + list2_obj_push_back_new(tmp); + list2_obj_splice(tmp, list); + M_LET(str, string_t) { + list2_obj_get_str(str, tmp, false); + bool b = list2_obj_parse_str(list, string_get_cstr(str), NULL); + assert(b); + } + list2_obj_it_t it; list2_obj_it(it, tmp); list2_obj_next(it); + list2_obj_insert(tmp, it, obj); + } + } M_CATCH(test1, 0) { + // Nothing to do + } + fclose(f); + + f = m_core_fopen ("a-elist.dat", "rt"); + assert(f != NULL); + M_TRY(test1) { + M_LET(obj, test_obj_except__t) + M_LET(list, tmp, list2_obj_t) { + bool b = list2_obj_in_str(list, f); + (void)b; + } + } M_CATCH(test1, 0) { + // Nothing to do + } + fclose(f); +} + +static void do_test1(void (*test)(unsigned)) { // Run once to get the number of exceptions point existing in the test service test_obj_except__trigger_exception = 0; - test1(10); + test(10); + test_obj_except__final_check(); int count = -test_obj_except__trigger_exception; assert(count > 0); // Run once again the test service, and for each registered exception point, throw an exception for(int i = 1; i <= count; i++) { test_obj_except__trigger_exception = i; - test1(10); + test(10); // Check there is no memory leak test_obj_except__final_check(); } @@ -90,6 +141,7 @@ static void do_test1(void) int main(void) { - do_test1(); + do_test1(test1); + //do_test1(test2); exit(0); } diff --git a/tests/test-obj-except.h b/tests/test-obj-except.h index b42b63b9..bd309e06 100644 --- a/tests/test-obj-except.h +++ b/tests/test-obj-except.h @@ -32,19 +32,21 @@ // Overwrite malloc to throw exceptions sometimes // Needs to be done before including M*LIB headers +// TODO: Not representative enough: it shall not throw an exception itself but return NULL static inline void test_obj_except_trigger(void); +extern int test_obj_except__malloc_counter; #ifdef __cplusplus # include -# define M_MEMORY_ALLOC(type) (test_obj_except_trigger(), (type*)std::malloc (sizeof (type))) -# define M_MEMORY_DEL(ptr) std::free(ptr) +# define M_MEMORY_ALLOC(type) (test_obj_except_trigger(), test_obj_except__malloc_counter++, (type*)std::malloc (sizeof (type))) +# define M_MEMORY_DEL(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), std::free(ptr)) # define M_MEMORY_REALLOC(type, ptr, n) \ - ((type*) (test_obj_except_trigger(), std::realloc ((ptr), (n)*sizeof (type)))) -# define M_MEMORY_FREE(ptr) std::free(ptr) + ((type*) (test_obj_except_trigger(), test_obj_except__malloc_counter+= (ptr == NULL), std::realloc ((ptr), (n)*sizeof (type)))) +# define M_MEMORY_FREE(ptr) (test_obj_except__malloc_counter--, std::free(ptr)) #else -# define M_MEMORY_ALLOC(type) (test_obj_except_trigger(), malloc (sizeof (type))) -# define M_MEMORY_DEL(ptr) free(ptr) -# define M_MEMORY_REALLOC(type, ptr, n) (test_obj_except_trigger(), realloc ((ptr), (n)*sizeof (type))) -# define M_MEMORY_FREE(ptr) free(ptr) +# define M_MEMORY_ALLOC(type) (test_obj_except_trigger(), test_obj_except__malloc_counter++, malloc (sizeof (type))) +# define M_MEMORY_DEL(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), free(ptr)) +# define M_MEMORY_REALLOC(type, ptr, n) (test_obj_except_trigger(), test_obj_except__malloc_counter+=(ptr == NULL), realloc ((ptr), (n)*sizeof (type))) +# define M_MEMORY_FREE(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), free(ptr)) #endif // Include M*LIB headers @@ -62,6 +64,7 @@ typedef struct test_obj_except__s { } test_obj_except__t[1]; int test_obj_except__init_counter = 0; +int test_obj_except__malloc_counter = 0; int test_obj_except__trigger_exception = 0; // Trigger an exception if the counter reaches 0 @@ -77,6 +80,7 @@ static inline void test_obj_except_trigger(void) static inline void test_obj_except__final_check(void) { assert(test_obj_except__init_counter == 0); + assert(test_obj_except__malloc_counter == 0); } static inline void test_obj_except__init(test_obj_except__t z) @@ -85,13 +89,11 @@ static inline void test_obj_except__init(test_obj_except__t z) z->n = 1; z->ptr = (unsigned int*) calloc(1, sizeof(unsigned int)); assert(z->ptr != NULL); - //printf("%p: init\n", z); test_obj_except__init_counter++; } static inline void test_obj_except__clear(test_obj_except__t z) { - //printf("%p: clear\n", z); assert (z->ptr != NULL); free(z->ptr); z->ptr = NULL; @@ -106,7 +108,6 @@ static inline void test_obj_except__init_set(test_obj_except__t d, const test_ob d->ptr = (unsigned int *) calloc (s->n, sizeof(unsigned int)); assert(d->ptr != NULL); memcpy(d->ptr, s->ptr, sizeof(unsigned int) * s->n); - //printf("%p: init_set\n", d); test_obj_except__init_counter++; } @@ -114,7 +115,6 @@ static inline void test_obj_except__set(test_obj_except__t d, const test_obj_exc { if (d != s) { test_obj_except_trigger(); - //printf("%p: set\n", d); test_obj_except__clear(d); // Don't trigger exception for init_set now test_obj_except__trigger_exception++;