From e4dbeae46dc9df460ea2ef612cf999ef45096a56 Mon Sep 17 00:00:00 2001 From: Leonardo Vencovsky Date: Fri, 3 Mar 2023 22:12:28 +0100 Subject: [PATCH] List stabilization part 1 --- src/cmc/cmc/list/code.h | 27 ++++++++++++++++-------- src/cmc/utl/futils.h | 10 +++++++++ tests/unt_list.h | 46 ++++++++++++++++++++--------------------- tests/utl.h | 7 +++++++ 4 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/cmc/cmc/list/code.h b/src/cmc/cmc/list/code.h index f038a14..f64c96f 100644 --- a/src/cmc/cmc/list/code.h +++ b/src/cmc/cmc/list/code.h @@ -61,9 +61,6 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) return NULL; #endif - if (!f_val) - return NULL; - if (!alloc) alloc = &cmc_alloc_node_default; @@ -104,7 +101,7 @@ void CMC_(PFX, _clear)(struct SNAME *_list_) CMC_DEV_FCALL; #endif - if (_list_->f_val->free) + if (_list_->f_val && _list_->f_val->free) { for (size_t i = 0; i < _list_->count; i++) _list_->f_val->free(_list_->buffer[i]); @@ -126,7 +123,7 @@ void CMC_(PFX, _free)(struct SNAME *_list_) CMC_DEV_FCALL; #endif - if (_list_->f_val->free) + if (_list_->f_val && _list_->f_val->free) { for (size_t i = 0; i < _list_->count; i++) _list_->f_val->free(_list_->buffer[i]); @@ -418,6 +415,12 @@ size_t CMC_(PFX, _index_of)(struct SNAME *_list_, V value, bool from_start) CMC_DEV_FCALL; #endif + if (_list_->f_val == NULL || _list_->f_val->cmp == NULL) + { + _list_->flag = CMC_FLAG_FTABLE; + return 0; + } + _list_->flag = CMC_FLAG_OK; size_t result = _list_->count; @@ -426,7 +429,7 @@ size_t CMC_(PFX, _index_of)(struct SNAME *_list_, V value, bool from_start) { for (size_t i = 0; i < _list_->count; i++) { - if (_list_->f_val->cmp(_list_->buffer[i], value) == 0) + if (0 == _list_->f_val->cmp(_list_->buffer[i], value)) { result = i; break; @@ -437,7 +440,7 @@ size_t CMC_(PFX, _index_of)(struct SNAME *_list_, V value, bool from_start) { for (size_t i = _list_->count; i > 0; i--) { - if (_list_->f_val->cmp(_list_->buffer[i - 1], value) == 0) + if (0 == _list_->f_val->cmp(_list_->buffer[i - 1], value)) { result = i - 1; break; @@ -456,13 +459,19 @@ bool CMC_(PFX, _contains)(struct SNAME *_list_, V value) CMC_DEV_FCALL; #endif + if (_list_->f_val == NULL || _list_->f_val->cmp == NULL) + { + _list_->flag = CMC_FLAG_FTABLE; + return false; + } + _list_->flag = CMC_FLAG_OK; bool result = false; for (size_t i = 0; i < _list_->count; i++) { - if (_list_->f_val->cmp(_list_->buffer[i], value) == 0) + if (0 == _list_->f_val->cmp(_list_->buffer[i], value)) { result = true; break; @@ -598,7 +607,7 @@ struct SNAME *CMC_(PFX, _copy_of)(struct SNAME *_list_) CMC_CALLBACKS_ASSIGN(result, _list_->callbacks); - if (_list_->f_val->cpy) + if (_list_->f_val && _list_->f_val->cpy) { for (size_t i = 0; i < _list_->count; i++) result->buffer[i] = _list_->f_val->cpy(_list_->buffer[i]); diff --git a/src/cmc/utl/futils.h b/src/cmc/utl/futils.h index 93ae905..d8f8050 100644 --- a/src/cmc/utl/futils.h +++ b/src/cmc/utl/futils.h @@ -123,6 +123,11 @@ static inline int cmc_double_cmp(double x1, double x2) return (x1 > x2) - (x1 < x2); } +static inline int cmc_double_long_cmp(long double x1, long double x2) +{ + return (x1 > x2) - (x1 < x2); +} + // String static inline int cmc_chr_cmp(char ch1, char ch2) @@ -231,6 +236,11 @@ static inline bool cmc_double_str(FILE *file, double element) return fprintf(file, "%lf", element) > 0; } +static inline int cmc_double_long_str(FILE *file, long double element) +{ + return fprintf(file, "%LF", element); +} + // String static inline bool cmc_chr_chr(FILE *file, char element) diff --git a/tests/unt_list.h b/tests/unt_list.h index a605f92..e1a9f7e 100644 --- a/tests/unt_list.h +++ b/tests/unt_list.h @@ -13,30 +13,44 @@ struct list_fval *l_fval = &(struct list_fval){ }; CMC_CREATE_UNIT(CMCList, true, { - CMC_CREATE_TEST(new, { + CMC_CREATE_TEST(PFX##_new, { struct list *l = l_new(1000000, l_fval); cmc_assert_not_equals(ptr, NULL, l); cmc_assert_not_equals(ptr, NULL, l->buffer); + cmc_assert_equals(ptr, l->f_val, l_fval); cmc_assert_equals(size_t, 1000000, l_capacity(l)); cmc_assert_equals(size_t, 0, l_count(l)); l_free(l); - }); - CMC_CREATE_TEST(new[capacity = 0], { - struct list *l = l_new(0, l_fval); + // cap == 0 + l = l_new(0, l_fval); + cmc_assert_equals(ptr, NULL, l); + // cap == UINT64_MAX + l = l_new(UINT64_MAX, l_fval); cmc_assert_equals(ptr, NULL, l); }); - CMC_CREATE_TEST(new[capacity = UINT64_MAX], { - struct list *l = l_new(UINT64_MAX, l_fval); + CMC_CREATE_TEST(PFX##_new_custom, { + // All NULL + struct list *l = l_new_custom(100, NULL, NULL, NULL); + cmc_assert_not_equals(ptr, NULL, l); + l_free(l); - cmc_assert_equals(ptr, NULL, l); + l = l_new_custom(100, l_fval, custom_alloc, callbacks); + cmc_assert_not_equals(ptr, NULL, l); + cmc_assert_not_equals(ptr, NULL, l->buffer); + cmc_assert_equals(ptr, l_fval, l->f_val); + cmc_assert_equals(ptr, custom_alloc, l->alloc); + cmc_assert_equals(ptr, callbacks, l->callbacks); + cmc_assert_equals(size_t, 100, l_capacity(l)); + cmc_assert_equals(size_t, 0, l_count(l)); + cmc_assert_equals(int32_t, CMC_FLAG_OK, l_flag(l)); }); - CMC_CREATE_TEST(clear[count capacity], { + CMC_CREATE_TEST(PFX##_clear, { struct list *l = l_new(100, l_fval); cmc_assert_not_equals(ptr, NULL, l); @@ -54,22 +68,6 @@ CMC_CREATE_UNIT(CMCList, true, { l_free(l); }); - CMC_CREATE_TEST(buffer_growth[capacity = 1], { - struct list *l = l_new(1, l_fval); - - cmc_assert_not_equals(ptr, NULL, l); - - for (size_t i = 0; i < 100; i++) - l_push_back(l, i); - - cmc_assert_equals(size_t, 100, l_count(l)); - - for (size_t i = 0; i < 100; i++) - cmc_assert_equals(size_t, i, l_get(l, i)); - - l_free(l); - }); - CMC_CREATE_TEST(push_front[count], { struct list *l = l_new(100, l_fval); diff --git a/tests/utl.h b/tests/utl.h index cf670e4..c5b2a6a 100644 --- a/tests/utl.h +++ b/tests/utl.h @@ -134,4 +134,11 @@ void callbacks(CMC_UNUSED const char func_name[], CMC_UNUSED void *self) // EMPTY for now.. } +static struct CMC_ALLOC_NODE_NAME *custom_alloc = &(struct CMC_ALLOC_NODE_NAME){ + .malloc = malloc, + .calloc = calloc, + .realloc = realloc, + .free = free, +}; + #endif /* CMC_TESTS_UTL */