From ee1174d1b5f84ae29b5182e3a4497bf79f9177f2 Mon Sep 17 00:00:00 2001 From: Leonardo Date: Sun, 29 Mar 2020 19:13:39 -0300 Subject: [PATCH] Added flags to queue, sortedlist and stack --- src/cmc/queue.h | 32 ++++++++++++--- src/cmc/sortedlist.h | 32 +++++++++++++-- src/cmc/stack.h | 32 ++++++++++++--- tests/main/unt/queue.c | 68 +++++++++++++++++++++++++++++++ tests/main/unt/sortedlist.c | 79 +++++++++++++++++++++++++++++++++++++ tests/main/unt/stack.c | 66 +++++++++++++++++++++++++++++++ 6 files changed, 296 insertions(+), 13 deletions(-) diff --git a/src/cmc/queue.h b/src/cmc/queue.h index 861ef9c..e93645f 100644 --- a/src/cmc/queue.h +++ b/src/cmc/queue.h @@ -359,6 +359,9 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> " _queue_->count++; \ _queue_->flag = cmc_flags.OK; \ \ + if (_queue_->callbacks && _queue_->callbacks->create) \ + _queue_->callbacks->create(); \ + \ return true; \ } \ \ @@ -378,6 +381,9 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> " _queue_->count--; \ _queue_->flag = cmc_flags.OK; \ \ + if (_queue_->callbacks && _queue_->callbacks->delete) \ + _queue_->callbacks->delete (); \ + \ return true; \ } \ \ @@ -391,6 +397,9 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> " \ _queue_->flag = cmc_flags.OK; \ \ + if (_queue_->callbacks && _queue_->callbacks->read) \ + _queue_->callbacks->read(); \ + \ return _queue_->buffer[_queue_->front]; \ } \ \ @@ -398,15 +407,23 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> " { \ _queue_->flag = cmc_flags.OK; \ \ + bool result = false; \ + \ for (size_t i = _queue_->front, j = 0; j < _queue_->count; j++) \ { \ if (_queue_->f_val->cmp(_queue_->buffer[i], element) == 0) \ - return true; \ + { \ + result = true; \ + break; \ + } \ \ i = (i + 1) % _queue_->capacity; \ } \ \ - return false; \ + if (_queue_->callbacks && _queue_->callbacks->read) \ + _queue_->callbacks->read(); \ + \ + return result; \ } \ \ bool PFX##_empty(struct SNAME *_queue_) \ @@ -436,10 +453,8 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> " \ bool PFX##_resize(struct SNAME *_queue_, size_t capacity) \ { \ - _queue_->flag = cmc_flags.OK; \ - \ if (_queue_->capacity == capacity) \ - return true; \ + goto success; \ \ if (capacity < _queue_->count) \ { \ @@ -469,6 +484,13 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> " _queue_->front = 0; \ _queue_->back = _queue_->count; \ \ + success: \ + \ + _queue_->flag = cmc_flags.OK; \ + \ + if (_queue_->callbacks && _queue_->callbacks->resize) \ + _queue_->callbacks->resize(); \ + \ return true; \ } \ \ diff --git a/src/cmc/sortedlist.h b/src/cmc/sortedlist.h index 7307fdb..802204c 100644 --- a/src/cmc/sortedlist.h +++ b/src/cmc/sortedlist.h @@ -325,6 +325,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " _list_->is_sorted = false; \ _list_->flag = cmc_flags.OK; \ \ + if (_list_->callbacks && _list_->callbacks->create) \ + _list_->callbacks->create(); \ + \ return true; \ } \ \ @@ -349,6 +352,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ _list_->flag = cmc_flags.OK; \ \ + if (_list_->callbacks && _list_->callbacks->delete) \ + _list_->callbacks->delete (); \ + \ return true; \ } \ \ @@ -364,6 +370,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ _list_->flag = cmc_flags.OK; \ \ + if (_list_->callbacks && _list_->callbacks->read) \ + _list_->callbacks->read(); \ + \ return _list_->buffer[_list_->count - 1]; \ } \ \ @@ -379,6 +388,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ _list_->flag = cmc_flags.OK; \ \ + if (_list_->callbacks && _list_->callbacks->read) \ + _list_->callbacks->read(); \ + \ return _list_->buffer[0]; \ } \ \ @@ -400,6 +412,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ _list_->flag = cmc_flags.OK; \ \ + if (_list_->callbacks && _list_->callbacks->read) \ + _list_->callbacks->read(); \ + \ return _list_->buffer[index]; \ } \ \ @@ -409,6 +424,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ PFX##_sort(_list_); \ \ + if (_list_->callbacks && _list_->callbacks->read) \ + _list_->callbacks->read(); \ + \ if (from_start) \ { \ return PFX##_impl_binary_search_first(_list_, element); \ @@ -426,6 +444,9 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ PFX##_sort(_list_); \ \ + if (_list_->callbacks && _list_->callbacks->read) \ + _list_->callbacks->read(); \ + \ return PFX##_impl_binary_search_first(_list_, element) < \ _list_->count; \ } \ @@ -457,10 +478,8 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " \ bool PFX##_resize(struct SNAME *_list_, size_t capacity) \ { \ - _list_->flag = cmc_flags.OK; \ - \ if (_list_->capacity == capacity) \ - return true; \ + goto success; \ \ if (capacity < _list_->count) \ { \ @@ -482,6 +501,13 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " _list_->buffer = new_buffer; \ _list_->capacity = capacity; \ \ + success: \ + \ + _list_->flag = cmc_flags.OK; \ + \ + if (_list_->callbacks && _list_->callbacks->resize) \ + _list_->callbacks->resize(); \ + \ return true; \ } \ \ diff --git a/src/cmc/stack.h b/src/cmc/stack.h index d2c0794..ef98195 100644 --- a/src/cmc/stack.h +++ b/src/cmc/stack.h @@ -315,6 +315,9 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> " \ _stack_->flag = cmc_flags.OK; \ \ + if (_stack_->callbacks && _stack_->callbacks->create) \ + _stack_->callbacks->create(); \ + \ return true; \ } \ \ @@ -330,6 +333,9 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> " \ _stack_->flag = cmc_flags.OK; \ \ + if (_stack_->callbacks && _stack_->callbacks->delete) \ + _stack_->callbacks->delete (); \ + \ return true; \ } \ \ @@ -343,6 +349,9 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> " \ _stack_->flag = cmc_flags.OK; \ \ + if (_stack_->callbacks && _stack_->callbacks->read) \ + _stack_->callbacks->read(); \ + \ return _stack_->buffer[_stack_->count - 1]; \ } \ \ @@ -350,13 +359,21 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> " { \ _stack_->flag = cmc_flags.OK; \ \ + bool result = false; \ + \ for (size_t i = 0; i < _stack_->count; i++) \ { \ if (_stack_->f_val->cmp(_stack_->buffer[i], element) == 0) \ - return true; \ + { \ + result = true; \ + break; \ + } \ } \ \ - return false; \ + if (_stack_->callbacks && _stack_->callbacks->read) \ + _stack_->callbacks->read(); \ + \ + return result; \ } \ \ bool PFX##_empty(struct SNAME *_stack_) \ @@ -386,10 +403,8 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> " \ bool PFX##_resize(struct SNAME *_stack_, size_t capacity) \ { \ - _stack_->flag = cmc_flags.OK; \ - \ if (_stack_->capacity == capacity) \ - return true; \ + goto success; \ \ if (capacity < _stack_->count) \ { \ @@ -411,6 +426,13 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> " _stack_->buffer = new_buffer; \ _stack_->capacity = capacity; \ \ + success: \ + \ + _stack_->flag = cmc_flags.OK; \ + \ + if (_stack_->callbacks && _stack_->callbacks->resize) \ + _stack_->callbacks->resize(); \ + \ return true; \ } \ \ diff --git a/tests/main/unt/queue.c b/tests/main/unt/queue.c index b3d8667..7fe02b6 100644 --- a/tests/main/unt/queue.c +++ b/tests/main/unt/queue.c @@ -478,4 +478,72 @@ CMC_CREATE_UNIT(queue_test, true, { q_free(q); }); + + CMC_CREATE_TEST(callbacks, { + struct queue *q = q_new_custom(100, q_ftab_val, NULL, callbacks); + + cmc_assert_not_equals(ptr, NULL, q); + + total_create = 0; + total_read = 0; + total_update = 0; + total_delete = 0; + total_resize = 0; + + cmc_assert(q_enqueue(q, 10)); + cmc_assert_equals(int32_t, 1, total_create); + + cmc_assert(q_dequeue(q)); + cmc_assert_equals(int32_t, 1, total_delete); + + cmc_assert(q_enqueue(q, 10)); + cmc_assert_equals(int32_t, 2, total_create); + + cmc_assert_equals(size_t, 10, q_peek(q)); + cmc_assert_equals(int32_t, 1, total_read); + + cmc_assert(q_contains(q, 10)); + cmc_assert_equals(int32_t, 2, total_read); + + cmc_assert(q_resize(q, 1000)); + cmc_assert_equals(int32_t, 1, total_resize); + + cmc_assert(q_resize(q, 100)); + cmc_assert_equals(int32_t, 2, total_resize); + + cmc_assert_equals(int32_t, 2, total_create); + cmc_assert_equals(int32_t, 2, total_read); + cmc_assert_equals(int32_t, 0, total_update); + cmc_assert_equals(int32_t, 1, total_delete); + cmc_assert_equals(int32_t, 2, total_resize); + + q_customize(q, NULL, NULL); + + cmc_assert_equals(ptr, NULL, q->callbacks); + + q_clear(q); + cmc_assert(q_enqueue(q, 10)); + cmc_assert(q_dequeue(q)); + cmc_assert(q_enqueue(q, 10)); + cmc_assert_equals(size_t, 10, q_peek(q)); + cmc_assert(q_contains(q, 10)); + cmc_assert(q_resize(q, 1000)); + cmc_assert(q_resize(q, 100)); + + cmc_assert_equals(int32_t, 2, total_create); + cmc_assert_equals(int32_t, 2, total_read); + cmc_assert_equals(int32_t, 0, total_update); + cmc_assert_equals(int32_t, 1, total_delete); + cmc_assert_equals(int32_t, 2, total_resize); + + cmc_assert_equals(ptr, NULL, q->callbacks); + + q_free(q); + + total_create = 0; + total_read = 0; + total_update = 0; + total_delete = 0; + total_resize = 0; + }); }); diff --git a/tests/main/unt/sortedlist.c b/tests/main/unt/sortedlist.c index 8f1afc8..150dde4 100644 --- a/tests/main/unt/sortedlist.c +++ b/tests/main/unt/sortedlist.c @@ -235,4 +235,83 @@ CMC_CREATE_UNIT(sortedlist_test, true, { sl_free(sl); sl_free(sl2); }); + + CMC_CREATE_TEST(callbacks, { + struct sortedlist *sl = + sl_new_custom(100, sl_ftab_val, NULL, callbacks); + + cmc_assert_not_equals(ptr, NULL, sl); + + total_create = 0; + total_read = 0; + total_update = 0; + total_delete = 0; + total_resize = 0; + + cmc_assert(sl_insert(sl, 10)); + cmc_assert_equals(int32_t, 1, total_create); + + cmc_assert(sl_remove(sl, 0)); + cmc_assert_equals(int32_t, 1, total_delete); + + cmc_assert(sl_insert(sl, 1)); + cmc_assert(sl_insert(sl, 0)); + cmc_assert_equals(int32_t, 3, total_create); + + cmc_assert_equals(size_t, 1, sl_max(sl)); + cmc_assert_equals(int32_t, 1, total_read); + + cmc_assert_equals(size_t, 0, sl_min(sl)); + cmc_assert_equals(int32_t, 2, total_read); + + cmc_assert_equals(size_t, 0, sl_get(sl, 0)); + cmc_assert_equals(int32_t, 3, total_read); + + cmc_assert_equals(size_t, 0, sl_index_of(sl, 0, true)); + cmc_assert_equals(int32_t, 4, total_read); + + cmc_assert(sl_contains(sl, 1)); + cmc_assert_equals(int32_t, 5, total_read); + + cmc_assert(sl_resize(sl, 1000)); + cmc_assert_equals(int32_t, 1, total_resize); + + cmc_assert(sl_resize(sl, 50)); + cmc_assert_equals(int32_t, 2, total_resize); + + cmc_assert_equals(int32_t, 3, total_create); + cmc_assert_equals(int32_t, 5, total_read); + cmc_assert_equals(int32_t, 0, total_update); + cmc_assert_equals(int32_t, 1, total_delete); + cmc_assert_equals(int32_t, 2, total_resize); + + sl_customize(sl, NULL, NULL); + + sl_clear(sl); + cmc_assert(sl_insert(sl, 10)); + cmc_assert(sl_remove(sl, 0)); + cmc_assert(sl_insert(sl, 1)); + cmc_assert(sl_insert(sl, 0)); + cmc_assert_equals(size_t, 1, sl_max(sl)); + cmc_assert_equals(size_t, 0, sl_min(sl)); + cmc_assert_equals(size_t, 0, sl_get(sl, 0)); + cmc_assert_equals(size_t, 0, sl_index_of(sl, 0, true)); + cmc_assert(sl_contains(sl, 1)); + + cmc_assert_equals(int32_t, 3, total_create); + cmc_assert_equals(int32_t, 5, total_read); + cmc_assert_equals(int32_t, 0, total_update); + cmc_assert_equals(int32_t, 1, total_delete); + cmc_assert_equals(int32_t, 2, total_resize); + + cmc_assert_equals(ptr, NULL, sl->callbacks); + + sl_free(sl); + + total_create = 0; + total_read = 0; + total_update = 0; + total_delete = 0; + total_resize = 0; + }); }); diff --git a/tests/main/unt/stack.c b/tests/main/unt/stack.c index 22e872f..5ebc986 100644 --- a/tests/main/unt/stack.c +++ b/tests/main/unt/stack.c @@ -671,4 +671,70 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s); }); + + CMC_CREATE_TEST(callbacks, { + struct stack *s = s_new_custom(100, s_ftab_val, NULL, callbacks); + + cmc_assert_not_equals(ptr, NULL, s); + + total_create = 0; + total_read = 0; + total_update = 0; + total_delete = 0; + total_resize = 0; + + cmc_assert(s_push(s, 10)); + cmc_assert_equals(int32_t, 1, total_create); + + cmc_assert(s_pop(s)); + cmc_assert_equals(int32_t, 1, total_delete); + + cmc_assert(s_push(s, 1)); + cmc_assert_equals(int32_t, 2, total_create); + + cmc_assert_equals(size_t, 1, s_top(s)); + cmc_assert_equals(int32_t, 1, total_read); + + cmc_assert(s_contains(s, 1)); + cmc_assert_equals(int32_t, 2, total_read); + + cmc_assert(s_resize(s, 1000)); + cmc_assert_equals(int32_t, 1, total_resize); + + cmc_assert(s_resize(s, 10)); + cmc_assert_equals(int32_t, 2, total_resize); + + cmc_assert_equals(int32_t, 2, total_create); + cmc_assert_equals(int32_t, 2, total_read); + cmc_assert_equals(int32_t, 0, total_update); + cmc_assert_equals(int32_t, 1, total_delete); + cmc_assert_equals(int32_t, 2, total_resize); + + s_customize(s, NULL, NULL); + + s_clear(s); + cmc_assert(s_push(s, 10)); + cmc_assert(s_pop(s)); + cmc_assert(s_push(s, 1)); + cmc_assert_equals(size_t, 1, s_top(s)); + cmc_assert(s_contains(s, 1)); + cmc_assert(s_resize(s, 1000)); + cmc_assert(s_resize(s, 10)); + + cmc_assert_equals(int32_t, 2, total_create); + cmc_assert_equals(int32_t, 2, total_read); + cmc_assert_equals(int32_t, 0, total_update); + cmc_assert_equals(int32_t, 1, total_delete); + cmc_assert_equals(int32_t, 2, total_resize); + + cmc_assert_equals(ptr, NULL, s->callbacks); + + s_free(s); + + total_create = 0; + total_read = 0; + total_update = 0; + total_delete = 0; + total_resize = 0; + }); });