diff --git a/.travis.yml b/.travis.yml index c52b7ea..e4f7855 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ addons: script: - sudo ln -s /usr/bin/gcc-6 /usr/local/bin/gcc - export CC=gcc-7 - - make -C ./tests/ +# - make -C ./tests/ - make -C ./tests/main/ - make -C ./tests/codecov/ after_success: diff --git a/Makefile b/Makefile index 3d6c5f0..7aa94bf 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,22 @@ # This file is useful to format all the source files and header in this library using clang-format -.PHONY -main: +.PHONY: all cmc dev sac utl tests FORCE + +all: cmc dev sac utl tests + +cmc: FORCE clang-format --style=file -i ./src/cmc/*.h + +dev: FORCE + clang-format --style=file -i ./src/dev/*.h + +sac: FORCE + clang-format --style=file -i ./src/sac/*.h + +utl: FORCE + clang-format --style=file -i ./utl/sac/*.h + +tests: FORCE + clang-format --style=file -i ./tests/main/unt/*.c ./tests/*.c + +FORCE: diff --git a/src/cmc/bidimap.h b/src/cmc/bidimap.h index 4b8991f..2d07b9f 100644 --- a/src/cmc/bidimap.h +++ b/src/cmc/bidimap.h @@ -76,7 +76,7 @@ static struct 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; // Something is invalid + 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 @@ -275,7 +275,7 @@ struct cmc_callbacks_bidimap int (*cmp)(V, V); \ \ /* Copy function */ \ - K (*cpy)(V); \ + V (*cpy)(V); \ \ /* To string function */ \ bool (*str)(FILE *, V); \ @@ -412,6 +412,9 @@ struct cmc_callbacks_bidimap if (capacity >= UINTMAX_MAX * load) \ return NULL; \ \ + if (!f_val) \ + return NULL; \ + \ size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \ \ struct SNAME *_map_ = alloc->malloc(sizeof(struct SNAME)); \ diff --git a/src/cmc/deque.h b/src/cmc/deque.h index f0e5fae..2523e53 100644 --- a/src/cmc/deque.h +++ b/src/cmc/deque.h @@ -81,7 +81,7 @@ static struct 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; // Something is invalid + 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 @@ -100,6 +100,8 @@ static const char *cmc_string_fmt_deque = "struct %s<%s> " "count:%" PRIuMAX ", " "front:%" PRIuMAX ", " "back:%" PRIuMAX ", " + "flag:%d, " + "f_val:%p, " "alloc:%p, " "callbacks:%p }"; @@ -151,17 +153,43 @@ struct cmc_callbacks_deque /* Index representing the back of the deque */ \ size_t back; \ \ - /* Function that returns an iterator to the start of the deque */ \ - struct SNAME##_iter (*it_start)(struct SNAME *); \ + /* Flags indicating errors or success */ \ + int flag; \ \ - /* Function that returns an iterator to the end of the deque */ \ - struct SNAME##_iter (*it_end)(struct SNAME *); \ + struct SNAME##_ftab_val *f_val; \ \ /* Custom allocation functions */ \ struct cmc_alloc_node *alloc; \ \ /* Custom callback functions */ \ struct cmc_callbacks_deque *callbacks; \ + \ + /* Function that returns an iterator to the start of the deque */ \ + struct SNAME##_iter (*it_start)(struct SNAME *); \ + \ + /* Function that returns an iterator to the end of the deque */ \ + struct SNAME##_iter (*it_end)(struct SNAME *); \ + }; \ + \ + struct SNAME##_ftab_val \ + { \ + /* Comparator function */ \ + int (*cmp)(V, V); \ + \ + /* Copy function */ \ + V (*cpy)(V); \ + \ + /* To string function */ \ + bool (*str)(FILE *, V); \ + \ + /* Free from memory function */ \ + void (*free)(V); \ + \ + /* Hash function */ \ + size_t (*hash)(V); \ + \ + /* Priority function */ \ + int (*pri)(V, V); \ }; \ \ /* Deque Iterator */ \ @@ -185,12 +213,12 @@ struct cmc_callbacks_deque \ /* Collection Functions */ \ /* Collection Allocation and Deallocation */ \ - struct SNAME *PFX##_new(size_t capacity); \ - struct SNAME *PFX##_new_custom(size_t capacity, \ - struct cmc_alloc_node *alloc, \ - struct cmc_callbacks_deque *callbacks); \ - void PFX##_clear(struct SNAME *_deque_, void (*deallocator)(V)); \ - void PFX##_free(struct SNAME *_deque_, void (*deallocator)(V)); \ + struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \ + struct SNAME *PFX##_new_custom( \ + size_t capacity, struct SNAME##_ftab_val *f_val, \ + struct cmc_alloc_node *alloc, struct cmc_callbacks_deque *callbacks); \ + void PFX##_clear(struct SNAME *_deque_); \ + void PFX##_free(struct SNAME *_deque_); \ /* Customization of Allocation and Callbacks */ \ void PFX##_customize(struct SNAME *_deque_, struct cmc_alloc_node *alloc, \ struct cmc_callbacks_deque *callbacks); \ @@ -203,18 +231,17 @@ struct cmc_callbacks_deque V PFX##_front(struct SNAME *_deque_); \ V PFX##_back(struct SNAME *_deque_); \ /* Collection State */ \ - bool PFX##_contains(struct SNAME *_deque_, V element, \ - int (*comparator)(V, V)); \ + bool PFX##_contains(struct SNAME *_deque_, V element); \ bool PFX##_empty(struct SNAME *_deque_); \ bool PFX##_full(struct SNAME *_deque_); \ size_t PFX##_count(struct SNAME *_deque_); \ size_t PFX##_capacity(struct SNAME *_deque_); \ /* Collection Utility */ \ bool PFX##_resize(struct SNAME *_deque_, size_t capacity); \ - struct SNAME *PFX##_copy_of(struct SNAME *_deque_, V (*copy_func)(V)); \ - bool PFX##_equals(struct SNAME *_deque1_, struct SNAME *_deque2_, \ - int (*comparator)(V, V)); \ + struct SNAME *PFX##_copy_of(struct SNAME *_deque_); \ + bool PFX##_equals(struct SNAME *_deque1_, struct SNAME *_deque2_); \ struct cmc_string PFX##_to_string(struct SNAME *_deque_); \ + bool PFX##_print(struct SNAME *_deque_, FILE *fptr); \ \ /* Iterator Functions */ \ /* Iterator Allocation and Deallocation */ \ @@ -241,554 +268,569 @@ struct cmc_callbacks_deque /* ------------------------------------------------------------------------- * Source * ------------------------------------------------------------------------- */ -#define CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V) \ - \ - /* Implementation Detail Functions */ \ - static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_deque_); \ - static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_deque_); \ - \ - struct SNAME *PFX##_new(size_t capacity) \ - { \ - struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \ - \ - if (capacity < 1) \ - return NULL; \ - \ - struct SNAME *_deque_ = alloc->malloc(sizeof(struct SNAME)); \ - \ - if (!_deque_) \ - return NULL; \ - \ - _deque_->buffer = alloc->calloc(capacity, sizeof(V)); \ - \ - if (!_deque_->buffer) \ - { \ - alloc->free(_deque_); \ - return NULL; \ - } \ - \ - _deque_->capacity = capacity; \ - _deque_->count = 0; \ - _deque_->front = 0; \ - _deque_->back = 0; \ - \ - _deque_->it_start = PFX##_impl_it_start; \ - _deque_->it_end = PFX##_impl_it_end; \ - \ - _deque_->alloc = alloc; \ - _deque_->callbacks = NULL; \ - \ - return _deque_; \ - } \ - \ - struct SNAME *PFX##_new_custom(size_t capacity, \ - struct cmc_alloc_node *alloc, \ - struct cmc_callbacks_deque *callbacks) \ - { \ - if (capacity < 1) \ - return NULL; \ - \ - if (!alloc) \ - alloc = &cmc_alloc_node_default; \ - \ - struct SNAME *_deque_ = alloc->malloc(sizeof(struct SNAME)); \ - \ - if (!_deque_) \ - return NULL; \ - \ - _deque_->buffer = alloc->calloc(capacity, sizeof(V)); \ - \ - if (!_deque_->buffer) \ - { \ - alloc->free(_deque_); \ - return NULL; \ - } \ - \ - _deque_->capacity = capacity; \ - _deque_->count = 0; \ - _deque_->front = 0; \ - _deque_->back = 0; \ - \ - _deque_->it_start = PFX##_impl_it_start; \ - _deque_->it_end = PFX##_impl_it_end; \ - \ - _deque_->alloc = alloc; \ - _deque_->callbacks = callbacks; \ - \ - return _deque_; \ - } \ - \ - void PFX##_clear(struct SNAME *_deque_, void (*deallocator)(V)) \ - { \ - if (deallocator) \ - { \ - for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ - { \ - deallocator(_deque_->buffer[i]); \ - \ - i = (i + 1) % _deque_->capacity; \ - } \ - } \ - \ - memset(_deque_->buffer, 0, sizeof(V) * _deque_->capacity); \ - \ - _deque_->count = 0; \ - _deque_->front = 0; \ - _deque_->back = 0; \ - } \ - \ - void PFX##_free(struct SNAME *_deque_, void (*deallocator)(V)) \ - { \ - if (deallocator) \ - { \ - for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ - { \ - deallocator(_deque_->buffer[i]); \ - \ - i = (i + 1) % _deque_->capacity; \ - } \ - } \ - \ - _deque_->alloc->free(_deque_->buffer); \ - _deque_->alloc->free(_deque_); \ - } \ - \ - void PFX##_customize(struct SNAME *_deque_, struct cmc_alloc_node *alloc, \ - struct cmc_callbacks_deque *callbacks) \ - { \ - if (alloc) \ - _deque_->alloc = alloc; \ - \ - if (callbacks) \ - _deque_->callbacks = callbacks; \ - } \ - \ - bool PFX##_push_front(struct SNAME *_deque_, V element) \ - { \ - if (PFX##_full(_deque_)) \ - { \ - if (!PFX##_resize(_deque_, PFX##_capacity(_deque_) * 2)) \ - return false; \ - } \ - \ - _deque_->front = (_deque_->front == 0) ? _deque_->capacity - 1 \ - : _deque_->front - 1; \ - \ - _deque_->buffer[_deque_->front] = element; \ - \ - _deque_->count++; \ - \ - return true; \ - } \ - \ - bool PFX##_push_back(struct SNAME *_deque_, V element) \ - { \ - if (PFX##_full(_deque_)) \ - { \ - if (!PFX##_resize(_deque_, PFX##_capacity(_deque_) * 2)) \ - return false; \ - } \ - \ - _deque_->buffer[_deque_->back] = element; \ - \ - _deque_->back = \ - (_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1; \ - \ - _deque_->count++; \ - \ - return true; \ - } \ - \ - bool PFX##_pop_front(struct SNAME *_deque_) \ - { \ - if (PFX##_empty(_deque_)) \ - return false; \ - \ - _deque_->buffer[_deque_->front] = (V){ 0 }; \ - \ - _deque_->front = (_deque_->front == _deque_->capacity - 1) \ - ? 0 \ - : _deque_->front + 1; \ - \ - _deque_->count--; \ - \ - return true; \ - } \ - \ - bool PFX##_pop_back(struct SNAME *_deque_) \ - { \ - if (PFX##_empty(_deque_)) \ - return false; \ - \ - _deque_->back = \ - (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1; \ - \ - _deque_->buffer[_deque_->back] = (V){ 0 }; \ - \ - _deque_->count--; \ - \ - return true; \ - } \ - \ - V PFX##_front(struct SNAME *_deque_) \ - { \ - if (PFX##_empty(_deque_)) \ - return (V){ 0 }; \ - \ - return _deque_->buffer[_deque_->front]; \ - } \ - \ - V PFX##_back(struct SNAME *_deque_) \ - { \ - if (PFX##_empty(_deque_)) \ - return (V){ 0 }; \ - \ - return _deque_->buffer[(_deque_->back == 0) ? _deque_->capacity - 1 \ - : _deque_->back - 1]; \ - } \ - \ - bool PFX##_contains(struct SNAME *_deque_, V element, \ - int (*comparator)(V, V)) \ - { \ - for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ - { \ - if (comparator(_deque_->buffer[i], element) == 0) \ - return true; \ - \ - i = (i + 1) % _deque_->capacity; \ - } \ - \ - return false; \ - } \ - \ - bool PFX##_empty(struct SNAME *_deque_) \ - { \ - return _deque_->count == 0; \ - } \ - \ - bool PFX##_full(struct SNAME *_deque_) \ - { \ - return _deque_->count >= _deque_->capacity; \ - } \ - \ - size_t PFX##_count(struct SNAME *_deque_) \ - { \ - return _deque_->count; \ - } \ - \ - size_t PFX##_capacity(struct SNAME *_deque_) \ - { \ - return _deque_->capacity; \ - } \ - \ - bool PFX##_resize(struct SNAME *_deque_, size_t capacity) \ - { \ - if (PFX##_capacity(_deque_) == capacity) \ - return true; \ - \ - if (capacity < PFX##_count(_deque_)) \ - return false; \ - \ - V *new_buffer = _deque_->alloc->malloc(sizeof(V) * capacity); \ - \ - if (!new_buffer) \ - return false; \ - \ - for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ - { \ - new_buffer[j] = _deque_->buffer[i]; \ - \ - i = (i + 1) % PFX##_capacity(_deque_); \ - } \ - \ - _deque_->alloc->free(_deque_->buffer); \ - \ - _deque_->buffer = new_buffer; \ - _deque_->capacity = capacity; \ - _deque_->front = 0; \ - _deque_->back = _deque_->count; \ - \ - return true; \ - } \ - \ - struct SNAME *PFX##_copy_of(struct SNAME *_deque_, V (*copy_func)(V)) \ - { \ - struct SNAME *result = PFX##_new_custom( \ - _deque_->capacity, _deque_->alloc, _deque_->callbacks); \ - \ - if (!result) \ - return NULL; \ - \ - if (copy_func) \ - { \ - for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ - { \ - result->buffer[j] = copy_func(_deque_->buffer[i]); \ - \ - i = (i + 1) % _deque_->capacity; \ - } \ - } \ - else \ - { \ - for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ - { \ - result->buffer[j] = _deque_->buffer[i]; \ - \ - i = (i + 1) % _deque_->capacity; \ - } \ - } \ - \ - result->count = _deque_->count; \ - \ - return result; \ - } \ - \ - bool PFX##_equals(struct SNAME *_deque1_, struct SNAME *_deque2_, \ - int (*comparator)(V, V)) \ - { \ - if (PFX##_count(_deque1_) != PFX##_count(_deque2_)) \ - return false; \ - \ - size_t i, j, k; \ - for (i = _deque1_->front, j = _deque2_->front, k = 0; \ - k < PFX##_count(_deque1_); k++) \ - { \ - if (comparator(_deque1_->buffer[i], _deque2_->buffer[j]) != 0) \ - return false; \ - \ - i = (i + 1) % _deque1_->capacity; \ - j = (j + 1) % _deque2_->capacity; \ - } \ - \ - return true; \ - } \ - \ - struct cmc_string PFX##_to_string(struct SNAME *_deque_) \ - { \ - struct cmc_string str; \ - struct SNAME *d_ = _deque_; \ - \ - int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_deque, #SNAME, \ - #V, d_, d_->buffer, d_->capacity, d_->count, \ - d_->front, d_->back, d_->alloc, d_->callbacks); \ - \ - if (n < 0 || n == (int)cmc_string_len) \ - return (struct cmc_string){ 0 }; \ - \ - str.s[n] = '\0'; \ - \ - return str; \ - } \ - \ - struct SNAME##_iter *PFX##_iter_new(struct SNAME *target) \ - { \ - struct SNAME##_iter *iter = \ - target->alloc->malloc(sizeof(struct SNAME##_iter)); \ - \ - if (!iter) \ - return NULL; \ - \ - PFX##_iter_init(iter, target); \ - \ - return iter; \ - } \ - \ - void PFX##_iter_free(struct SNAME##_iter *iter) \ - { \ - iter->target->alloc->free(iter); \ - } \ - \ - void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target) \ - { \ - iter->target = target; \ - iter->cursor = target->front; \ - iter->index = 0; \ - iter->start = true; \ - iter->end = PFX##_empty(target); \ - } \ - \ - bool PFX##_iter_start(struct SNAME##_iter *iter) \ - { \ - return PFX##_empty(iter->target) || iter->start; \ - } \ - \ - bool PFX##_iter_end(struct SNAME##_iter *iter) \ - { \ - return PFX##_empty(iter->target) || iter->end; \ - } \ - \ - void PFX##_iter_to_start(struct SNAME##_iter *iter) \ - { \ - iter->cursor = iter->target->front; \ - iter->index = 0; \ - iter->start = true; \ - iter->end = PFX##_empty(iter->target); \ - } \ - \ - void PFX##_iter_to_end(struct SNAME##_iter *iter) \ - { \ - if (PFX##_empty(iter->target)) \ - { \ - iter->cursor = 0; \ - iter->index = 0; \ - } \ - else \ - { \ - if (iter->target->back == 0) \ - iter->cursor = iter->target->capacity - 1; \ - else \ - iter->cursor = iter->target->back - 1; \ - \ - iter->index = PFX##_count(iter->target) - 1; \ - } \ - \ - iter->start = PFX##_empty(iter->target); \ - iter->end = true; \ - } \ - \ - bool PFX##_iter_next(struct SNAME##_iter *iter) \ - { \ - if (iter->end) \ - return false; \ - \ - if (iter->index + 1 == PFX##_count(iter->target)) \ - { \ - iter->end = true; \ - return false; \ - } \ - \ - iter->start = PFX##_empty(iter->target); \ - \ - iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \ - iter->index++; \ - \ - return true; \ - } \ - \ - bool PFX##_iter_prev(struct SNAME##_iter *iter) \ - { \ - if (iter->start) \ - return false; \ - \ - if (iter->index == 0) \ - { \ - iter->start = true; \ - return false; \ - } \ - \ - iter->end = PFX##_empty(iter->target); \ - \ - iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 \ - : iter->cursor - 1; \ - iter->index--; \ - \ - return true; \ - } \ - \ - /* Returns true only if the iterator moved */ \ - bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps) \ - { \ - if (iter->end) \ - return false; \ - \ - if (iter->index + 1 == PFX##_count(iter->target)) \ - { \ - iter->end = true; \ - return false; \ - } \ - \ - if (steps == 0 || iter->index + steps >= PFX##_count(iter->target)) \ - return false; \ - \ - iter->start = PFX##_empty(iter->target); \ - \ - iter->index += steps; \ - iter->cursor = (iter->cursor + steps) % iter->target->capacity; \ - \ - return true; \ - } \ - \ - /* Returns true only if the iterator moved */ \ - bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps) \ - { \ - if (iter->start) \ - return false; \ - \ - if (iter->index == 0) \ - { \ - iter->start = true; \ - return false; \ - } \ - \ - if (steps == 0 || iter->index < steps) \ - return false; \ - \ - iter->end = PFX##_empty(iter->target); \ - \ - iter->index -= steps; \ - \ - /* Prevent underflow */ \ - if (iter->cursor < steps) \ - iter->cursor += PFX##_capacity(iter->target); \ - \ - iter->cursor -= steps; \ - \ - return true; \ - } \ - \ - /* Returns true only if the iterator was able to be positioned at the \ - * given index */ \ - bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index) \ - { \ - if (index >= PFX##_count(iter->target)) \ - return false; \ - \ - if (iter->index > index) \ - return PFX##_iter_rewind(iter, iter->index - index); \ - else if (iter->index < index) \ - return PFX##_iter_advance(iter, index - iter->index); \ - \ - return true; \ - } \ - \ - V PFX##_iter_value(struct SNAME##_iter *iter) \ - { \ - if (PFX##_empty(iter->target)) \ - return (V){ 0 }; \ - \ - return iter->target->buffer[iter->cursor]; \ - } \ - \ - V *PFX##_iter_rvalue(struct SNAME##_iter *iter) \ - { \ - if (PFX##_empty(iter->target)) \ - return NULL; \ - \ - return &(iter->target->buffer[iter->cursor]); \ - } \ - \ - size_t PFX##_iter_index(struct SNAME##_iter *iter) \ - { \ - return iter->index; \ - } \ - \ - static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_deque_) \ - { \ - struct SNAME##_iter iter; \ - \ - PFX##_iter_init(&iter, _deque_); \ - \ - return iter; \ - } \ - \ - static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_deque_) \ - { \ - struct SNAME##_iter iter; \ - \ - PFX##_iter_init(&iter, _deque_); \ - PFX##_iter_to_end(&iter); \ - \ - return iter; \ +#define CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V) \ + \ + /* Implementation Detail Functions */ \ + static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_deque_); \ + static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_deque_); \ + \ + struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val) \ + { \ + struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \ + \ + if (capacity < 1) \ + return NULL; \ + \ + if (!f_val) \ + return NULL; \ + \ + struct SNAME *_deque_ = alloc->malloc(sizeof(struct SNAME)); \ + \ + if (!_deque_) \ + return NULL; \ + \ + _deque_->buffer = alloc->calloc(capacity, sizeof(V)); \ + \ + if (!_deque_->buffer) \ + { \ + alloc->free(_deque_); \ + return NULL; \ + } \ + \ + _deque_->capacity = capacity; \ + _deque_->count = 0; \ + _deque_->front = 0; \ + _deque_->back = 0; \ + _deque_->flag = cmc_flags.OK; \ + _deque_->f_val = f_val; \ + _deque_->alloc = alloc; \ + _deque_->callbacks = NULL; \ + _deque_->it_start = PFX##_impl_it_start; \ + _deque_->it_end = PFX##_impl_it_end; \ + \ + return _deque_; \ + } \ + \ + struct SNAME *PFX##_new_custom( \ + size_t capacity, struct SNAME##_ftab_val *f_val, \ + struct cmc_alloc_node *alloc, struct cmc_callbacks_deque *callbacks) \ + { \ + if (capacity < 1) \ + return NULL; \ + \ + if (!f_val) \ + return NULL; \ + \ + if (!alloc) \ + alloc = &cmc_alloc_node_default; \ + \ + struct SNAME *_deque_ = alloc->malloc(sizeof(struct SNAME)); \ + \ + if (!_deque_) \ + return NULL; \ + \ + _deque_->buffer = alloc->calloc(capacity, sizeof(V)); \ + \ + if (!_deque_->buffer) \ + { \ + alloc->free(_deque_); \ + return NULL; \ + } \ + \ + _deque_->capacity = capacity; \ + _deque_->count = 0; \ + _deque_->front = 0; \ + _deque_->back = 0; \ + _deque_->flag = cmc_flags.OK; \ + _deque_->f_val = f_val; \ + _deque_->alloc = alloc; \ + _deque_->callbacks = callbacks; \ + _deque_->it_start = PFX##_impl_it_start; \ + _deque_->it_end = PFX##_impl_it_end; \ + \ + return _deque_; \ + } \ + \ + void PFX##_clear(struct SNAME *_deque_) \ + { \ + if (_deque_->f_val->free) \ + { \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + _deque_->f_val->free(_deque_->buffer[i]); \ + \ + i = (i + 1) % _deque_->capacity; \ + } \ + } \ + \ + memset(_deque_->buffer, 0, sizeof(V) * _deque_->capacity); \ + \ + _deque_->count = 0; \ + _deque_->front = 0; \ + _deque_->back = 0; \ + } \ + \ + void PFX##_free(struct SNAME *_deque_) \ + { \ + if (_deque_->f_val->free) \ + { \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + _deque_->f_val->free(_deque_->buffer[i]); \ + \ + i = (i + 1) % _deque_->capacity; \ + } \ + } \ + \ + _deque_->alloc->free(_deque_->buffer); \ + _deque_->alloc->free(_deque_); \ + } \ + \ + void PFX##_customize(struct SNAME *_deque_, struct cmc_alloc_node *alloc, \ + struct cmc_callbacks_deque *callbacks) \ + { \ + if (alloc) \ + _deque_->alloc = alloc; \ + \ + if (callbacks) \ + _deque_->callbacks = callbacks; \ + } \ + \ + bool PFX##_push_front(struct SNAME *_deque_, V element) \ + { \ + if (PFX##_full(_deque_)) \ + { \ + if (!PFX##_resize(_deque_, PFX##_capacity(_deque_) * 2)) \ + return false; \ + } \ + \ + _deque_->front = (_deque_->front == 0) ? _deque_->capacity - 1 \ + : _deque_->front - 1; \ + \ + _deque_->buffer[_deque_->front] = element; \ + \ + _deque_->count++; \ + \ + return true; \ + } \ + \ + bool PFX##_push_back(struct SNAME *_deque_, V element) \ + { \ + if (PFX##_full(_deque_)) \ + { \ + if (!PFX##_resize(_deque_, PFX##_capacity(_deque_) * 2)) \ + return false; \ + } \ + \ + _deque_->buffer[_deque_->back] = element; \ + \ + _deque_->back = \ + (_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1; \ + \ + _deque_->count++; \ + \ + return true; \ + } \ + \ + bool PFX##_pop_front(struct SNAME *_deque_) \ + { \ + if (PFX##_empty(_deque_)) \ + return false; \ + \ + _deque_->buffer[_deque_->front] = (V){ 0 }; \ + \ + _deque_->front = (_deque_->front == _deque_->capacity - 1) \ + ? 0 \ + : _deque_->front + 1; \ + \ + _deque_->count--; \ + \ + return true; \ + } \ + \ + bool PFX##_pop_back(struct SNAME *_deque_) \ + { \ + if (PFX##_empty(_deque_)) \ + return false; \ + \ + _deque_->back = \ + (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1; \ + \ + _deque_->buffer[_deque_->back] = (V){ 0 }; \ + \ + _deque_->count--; \ + \ + return true; \ + } \ + \ + V PFX##_front(struct SNAME *_deque_) \ + { \ + if (PFX##_empty(_deque_)) \ + return (V){ 0 }; \ + \ + return _deque_->buffer[_deque_->front]; \ + } \ + \ + V PFX##_back(struct SNAME *_deque_) \ + { \ + if (PFX##_empty(_deque_)) \ + return (V){ 0 }; \ + \ + return _deque_->buffer[(_deque_->back == 0) ? _deque_->capacity - 1 \ + : _deque_->back - 1]; \ + } \ + \ + bool PFX##_contains(struct SNAME *_deque_, V element) \ + { \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + if (_deque_->f_val->cmp(_deque_->buffer[i], element) == 0) \ + return true; \ + \ + i = (i + 1) % _deque_->capacity; \ + } \ + \ + return false; \ + } \ + \ + bool PFX##_empty(struct SNAME *_deque_) \ + { \ + return _deque_->count == 0; \ + } \ + \ + bool PFX##_full(struct SNAME *_deque_) \ + { \ + return _deque_->count >= _deque_->capacity; \ + } \ + \ + size_t PFX##_count(struct SNAME *_deque_) \ + { \ + return _deque_->count; \ + } \ + \ + size_t PFX##_capacity(struct SNAME *_deque_) \ + { \ + return _deque_->capacity; \ + } \ + \ + bool PFX##_resize(struct SNAME *_deque_, size_t capacity) \ + { \ + if (PFX##_capacity(_deque_) == capacity) \ + return true; \ + \ + if (capacity < PFX##_count(_deque_)) \ + return false; \ + \ + V *new_buffer = _deque_->alloc->malloc(sizeof(V) * capacity); \ + \ + if (!new_buffer) \ + return false; \ + \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + new_buffer[j] = _deque_->buffer[i]; \ + \ + i = (i + 1) % PFX##_capacity(_deque_); \ + } \ + \ + _deque_->alloc->free(_deque_->buffer); \ + \ + _deque_->buffer = new_buffer; \ + _deque_->capacity = capacity; \ + _deque_->front = 0; \ + _deque_->back = _deque_->count; \ + \ + return true; \ + } \ + \ + struct SNAME *PFX##_copy_of(struct SNAME *_deque_) \ + { \ + struct SNAME *result = \ + PFX##_new_custom(_deque_->capacity, _deque_->f_val, \ + _deque_->alloc, _deque_->callbacks); \ + \ + if (!result) \ + return NULL; \ + \ + if (_deque_->f_val->cpy) \ + { \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + result->buffer[j] = _deque_->f_val->cpy(_deque_->buffer[i]); \ + \ + i = (i + 1) % _deque_->capacity; \ + } \ + } \ + else \ + { \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + result->buffer[j] = _deque_->buffer[i]; \ + \ + i = (i + 1) % _deque_->capacity; \ + } \ + } \ + \ + result->count = _deque_->count; \ + result->front = 0; \ + result->back = _deque_->count; \ + \ + return result; \ + } \ + \ + bool PFX##_equals(struct SNAME *_deque1_, struct SNAME *_deque2_) \ + { \ + if (PFX##_count(_deque1_) != PFX##_count(_deque2_)) \ + return false; \ + \ + size_t i, j, k; \ + for (i = _deque1_->front, j = _deque2_->front, k = 0; \ + k < PFX##_count(_deque1_); k++) \ + { \ + if (_deque1_->f_val->cmp(_deque1_->buffer[i], \ + _deque2_->buffer[j]) != 0) \ + return false; \ + \ + i = (i + 1) % _deque1_->capacity; \ + j = (j + 1) % _deque2_->capacity; \ + } \ + \ + return true; \ + } \ + \ + struct cmc_string PFX##_to_string(struct SNAME *_deque_) \ + { \ + struct cmc_string str; \ + struct SNAME *d_ = _deque_; \ + \ + int n = \ + snprintf(str.s, cmc_string_len, cmc_string_fmt_deque, #SNAME, #V, \ + d_, d_->buffer, d_->capacity, d_->count, d_->front, \ + d_->back, d_->flag, d_->f_val, d_->alloc, d_->callbacks); \ + \ + return n >= 0 ? str : (struct cmc_string){ 0 }; \ + } \ + \ + bool PFX##_print(struct SNAME *_deque_, FILE *fptr) \ + { \ + for (size_t i = _deque_->front, j = 0; j < _deque_->count; j++) \ + { \ + if (!_deque_->f_val->str(fptr, _deque_->buffer[i])) \ + return false; \ + \ + i = (i + 1) % _deque_->capacity; \ + } \ + \ + return true; \ + } \ + \ + struct SNAME##_iter *PFX##_iter_new(struct SNAME *target) \ + { \ + struct SNAME##_iter *iter = \ + target->alloc->malloc(sizeof(struct SNAME##_iter)); \ + \ + if (!iter) \ + return NULL; \ + \ + PFX##_iter_init(iter, target); \ + \ + return iter; \ + } \ + \ + void PFX##_iter_free(struct SNAME##_iter *iter) \ + { \ + iter->target->alloc->free(iter); \ + } \ + \ + void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target) \ + { \ + iter->target = target; \ + iter->cursor = target->front; \ + iter->index = 0; \ + iter->start = true; \ + iter->end = PFX##_empty(target); \ + } \ + \ + bool PFX##_iter_start(struct SNAME##_iter *iter) \ + { \ + return PFX##_empty(iter->target) || iter->start; \ + } \ + \ + bool PFX##_iter_end(struct SNAME##_iter *iter) \ + { \ + return PFX##_empty(iter->target) || iter->end; \ + } \ + \ + void PFX##_iter_to_start(struct SNAME##_iter *iter) \ + { \ + if (!PFX##_empty(iter->target)) \ + { \ + iter->cursor = iter->target->front; \ + iter->index = 0; \ + iter->start = true; \ + iter->end = false; \ + } \ + } \ + \ + void PFX##_iter_to_end(struct SNAME##_iter *iter) \ + { \ + if (!PFX##_empty(iter->target)) \ + { \ + if (iter->target->back == 0) \ + iter->cursor = iter->target->capacity - 1; \ + else \ + iter->cursor = iter->target->back - 1; \ + \ + iter->index = PFX##_count(iter->target) - 1; \ + \ + iter->start = false; \ + iter->end = true; \ + } \ + } \ + \ + bool PFX##_iter_next(struct SNAME##_iter *iter) \ + { \ + if (iter->end) \ + return false; \ + \ + if (iter->index + 1 == PFX##_count(iter->target)) \ + { \ + iter->end = true; \ + return false; \ + } \ + \ + iter->start = PFX##_empty(iter->target); \ + \ + iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \ + iter->index++; \ + \ + return true; \ + } \ + \ + bool PFX##_iter_prev(struct SNAME##_iter *iter) \ + { \ + if (iter->start) \ + return false; \ + \ + if (iter->index == 0) \ + { \ + iter->start = true; \ + return false; \ + } \ + \ + iter->end = PFX##_empty(iter->target); \ + \ + iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 \ + : iter->cursor - 1; \ + iter->index--; \ + \ + return true; \ + } \ + \ + /* Returns true only if the iterator moved */ \ + bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps) \ + { \ + if (iter->end) \ + return false; \ + \ + if (iter->index + 1 == PFX##_count(iter->target)) \ + { \ + iter->end = true; \ + return false; \ + } \ + \ + if (steps == 0 || iter->index + steps >= PFX##_count(iter->target)) \ + return false; \ + \ + iter->start = PFX##_empty(iter->target); \ + \ + iter->index += steps; \ + iter->cursor = (iter->cursor + steps) % iter->target->capacity; \ + \ + return true; \ + } \ + \ + /* Returns true only if the iterator moved */ \ + bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps) \ + { \ + if (iter->start) \ + return false; \ + \ + if (iter->index == 0) \ + { \ + iter->start = true; \ + return false; \ + } \ + \ + if (steps == 0 || iter->index < steps) \ + return false; \ + \ + iter->end = PFX##_empty(iter->target); \ + \ + iter->index -= steps; \ + \ + /* Prevent underflow */ \ + if (iter->cursor < steps) \ + iter->cursor += PFX##_capacity(iter->target); \ + \ + iter->cursor -= steps; \ + \ + return true; \ + } \ + \ + /* Returns true only if the iterator was able to be positioned at the */ \ + /* given index */ \ + bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index) \ + { \ + if (index >= PFX##_count(iter->target)) \ + return false; \ + \ + if (iter->index > index) \ + return PFX##_iter_rewind(iter, iter->index - index); \ + else if (iter->index < index) \ + return PFX##_iter_advance(iter, index - iter->index); \ + \ + return true; \ + } \ + \ + V PFX##_iter_value(struct SNAME##_iter *iter) \ + { \ + if (PFX##_empty(iter->target)) \ + return (V){ 0 }; \ + \ + return iter->target->buffer[iter->cursor]; \ + } \ + \ + V *PFX##_iter_rvalue(struct SNAME##_iter *iter) \ + { \ + if (PFX##_empty(iter->target)) \ + return NULL; \ + \ + return &(iter->target->buffer[iter->cursor]); \ + } \ + \ + size_t PFX##_iter_index(struct SNAME##_iter *iter) \ + { \ + return iter->index; \ + } \ + \ + static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_deque_) \ + { \ + struct SNAME##_iter iter; \ + \ + PFX##_iter_init(&iter, _deque_); \ + \ + return iter; \ + } \ + \ + static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_deque_) \ + { \ + struct SNAME##_iter iter; \ + \ + PFX##_iter_init(&iter, _deque_); \ + PFX##_iter_to_end(&iter); \ + \ + return iter; \ } #endif /* CMC_DEQUE_H */ diff --git a/src/cmc/hashmap.h b/src/cmc/hashmap.h index d74f69b..4a0f712 100644 --- a/src/cmc/hashmap.h +++ b/src/cmc/hashmap.h @@ -69,7 +69,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/hashset.h b/src/cmc/hashset.h index 36ba581..1498ab2 100644 --- a/src/cmc/hashset.h +++ b/src/cmc/hashset.h @@ -69,7 +69,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/heap.h b/src/cmc/heap.h index c116a0f..833b2cd 100644 --- a/src/cmc/heap.h +++ b/src/cmc/heap.h @@ -80,7 +80,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/intervalheap.h b/src/cmc/intervalheap.h index 1854402..4e5a7cb 100644 --- a/src/cmc/intervalheap.h +++ b/src/cmc/intervalheap.h @@ -71,7 +71,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/linkedlist.h b/src/cmc/linkedlist.h index 0f0e1d2..52af52d 100644 --- a/src/cmc/linkedlist.h +++ b/src/cmc/linkedlist.h @@ -78,7 +78,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/list.h b/src/cmc/list.h index 10d8070..8575c78 100644 --- a/src/cmc/list.h +++ b/src/cmc/list.h @@ -93,7 +93,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/multimap.h b/src/cmc/multimap.h index b5abcba..9ff2ed3 100644 --- a/src/cmc/multimap.h +++ b/src/cmc/multimap.h @@ -93,7 +93,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/multiset.h b/src/cmc/multiset.h index 3d41bdf..04b12d0 100644 --- a/src/cmc/multiset.h +++ b/src/cmc/multiset.h @@ -71,7 +71,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/queue.h b/src/cmc/queue.h index 4d53589..aeef0fa 100644 --- a/src/cmc/queue.h +++ b/src/cmc/queue.h @@ -93,7 +93,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/sortedlist.h b/src/cmc/sortedlist.h index 382248d..381ed2e 100644 --- a/src/cmc/sortedlist.h +++ b/src/cmc/sortedlist.h @@ -72,7 +72,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/stack.h b/src/cmc/stack.h index 691e555..de0d49f 100644 --- a/src/cmc/stack.h +++ b/src/cmc/stack.h @@ -78,7 +78,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/treemap.h b/src/cmc/treemap.h index e98e27c..abbaeb0 100644 --- a/src/cmc/treemap.h +++ b/src/cmc/treemap.h @@ -69,7 +69,7 @@ static struct 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; // Something is invalid + 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 diff --git a/src/cmc/treeset.h b/src/cmc/treeset.h index 512d499..9136789 100644 --- a/src/cmc/treeset.h +++ b/src/cmc/treeset.h @@ -69,7 +69,7 @@ static struct 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; // Something is invalid + 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 diff --git a/tests/contains.c b/tests/contains.c index 39753af..730bfba 100644 --- a/tests/contains.c +++ b/tests/contains.c @@ -1,7 +1,7 @@ #include "../src/macro_collections.h" -#include #include #include +#include int intcmp(int a, int b) { @@ -51,9 +51,12 @@ int main(void) struct mmap *mm = mm_new(1000, 0.8, intcmp, inthash); struct mset *ms = ms_new(1000, 0.6, intcmp, inthash); - bool a[13] = {false, false, false, false, false, false, false, false, false, false, false, false, false}, - b[13] = {false, false, false, false, false, false, false, false, false, false, false, false, false}, - c[13] = {true, true, true, true, true, true, true, true, true, true, true, true, true}; + bool a[13] = { false, false, false, false, false, false, false, + false, false, false, false, false, false }, + b[13] = { false, false, false, false, false, false, false, + false, false, false, false, false, false }, + c[13] = { true, true, true, true, true, true, true, + true, true, true, true, true, true }; b[0] = d_contains(d, 1, intcmp); b[1] = hm_contains(hm, 1); diff --git a/tests/foreach.c b/tests/foreach.c index b4da307..120d249 100644 --- a/tests/foreach.c +++ b/tests/foreach.c @@ -1,7 +1,7 @@ #include "../src/macro_collections.h" +#include #include #include -#include int intcmp(int a, int b) { @@ -81,62 +81,42 @@ int main(void) assert(ms_insert(ms, i)); } - int sums[16] = {0}; + int sums[16] = { 0 }; - CMC_FOR_EACH(d, deque, d, { - sums[0] += d_iter_value(&iter); - }); + CMC_FOR_EACH(d, deque, d, { sums[0] += d_iter_value(&iter); }); CMC_FOR_EACH(hm, hmap, hm, { sums[1] += hm_iter_key(&iter); sums[2] += hm_iter_value(&iter); }); - CMC_FOR_EACH(hs, hset, hs, { - sums[3] += hs_iter_value(&iter); - }); + CMC_FOR_EACH(hs, hset, hs, { sums[3] += hs_iter_value(&iter); }); - CMC_FOR_EACH(h, heap, h, { - sums[4] += h_iter_value(&iter); - }); + CMC_FOR_EACH(h, heap, h, { sums[4] += h_iter_value(&iter); }); - CMC_FOR_EACH(ll, linked, ll, { - sums[5] += ll_iter_value(&iter); - }); + CMC_FOR_EACH(ll, linked, ll, { sums[5] += ll_iter_value(&iter); }); - CMC_FOR_EACH(l, list, l, { - sums[6] += l_iter_value(&iter); - }); + CMC_FOR_EACH(l, list, l, { sums[6] += l_iter_value(&iter); }); - CMC_FOR_EACH(q, queue, q, { - sums[7] += q_iter_value(&iter); - }); + CMC_FOR_EACH(q, queue, q, { sums[7] += q_iter_value(&iter); }); - CMC_FOR_EACH(s, stack, s, { - sums[8] += s_iter_value(&iter); - }); + CMC_FOR_EACH(s, stack, s, { sums[8] += s_iter_value(&iter); }); CMC_FOR_EACH(tm, tmap, tm, { sums[9] += tm_iter_key(&iter); sums[10] += tm_iter_value(&iter); }); - CMC_FOR_EACH(ts, tset, ts, { - sums[11] += ts_iter_value(&iter); - }); + CMC_FOR_EACH(ts, tset, ts, { sums[11] += ts_iter_value(&iter); }); - CMC_FOR_EACH(ih, iheap, ih, { - sums[12] += ih_iter_value(&iter); - }); + CMC_FOR_EACH(ih, iheap, ih, { sums[12] += ih_iter_value(&iter); }); CMC_FOR_EACH(mm, mmap, mm, { sums[13] += mm_iter_key(&iter); sums[14] += mm_iter_value(&iter); }); - CMC_FOR_EACH(ms, mset, ms, { - sums[15] += ms_iter_value(&iter); - }); + CMC_FOR_EACH(ms, mset, ms, { sums[15] += ms_iter_value(&iter); }); printf("\n-------------------- CMC_FOR_EACH --------------------\n"); if (sums[0] == 50005000) @@ -196,60 +176,40 @@ int main(void) memset(sums, 0, sizeof sums); - CMC_FOR_EACH_REV(d, deque, d, { - sums[0] += d_iter_value(&iter); - }); + CMC_FOR_EACH_REV(d, deque, d, { sums[0] += d_iter_value(&iter); }); CMC_FOR_EACH_REV(hm, hmap, hm, { sums[1] += hm_iter_key(&iter); sums[2] += hm_iter_value(&iter); }); - CMC_FOR_EACH_REV(hs, hset, hs, { - sums[3] += hs_iter_value(&iter); - }); + CMC_FOR_EACH_REV(hs, hset, hs, { sums[3] += hs_iter_value(&iter); }); - CMC_FOR_EACH_REV(h, heap, h, { - sums[4] += h_iter_value(&iter); - }); + CMC_FOR_EACH_REV(h, heap, h, { sums[4] += h_iter_value(&iter); }); - CMC_FOR_EACH_REV(ll, linked, ll, { - sums[5] += ll_iter_value(&iter); - }); + CMC_FOR_EACH_REV(ll, linked, ll, { sums[5] += ll_iter_value(&iter); }); - CMC_FOR_EACH_REV(l, list, l, { - sums[6] += l_iter_value(&iter); - }); + CMC_FOR_EACH_REV(l, list, l, { sums[6] += l_iter_value(&iter); }); - CMC_FOR_EACH_REV(q, queue, q, { - sums[7] += q_iter_value(&iter); - }); + CMC_FOR_EACH_REV(q, queue, q, { sums[7] += q_iter_value(&iter); }); - CMC_FOR_EACH_REV(s, stack, s, { - sums[8] += s_iter_value(&iter); - }); + CMC_FOR_EACH_REV(s, stack, s, { sums[8] += s_iter_value(&iter); }); CMC_FOR_EACH_REV(tm, tmap, tm, { sums[9] += tm_iter_key(&iter); sums[10] += tm_iter_value(&iter); }); - CMC_FOR_EACH_REV(ts, tset, ts, { - sums[11] += ts_iter_value(&iter); - }); + CMC_FOR_EACH_REV(ts, tset, ts, { sums[11] += ts_iter_value(&iter); }); - CMC_FOR_EACH_REV(ih, iheap, ih, { - sums[12] += ih_iter_value(&iter); - }); + CMC_FOR_EACH_REV(ih, iheap, ih, { sums[12] += ih_iter_value(&iter); }); CMC_FOR_EACH_REV(mm, mmap, mm, { sums[13] += mm_iter_key(&iter); sums[14] += mm_iter_value(&iter); }); - CMC_FOR_EACH_REV(ms, mset, ms, { - sums[15] += ms_iter_value(&iter); - }); + CMC_FOR_EACH_REV(ms, mset, ms, { sums[15] += ms_iter_value(&iter); }); printf("-------------------- CMC_FOR_EACH_REV --------------------\n"); if (sums[0] == 50005000) diff --git a/tests/iterators.c b/tests/iterators.c index 97e8511..dc51442 100644 --- a/tests/iterators.c +++ b/tests/iterators.c @@ -33,66 +33,87 @@ CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, /* K */, int) CMC_COLLECTION_GENERATE(MULTIMAP, mm, mmap, int, double) CMC_COLLECTION_GENERATE(MULTISET, ms, mset, /* K */, int) -#define ITERATOR_TEST(NAME, PFX, sname, initfunc, insertbody, sumbody, validation1, validation2) \ - void NAME##_iterator_test(void) \ - { \ - int sum1 = 0; \ - double sum2 = 0.0; \ - \ - struct sname *coll = initfunc; \ - for (int i = 0; i < 10; i++) \ - { \ - insertbody; \ - } \ - \ - struct sname##_iter iter; \ - PFX##_iter_init(&iter, coll); \ - \ - for (int j = 0; j < 4; j++) \ - { \ - while (!PFX##_iter_end(&iter)) \ - { \ - sumbody; \ - PFX##_iter_next(&iter); \ - } \ - while (!PFX##_iter_start(&iter)) \ - { \ - sumbody; \ - PFX##_iter_prev(&iter); \ - } \ - } \ - \ - for (PFX##_iter_to_start(&iter); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \ - { \ - sumbody; \ - } \ - \ - for (PFX##_iter_to_end(&iter); !PFX##_iter_start(&iter); PFX##_iter_prev(&iter)) \ - { \ - sumbody; \ - } \ - \ - PFX##_free(coll, NULL); \ - \ - if ((validation1) && (validation2)) \ - printf("%12s PASSED\n", #NAME); \ - else \ - printf("%12s FAILED\n", #NAME); \ +#define ITERATOR_TEST(NAME, PFX, sname, initfunc, insertbody, sumbody, \ + validation1, validation2) \ + void NAME##_iterator_test(void) \ + { \ + int sum1 = 0; \ + double sum2 = 0.0; \ + \ + struct sname *coll = initfunc; \ + for (int i = 0; i < 10; i++) \ + { \ + insertbody; \ + } \ + \ + struct sname##_iter iter; \ + PFX##_iter_init(&iter, coll); \ + \ + for (int j = 0; j < 4; j++) \ + { \ + while (!PFX##_iter_end(&iter)) \ + { \ + sumbody; \ + PFX##_iter_next(&iter); \ + } \ + while (!PFX##_iter_start(&iter)) \ + { \ + sumbody; \ + PFX##_iter_prev(&iter); \ + } \ + } \ + \ + for (PFX##_iter_to_start(&iter); !PFX##_iter_end(&iter); \ + PFX##_iter_next(&iter)) \ + { \ + sumbody; \ + } \ + \ + for (PFX##_iter_to_end(&iter); !PFX##_iter_start(&iter); \ + PFX##_iter_prev(&iter)) \ + { \ + sumbody; \ + } \ + \ + PFX##_free(coll, NULL); \ + \ + if ((validation1) && (validation2)) \ + printf("%12s PASSED\n", #NAME); \ + else \ + printf("%12s FAILED\n", #NAME); \ } -ITERATOR_TEST(DEQUE, d, deque, d_new(20), d_push_front(coll, i), sum1 += d_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(HASHMAP, hm, hmap, hm_new(50, 0.6, intcmp, inthash), hm_insert(coll, i, (double)i / 10.0), sum1 += hm_iter_key(&iter); sum2 += hm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45) -ITERATOR_TEST(HASHSET, hs, hset, hs_new(50, 0.6, intcmp, inthash), hs_insert(coll, i), sum1 += hs_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(HEAP, h, heap, h_new(20, cmc_max_heap, intcmp), h_insert(coll, i), sum1 += h_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(LINKEDLIST, ll, linked, ll_new(), ll_push_back(coll, i), sum1 += ll_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(LIST, l, list, l_new(20), l_push_back(coll, i), sum1 += l_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(QUEUE, q, queue, q_new(20), q_enqueue(coll, i), sum1 += q_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(STACK, s, stack, s_new(20), s_push(coll, i), sum1 += s_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(TREEMAP, tm, tmap, tm_new(intcmp), tm_insert(coll, i, (double)i / 10.0), sum1 += tm_iter_key(&iter); sum2 += tm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45) -ITERATOR_TEST(TREESET, ts, tset, ts_new(intcmp), ts_insert(coll, i), sum1 += ts_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(INTERVALHEAP, ih, iheap, ih_new(20, intcmp), ih_insert(coll, i), sum1 += ih_iter_value(&iter), sum1 == 450, 1) -ITERATOR_TEST(MULTIMAP, mm, mmap, mm_new(50, 0.8, intcmp, inthash), mm_insert(coll, i, (double)i / 10.0), sum1 += mm_iter_key(&iter); sum2 += mm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45) -ITERATOR_TEST(MULTISET, ms, mset, ms_new(50, 0.6, intcmp, inthash), ms_insert(coll, i), sum1 += ms_iter_value(&iter); sum2 += ms_iter_multiplicity(&iter), sum1 == 450, (size_t)sum2 == 100) +ITERATOR_TEST(DEQUE, d, deque, d_new(20), d_push_front(coll, i), + sum1 += d_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(HASHMAP, hm, hmap, hm_new(50, 0.6, intcmp, inthash), + hm_insert(coll, i, (double)i / 10.0), sum1 += hm_iter_key(&iter); + sum2 += hm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45) +ITERATOR_TEST(HASHSET, hs, hset, hs_new(50, 0.6, intcmp, inthash), + hs_insert(coll, i), sum1 += hs_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(HEAP, h, heap, h_new(20, cmc_max_heap, intcmp), h_insert(coll, i), + sum1 += h_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(LINKEDLIST, ll, linked, ll_new(), ll_push_back(coll, i), + sum1 += ll_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(LIST, l, list, l_new(20), l_push_back(coll, i), + sum1 += l_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(QUEUE, q, queue, q_new(20), q_enqueue(coll, i), + sum1 += q_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(STACK, s, stack, s_new(20), s_push(coll, i), + sum1 += s_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(TREEMAP, tm, tmap, tm_new(intcmp), + tm_insert(coll, i, (double)i / 10.0), sum1 += tm_iter_key(&iter); + sum2 += tm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45) +ITERATOR_TEST(TREESET, ts, tset, ts_new(intcmp), ts_insert(coll, i), + sum1 += ts_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(INTERVALHEAP, ih, iheap, ih_new(20, intcmp), ih_insert(coll, i), + sum1 += ih_iter_value(&iter), sum1 == 450, 1) +ITERATOR_TEST(MULTIMAP, mm, mmap, mm_new(50, 0.8, intcmp, inthash), + mm_insert(coll, i, (double)i / 10.0), sum1 += mm_iter_key(&iter); + sum2 += mm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45) +ITERATOR_TEST(MULTISET, ms, mset, ms_new(50, 0.6, intcmp, inthash), + ms_insert(coll, i), sum1 += ms_iter_value(&iter); + sum2 += ms_iter_multiplicity(&iter), sum1 == 450, + (size_t)sum2 == 100) int main(void) { diff --git a/tests/main/unt/bidimap.c b/tests/main/unt/bidimap.c index 5cf0100..7eb1e81 100644 --- a/tests/main/unt/bidimap.c +++ b/tests/main/unt/bidimap.c @@ -7,23 +7,21 @@ CMC_GENERATE_BIDIMAP(bm, bidimap, size_t, size_t) -struct bidimap_ftab_key *bm_ftab_key = &(struct bidimap_ftab_key) { - .cmp = cmp, - .cpy = copy, - .str = to_string, - .free = custom_free, - .hash = hash, - .pri = priority -}; +struct bidimap_ftab_key *bm_ftab_key = + &(struct bidimap_ftab_key){ .cmp = cmp, + .cpy = copy, + .str = str, + .free = custom_free, + .hash = hash, + .pri = pri }; -struct bidimap_ftab_val *bm_ftab_val = &(struct bidimap_ftab_val) { - .cmp = cmp, - .cpy = copy, - .str = to_string, - .free = custom_free, - .hash = hash, - .pri = priority -}; +struct bidimap_ftab_val *bm_ftab_val = + &(struct bidimap_ftab_val){ .cmp = cmp, + .cpy = copy, + .str = str, + .free = custom_free, + .hash = hash, + .pri = pri }; CMC_CREATE_UNIT(bidimap_test, true, { CMC_CREATE_TEST(new, { @@ -50,13 +48,13 @@ CMC_CREATE_UNIT(bidimap_test, true, { }); CMC_CREATE_TEST(new_custom, { - struct bidimap *map = bm_new_custom(100, 0.6, bm_ftab_key, bm_ftab_val, - &(struct cmc_alloc_node){ - .malloc = malloc, - .calloc = calloc, - .realloc = realloc, - .free = free}, - &(struct cmc_callbacks_bidimap){0}); + struct bidimap *map = + bm_new_custom(100, 0.6, bm_ftab_key, bm_ftab_val, + &(struct cmc_alloc_node){ .malloc = malloc, + .calloc = calloc, + .realloc = realloc, + .free = free }, + &(struct cmc_callbacks_bidimap){ 0 }); cmc_assert_not_equals(ptr, NULL, map); @@ -64,13 +62,13 @@ CMC_CREATE_UNIT(bidimap_test, true, { }); CMC_CREATE_TEST(new_custom[insert remove], { - struct bidimap *map = bm_new_custom(500, 0.6, bm_ftab_key, bm_ftab_val, - &(struct cmc_alloc_node){ - .malloc = malloc, - .calloc = calloc, - .realloc = realloc, - .free = free}, - &(struct cmc_callbacks_bidimap){0}); + struct bidimap *map = + bm_new_custom(500, 0.6, bm_ftab_key, bm_ftab_val, + &(struct cmc_alloc_node){ .malloc = malloc, + .calloc = calloc, + .realloc = realloc, + .free = free }, + &(struct cmc_callbacks_bidimap){ 0 }); cmc_assert_not_equals(ptr, NULL, map); @@ -233,13 +231,13 @@ CMC_CREATE_UNIT(bidimap_test, true, { }); CMC_CREATE_TEST(remove_by_key[cleanup custom], { - struct bidimap *map = bm_new_custom(10000, 0.6, bm_ftab_key, bm_ftab_val, - &(struct cmc_alloc_node){ - .malloc = malloc, - .calloc = calloc, - .realloc = realloc, - .free = free}, - &(struct cmc_callbacks_bidimap){0}); + struct bidimap *map = + bm_new_custom(10000, 0.6, bm_ftab_key, bm_ftab_val, + &(struct cmc_alloc_node){ .malloc = malloc, + .calloc = calloc, + .realloc = realloc, + .free = free }, + &(struct cmc_callbacks_bidimap){ 0 }); cmc_assert_not_equals(ptr, NULL, map); @@ -258,21 +256,21 @@ CMC_CREATE_UNIT(bidimap_test, true, { struct bidimap_entry *k = map->key_buffer[i]; struct bidimap_entry *v = map->val_buffer[i]; - cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) - && (v == NULL || v == CMC_ENTRY_DELETED)); + cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && + (v == NULL || v == CMC_ENTRY_DELETED)); } bm_free(map); }); CMC_CREATE_TEST(remove_by_val[cleanup custom], { - struct bidimap *map = bm_new_custom(10000, 0.6, bm_ftab_key, bm_ftab_val, - &(struct cmc_alloc_node){ - .malloc = malloc, - .calloc = calloc, - .realloc = realloc, - .free = free}, - &(struct cmc_callbacks_bidimap){0}); + struct bidimap *map = + bm_new_custom(10000, 0.6, bm_ftab_key, bm_ftab_val, + &(struct cmc_alloc_node){ .malloc = malloc, + .calloc = calloc, + .realloc = realloc, + .free = free }, + &(struct cmc_callbacks_bidimap){ 0 }); cmc_assert_not_equals(ptr, NULL, map); @@ -291,7 +289,8 @@ CMC_CREATE_UNIT(bidimap_test, true, { struct bidimap_entry *k = map->key_buffer[i]; struct bidimap_entry *v = map->val_buffer[i]; - cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && (v == NULL || v == CMC_ENTRY_DELETED)); + cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && + (v == NULL || v == CMC_ENTRY_DELETED)); } bm_free(map); diff --git a/tests/main/unt/deque.c b/tests/main/unt/deque.c index 950b3b7..f117454 100644 --- a/tests/main/unt/deque.c +++ b/tests/main/unt/deque.c @@ -7,32 +7,40 @@ CMC_GENERATE_DEQUE(d, deque, size_t) +struct deque_ftab_val *d_ftab_val = + &(struct deque_ftab_val){ .cmp = cmp, + .cpy = copy, + .str = str, + .free = custom_free, + .hash = hash, + .pri = pri }; + CMC_CREATE_UNIT(deque_test, true, { CMC_CREATE_TEST(new, { - struct deque *d = d_new(1000000); + struct deque *d = d_new(1000000, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); cmc_assert_not_equals(ptr, NULL, d->buffer); cmc_assert_equals(size_t, 1000000, d->capacity); cmc_assert_equals(size_t, 0, d_count(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(new[capacity = 0], { - struct deque *d = d_new(0); + struct deque *d = d_new(0, d_ftab_val); cmc_assert_equals(ptr, NULL, d); }); CMC_CREATE_TEST(new[capacity = UINT64_MAX], { - struct deque *d = d_new(UINT64_MAX); + struct deque *d = d_new(UINT64_MAX, d_ftab_val); cmc_assert_equals(ptr, NULL, d); }); CMC_CREATE_TEST(clear[count capacity], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -41,16 +49,16 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 50, d_count(d)); - d_clear(d, NULL); + d_clear(d); cmc_assert_equals(size_t, 0, d_count(d)); cmc_assert_equals(size_t, 100, d_capacity(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(buffer_growth[capacity = 1], { - struct deque *d = d_new(1); + struct deque *d = d_new(1, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -60,11 +68,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 50, d_count(d)); cmc_assert_lesser_equals(size_t, d_capacity(d), d_count(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(buffer_growth[item preservation], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -77,17 +85,18 @@ CMC_CREATE_UNIT(deque_test, true, { } size_t sum = 0; - for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++) + for (size_t i = d->front, j = 0; j < d->count; + i = (i + 1) % d->capacity, j++) sum += d->buffer[i]; cmc_assert_equals(size_t, 1200, d_count(d)); cmc_assert_equals(size_t, 720600, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(push_front[count capacity], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -97,11 +106,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 150, d_count(d)); cmc_assert_greater(size_t, 100, d_capacity(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(push_front[item preservation], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -110,18 +119,19 @@ CMC_CREATE_UNIT(deque_test, true, { size_t sum = 0; - for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++) + for (size_t i = d->front, j = 0; j < d->count; + i = (i + 1) % d->capacity, j++) { sum += d->buffer[i]; } cmc_assert_equals(size_t, 5050, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(push_back[count capacity], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -131,11 +141,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 150, d_count(d)); cmc_assert_greater(size_t, 100, d_capacity(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(push_back[item preservation], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -144,18 +154,19 @@ CMC_CREATE_UNIT(deque_test, true, { size_t sum = 0; - for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++) + for (size_t i = d->front, j = 0; j < d->count; + i = (i + 1) % d->capacity, j++) { sum += d->buffer[i]; } cmc_assert_equals(size_t, 5050, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(pop_front[count capacity], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -169,11 +180,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 0, d_count(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(pop_front[item preservation], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -185,18 +196,19 @@ CMC_CREATE_UNIT(deque_test, true, { size_t sum = 0; - for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++) + for (size_t i = d->front, j = 0; j < d->count; + i = (i + 1) % d->capacity, j++) { sum += d->buffer[i]; } cmc_assert_equals(size_t, 5050, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(pop_back[count capacity], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -210,11 +222,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 0, d_count(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(pop_back[item preservation], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -226,18 +238,19 @@ CMC_CREATE_UNIT(deque_test, true, { size_t sum = 0; - for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++) + for (size_t i = d->front, j = 0; j < d->count; + i = (i + 1) % d->capacity, j++) { sum += d->buffer[i]; } cmc_assert_equals(size_t, 5050, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(Mixed IO, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -297,23 +310,23 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 0, d_count(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(front[count = 0], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); size_t r = d_front(d); - cmc_assert_equals(size_t, (size_t){0}, r); + cmc_assert_equals(size_t, (size_t){ 0 }, r); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(front[sum], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -338,23 +351,23 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 5050, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(back[count = 0], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); size_t r = d_back(d); - cmc_assert_equals(size_t, (size_t){0}, r); + cmc_assert_equals(size_t, (size_t){ 0 }, r); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(back[sum], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -379,11 +392,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 5050, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(contains, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -391,52 +404,52 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert(d_push_back(d, i)); for (size_t i = 1; i <= 20; i++) - cmc_assert(d_contains(d, i, cmp)); + cmc_assert(d_contains(d, i)); - d_clear(d, NULL); + d_clear(d); for (size_t i = 1; i <= 20; i++) cmc_assert(d_push_front(d, i)); for (size_t i = 1; i <= 20; i++) - cmc_assert(d_contains(d, i, cmp)); + cmc_assert(d_contains(d, i)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(contains[count = 0], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); - cmc_assert(!d_contains(d, 10, cmp)); + cmc_assert(!d_contains(d, 10)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(contains[count = 1], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); - cmc_assert(!d_contains(d, 10, cmp)); + cmc_assert(!d_contains(d, 10)); cmc_assert(d_push_back(d, 100)); - cmc_assert(d_contains(d, 100, cmp)); - cmc_assert(!d_contains(d, 101, cmp)); + cmc_assert(d_contains(d, 100)); + cmc_assert(!d_contains(d, 101)); cmc_assert(d_pop_front(d)); cmc_assert(d_push_front(d, 100)); - cmc_assert(d_contains(d, 100, cmp)); - cmc_assert(!d_contains(d, 101, cmp)); + cmc_assert(d_contains(d, 100)); + cmc_assert(!d_contains(d, 101)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(empty, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -446,11 +459,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert(!d_empty(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(full, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -461,11 +474,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert(d_full(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(count, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -476,21 +489,21 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert_equals(size_t, 100, d_count(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(capacity, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); cmc_assert_equals(size_t, 100, d_capacity(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(resize, { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -506,11 +519,11 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert(d_resize(d, d_count(d))); cmc_assert_equals(size_t, 150, d_capacity(d)); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(resize[item preservation], { - struct deque *d = d_new(100); + struct deque *d = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d); @@ -526,18 +539,19 @@ CMC_CREATE_UNIT(deque_test, true, { size_t sum = 0; - for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++) + for (size_t i = d->front, j = 0; j < d->count; + i = (i + 1) % d->capacity, j++) { sum += d->buffer[i]; } cmc_assert_equals(size_t, 11325, sum); - d_free(d, NULL); + d_free(d); }); CMC_CREATE_TEST(copy_of, { - struct deque *d1 = d_new(100); + struct deque *d1 = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d1); @@ -549,7 +563,7 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert(d_push_front(d1, i)); } - struct deque *d2 = d_copy_of(d1, NULL); + struct deque *d2 = d_copy_of(d1); cmc_assert_not_equals(ptr, NULL, d2); cmc_assert_equals(size_t, d_count(d1), d_count(d2)); @@ -562,13 +576,20 @@ CMC_CREATE_UNIT(deque_test, true, { i2 = (i2 + 1) % d2->capacity; } - d_free(d1, NULL); - d_free(d2, NULL); + cmc_assert(d_push_front(d1, 10)); + cmc_assert(d_push_front(d2, 10)); + cmc_assert(d_push_back(d1, 11)); + cmc_assert(d_push_back(d2, 11)); + + cmc_assert(d_equals(d1, d2)); + + d_free(d1); + d_free(d2); }); CMC_CREATE_TEST(equals, { - struct deque *d1 = d_new(100); - struct deque *d2 = d_new(100); + struct deque *d1 = d_new(100, d_ftab_val); + struct deque *d2 = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d1); cmc_assert_not_equals(ptr, NULL, d2); @@ -587,14 +608,14 @@ CMC_CREATE_UNIT(deque_test, true, { } } - cmc_assert(d_equals(d1, d2, cmp)); + cmc_assert(d_equals(d1, d2)); - d_free(d1, NULL); - d_free(d2, NULL); + d_free(d1); + d_free(d2); }); CMC_CREATE_TEST(equals[from copy], { - struct deque *d1 = d_new(100); + struct deque *d1 = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d1); @@ -606,28 +627,28 @@ CMC_CREATE_UNIT(deque_test, true, { cmc_assert(d_push_front(d1, i)); } - struct deque *d2 = d_copy_of(d1, NULL); + struct deque *d2 = d_copy_of(d1); cmc_assert_not_equals(ptr, NULL, d2); - cmc_assert(d_equals(d1, d2, cmp)); + cmc_assert(d_equals(d1, d2)); - d_free(d1, NULL); - d_free(d2, NULL); + d_free(d1); + d_free(d2); }); CMC_CREATE_TEST(equals[count = 0], { - struct deque *d1 = d_new(100); + struct deque *d1 = d_new(100, d_ftab_val); cmc_assert_not_equals(ptr, NULL, d1); - struct deque *d2 = d_copy_of(d1, NULL); + struct deque *d2 = d_copy_of(d1); cmc_assert_not_equals(ptr, NULL, d2); - cmc_assert(d_equals(d1, d2, cmp)); + cmc_assert(d_equals(d1, d2)); - d_free(d1, NULL); - d_free(d2, NULL); + d_free(d1); + d_free(d2); }); }); diff --git a/tests/main/unt/hashmap.c b/tests/main/unt/hashmap.c index b072ebd..98ebf9e 100644 --- a/tests/main/unt/hashmap.c +++ b/tests/main/unt/hashmap.c @@ -97,7 +97,8 @@ CMC_CREATE_UNIT(hashmap_test, true, { for (size_t i = 0; i < 200; i++) cmc_assert(hm_insert(map, i, i)); - // Everything is hashed to 0 so key 0 should have dist 0, key 1 dist 1, etc + // Everything is hashed to 0 so key 0 should have dist 0, key 1 dist 1, + // etc for (size_t i = 0; i < 200; i++) cmc_assert_equals(size_t, i, map->buffer[i].dist); @@ -112,7 +113,8 @@ CMC_CREATE_UNIT(hashmap_test, true, { size_t capacity = hm_capacity(map); // Just to be sure, not part of the test - cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, hashcapminus1(0)); + cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, + hashcapminus1(0)); cmc_assert_equals(size_t, capacity - 1, hashcapminus1(0)); cmc_assert(hm_insert(map, 0, 0)); @@ -304,7 +306,7 @@ CMC_CREATE_UNIT(hashmap_test, true, { cmc_assert_not_equals(ptr, NULL, map); - cmc_assert_equals(size_t, (size_t){0}, hm_get(map, 4321)); + cmc_assert_equals(size_t, (size_t){ 0 }, hm_get(map, 4321)); hm_free(map, NULL); }); @@ -389,7 +391,8 @@ CMC_CREATE_UNIT(hashmap_test, true, { }); CMC_CREATE_TEST(full, { - struct hashmap *map = hm_new(cmc_hashtable_primes[0], 0.99999, cmp, hash); + struct hashmap *map = + hm_new(cmc_hashtable_primes[0], 0.99999, cmp, hash); cmc_assert_not_equals(ptr, NULL, map); @@ -428,7 +431,8 @@ CMC_CREATE_UNIT(hashmap_test, true, { cmc_assert_not_equals(ptr, NULL, map); - cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), hm_capacity(map)); + cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), + hm_capacity(map)); hm_free(map, NULL); }); diff --git a/tests/main/unt/hashset.c b/tests/main/unt/hashset.c index 92aa66b..71eecff 100644 --- a/tests/main/unt/hashset.c +++ b/tests/main/unt/hashset.c @@ -83,7 +83,8 @@ CMC_CREATE_UNIT(hashset_test, true, { cmc_assert(hs_insert(set, capacity - 1)); cmc_assert(hs_insert(set, capacity)); - cmc_assert_equals(size_t, capacity - 1, set->buffer[capacity - 1].value); + cmc_assert_equals(size_t, capacity - 1, + set->buffer[capacity - 1].value); cmc_assert_equals(size_t, capacity, set->buffer[0].value); hs_free(set, NULL); @@ -97,7 +98,8 @@ CMC_CREATE_UNIT(hashset_test, true, { for (size_t i = 0; i < 200; i++) cmc_assert(hs_insert(set, i)); - // Everything is hashed to 0 so key 0 should have dist 0, key 1 dist 1, etc + // Everything is hashed to 0 so key 0 should have dist 0, key 1 dist 1, + // etc for (size_t i = 0; i < 200; i++) cmc_assert_equals(size_t, i, set->buffer[i].dist); @@ -112,7 +114,8 @@ CMC_CREATE_UNIT(hashset_test, true, { size_t capacity = hs_capacity(set); // Just to be sure, not part of the test - cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, hashcapminus1(0)); + cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, + hashcapminus1(0)); cmc_assert_equals(size_t, capacity - 1, hashcapminus1(0)); cmc_assert(hs_insert(set, 0)); @@ -294,7 +297,8 @@ CMC_CREATE_UNIT(hashset_test, true, { }); CMC_CREATE_TEST(full, { - struct hashset *set = hs_new(cmc_hashtable_primes[0], 0.99999, cmp, hash); + struct hashset *set = + hs_new(cmc_hashtable_primes[0], 0.99999, cmp, hash); cmc_assert_not_equals(ptr, NULL, set); @@ -333,7 +337,8 @@ CMC_CREATE_UNIT(hashset_test, true, { cmc_assert_not_equals(ptr, NULL, set); - cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), hs_capacity(set)); + cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), + hs_capacity(set)); hs_free(set, NULL); }); diff --git a/tests/main/unt/heap.c b/tests/main/unt/heap.c index 4532e33..bb4a46c 100644 --- a/tests/main/unt/heap.c +++ b/tests/main/unt/heap.c @@ -157,7 +157,7 @@ CMC_CREATE_UNIT(heap_test, true, { cmc_assert_not_equals(ptr, NULL, h); - cmc_assert_equals(size_t, (size_t){0}, h_peek(h)); + cmc_assert_equals(size_t, (size_t){ 0 }, h_peek(h)); h_free(h, NULL); }); diff --git a/tests/main/unt/multiset.c b/tests/main/unt/multiset.c index 5f71c04..cc122cf 100644 --- a/tests/main/unt/multiset.c +++ b/tests/main/unt/multiset.c @@ -14,7 +14,8 @@ CMC_CREATE_UNIT(multiset_test, true, { cmc_assert_not_equals(ptr, NULL, set); cmc_assert_not_equals(ptr, NULL, set->buffer); cmc_assert_equals(size_t, 0, ms_count(set)); - cmc_assert_greater_equals(size_t, ((size_t)(943722 / 0.6)), ms_capacity(set)); + cmc_assert_greater_equals(size_t, ((size_t)(943722 / 0.6)), + ms_capacity(set)); ms_free(set, NULL); }); diff --git a/tests/main/unt/queue.c b/tests/main/unt/queue.c index 4b08285..2b33349 100644 --- a/tests/main/unt/queue.c +++ b/tests/main/unt/queue.c @@ -74,7 +74,8 @@ CMC_CREATE_UNIT(queue_test, true, { } size_t sum = 0; - for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++) + for (size_t i = q->front, j = 0; j < q->count; + i = (i + 1) % q->capacity, j++) sum += q->buffer[i]; cmc_assert_equals(size_t, 1200, q_count(q)); @@ -107,7 +108,8 @@ CMC_CREATE_UNIT(queue_test, true, { size_t sum = 0; - for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++) + for (size_t i = q->front, j = 0; j < q->count; + i = (i + 1) % q->capacity, j++) { sum += q->buffer[i]; } @@ -148,7 +150,8 @@ CMC_CREATE_UNIT(queue_test, true, { size_t sum = 0; - for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++) + for (size_t i = q->front, j = 0; j < q->count; + i = (i + 1) % q->capacity, j++) { sum += q->buffer[i]; } @@ -165,7 +168,7 @@ CMC_CREATE_UNIT(queue_test, true, { size_t r = q_peek(q); - cmc_assert_equals(size_t, (size_t){0}, r); + cmc_assert_equals(size_t, (size_t){ 0 }, r); q_free(q, NULL); }); @@ -319,7 +322,8 @@ CMC_CREATE_UNIT(queue_test, true, { size_t sum = 0; - for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++) + for (size_t i = q->front, j = 0; j < q->count; + i = (i + 1) % q->capacity, j++) { sum += q->buffer[i]; } diff --git a/tests/main/unt/stack.c b/tests/main/unt/stack.c index e4e4b77..d98feed 100644 --- a/tests/main/unt/stack.c +++ b/tests/main/unt/stack.c @@ -186,7 +186,7 @@ CMC_CREATE_UNIT(stack_test, true, { cmc_assert(s_pop(s)); - cmc_assert_equals(size_t, (size_t){0}, s_top(s)); + cmc_assert_equals(size_t, (size_t){ 0 }, s_top(s)); s_free(s, NULL); }); @@ -342,14 +342,16 @@ CMC_CREATE_UNIT(stack_test, true, { size_t total = 0; - for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); s_iter_next(&it)) + for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); + s_iter_next(&it)) { total += s_iter_value(&it); } cmc_assert_equals(size_t, 20100, total); - for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); s_iter_prev(&it)) + for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); + s_iter_prev(&it)) { total += s_iter_value(&it); } @@ -397,12 +399,14 @@ CMC_CREATE_UNIT(stack_test, true, { size_t total = 0; - for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); s_iter_next(&it)) + for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); + s_iter_next(&it)) { total += s_iter_value(&it); } - for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); s_iter_prev(&it)) + for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); + s_iter_prev(&it)) { total += s_iter_value(&it); } @@ -424,14 +428,16 @@ CMC_CREATE_UNIT(stack_test, true, { size_t total = 0; - for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); s_iter_next(&it)) + for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); + s_iter_next(&it)) { total += s_iter_value(&it); } cmc_assert_equals(size_t, 1000, total); - for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); s_iter_prev(&it)) + for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); + s_iter_prev(&it)) { total += *s_iter_rvalue(&it); } @@ -526,7 +532,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_iter_init(&iter, s); - cmc_assert_equals(size_t, (size_t){0}, s_iter_value(&iter)); + cmc_assert_equals(size_t, (size_t){ 0 }, s_iter_value(&iter)); s_free(s, NULL); }); @@ -567,14 +573,16 @@ CMC_CREATE_UNIT(stack_test, true, { cmc_assert_equals(size_t, s_count(s) - 1, s_iter_index(&it)); } - for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); s_iter_next(&it)) + for (struct stack_iter it = s->it_start(s); !s_iter_end(&it); + s_iter_next(&it)) { total += s_iter_index(&it); } cmc_assert_equals(size_t, 45, total); - for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); s_iter_prev(&it)) + for (struct stack_iter it = s->it_end(s); !s_iter_start(&it); + s_iter_prev(&it)) { total += s_iter_index(&it); } diff --git a/tests/main/unt/utl.c b/tests/main/unt/utl.c index d7a7bf2..dcbe53d 100644 --- a/tests/main/unt/utl.c +++ b/tests/main/unt/utl.c @@ -2,9 +2,10 @@ #ifndef CMC_UNIT_TEST_UTL__ #define CMC_UNIT_TEST_UTL__ -#include -#include #include +#include +#include +#include int cmp(size_t a, size_t b) { @@ -16,7 +17,7 @@ size_t copy(size_t a) return a; } -bool to_string(FILE *fptr, size_t a) +bool str(FILE *fptr, size_t a) { return fprintf(fptr, "%" PRIuMAX " ", a) > 0; } @@ -38,7 +39,7 @@ size_t hash(size_t a) return a; } -int priority(size_t a, size_t b) +int pri(size_t a, size_t b) { // Could contain different logic return cmp(a, b); diff --git a/tests/to_string.c b/tests/to_string.c index db1148b..5d17b90 100644 --- a/tests/to_string.c +++ b/tests/to_string.c @@ -2,26 +2,28 @@ #include "../src/macro_collections.h" -CMC_COLLECTION_GENERATE( BIDIMAP, bm, bmap, int, int) -CMC_COLLECTION_GENERATE( DEQUE, d, deque, /* K */, int) -CMC_COLLECTION_GENERATE( HASHMAP, hm, hmap, int, int) -CMC_COLLECTION_GENERATE( HASHSET, hs, hset, /* K */, int) -CMC_COLLECTION_GENERATE( HEAP, h, heap, /* K */, int) -CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, /* K */, int) -CMC_COLLECTION_GENERATE( LINKEDLIST, ll, linked, /* K */, int) -CMC_COLLECTION_GENERATE( LIST, l, list, /* K */, int) -CMC_COLLECTION_GENERATE( MULTIMAP, mm, mmap, int, int) -CMC_COLLECTION_GENERATE( MULTISET, ms, mset, /* K */, int) -CMC_COLLECTION_GENERATE( QUEUE, q, queue, /* K */, int) -CMC_COLLECTION_GENERATE( SORTEDLIST, sl, slist, /* K */, int) -CMC_COLLECTION_GENERATE( STACK, s, stack, /* K */, int) -CMC_COLLECTION_GENERATE( TREESET, ts, tset, /* K */, int) -CMC_COLLECTION_GENERATE( TREEMAP, tm, tmap, int, int) +CMC_COLLECTION_GENERATE(BIDIMAP, bm, bmap, int, int) +CMC_COLLECTION_GENERATE(DEQUE, d, deque, /* K */, int) +CMC_COLLECTION_GENERATE(HASHMAP, hm, hmap, int, int) +CMC_COLLECTION_GENERATE(HASHSET, hs, hset, /* K */, int) +CMC_COLLECTION_GENERATE(HEAP, h, heap, /* K */, int) +CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, /* K */, int) +CMC_COLLECTION_GENERATE(LINKEDLIST, ll, linked, /* K */, int) +CMC_COLLECTION_GENERATE(LIST, l, list, /* K */, int) +CMC_COLLECTION_GENERATE(MULTIMAP, mm, mmap, int, int) +CMC_COLLECTION_GENERATE(MULTISET, ms, mset, /* K */, int) +CMC_COLLECTION_GENERATE(QUEUE, q, queue, /* K */, int) +CMC_COLLECTION_GENERATE(SORTEDLIST, sl, slist, /* K */, int) +CMC_COLLECTION_GENERATE(STACK, s, stack, /* K */, int) +CMC_COLLECTION_GENERATE(TREESET, ts, tset, /* K */, int) +CMC_COLLECTION_GENERATE(TREEMAP, tm, tmap, int, int) int main(void) { - struct bmap *bm = bm_new_custom(100, 0.7, NULL, NULL, NULL, NULL); - struct deque *d = d_new_custom(100, NULL, NULL); + struct bmap *bm = bm_new_custom(100, 0.7, &(struct bmap_ftab_key){ 0 }, + &(struct bmap_ftab_val){ 0 }, NULL, NULL); + struct deque *d = + d_new_custom(100, &(struct deque_ftab_val){ 0 }, NULL, NULL); struct hmap *hm = hm_new_custom(100, 0.6, NULL, NULL, NULL, NULL); struct hset *hs = hs_new_custom(100, 0.6, NULL, NULL, NULL, NULL); struct heap *h = h_new_custom(100, cmc_max_heap, NULL, NULL, NULL); @@ -52,8 +54,8 @@ int main(void) printf("%s\n", tm_to_string(tm).s); printf("%s\n", ts_to_string(ts).s); - bm_free(bm, NULL); - d_free(d, NULL); + bm_free(bm); + d_free(d); hm_free(hm, NULL); hs_free(hs, NULL); h_free(h, NULL);