diff --git a/src/cmc/bidimap.h b/src/cmc/bidimap.h index dcbbef9..cfe9213 100644 --- a/src/cmc/bidimap.h +++ b/src/cmc/bidimap.h @@ -72,14 +72,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/deque.h b/src/cmc/deque.h index 1d94336..ad4b1e1 100644 --- a/src/cmc/deque.h +++ b/src/cmc/deque.h @@ -77,14 +77,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/hashmap.h b/src/cmc/hashmap.h index 7a26777..b225e38 100644 --- a/src/cmc/hashmap.h +++ b/src/cmc/hashmap.h @@ -65,14 +65,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/hashset.h b/src/cmc/hashset.h index 98bd4f0..00006d2 100644 --- a/src/cmc/hashset.h +++ b/src/cmc/hashset.h @@ -65,14 +65,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/heap.h b/src/cmc/heap.h index c4bb0ce..8aff2b3 100644 --- a/src/cmc/heap.h +++ b/src/cmc/heap.h @@ -76,14 +76,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/intervalheap.h b/src/cmc/intervalheap.h index d27127b..5527e98 100644 --- a/src/cmc/intervalheap.h +++ b/src/cmc/intervalheap.h @@ -67,14 +67,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/linkedlist.h b/src/cmc/linkedlist.h index a7eaf44..37dc69a 100644 --- a/src/cmc/linkedlist.h +++ b/src/cmc/linkedlist.h @@ -74,14 +74,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ @@ -425,7 +425,7 @@ struct cmc_callbacks_linkedlist { \ if (index > _list_->count) \ { \ - _list_->flag = cmc_flags.OUT_OF_RANGE; \ + _list_->flag = cmc_flags.RANGE; \ return false; \ } \ \ @@ -515,7 +515,7 @@ struct cmc_callbacks_linkedlist \ if (index >= _list_->count) \ { \ - _list_->flag = cmc_flags.OUT_OF_RANGE; \ + _list_->flag = cmc_flags.RANGE; \ return false; \ } \ \ @@ -591,7 +591,7 @@ struct cmc_callbacks_linkedlist \ if (index >= _list_->count) \ { \ - _list_->flag = cmc_flags.OUT_OF_RANGE; \ + _list_->flag = cmc_flags.RANGE; \ return (V){ 0 }; \ } \ \ @@ -613,7 +613,7 @@ struct cmc_callbacks_linkedlist \ if (index >= _list_->count) \ { \ - _list_->flag = cmc_flags.OUT_OF_RANGE; \ + _list_->flag = cmc_flags.RANGE; \ return NULL; \ } \ \ @@ -787,7 +787,7 @@ struct cmc_callbacks_linkedlist \ if (index >= _list_->count) \ { \ - _list_->flag = cmc_flags.OUT_OF_RANGE; \ + _list_->flag = cmc_flags.RANGE; \ return NULL; \ } \ \ diff --git a/src/cmc/list.h b/src/cmc/list.h index c937799..2662686 100644 --- a/src/cmc/list.h +++ b/src/cmc/list.h @@ -89,14 +89,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ @@ -245,7 +245,7 @@ struct cmc_callbacks_list V PFX##_get(struct SNAME *_list_, size_t index); \ V *PFX##_get_ref(struct SNAME *_list_, size_t index); \ V PFX##_back(struct SNAME *_list_); \ - size_t PFX##_indexof(struct SNAME *_list_, V element, bool from_start); \ + size_t PFX##_index_of(struct SNAME *_list_, V element, bool from_start); \ /* Collection State */ \ bool PFX##_contains(struct SNAME *_list_, V element); \ bool PFX##_empty(struct SNAME *_list_); \ @@ -253,6 +253,7 @@ struct cmc_callbacks_list size_t PFX##_count(struct SNAME *_list_); \ bool PFX##_fits(struct SNAME *_list_, size_t size); \ size_t PFX##_capacity(struct SNAME *_list_); \ + int PFX##_flag(struct SNAME *_list_); \ /* Collection Utility */ \ bool PFX##_resize(struct SNAME *_list_, size_t capacity); \ struct SNAME *PFX##_copy_of(struct SNAME *_list_); \ @@ -371,6 +372,7 @@ struct cmc_callbacks_list memset(_list_->buffer, 0, sizeof(V) * _list_->capacity); \ \ _list_->count = 0; \ + _list_->flag = cmc_flags.OK; \ } \ \ void PFX##_free(struct SNAME *_list_) \ @@ -393,13 +395,15 @@ struct cmc_callbacks_list \ if (callbacks) \ _list_->callbacks = callbacks; \ + \ + _list_->flag = cmc_flags.OK; \ } \ \ bool PFX##_push_front(struct SNAME *_list_, V element) \ { \ if (PFX##_full(_list_)) \ { \ - if (!PFX##_resize(_list_, PFX##_count(_list_) * 2)) \ + if (!PFX##_resize(_list_, _list_->count * 2)) \ return false; \ } \ \ @@ -412,6 +416,7 @@ struct cmc_callbacks_list _list_->buffer[0] = element; \ \ _list_->count++; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -419,11 +424,14 @@ struct cmc_callbacks_list bool PFX##_push_at(struct SNAME *_list_, V element, size_t index) \ { \ if (index > _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ return false; \ + } \ \ if (PFX##_full(_list_)) \ { \ - if (!PFX##_resize(_list_, PFX##_count(_list_) * 2)) \ + if (!PFX##_resize(_list_, _list_->count * 2)) \ return false; \ } \ \ @@ -432,6 +440,7 @@ struct cmc_callbacks_list \ _list_->buffer[index] = element; \ _list_->count++; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -440,11 +449,12 @@ struct cmc_callbacks_list { \ if (PFX##_full(_list_)) \ { \ - if (!PFX##_resize(_list_, PFX##_count(_list_) * 2)) \ + if (!PFX##_resize(_list_, _list_->count * 2)) \ return false; \ } \ \ _list_->buffer[_list_->count++] = element; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -452,25 +462,39 @@ struct cmc_callbacks_list bool PFX##_pop_front(struct SNAME *_list_) \ { \ if (PFX##_empty(_list_)) \ + { \ + _list_->flag = cmc_flags.EMPTY; \ return false; \ + } \ \ memmove(_list_->buffer, _list_->buffer + 1, \ (_list_->count - 1) * sizeof(V)); \ \ _list_->buffer[--_list_->count] = (V){ 0 }; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ \ bool PFX##_pop_at(struct SNAME *_list_, size_t index) \ { \ - if (index >= _list_->count) \ + if (PFX##_empty(_list_)) \ + { \ + _list_->flag = cmc_flags.EMPTY; \ return false; \ + } \ + \ + if (index >= _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ + return false; \ + } \ \ memmove(_list_->buffer + index, _list_->buffer + index + 1, \ (_list_->count - index - 1) * sizeof(V)); \ \ _list_->buffer[--_list_->count] = (V){ 0 }; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -481,6 +505,7 @@ struct cmc_callbacks_list return false; \ \ _list_->buffer[--_list_->count] = (V){ 0 }; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -488,11 +513,14 @@ struct cmc_callbacks_list bool PFX##_seq_push_front(struct SNAME *_list_, V *elements, size_t size) \ { \ if (size == 0) \ + { \ + _list_->flag = cmc_flags.INVALID; \ return false; \ + } \ \ if (!PFX##_fits(_list_, size)) \ { \ - if (!PFX##_resize(_list_, PFX##_count(_list_) + size)) \ + if (!PFX##_resize(_list_, _list_->count + size)) \ return false; \ } \ \ @@ -502,6 +530,7 @@ struct cmc_callbacks_list memcpy(_list_->buffer, elements, size * sizeof(V)); \ \ _list_->count += size; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -509,36 +538,47 @@ struct cmc_callbacks_list bool PFX##_seq_push_at(struct SNAME *_list_, V *elements, size_t size, \ size_t index) \ { \ - if (size == 0 || index > _list_->count) \ + if (size == 0) \ + { \ + _list_->flag = cmc_flags.INVALID; \ return false; \ + } \ + \ + if (index > _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ + return false; \ + } \ \ if (index == 0) \ return PFX##_seq_push_front(_list_, elements, size); \ else if (index == _list_->count) \ return PFX##_seq_push_back(_list_, elements, size); \ - else \ + \ + if (!PFX##_fits(_list_, size)) \ { \ - if (!PFX##_fits(_list_, size)) \ - { \ - if (!PFX##_resize(_list_, PFX##_count(_list_) + size)) \ - return false; \ - } \ - \ - memmove(_list_->buffer + index + size, _list_->buffer + index, \ - (_list_->count - index) * sizeof(V)); \ - \ - memcpy(_list_->buffer + index, elements, size * sizeof(V)); \ - \ - _list_->count += size; \ + if (!PFX##_resize(_list_, _list_->count + size)) \ + return false; \ } \ \ + memmove(_list_->buffer + index + size, _list_->buffer + index, \ + (_list_->count - index) * sizeof(V)); \ + \ + memcpy(_list_->buffer + index, elements, size * sizeof(V)); \ + \ + _list_->count += size; \ + _list_->flag = cmc_flags.OK; \ + \ return true; \ } \ \ bool PFX##_seq_push_back(struct SNAME *_list_, V *elements, size_t size) \ { \ if (size == 0) \ + { \ + _list_->flag = cmc_flags.INVALID; \ return false; \ + } \ \ if (!PFX##_fits(_list_, size)) \ { \ @@ -549,14 +589,24 @@ struct cmc_callbacks_list memcpy(_list_->buffer + _list_->count, elements, size * sizeof(V)); \ \ _list_->count += size; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ \ bool PFX##_seq_pop_at(struct SNAME *_list_, size_t from, size_t to) \ { \ - if (from > to || to >= _list_->count) \ + if (from > to) \ + { \ + _list_->flag = cmc_flags.INVALID; \ return false; \ + } \ + \ + if (to >= _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ + return false; \ + } \ \ size_t length = (to - from + 1); \ \ @@ -567,6 +617,7 @@ struct cmc_callbacks_list length * sizeof(V)); \ \ _list_->count -= to - from + 1; \ + _list_->flag = cmc_flags.OK; \ \ return true; \ } \ @@ -574,15 +625,28 @@ struct cmc_callbacks_list struct SNAME *PFX##_seq_sublist(struct SNAME *_list_, size_t from, \ size_t to) \ { \ - if (from > to || to >= _list_->count) \ - return false; \ + if (from > to) \ + { \ + _list_->flag = cmc_flags.INVALID; \ + return NULL; \ + } \ + \ + if (to >= _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ + return NULL; \ + } \ \ size_t length = to - from + 1; \ \ - struct SNAME *result = PFX##_new(length, _list_->f_val); \ + struct SNAME *result = PFX##_new_custom( \ + length, _list_->f_val, _list_->alloc, _list_->callbacks); \ \ if (!result) \ + { \ + _list_->flag = cmc_flags.ALLOC; \ return NULL; \ + } \ \ memcpy(result->buffer, _list_->buffer, _list_->count * sizeof(V)); \ \ @@ -595,32 +659,58 @@ struct cmc_callbacks_list _list_->count -= length; \ result->count = length; \ \ + _list_->flag = cmc_flags.OK; \ + \ return result; \ } \ \ V PFX##_front(struct SNAME *_list_) \ { \ if (PFX##_empty(_list_)) \ + { \ + _list_->flag = cmc_flags.EMPTY; \ return (V){ 0 }; \ + } \ + \ + _list_->flag = cmc_flags.OK; \ \ return _list_->buffer[0]; \ } \ \ V PFX##_get(struct SNAME *_list_, size_t index) \ { \ - if (index >= _list_->count || PFX##_empty(_list_)) \ + if (PFX##_empty(_list_)) \ + { \ + _list_->flag = cmc_flags.EMPTY; \ return (V){ 0 }; \ + } \ + \ + if (index >= _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ + return (V){ 0 }; \ + } \ + \ + _list_->flag = cmc_flags.OK; \ \ return _list_->buffer[index]; \ } \ \ V *PFX##_get_ref(struct SNAME *_list_, size_t index) \ { \ - if (index >= _list_->count) \ - return NULL; \ - \ if (PFX##_empty(_list_)) \ + { \ + _list_->flag = cmc_flags.EMPTY; \ return NULL; \ + } \ + \ + if (index >= _list_->count) \ + { \ + _list_->flag = cmc_flags.RANGE; \ + return NULL; \ + } \ + \ + _list_->flag = cmc_flags.OK; \ \ return &(_list_->buffer[index]); \ } \ @@ -628,13 +718,20 @@ struct cmc_callbacks_list V PFX##_back(struct SNAME *_list_) \ { \ if (PFX##_empty(_list_)) \ + { \ + _list_->flag = cmc_flags.EMPTY; \ return (V){ 0 }; \ + } \ + \ + _list_->flag = cmc_flags.OK; \ \ return _list_->buffer[_list_->count - 1]; \ } \ \ - size_t PFX##_indexof(struct SNAME *_list_, V element, bool from_start) \ + size_t PFX##_index_of(struct SNAME *_list_, V element, bool from_start) \ { \ + _list_->flag = cmc_flags.OK; \ + \ if (from_start) \ { \ for (size_t i = 0; i < _list_->count; i++) \ @@ -657,6 +754,8 @@ struct cmc_callbacks_list \ bool PFX##_contains(struct SNAME *_list_, V element) \ { \ + _list_->flag = cmc_flags.OK; \ + \ for (size_t i = 0; i < _list_->count; i++) \ { \ if (_list_->f_val->cmp(_list_->buffer[i], element) == 0) \ @@ -691,19 +790,32 @@ struct cmc_callbacks_list return _list_->capacity; \ } \ \ + int PFX##_flag(struct SNAME *_list_) \ + { \ + return _list_->flag; \ + } \ + \ bool PFX##_resize(struct SNAME *_list_, size_t capacity) \ { \ - if (PFX##_capacity(_list_) == capacity) \ + _list_->flag = cmc_flags.OK; \ + \ + if (_list_->capacity == capacity) \ return true; \ \ - if (capacity < PFX##_count(_list_)) \ + if (capacity < _list_->count) \ + { \ + _list_->flag = cmc_flags.INVALID; \ return false; \ + } \ \ V *new_buffer = \ _list_->alloc->realloc(_list_->buffer, sizeof(V) * capacity); \ \ if (!new_buffer) \ + { \ + _list_->flag = cmc_flags.ALLOC; \ return false; \ + } \ \ /* TODO zero out new slots */ \ \ @@ -720,7 +832,10 @@ struct cmc_callbacks_list _list_->callbacks); \ \ if (!result) \ + { \ + _list_->flag = cmc_flags.ALLOC; \ return NULL; \ + } \ \ if (_list_->f_val->cpy) \ { \ @@ -732,15 +847,20 @@ struct cmc_callbacks_list \ result->count = _list_->count; \ \ + _list_->flag = cmc_flags.OK; \ + \ return result; \ } \ \ bool PFX##_equals(struct SNAME *_list1_, struct SNAME *_list2_) \ { \ - if (PFX##_count(_list1_) != PFX##_count(_list2_)) \ + _list1_->flag = cmc_flags.OK; \ + _list2_->flag = cmc_flags.OK; \ + \ + if (_list1_->count != _list2_->count) \ return false; \ \ - for (size_t i = 0; i < PFX##_count(_list1_); i++) \ + for (size_t i = 0; i < _list1_->count; i++) \ { \ if (_list1_->f_val->cmp(_list1_->buffer[i], _list2_->buffer[i]) != \ 0) \ @@ -824,7 +944,7 @@ struct cmc_callbacks_list if (iter->end) \ return false; \ \ - if (iter->cursor + 1 == PFX##_count(iter->target)) \ + if (iter->cursor + 1 == iter->target->count) \ { \ iter->end = true; \ return false; \ @@ -867,7 +987,7 @@ struct cmc_callbacks_list return false; \ } \ \ - if (steps == 0 || iter->cursor + steps >= PFX##_count(iter->target)) \ + if (steps == 0 || iter->cursor + steps >= iter->target->count) \ return false; \ \ iter->start = PFX##_empty(iter->target); \ @@ -903,7 +1023,7 @@ struct cmc_callbacks_list /* given index */ \ bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index) \ { \ - if (index >= PFX##_count(iter->target)) \ + if (index >= iter->target->count) \ return false; \ \ if (iter->cursor > index) \ diff --git a/src/cmc/multimap.h b/src/cmc/multimap.h index 4327259..80c3c7c 100644 --- a/src/cmc/multimap.h +++ b/src/cmc/multimap.h @@ -89,14 +89,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/multiset.h b/src/cmc/multiset.h index 0cd77cd..8169e6f 100644 --- a/src/cmc/multiset.h +++ b/src/cmc/multiset.h @@ -67,14 +67,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/queue.h b/src/cmc/queue.h index 52e7292..dc09ee1 100644 --- a/src/cmc/queue.h +++ b/src/cmc/queue.h @@ -89,14 +89,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/sortedlist.h b/src/cmc/sortedlist.h index aa9d7cb..72c74d4 100644 --- a/src/cmc/sortedlist.h +++ b/src/cmc/sortedlist.h @@ -68,14 +68,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ @@ -216,7 +216,7 @@ struct cmc_callbacks_sortedlist bool PFX##_max(struct SNAME *_list_, V *result); \ bool PFX##_min(struct SNAME *_list_, V *result); \ V PFX##_get(struct SNAME *_list_, size_t index); \ - size_t PFX##_indexof(struct SNAME *_list_, V element, bool from_start); \ + size_t PFX##_index_of(struct SNAME *_list_, V element, bool from_start); \ /* Collection State */ \ bool PFX##_contains(struct SNAME *_list_, V element); \ bool PFX##_empty(struct SNAME *_list_); \ @@ -438,7 +438,7 @@ struct cmc_callbacks_sortedlist return _list_->buffer[index]; \ } \ \ - size_t PFX##_indexof(struct SNAME *_list_, V element, bool from_start) \ + size_t PFX##_index_of(struct SNAME *_list_, V element, bool from_start) \ { \ PFX##_sort(_list_); \ \ diff --git a/src/cmc/stack.h b/src/cmc/stack.h index 14f732e..6be4b03 100644 --- a/src/cmc/stack.h +++ b/src/cmc/stack.h @@ -74,14 +74,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/treemap.h b/src/cmc/treemap.h index 8bd288d..eb3190c 100644 --- a/src/cmc/treemap.h +++ b/src/cmc/treemap.h @@ -65,14 +65,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/src/cmc/treeset.h b/src/cmc/treeset.h index 737113c..914533e 100644 --- a/src/cmc/treeset.h +++ b/src/cmc/treeset.h @@ -65,14 +65,14 @@ static struct cmc_alloc_node */ static struct { - int OK; // Everything went as expected - int ALLOC; // Allocation failed - int EMPTY; // The collection is empty and the operation could not proceed - int NOT_FOUND; // Key or value not found - int INVALID; // Invalid argument - int OUT_OF_RANGE; // Index out of array range - int DUPLICATE; // Duplicate key or value - int ERROR; // Generic error, usually caused by unexpected behaviour + int OK; // No errors + int ALLOC; // Allocation failed + int EMPTY; // The collection is empty when it should not + int NOT_FOUND; // Key or value not found + int INVALID; // Invalid argument or operation + int RANGE; // Index out of range + int DUPLICATE; // Duplicate key or value + int ERROR; // Generic error, usually caused by algorithm error } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; #endif /* CMC_CORE_H */ diff --git a/tests/main/unt/linkedlist.c b/tests/main/unt/linkedlist.c index a76f5a8..0ff2695 100644 --- a/tests/main/unt/linkedlist.c +++ b/tests/main/unt/linkedlist.c @@ -110,7 +110,7 @@ CMC_CREATE_UNIT(linkedlist_test, true, { cmc_assert(ll_push_at(ll, 10, 1)); cmc_assert_equals(int32_t, cmc_flags.OK, ll_flag(ll)); cmc_assert(!ll_push_at(ll, 10, 300)); - cmc_assert_equals(int32_t, cmc_flags.OUT_OF_RANGE, ll_flag(ll)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, ll_flag(ll)); // push_back ll->flag = cmc_flags.ERROR; @@ -133,7 +133,7 @@ CMC_CREATE_UNIT(linkedlist_test, true, { cmc_assert_equals(int32_t, cmc_flags.EMPTY, ll_flag(ll)); cmc_assert(ll_push_front(ll, 10)); cmc_assert(!ll_pop_at(ll, 1)); - cmc_assert_equals(int32_t, cmc_flags.OUT_OF_RANGE, ll_flag(ll)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, ll_flag(ll)); cmc_assert(ll_pop_at(ll, 0)); cmc_assert_equals(int32_t, cmc_flags.OK, ll_flag(ll)); @@ -180,11 +180,11 @@ CMC_CREATE_UNIT(linkedlist_test, true, { ll_get(ll, 0); cmc_assert_equals(int32_t, cmc_flags.OK, ll_flag(ll)); ll_get(ll, 1); - cmc_assert_equals(int32_t, cmc_flags.OUT_OF_RANGE, ll_flag(ll)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, ll_flag(ll)); ll_get_ref(ll, 0); cmc_assert_equals(int32_t, cmc_flags.OK, ll_flag(ll)); ll_get_ref(ll, 1); - cmc_assert_equals(int32_t, cmc_flags.OUT_OF_RANGE, ll_flag(ll)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, ll_flag(ll)); // contains cmc_assert(ll_contains(ll, 10)); @@ -214,7 +214,7 @@ CMC_CREATE_UNIT(linkedlist_test, true, { cmc_assert_equals(int32_t, cmc_flags.OK, ll_flag(ll)); n = ll_get_node(ll, 1); - cmc_assert_equals(int32_t, cmc_flags.OUT_OF_RANGE, ll_flag(ll)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, ll_flag(ll)); // functions relative to node ll_clear(ll); diff --git a/tests/main/unt/list.c b/tests/main/unt/list.c index d08eda2..53e51c3 100644 --- a/tests/main/unt/list.c +++ b/tests/main/unt/list.c @@ -314,4 +314,178 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l); }); + + CMC_CREATE_TEST(flags, { + struct list *l = l_new(100, l_ftab_val); + + cmc_assert_not_equals(ptr, NULL, l); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // clear + l->flag = cmc_flags.ERROR; + l_clear(l); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // customize + l->flag = cmc_flags.ERROR; + l_customize(l, NULL, NULL); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // push_front + l->flag = cmc_flags.ERROR; + cmc_assert(l_push_front(l, 10)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // push_at + cmc_assert(!l_push_at(l, 5, 2)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + cmc_assert(l_push_at(l, 5, 1)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // push_back + l->flag = cmc_flags.ERROR; + cmc_assert(l_push_back(l, 0)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // pop_front + l->flag = cmc_flags.ERROR; + cmc_assert(l_pop_front(l)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + l_clear(l); + cmc_assert(!l_pop_front(l)); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + + // pop_back + cmc_assert(l_push_front(l, 10)); + l->flag = cmc_flags.ERROR; + cmc_assert(l_pop_back(l)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + cmc_assert(!l_pop_front(l)); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + + // pop_at + l->flag = cmc_flags.ERROR; + cmc_assert(!l_pop_at(l, 0)); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + + cmc_assert(l_push_back(l, 10)); + cmc_assert(!l_pop_at(l, 1)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + + cmc_assert(l_pop_at(l, 0)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // seq_push_front + cmc_assert(!l_seq_push_front(l, NULL, 0)); + cmc_assert_equals(int32_t, cmc_flags.INVALID, l_flag(l)); + + cmc_assert(l_seq_push_front(l, (size_t[2]){ 1, 2 }, 2)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // seq_push_at + cmc_assert(!l_seq_push_at(l, NULL, 0, 0)); + cmc_assert_equals(int32_t, cmc_flags.INVALID, l_flag(l)); + + cmc_assert(!l_seq_push_at(l, (size_t[2]){ 1, 2 }, 2, 10)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + + cmc_assert(l_seq_push_at(l, (size_t[2]){ 1, 2 }, 2, l_count(l) - 1)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // seq_push_back + cmc_assert(!l_seq_push_back(l, NULL, 0)); + cmc_assert_equals(int32_t, cmc_flags.INVALID, l_flag(l)); + + cmc_assert(l_seq_push_back(l, (size_t[2]){ 1, 2 }, 2)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // seq_pop_at + cmc_assert(!l_seq_pop_at(l, 10, 1)); + cmc_assert_equals(int32_t, cmc_flags.INVALID, l_flag(l)); + + cmc_assert(!l_seq_pop_at(l, 100, 200)); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + + cmc_assert(l_seq_pop_at(l, 1, l_count(l) - 2)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // seq_sublist + struct list *l2 = l_seq_sublist(l, 10, 1); + cmc_assert_equals(ptr, NULL, l2); + cmc_assert_equals(int32_t, cmc_flags.INVALID, l_flag(l)); + + l2 = l_seq_sublist(l, 100, 200); + cmc_assert_equals(ptr, NULL, l2); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + + l2 = l_seq_sublist(l, 0, l_count(l) - 1); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l2)); + + l_clear(l); + + // l_front + l_front(l); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + + cmc_assert(l_push_back(l, 10)); + l->flag = cmc_flags.ERROR; + l_front(l); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + l_clear(l); + + // l_back + l_back(l); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + + cmc_assert(l_push_back(l, 10)); + l->flag = cmc_flags.ERROR; + l_back(l); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + l_clear(l); + + // get, get_ref + l_get(l, 0); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + l->flag = cmc_flags.ERROR; + l_get_ref(l, 0); + cmc_assert_equals(int32_t, cmc_flags.EMPTY, l_flag(l)); + + cmc_assert(l_push_back(l, 10)); + + l_get(l, 1); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + l->flag = cmc_flags.ERROR; + l_get_ref(l, 1); + cmc_assert_equals(int32_t, cmc_flags.RANGE, l_flag(l)); + + l_get(l, 0); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + l->flag = cmc_flags.ERROR; + l_get_ref(l, 0); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // index_of + l->flag = cmc_flags.ERROR; + l_index_of(l, 10, false); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // contains + l->flag = cmc_flags.ERROR; + cmc_assert(l_contains(l, 10)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + + // copy_of + l->flag = cmc_flags.ERROR; + struct list *l3 = l_copy_of(l); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l)); + cmc_assert_equals(int32_t, cmc_flags.OK, l_flag(l3)); + + l_free(l); + l_free(l2); + }); }) diff --git a/tests/main/unt/sortedlist.c b/tests/main/unt/sortedlist.c index 329f3cc..bdcc1a7 100644 --- a/tests/main/unt/sortedlist.c +++ b/tests/main/unt/sortedlist.c @@ -118,7 +118,7 @@ CMC_CREATE_UNIT(sortedlist_test, true, { sl_free(sl); }); - CMC_CREATE_TEST(indexof, { + CMC_CREATE_TEST(index_of, { struct sortedlist *sl = sl_new(1, sl_ftab_val); cmc_assert_not_equals(ptr, NULL, sl); @@ -127,21 +127,21 @@ CMC_CREATE_UNIT(sortedlist_test, true, { cmc_assert(sl_insert(sl, i % 10)); /* 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 */ - cmc_assert_equals(size_t, 4, sl_indexof(sl, 2, true)); - cmc_assert_equals(size_t, 5, sl_indexof(sl, 2, false)); + cmc_assert_equals(size_t, 4, sl_index_of(sl, 2, true)); + cmc_assert_equals(size_t, 5, sl_index_of(sl, 2, false)); - cmc_assert_equals(size_t, 0, sl_indexof(sl, 0, true)); - cmc_assert_equals(size_t, 1, sl_indexof(sl, 0, false)); + cmc_assert_equals(size_t, 0, sl_index_of(sl, 0, true)); + cmc_assert_equals(size_t, 1, sl_index_of(sl, 0, false)); cmc_assert(sl_remove(sl, 0)); - cmc_assert_equals(size_t, 0, sl_indexof(sl, 0, true)); - cmc_assert_equals(size_t, 0, sl_indexof(sl, 0, false)); + cmc_assert_equals(size_t, 0, sl_index_of(sl, 0, true)); + cmc_assert_equals(size_t, 0, sl_index_of(sl, 0, false)); cmc_assert(sl_remove(sl, 0)); - cmc_assert_equals(size_t, sl_count(sl), sl_indexof(sl, 0, true)); - cmc_assert_equals(size_t, sl_count(sl), sl_indexof(sl, 0, false)); + cmc_assert_equals(size_t, sl_count(sl), sl_index_of(sl, 0, true)); + cmc_assert_equals(size_t, sl_count(sl), sl_index_of(sl, 0, false)); sl_free(sl); });