Skip to content

Commit

Permalink
List stabilization part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoVen committed Dec 10, 2023
1 parent 32deb83 commit e4dbeae
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
27 changes: 18 additions & 9 deletions src/cmc/cmc/list/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down
10 changes: 10 additions & 0 deletions src/cmc/utl/futils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 22 additions & 24 deletions tests/unt_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions tests/utl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit e4dbeae

Please sign in to comment.