More refactoring
This commit is contained in:
1 parent
c1070c4858
commit
e4d58db474
34 files changed
+5295
-74
No files matched your search
+1
-1
@@ -1,5 +1,5 @@
|
||||
CC=gcc
|
||||
CFLAGS=-Wall -Wextra -Werror -fprofile-arcs -ftest-coverage -g -O0
|
||||
CFLAGS=-Wall -Wextra -Werror -fprofile-arcs -ftest-coverage -ftime-report -g -O0
|
||||
INCLUDE=-I ../src
|
||||
|
||||
main:
|
||||
|
||||
+12
-12
@@ -1,18 +1,18 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// #include "unt_bitset.h"
|
||||
#include "unt_bitset.h"
|
||||
#include "unt_deque.h"
|
||||
#include "unt_hashbidimap.h"
|
||||
#include "unt_hashmap.h"
|
||||
#include "unt_hashmultimap.h"
|
||||
#include "unt_hashmultiset.h"
|
||||
#include "unt_hashset.h"
|
||||
// #include "unt_heap.h"
|
||||
// #include "unt_intervalheap.h"
|
||||
#include "unt_heap.h"
|
||||
#include "unt_intervalheap.h"
|
||||
// #include "unt_linkedlist.h"
|
||||
#include "unt_list.h"
|
||||
// #include "unt_queue.h"
|
||||
#include "unt_queue.h"
|
||||
// #include "unt_sortedlist.h"
|
||||
#include "unt_stack.h"
|
||||
// #include "unt_treemap.h"
|
||||
@@ -52,8 +52,8 @@ int main(void)
|
||||
cmc_timer_start(timer);
|
||||
uintmax_t tests = 0, units = 0;
|
||||
|
||||
// cmc_run(CMCBitSet, units, tests);
|
||||
// cmc_run(CMCBitSetIter, units, tests);
|
||||
cmc_run(CMCBitSet, units, tests);
|
||||
cmc_run(CMCBitSetIter, units, tests);
|
||||
cmc_run(CMCDeque, units, tests);
|
||||
cmc_run(CMCDequeIter, units, tests);
|
||||
cmc_run(CMCHashBidiMap, units, tests);
|
||||
@@ -66,16 +66,16 @@ int main(void)
|
||||
cmc_run(CMCHashMultiSetIter, units, tests);
|
||||
cmc_run(CMCHashSet, units, tests);
|
||||
cmc_run(CMCHashSetIter, units, tests);
|
||||
// cmc_run(CMCHeap, units, tests);
|
||||
// cmc_run(CMCHeapIter, units, tests);
|
||||
// cmc_run(CMCIntervalHeap, units, tests);
|
||||
// cmc_run(CMCIntervalHeapIter, units, tests);
|
||||
cmc_run(CMCHeap, units, tests);
|
||||
cmc_run(CMCHeapIter, units, tests);
|
||||
cmc_run(CMCIntervalHeap, units, tests);
|
||||
cmc_run(CMCIntervalHeapIter, units, tests);
|
||||
// cmc_run(CMCLinkedList, units, tests);
|
||||
// cmc_run(CMCLinkedListIter, units, tests);
|
||||
cmc_run(CMCList, units, tests);
|
||||
cmc_run(CMCListIter, units, tests);
|
||||
// cmc_run(CMCQueue, units, tests);
|
||||
// cmc_run(CMCQueueIter, units, tests);
|
||||
cmc_run(CMCQueue, units, tests);
|
||||
cmc_run(CMCQueueIter, units, tests);
|
||||
// cmc_run(CMCSortedList, units, tests);
|
||||
// cmc_run(CMCSortedListIter, units, tests);
|
||||
cmc_run(CMCStack, units, tests);
|
||||
|
||||
@@ -0,0 +1,454 @@
|
||||
#ifndef CMC_TESTS_UNT_BITSET_H
|
||||
#define CMC_TESTS_UNT_BITSET_H
|
||||
|
||||
#include "utl.h"
|
||||
|
||||
#define PFX bs
|
||||
#define SNAME bitset
|
||||
#define CMC_EXT_BITSET_INIT
|
||||
#define CMC_EXT_BITSET_ITER
|
||||
#define CMC_EXT_BITSET_STR
|
||||
#include "cmc/bitset.h"
|
||||
|
||||
struct cmc_alloc_node *bs_alloc_node =
|
||||
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
|
||||
|
||||
CMC_CREATE_UNIT(CMCBitSet, true, {
|
||||
const size_t word_bits = sizeof(cmc_bitset_word) * CHAR_BIT;
|
||||
|
||||
printf("%s", cmc_test_color(5));
|
||||
printf(" BitSet<" CMC_TO_STRING(CMC_BITSET_WORD_TYPE) ">\n");
|
||||
printf("%s", cmc_test_color(0));
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new(), {
|
||||
struct bitset *bs = bs_new(64);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_not_equals(ptr, NULL, bs->buffer);
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(63) + 1, bs->capacity);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.malloc, bs->alloc->malloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.calloc, bs->alloc->calloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.realloc, bs->alloc->realloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.free, bs->alloc->free);
|
||||
cmc_assert_equals(ptr, NULL, bs->callbacks);
|
||||
|
||||
bs_free(bs);
|
||||
|
||||
bs = bs_new(129);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(128) + 1, bs->capacity);
|
||||
|
||||
bs_free(bs);
|
||||
|
||||
bs = bs_new(0);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new_custom(), {
|
||||
struct bitset *bs = bs_new_custom(64, bs_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_not_equals(ptr, NULL, bs->buffer);
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(63) + 1, bs->capacity);
|
||||
cmc_assert_equals(ptr, bs_alloc_node, bs->alloc);
|
||||
cmc_assert_equals(ptr, callbacks, bs->callbacks);
|
||||
|
||||
bs_free(bs);
|
||||
|
||||
bs = bs_new_custom(129, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(128) + 1, bs->capacity);
|
||||
bs_free(bs);
|
||||
|
||||
bs = bs_new_custom(0, bs_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_init(), {
|
||||
struct bitset bs = bs_init(64);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs.buffer);
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(63) + 1, bs.capacity);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.malloc, bs.alloc->malloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.calloc, bs.alloc->calloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.realloc, bs.alloc->realloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.free, bs.alloc->free);
|
||||
cmc_assert_equals(ptr, NULL, bs.callbacks);
|
||||
|
||||
bs_release(bs);
|
||||
|
||||
bs = bs_init(129);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(128) + 1, bs.capacity);
|
||||
|
||||
bs_release(bs);
|
||||
|
||||
bs = bs_init(0);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, bs.buffer);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_init_custom(), {
|
||||
struct bitset bs = bs_init_custom(64, bs_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs.buffer);
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(63) + 1, bs.capacity);
|
||||
cmc_assert_equals(ptr, bs_alloc_node, bs.alloc);
|
||||
cmc_assert_equals(ptr, callbacks, bs.callbacks);
|
||||
|
||||
bs_release(bs);
|
||||
|
||||
bs = bs_init_custom(129, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_bidx_to_widx(128) + 1, bs.capacity);
|
||||
|
||||
bs_release(bs);
|
||||
|
||||
bs = bs_init_custom(0, bs_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, bs.buffer);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_customize(), {
|
||||
struct bitset *bs = bs_new(word_bits);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
|
||||
bs->flag = CMC_FLAG_ERROR;
|
||||
bs_customize(bs, bs_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_equals(ptr, bs_alloc_node, bs->alloc);
|
||||
cmc_assert_equals(ptr, callbacks, bs->callbacks);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, bs->flag);
|
||||
|
||||
bs->flag = CMC_FLAG_ERROR;
|
||||
bs_customize(bs, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.malloc, bs->alloc->malloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.calloc, bs->alloc->calloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.realloc, bs->alloc->realloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.free, bs->alloc->free);
|
||||
cmc_assert_equals(ptr, NULL, bs->callbacks);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, bs->flag);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_set(), {
|
||||
struct bitset *bs = bs_new(word_bits);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
bs->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(bs_set(bs, 0));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 1, bs->buffer[0]);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, bs->flag);
|
||||
|
||||
bs->buffer[0] = 0;
|
||||
|
||||
cmc_assert(bs_set(bs, word_bits - 1));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 1)), bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_set(bs, word_bits * 8 - 1));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 1)),
|
||||
bs->buffer[cmc_bidx_to_widx(word_bits * 8 - 1)]);
|
||||
|
||||
cmc_assert(bs_set(bs, word_bits * 18 - 2));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 2)),
|
||||
bs->buffer[cmc_bidx_to_widx(word_bits * 18 - 2)]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_set_range(), {
|
||||
struct bitset *bs = bs_new(word_bits);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
bs->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(bs_set_range(bs, 0, 0));
|
||||
cmc_assert_equals(uint8_t, 1, bs->buffer[0]);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, bs->flag);
|
||||
|
||||
cmc_assert(!bs_set_range(bs, 1, 0));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_INVALID, bs->flag);
|
||||
|
||||
bs->buffer[0] = 0;
|
||||
|
||||
cmc_assert(bs_set_range(bs, 4, 7)); /* 1111 0000 */
|
||||
cmc_assert_equals(uint8_t, 240, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_set_range(bs, 2, 2)); /* 1111 0100 */
|
||||
cmc_assert_equals(uint8_t, 244, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_set_range(bs, word_bits * 6 - 2, word_bits * 6 - 2));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 2)),
|
||||
bs->buffer[cmc_bidx_to_widx(word_bits * 6 - 2)]);
|
||||
|
||||
// Make sure we have at least word_bits * 40 slots
|
||||
bs_set(bs, word_bits * 40);
|
||||
|
||||
cmc_assert(bs_set_range(bs, word_bits * 20, word_bits * 21 - 1));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, (~(cmc_bitset_word)0), bs->buffer[cmc_bidx_to_widx(word_bits * 20)]);
|
||||
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[cmc_bidx_to_widx(word_bits * 20 - 1)]);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[cmc_bidx_to_widx(word_bits * 21 + 1)]);
|
||||
|
||||
cmc_assert(bs_set_range(bs, 0, word_bits * 100 - 1));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_clear(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
bs->buffer[0] = 1; // 0001
|
||||
cmc_assert(bs_clear(bs, 0));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
bs->buffer[0] = 10; // 1010
|
||||
cmc_assert(bs_clear(bs, 1));
|
||||
cmc_assert(bs_clear(bs, 3));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_clear(bs, word_bits * 100 - 1));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_clear_range(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(!bs_clear_range(bs, 1, 0));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_INVALID, bs->flag);
|
||||
|
||||
cmc_assert(bs_clear_range(bs, 0, word_bits * 100 - 1));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
cmc_assert(bs_set_range(bs, word_bits * 20, word_bits * 21 - 1));
|
||||
cmc_assert(bs_clear_range(bs, word_bits * 20, word_bits * 21 - 1));
|
||||
|
||||
cmc_assert_equals(uint32_t, 0, bs->buffer[cmc_bidx_to_widx(word_bits * 20)]);
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
bs->buffer[i] = ~((cmc_bitset_word)0);
|
||||
|
||||
cmc_assert(bs_set_range(bs, 0, word_bits * 100 - 1));
|
||||
cmc_assert(bs_clear_range(bs, 0, word_bits * 100 - 1));
|
||||
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(size_t, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_clear_range(bs, 0, word_bits * 200 - 1));
|
||||
cmc_assert_equals(size_t, 200, bs->capacity);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_flip(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip(bs, 0));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 1, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip(bs, 0));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_set_range(bs, 0, 2)); // 0111
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 7, bs->buffer[0]);
|
||||
cmc_assert(bs_flip(bs, 1)); // 0101
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 5, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip(bs, word_bits * 100 - 1));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_flip_range(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip_range(bs, 0, 0));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 1, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip_range(bs, 0, 0));
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip_range(bs, 1, 4)); // 0001 1110
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 30, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip_range(bs, 2, 3)); // 0001 0010
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 18, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_flip_range(bs, 0, 2)); // 0001 0101
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 21, bs->buffer[0]);
|
||||
|
||||
bs->buffer[0] = 0;
|
||||
|
||||
cmc_assert(bs_flip_range(bs, 0, word_bits * 100 - 1));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_put(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_put(bs, 1, true)); // 0010
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 2, bs->buffer[0]);
|
||||
cmc_assert(bs_put(bs, 0, false)); // 0010
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 2, bs->buffer[0]);
|
||||
cmc_assert(bs_put(bs, 1, false)); // 0000
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
cmc_assert(bs_put(bs, 3, true)); // 1000
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 8, bs->buffer[0]);
|
||||
cmc_assert(bs_put(bs, 0, true)); // 1001
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 9, bs->buffer[0]);
|
||||
cmc_assert(bs_put(bs, 3, false)); // 0001
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 1, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_put(bs, word_bits * 100 - 1, true));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_put_range(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_put_range(bs, 1, 2, true)); // 0110
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 6, bs->buffer[0]);
|
||||
cmc_assert(bs_put_range(bs, 0, 1, false)); // 0100
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 4, bs->buffer[0]);
|
||||
cmc_assert(bs_put_range(bs, 1, 3, true)); // 1110
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 14, bs->buffer[0]);
|
||||
cmc_assert(bs_put_range(bs, 2, 3, false)); // 0010
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 2, bs->buffer[0]);
|
||||
|
||||
cmc_assert(bs_put_range(bs, 0, word_bits * 100 - 1, true));
|
||||
cmc_assert_equals(size_t, 100, bs->capacity);
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
|
||||
|
||||
cmc_assert(bs_put_range(bs, 0, word_bits * 100 - 1, false));
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[i]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_set_all(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
|
||||
cmc_assert(bs_set_all(bs));
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_clear_all(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
|
||||
cmc_assert(bs_set_all(bs));
|
||||
cmc_assert(bs_clear_all(bs));
|
||||
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[i]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_flip_all(), {
|
||||
struct bitset *bs = bs_new(word_bits * 10 - 1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
|
||||
cmc_assert(bs_flip_all(bs));
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
|
||||
|
||||
cmc_assert(bs_flip_all(bs));
|
||||
for (size_t i = 0; i < bs->capacity; i++)
|
||||
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[i]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_get(), {
|
||||
struct bitset *bs = bs_new(64);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, bs);
|
||||
|
||||
cmc_assert(bs_set(bs, 1));
|
||||
|
||||
cmc_assert(bs_get(bs, 1));
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_resize(), {
|
||||
struct bitset *bs = bs_new(64);
|
||||
|
||||
// WIP
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
});
|
||||
|
||||
CMC_CREATE_UNIT(CMCBitSetIter, true, {
|
||||
// const size_t word_bytes = sizeof(cmc_bitset_word);
|
||||
// const size_t word_bits = sizeof(cmc_bitset_word) * CHAR_BIT;
|
||||
|
||||
printf("%s", cmc_test_color(5));
|
||||
printf(" BitSet<" CMC_TO_STRING(CMC_BITSET_WORD_TYPE) ">\n");
|
||||
printf("%s", cmc_test_color(0));
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_start(), {
|
||||
struct bitset *bs = bs_new(64);
|
||||
|
||||
// WIP
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
});
|
||||
|
||||
#endif /* CMC_TESTS_UNT_BITSET_H */
|
||||
@@ -308,6 +308,7 @@ CMC_CREATE_UNIT(CMCHashMultiMap, true, {
|
||||
cmc_assert(hmm_equals(map, map2));
|
||||
|
||||
hmm_free(map);
|
||||
hmm_free(map2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_equals(), {
|
||||
@@ -1013,19 +1014,4 @@ CMC_CREATE_UNIT(CMCHashMultiMapIter, true, {
|
||||
});
|
||||
});
|
||||
|
||||
#ifdef CMC_TEST_MAIN
|
||||
int main(void)
|
||||
{
|
||||
int result = CMCHashMultiMap() + CMCHashMultiMapIter();
|
||||
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n");
|
||||
printf(" | CMCHashMultiMap Suit : %-41s |\n", result == 0 ? "PASSED" : "FAILED");
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n\n\n");
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMC_TESTS_UNT_HASHMULTIMAP_H */
|
||||
@@ -853,19 +853,4 @@ CMC_CREATE_UNIT(CMCHashMultiSetIter, true, {
|
||||
});
|
||||
});
|
||||
|
||||
#ifdef CMC_TEST_MAIN
|
||||
int main(void)
|
||||
{
|
||||
int result = CMCHashMultiSet() + CMCHashMultiSetIter();
|
||||
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n");
|
||||
printf(" | CMCHashMultiSet Suit : %-41s |\n", result == 0 ? "PASSED" : "FAILED");
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n\n\n");
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMC_TESTS_UNT_HASHMULTISET_H */
|
||||
@@ -1033,19 +1033,4 @@ CMC_CREATE_UNIT(CMCHashSetIter, true, {
|
||||
});
|
||||
});
|
||||
|
||||
#ifdef CMC_TEST_MAIN
|
||||
int main(void)
|
||||
{
|
||||
int result = CMCHashSet() + CMCHashSetIter();
|
||||
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n");
|
||||
printf(" | CMCHashSet Suit : %-46s |\n", result == 0 ? "PASSED" : "FAILED");
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n\n\n");
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMC_TESTS_UNT_HASHSET_H */
|
||||
@@ -0,0 +1,720 @@
|
||||
#ifndef CMC_TESTS_UNT_HEAP_H
|
||||
#define CMC_TESTS_UNT_HEAP_H
|
||||
|
||||
#include "utl.h"
|
||||
|
||||
#define V size_t
|
||||
#define PFX h
|
||||
#define SNAME heap
|
||||
#define CMC_EXT_HEAP_INIT
|
||||
#define CMC_EXT_HEAP_ITER
|
||||
#define CMC_EXT_HEAP_STR
|
||||
#include "cmc/heap.h"
|
||||
|
||||
struct heap_fval *h_fval = &(struct heap_fval){
|
||||
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
|
||||
};
|
||||
|
||||
CMC_CREATE_UNIT(CMCHeap, true, {
|
||||
CMC_CREATE_TEST(new, {
|
||||
struct heap *h = h_new(1000000, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
cmc_assert_not_equals(ptr, NULL, h->buffer);
|
||||
cmc_assert_equals(size_t, 1000000, h_capacity(h));
|
||||
cmc_assert_equals(size_t, 0, h_count(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(new[edge_case capacity = 0], {
|
||||
struct heap *h = h_new(0, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(new[edge_case capacity = UINT64_MAX], {
|
||||
struct heap *h = h_new(UINT64_MAX, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(clear[count capacity], {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 0; i < 50; i++)
|
||||
h_insert(h, i);
|
||||
|
||||
cmc_assert_equals(size_t, 50, h_count(h));
|
||||
|
||||
h_clear(h);
|
||||
|
||||
cmc_assert_equals(size_t, 0, h_count(h));
|
||||
cmc_assert_equals(size_t, 100, h_capacity(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(buffer_growth[edge_case capacity = 1], {
|
||||
struct heap *h = h_new(1, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 0; i < 50; i++)
|
||||
cmc_assert(h_insert(h, i));
|
||||
|
||||
cmc_assert_equals(size_t, 50, h_count(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert, {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
cmc_assert(h_insert(h, 10));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[item preservation], {
|
||||
struct heap *h = h_new(50, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 0; i <= 100; i++)
|
||||
cmc_assert(h_insert(h, i));
|
||||
|
||||
size_t sum = 0;
|
||||
for (size_t i = 0; i < h_count(h); i++)
|
||||
sum += h->buffer[i];
|
||||
|
||||
cmc_assert_equals(size_t, 5050, sum);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove, {
|
||||
struct heap *h = h_new(50, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 0; i <= 100; i++)
|
||||
cmc_assert(h_insert(h, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, h_peek(h));
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert_equals(size_t, 99, h_peek(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[count = 0], {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
cmc_assert(!h_remove(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[sorted], {
|
||||
struct heap *h = h_new(100, CMC_MIN_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(h_insert(h, i));
|
||||
|
||||
size_t i = 0;
|
||||
size_t arr[100];
|
||||
while (!h_empty(h))
|
||||
{
|
||||
arr[i++] = h_peek(h);
|
||||
cmc_assert(h_remove(h));
|
||||
}
|
||||
|
||||
cmc_assert_array_sorted_any(size_t, arr, cmc_size_cmp, 0, 99);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(peek, {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(h_insert(h, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, h_peek(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(peek[count = 0], {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, h_peek(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(flags, {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
|
||||
h->flag = CMC_FLAG_ERROR;
|
||||
|
||||
// insert
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
|
||||
h->flag = CMC_FLAG_ERROR;
|
||||
|
||||
// remove
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
|
||||
cmc_assert(!h_remove(h));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, h_flag(h));
|
||||
|
||||
// peek
|
||||
cmc_assert(h_insert(h, 10));
|
||||
h_peek(h);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
cmc_assert(h_remove(h));
|
||||
h_peek(h);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, h_flag(h));
|
||||
|
||||
// clear
|
||||
h_clear(h);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
|
||||
h->flag = CMC_FLAG_ERROR;
|
||||
|
||||
// contains
|
||||
h_contains(h, 10);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
|
||||
h->flag = CMC_FLAG_ERROR;
|
||||
|
||||
// copy of
|
||||
struct heap *h2 = h_copy_of(h);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h2));
|
||||
|
||||
h->flag = CMC_FLAG_ERROR;
|
||||
h2->flag = CMC_FLAG_ERROR;
|
||||
|
||||
// equals
|
||||
cmc_assert(h_equals(h, h2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, h_flag(h2));
|
||||
|
||||
h_free(h);
|
||||
h_free(h2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct heap *h = h_new_custom(100, CMC_MAX_HEAP, h_fval, NULL, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert_equals(size_t, 10, h_peek(h));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert(h_contains(h, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(h_resize(h, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(h_resize(h, 100));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
cmc_assert_equals(int32_t, 0, total_update);
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
h_customize(h, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, h->callbacks);
|
||||
|
||||
h_clear(h);
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert_equals(size_t, 10, h_peek(h));
|
||||
cmc_assert(h_contains(h, 10));
|
||||
cmc_assert(h_resize(h, 1000));
|
||||
cmc_assert(h_resize(h, 100));
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
cmc_assert_equals(int32_t, 0, total_update);
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, h->callbacks);
|
||||
|
||||
h_free(h);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
});
|
||||
});
|
||||
|
||||
CMC_CREATE_UNIT(CMCHeapIter, true, {
|
||||
CMC_CREATE_TEST(PFX##_iter_start(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
cmc_assert_equals(ptr, h, it.target);
|
||||
cmc_assert_equals(size_t, 0, it.cursor);
|
||||
cmc_assert_equals(bool, true, it.start);
|
||||
cmc_assert_equals(bool, true, it.end);
|
||||
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
|
||||
cmc_assert(h_insert(h, 1));
|
||||
cmc_assert(h_insert(h, 2));
|
||||
|
||||
it = h_iter_start(h);
|
||||
|
||||
cmc_assert_equals(size_t, 0, it.cursor);
|
||||
cmc_assert_equals(bool, false, it.end);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_end(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_end(h);
|
||||
|
||||
cmc_assert_equals(ptr, h, it.target);
|
||||
cmc_assert_equals(size_t, 0, it.cursor);
|
||||
cmc_assert_equals(bool, true, it.start);
|
||||
cmc_assert_equals(bool, true, it.end);
|
||||
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
|
||||
cmc_assert(h_insert(h, 1));
|
||||
cmc_assert(h_insert(h, 2));
|
||||
cmc_assert(h_insert(h, 3));
|
||||
|
||||
it = h_iter_end(h);
|
||||
|
||||
cmc_assert_equals(size_t, h->count - 1, it.cursor);
|
||||
cmc_assert_equals(bool, false, it.start);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_at_start(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
// Empty checks
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
it = h_iter_end(h);
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
|
||||
// Non-empty check
|
||||
cmc_assert(h_insert(h, 1));
|
||||
it = h_iter_end(h);
|
||||
cmc_assert(!h_iter_at_start(&it));
|
||||
it = h_iter_start(h);
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_at_end(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
// Empty check
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
it = h_iter_end(h);
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
|
||||
// Non-empty check
|
||||
cmc_assert(h_insert(h, 1));
|
||||
it = h_iter_end(h);
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
it = h_iter_start(h);
|
||||
cmc_assert(!h_iter_at_end(&it));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_to_start(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
cmc_assert(!h_iter_to_start(&it));
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
h_insert(h, i);
|
||||
else
|
||||
h_insert(h, i);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 100, h->count);
|
||||
|
||||
it = h_iter_end(h);
|
||||
cmc_assert(!h_iter_at_start(&it));
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
cmc_assert_equals(size_t, 99, it.cursor);
|
||||
|
||||
cmc_assert(h_iter_to_start(&it));
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
cmc_assert(!h_iter_at_end(&it));
|
||||
cmc_assert_equals(size_t, 0, it.cursor);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_to_end(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_end(h);
|
||||
|
||||
cmc_assert(!h_iter_to_end(&it));
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
h_insert(h, i);
|
||||
else
|
||||
h_insert(h, i);
|
||||
}
|
||||
|
||||
it = h_iter_start(h);
|
||||
cmc_assert(h_iter_at_start(&it));
|
||||
cmc_assert(!h_iter_at_end(&it));
|
||||
|
||||
cmc_assert(h_iter_to_end(&it));
|
||||
cmc_assert(!h_iter_at_start(&it));
|
||||
cmc_assert(h_iter_at_end(&it));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_next(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
cmc_assert(!h_iter_next(&it));
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
h_insert(h, i);
|
||||
else
|
||||
h_insert(h, i);
|
||||
}
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = h_iter_start(h); !h_iter_at_end(&it); h_iter_next(&it))
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
|
||||
h_iter_to_start(&it);
|
||||
do
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
} while (h_iter_next(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_prev(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_end(h);
|
||||
|
||||
cmc_assert(!h_iter_prev(&it));
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
h_insert(h, i);
|
||||
else
|
||||
h_insert(h, i);
|
||||
}
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = h_iter_end(h); !h_iter_at_start(&it); h_iter_prev(&it))
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
|
||||
h_iter_to_end(&it);
|
||||
do
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
} while (h_iter_prev(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_advance(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
cmc_assert(!h_iter_advance(&it, 1));
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
h_insert(h, i);
|
||||
|
||||
it = h_iter_start(h);
|
||||
|
||||
cmc_assert(!h_iter_advance(&it, 0));
|
||||
cmc_assert(!h_iter_advance(&it, h->count));
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = h_iter_start(h);;)
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
|
||||
if (!h_iter_advance(&it, 2))
|
||||
break;
|
||||
|
||||
cmc_assert_equals(size_t, 0, h_iter_index(&it) % 2);
|
||||
}
|
||||
|
||||
cmc_assert(h_iter_to_start(&it));
|
||||
cmc_assert(h_iter_next(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 1, h_iter_index(&it));
|
||||
|
||||
for (;;)
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
|
||||
if (!h_iter_advance(&it, 2))
|
||||
break;
|
||||
|
||||
cmc_assert_equals(size_t, 1, h_iter_index(&it) % 2);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
h_iter_to_start(&it);
|
||||
cmc_assert(h_iter_advance(&it, h->count - 1));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_rewind(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_end(h);
|
||||
|
||||
cmc_assert(!h_iter_rewind(&it, 1));
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
h_insert(h, i);
|
||||
|
||||
it = h_iter_end(h);
|
||||
|
||||
cmc_assert(!h_iter_rewind(&it, 0));
|
||||
cmc_assert(!h_iter_rewind(&it, h->count));
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = h_iter_end(h);;)
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
|
||||
if (!h_iter_rewind(&it, 2))
|
||||
break;
|
||||
|
||||
cmc_assert_equals(size_t, 0, h_iter_index(&it) % 2);
|
||||
}
|
||||
|
||||
cmc_assert(h_iter_to_end(&it));
|
||||
cmc_assert(h_iter_prev(&it));
|
||||
|
||||
cmc_assert_equals(size_t, h->count - 2, h_iter_index(&it));
|
||||
|
||||
for (;;)
|
||||
{
|
||||
sum += h_iter_value(&it);
|
||||
|
||||
if (!h_iter_rewind(&it, 2))
|
||||
break;
|
||||
|
||||
cmc_assert_equals(size_t, 1, h_iter_index(&it) % 2);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
h_iter_to_end(&it);
|
||||
cmc_assert(h_iter_rewind(&it, h->count - 1));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_go_to(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_end(h);
|
||||
cmc_assert(!h_iter_go_to(&it, 0));
|
||||
|
||||
it = h_iter_start(h);
|
||||
cmc_assert(!h_iter_go_to(&it, 0));
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
h_insert(h, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1001, h->count);
|
||||
|
||||
it = h_iter_start(h);
|
||||
|
||||
size_t sum = 0;
|
||||
for (size_t i = 0; i < 1001; i++)
|
||||
{
|
||||
h_iter_go_to(&it, i);
|
||||
|
||||
sum += h_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
for (size_t i = 1001; i > 0; i--)
|
||||
{
|
||||
cmc_assert(h_iter_go_to(&it, i - 1));
|
||||
|
||||
sum += h_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_value(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
struct heap_iter it = h_iter_end(h);
|
||||
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, h_iter_value(&it));
|
||||
|
||||
cmc_assert(h_insert(h, 1));
|
||||
cmc_assert(h_insert(h, 3));
|
||||
cmc_assert(h_insert(h, 2));
|
||||
|
||||
it = h_iter_start(h);
|
||||
|
||||
cmc_assert_equals(size_t, 3, h_iter_value(&it));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_index(), {
|
||||
struct heap *h = h_new(100, CMC_MAX_HEAP, h_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
h_insert(h, i);
|
||||
|
||||
struct heap_iter it = h_iter_start(h);
|
||||
|
||||
for (size_t i = 0; i < 1001; i++)
|
||||
{
|
||||
cmc_assert_equals(size_t, i, h_iter_index(&it));
|
||||
h_iter_next(&it);
|
||||
}
|
||||
|
||||
it = h_iter_end(h);
|
||||
for (size_t i = 1001; i > 0; i--)
|
||||
{
|
||||
cmc_assert_equals(size_t, i - 1, h_iter_index(&it));
|
||||
h_iter_prev(&it);
|
||||
}
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
});
|
||||
|
||||
#endif /* CMC_TESTS_UNT_HEAP_H */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -837,19 +837,4 @@ CMC_CREATE_UNIT(CMCStackIter, true, {
|
||||
});
|
||||
});
|
||||
|
||||
#ifdef CMC_TEST_MAIN
|
||||
int main(void)
|
||||
{
|
||||
int result = CMCStack() + CMCStackIter();
|
||||
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n");
|
||||
printf(" | CMCStack Suit : %-48s |\n", result == 0 ? "PASSED" : "FAILED");
|
||||
printf(" +---------------------------------------------------------------+");
|
||||
printf("\n\n\n");
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMC_TESTS_UNT_STACK_H */
|
||||
Reference in new issue
Block a user