Added callbacks to linkedlist and list

This commit is contained in:
Leonardo committed 2020-03-20 15:18:12 -03:00
1 parent 5b8f64b421
commit 701a24f47c
4 files changed
+225 -8

No files matched your search

+40 -2
View File
@@ -351,6 +351,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
_list_->count++; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -386,6 +389,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
_list_->count++; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -411,6 +417,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
_list_->count++; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -435,6 +444,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
_list_->count--; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -474,6 +486,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
_list_->count--; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -498,6 +513,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
_list_->count--; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -511,6 +529,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return _list_->head->value; \
} \
\
@@ -533,6 +554,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
if (scan == NULL) \
return (V){ 0 }; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return scan->value; \
} \
\
@@ -555,6 +579,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
if (scan == NULL) \
return NULL; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return &(scan->value); \
} \
\
@@ -568,6 +595,9 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return _list_->tail->value; \
} \
\
@@ -575,17 +605,25 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
{ \
_list_->flag = cmc_flags.OK; \
\
bool result = false; \
\
struct SNAME##_node *scan = _list_->head; \
\
while (scan != NULL) \
{ \
if (_list_->f_val->cmp(scan->value, element) == 0) \
return true; \
{ \
result = true; \
break; \
} \
\
scan = scan->next; \
} \
\
return false; \
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return result; \
} \
\
bool PFX##_empty(struct SNAME *_list_) \
+73 -6
View File
@@ -351,6 +351,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->count++; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -375,6 +378,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->count++; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -389,6 +395,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->buffer[_list_->count++] = element; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -406,6 +415,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->buffer[--_list_->count] = (V){ 0 }; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -429,6 +441,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->buffer[--_list_->count] = (V){ 0 }; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -440,6 +455,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->buffer[--_list_->count] = (V){ 0 }; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -465,6 +483,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->count += size; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -502,6 +523,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->count += size; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -524,6 +548,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->count += size; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->create) \
_list_->callbacks->create(); \
\
return true; \
} \
\
@@ -552,6 +579,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->count -= to - from + 1; \
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return true; \
} \
\
@@ -581,7 +611,7 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
return NULL; \
} \
\
memcpy(result->buffer, _list_->buffer, _list_->count * sizeof(V)); \
memcpy(result->buffer, _list_->buffer, length * sizeof(V)); \
\
memmove(_list_->buffer + from, _list_->buffer + to + 1, \
(_list_->count - to - 1) * sizeof(V)); \
@@ -594,6 +624,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->delete) \
_list_->callbacks->delete (); \
\
return result; \
} \
\
@@ -607,6 +640,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return _list_->buffer[0]; \
} \
\
@@ -626,6 +662,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return _list_->buffer[index]; \
} \
\
@@ -645,6 +684,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return &(_list_->buffer[index]); \
} \
\
@@ -658,6 +700,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
\
_list_->flag = cmc_flags.OK; \
\
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return _list_->buffer[_list_->count - 1]; \
} \
\
@@ -665,12 +710,17 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
{ \
_list_->flag = cmc_flags.OK; \
\
size_t result = _list_->count; \
\
if (from_start) \
{ \
for (size_t i = 0; i < _list_->count; i++) \
{ \
if (_list_->f_val->cmp(_list_->buffer[i], element) == 0) \
return i; \
{ \
result = i; \
break; \
} \
} \
} \
else \
@@ -678,24 +728,38 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
for (size_t i = _list_->count; i > 0; i--) \
{ \
if (_list_->f_val->cmp(_list_->buffer[i - 1], element) == 0) \
return i - 1; \
{ \
result = i - 1; \
break; \
} \
} \
} \
\
return _list_->count; \
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return result; \
} \
\
bool PFX##_contains(struct SNAME *_list_, V element) \
{ \
_list_->flag = cmc_flags.OK; \
\
bool result = false; \
\
for (size_t i = 0; i < _list_->count; i++) \
{ \
if (_list_->f_val->cmp(_list_->buffer[i], element) == 0) \
return true; \
{ \
result = true; \
break; \
} \
} \
\
return false; \
if (_list_->callbacks && _list_->callbacks->read) \
_list_->callbacks->read(); \
\
return result; \
} \
\
bool PFX##_empty(struct SNAME *_list_) \
@@ -755,6 +819,9 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->buffer = new_buffer; \
_list_->capacity = capacity; \
\
if (_list_->callbacks && _list_->callbacks->resize) \
_list_->callbacks->resize(); \
\
return true; \
} \
\
+48
View File
@@ -256,4 +256,52 @@ CMC_CREATE_UNIT(linkedlist_test, true, {
ll_free(ll);
ll_free(ll2);
});
CMC_CREATE_TEST(callbacks, {
struct linkedlist *ll = ll_new_custom(ll_ftab_val, NULL, callbacks);
total_create = 0;
total_read = 0;
total_update = 0;
total_delete = 0;
total_resize = 0;
cmc_assert(ll_push_front(ll, 10));
cmc_assert_equals(int32_t, 1, total_create);
cmc_assert(ll_push_back(ll, 10));
cmc_assert_equals(int32_t, 2, total_create);
cmc_assert(ll_push_at(ll, 10, 1));
cmc_assert_equals(int32_t, 3, total_create);
cmc_assert(ll_pop_at(ll, 1));
cmc_assert_equals(int32_t, 1, total_delete);
cmc_assert(ll_pop_front(ll));
cmc_assert_equals(int32_t, 2, total_delete);
cmc_assert(ll_pop_back(ll));
cmc_assert_equals(int32_t, 3, total_delete);
cmc_assert(ll_push_back(ll, 10));
cmc_assert_equals(int32_t, 4, total_create);
cmc_assert_equals(size_t, 10, ll_front(ll));
cmc_assert_equals(int32_t, 1, total_read);
cmc_assert_equals(size_t, 10, ll_back(ll));
cmc_assert_equals(int32_t, 2, total_read);
cmc_assert_equals(size_t, 10, ll_get(ll, 0));
cmc_assert_equals(int32_t, 3, total_read);
cmc_assert_not_equals(ptr, NULL, ll_get_ref(ll, 0));
cmc_assert_equals(int32_t, 4, total_read);
cmc_assert(ll_contains(ll, 10));
cmc_assert_equals(int32_t, 5, total_read);
ll_free(ll);
});
});
+64
View File
@@ -496,4 +496,68 @@ CMC_CREATE_UNIT(list_test, true, {
l_free(l2);
l_free(l3);
});
CMC_CREATE_TEST(callbacks, {
struct list *l = l_new_custom(100, l_ftab_val, NULL, callbacks);
total_create = 0;
total_read = 0;
total_update = 0;
total_delete = 0;
total_resize = 0;
cmc_assert(l_push_front(l, 10));
cmc_assert_equals(int32_t, 1, total_create);
cmc_assert(l_push_back(l, 10));
cmc_assert_equals(int32_t, 2, total_create);
cmc_assert(l_push_at(l, 10, 1));
cmc_assert_equals(int32_t, 3, total_create);
cmc_assert(l_pop_at(l, 1));
cmc_assert_equals(int32_t, 1, total_delete);
cmc_assert(l_pop_front(l));
cmc_assert_equals(int32_t, 2, total_delete);
cmc_assert(l_pop_back(l));
cmc_assert_equals(int32_t, 3, total_delete);
cmc_assert(l_push_back(l, 10));
cmc_assert_equals(int32_t, 4, total_create);
cmc_assert_equals(size_t, 10, l_front(l));
cmc_assert_equals(int32_t, 1, total_read);
cmc_assert_equals(size_t, 10, l_back(l));
cmc_assert_equals(int32_t, 2, total_read);
cmc_assert_equals(size_t, 10, l_get(l, 0));
cmc_assert_equals(int32_t, 3, total_read);
cmc_assert_not_equals(ptr, NULL, l_get_ref(l, 0));
cmc_assert_equals(int32_t, 4, total_read);
cmc_assert(l_contains(l, 10));
cmc_assert_equals(int32_t, 5, total_read);
cmc_assert(l_seq_push_front(l, (size_t[]) {7, 8, 9}, 3));
cmc_assert_equals(int32_t, 5, total_create);
cmc_assert(l_seq_push_back(l, (size_t[]) {14, 15, 16}, 3));
cmc_assert_equals(int32_t, 6, total_create);
cmc_assert(l_seq_push_at(l, (size_t[]) {11, 12, 13}, 3, 4));
cmc_assert_equals(int32_t, 7, total_create);
cmc_assert(l_seq_pop_at(l, 1, 4));
cmc_assert_equals(int32_t, 4, total_delete);
struct list *l2 = l_seq_sublist(l, 0, 4);
cmc_assert_equals(int32_t, 5, total_delete);
l_free(l);
l_free(l2);
});
})