More refactoring
This commit is contained in:
1 parent
0e6c44ac46
commit
f6bc58a3d7
59 files changed
+10562
-98
No files matched your search
+9
-5
@@ -3,11 +3,15 @@ CFLAGS=-Wall -fprofile-arcs -ftest-coverage -g -O0
|
||||
INCLUDE=-I ../src
|
||||
|
||||
main:
|
||||
$(CC) ./main.c $(INCLUDE) $(CFLAGS) -o $@.exe
|
||||
valgrind --leak-check=yes ./$@.exe
|
||||
mkdir -p ./out
|
||||
$(CC) ./main.c $(INCLUDE) $(CFLAGS) -o ./out/$@.exe
|
||||
valgrind --leak-check=yes ./out/$@.exe
|
||||
gcov -p ./out/$@.gcda
|
||||
|
||||
dev: main
|
||||
mkdir -p ./coverage
|
||||
cd ./coverage && gcov -p ../main.gcda
|
||||
mv *.gcov ./coverage
|
||||
|
||||
clean:
|
||||
rm ./*.gcda ./*.gcno ./*.exe -f
|
||||
rm ./coverage -rf
|
||||
rm ./*.gcov ./*.gcda ./*.gcno ./*.exe -f
|
||||
rm ./coverage ./out -rf
|
||||
+9
-9
@@ -3,9 +3,9 @@
|
||||
|
||||
// #include "unt_bitset.h"
|
||||
#include "unt_deque.h"
|
||||
// #include "unt_hashbidimap.h"
|
||||
// #include "unt_hashmap.h"
|
||||
// #include "unt_hashmultimap.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"
|
||||
@@ -56,12 +56,12 @@ int main(void)
|
||||
// cmc_run(CMCBitSetIter, units, tests);
|
||||
cmc_run(CMCDeque, units, tests);
|
||||
cmc_run(CMCDequeIter, units, tests);
|
||||
// cmc_run(CMCHashBidiMap, units, tests);
|
||||
// cmc_run(CMCHashBidiMapIter, units, tests);
|
||||
// cmc_run(CMCHashMap, units, tests);
|
||||
// cmc_run(CMCHashMapIter, units, tests);
|
||||
// cmc_run(CMCHashMultiMap, units, tests);
|
||||
// cmc_run(CMCHashMultiMapIter, units, tests);
|
||||
cmc_run(CMCHashBidiMap, units, tests);
|
||||
cmc_run(CMCHashBidiMapIter, units, tests);
|
||||
cmc_run(CMCHashMap, units, tests);
|
||||
cmc_run(CMCHashMapIter, units, tests);
|
||||
cmc_run(CMCHashMultiMap, units, tests);
|
||||
cmc_run(CMCHashMultiMapIter, units, tests);
|
||||
// cmc_run(CMCHashMultiSet, units, tests);
|
||||
// cmc_run(CMCHashMultiSetIter, units, tests);
|
||||
// cmc_run(CMCHashSet, units, tests);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#define SNAME deque
|
||||
#define CMC_EXT_DEQUE_INIT
|
||||
#define CMC_EXT_DEQUE_ITER
|
||||
#define CMC_EXT_DEQUE_STR
|
||||
#include "cmc/deque.h"
|
||||
|
||||
struct deque_fval *d_fval = &(struct deque_fval){
|
||||
|
||||
@@ -0,0 +1,818 @@
|
||||
#ifndef CMC_TESTS_UNT_HASHBIDIMAP_H
|
||||
#define CMC_TESTS_UNT_HASHBIDIMAP_H
|
||||
|
||||
#include "utl.h"
|
||||
|
||||
#define V size_t
|
||||
#define K size_t
|
||||
#define PFX hbm
|
||||
#define SNAME hashbidimap
|
||||
#define CMC_EXT_HASHBIDIMAP_INIT
|
||||
#define CMC_EXT_HASHBIDIMAP_ITER
|
||||
#define CMC_EXT_HASHBIDIMAP_STR
|
||||
#include "cmc/hashbidimap.h"
|
||||
|
||||
struct hashbidimap_fkey *hbm_fkey = &(struct hashbidimap_fkey){
|
||||
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
|
||||
};
|
||||
|
||||
struct hashbidimap_fval *hbm_fval = &(struct hashbidimap_fval){
|
||||
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
|
||||
};
|
||||
|
||||
struct hashbidimap_fkey *hbm_fkey_counter = &(struct hashbidimap_fkey){
|
||||
.cmp = k_c_cmp, .cpy = k_c_cpy, .str = k_c_str, .free = k_c_free, .hash = k_c_hash, .pri = k_c_pri
|
||||
};
|
||||
|
||||
struct hashbidimap_fval *hbm_fval_counter = &(struct hashbidimap_fval){
|
||||
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
|
||||
};
|
||||
|
||||
struct cmc_alloc_node *hbm_alloc_node =
|
||||
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
|
||||
|
||||
CMC_CREATE_UNIT(CMCHashBidiMap, true, {
|
||||
CMC_CREATE_TEST(PFX##_new(), {
|
||||
struct hashbidimap *map = hbm_new(943722, 0.6, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_not_equals(ptr, NULL, map->buffer);
|
||||
cmc_assert_equals(size_t, 0, map->count);
|
||||
cmc_assert_equals(double, 0.6, map->load);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, map->flag);
|
||||
cmc_assert_equals(ptr, hbm_fkey, map->f_key);
|
||||
cmc_assert_equals(ptr, hbm_fval, map->f_val);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.malloc, map->alloc->malloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.calloc, map->alloc->calloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.realloc, map->alloc->realloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.free, map->alloc->free);
|
||||
cmc_assert_equals(ptr, NULL, map->callbacks);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (943722 / 0.6), hbm_capacity(map));
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
map = hbm_new(100, 0.6, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 0; i < map->capacity; i++)
|
||||
{
|
||||
cmc_assert_equals(ptr, NULL, map->buffer[i][0]);
|
||||
cmc_assert_equals(ptr, NULL, map->buffer[i][1]);
|
||||
}
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
map = hbm_new(0, 0.6, hbm_fkey, hbm_fval);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new(UINT64_MAX, 0.99, hbm_fkey, hbm_fval);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new(1000, 0.6, hbm_fkey, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new(1000, 0.6, NULL, hbm_fval);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new(1000, 0.6, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new_custom(), {
|
||||
struct hashbidimap *map = hbm_new_custom(943722, 0.6, hbm_fkey, hbm_fval, hbm_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_not_equals(ptr, NULL, map->buffer);
|
||||
cmc_assert_equals(size_t, 0, map->count);
|
||||
cmc_assert_equals(double, 0.6, map->load);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, map->flag);
|
||||
cmc_assert_equals(ptr, hbm_fkey, map->f_key);
|
||||
cmc_assert_equals(ptr, hbm_fval, map->f_val);
|
||||
cmc_assert_equals(ptr, hbm_alloc_node, map->alloc);
|
||||
cmc_assert_equals(ptr, callbacks, map->callbacks);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (943722 / 0.6), hbm_capacity(map));
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
map = hbm_new_custom(0, 0.6, hbm_fkey, hbm_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new_custom(UINT64_MAX, 0.99, hbm_fkey, hbm_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new_custom(1000, 0.6, hbm_fkey, NULL, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new_custom(1000, 0.6, NULL, hbm_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
|
||||
map = hbm_new_custom(1000, 0.6, NULL, NULL, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_clear(), {
|
||||
k_total_free = 0;
|
||||
v_total_free = 0;
|
||||
struct hashbidimap *map = hbm_new(100, 0.6, hbm_fkey_counter, hbm_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
hbm_insert(map, i, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1000, map->count);
|
||||
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
hbm_clear(map);
|
||||
|
||||
cmc_assert_equals(size_t, 0, map->count);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, map->flag);
|
||||
cmc_assert_equals(int32_t, 1000, k_total_free);
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
|
||||
hbm_free(map);
|
||||
k_total_free = 0;
|
||||
v_total_free = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_free(), {
|
||||
k_total_free = 0;
|
||||
v_total_free = 0;
|
||||
struct hashbidimap *map = hbm_new(100, 0.6, hbm_fkey_counter, hbm_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
hbm_insert(map, i, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1000, map->count);
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
cmc_assert_equals(int32_t, 1000, k_total_free);
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
|
||||
map = hbm_new(1000, 0.6, hbm_fkey_counter, hbm_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_not_equals(ptr, NULL, map->buffer);
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
cmc_assert_equals(int32_t, 1000, k_total_free);
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
k_total_free = 0;
|
||||
v_total_free = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(customize, {
|
||||
struct hashbidimap *map = hbm_new_custom(100, 0.6, hbm_fkey, hbm_fval, NULL, NULL);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.malloc, map->alloc->malloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.calloc, map->alloc->calloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.realloc, map->alloc->realloc);
|
||||
cmc_assert_equals(ptr, cmc_alloc_node_default.free, map->alloc->free);
|
||||
cmc_assert_equals(ptr, NULL, map->callbacks);
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
struct cmc_alloc_node node;
|
||||
node.malloc = malloc;
|
||||
node.realloc = realloc;
|
||||
node.free = free;
|
||||
node.calloc = calloc;
|
||||
|
||||
map = hbm_new_custom(100, 0.6, hbm_fkey, hbm_fval, &node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert_equals(ptr, &node, map->alloc);
|
||||
cmc_assert_equals(ptr, callbacks, map->callbacks);
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(buffer_growth[capacity = 1], {
|
||||
struct hashbidimap *map = hbm_new(1, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 1; i <= 10000; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
size_t sum = 0;
|
||||
|
||||
for (size_t i = 0; i < hbm_capacity(map); i++)
|
||||
{
|
||||
if (map->buffer[i][0] && map->buffer[i][0] != CMC_ENTRY_DELETED)
|
||||
{
|
||||
sum += map->buffer[i][0]->key;
|
||||
}
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 50005000, sum);
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert, {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
cmc_assert(hbm_insert(map, 2, 2));
|
||||
cmc_assert(hbm_insert(map, 3, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, hbm_count(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[duplicate], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
cmc_assert(!hbm_insert(map, 1, 1));
|
||||
cmc_assert(!hbm_insert(map, 2, 1));
|
||||
cmc_assert(!hbm_insert(map, 1, 2));
|
||||
cmc_assert(hbm_insert(map, 2, 2));
|
||||
|
||||
cmc_assert_equals(size_t, 2, hbm_count(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[references], {
|
||||
struct hashbidimap *map = hbm_new(500, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 0; i < 500; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
for (size_t i = 0; i < map->capacity; i++)
|
||||
{
|
||||
struct hashbidimap_entry *kentry = map->buffer[i][0];
|
||||
struct hashbidimap_entry *ventry = map->buffer[i][1];
|
||||
|
||||
if (kentry && kentry != CMC_ENTRY_DELETED)
|
||||
cmc_assert_equals(ptr, kentry, *(kentry->ref[0]));
|
||||
if (ventry && ventry != CMC_ENTRY_DELETED)
|
||||
cmc_assert_equals(ptr, ventry, *(ventry->ref[1]));
|
||||
}
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[growth remove clear], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
cmc_assert_equals(size_t, 1000, hbm_count(map));
|
||||
|
||||
size_t j = 1;
|
||||
while (!hbm_empty(map))
|
||||
{
|
||||
if (hbm_count(map) % 2 == 0)
|
||||
cmc_assert(hbm_remove_by_key(map, j++, NULL, NULL));
|
||||
else
|
||||
cmc_assert(hbm_remove_by_val(map, j++, NULL, NULL));
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 0, hbm_count(map));
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
cmc_assert_equals(size_t, 1000, hbm_count(map));
|
||||
|
||||
hbm_clear(map);
|
||||
|
||||
cmc_assert_equals(size_t, 0, hbm_count(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[ftab hash calls], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey_counter, hbm_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
k_total_hash = 0;
|
||||
v_total_hash = 0;
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
cmc_assert_greater_equals(int32_t, 1000, k_total_hash);
|
||||
cmc_assert_greater_equals(int32_t, 1000, v_total_hash);
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
k_total_hash = 0;
|
||||
v_total_hash = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update_key, {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, hbm_count(map));
|
||||
|
||||
for (size_t i = 100; i > 0; i--)
|
||||
cmc_assert(hbm_update_key(map, i, i + 10));
|
||||
|
||||
cmc_assert_equals(size_t, 100, hbm_count(map));
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert_equals(size_t, i + 10, hbm_get_key(map, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, hbm_count(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update_key[empty], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert_equals(size_t, 0, hbm_count(map));
|
||||
|
||||
cmc_assert(!hbm_update_key(map, 1, 1));
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, hbm_flag(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update_key[not_found duplicate], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
|
||||
cmc_assert_equals(size_t, 1, hbm_count(map));
|
||||
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(hbm_update_key(map, 1, 1));
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
cmc_assert(!hbm_update_key(map, 2, 2));
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_insert(map, 2, 2));
|
||||
|
||||
cmc_assert(!hbm_update_key(map, 1, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
cmc_assert(!hbm_update_key(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_update_key(map, 2, 2));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update_val, {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, hbm_count(map));
|
||||
|
||||
for (size_t i = 100; i > 0; i--)
|
||||
cmc_assert(hbm_update_val(map, i, i + 10));
|
||||
|
||||
cmc_assert_equals(size_t, 100, hbm_count(map));
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert_equals(size_t, i + 10, hbm_get_val(map, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, hbm_count(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update_val[empty], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert_equals(size_t, 0, hbm_count(map));
|
||||
|
||||
cmc_assert(!hbm_update_val(map, 1, 1));
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, hbm_flag(map));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update_val[not_found duplicate], {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
|
||||
cmc_assert_equals(size_t, 1, hbm_count(map));
|
||||
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(hbm_update_val(map, 1, 1));
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
cmc_assert(!hbm_update_val(map, 2, 2));
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_insert(map, 2, 2));
|
||||
|
||||
cmc_assert(!hbm_update_val(map, 1, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
cmc_assert(!hbm_update_val(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_update_val(map, 2, 2));
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove_by_key[cleanup custom], {
|
||||
struct hashbidimap *map = hbm_new_custom(
|
||||
10000, 0.6, hbm_fkey, hbm_fval,
|
||||
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free },
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 0; i < 10000; i++)
|
||||
{
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 10000; i++)
|
||||
{
|
||||
cmc_assert(hbm_remove_by_key(map, i, NULL, NULL));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < map->capacity; i++)
|
||||
{
|
||||
struct hashbidimap_entry *k = map->buffer[i][0];
|
||||
struct hashbidimap_entry *v = map->buffer[i][1];
|
||||
|
||||
cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && (v == NULL || v == CMC_ENTRY_DELETED));
|
||||
}
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove_by_val[cleanup custom], {
|
||||
struct hashbidimap *map = hbm_new_custom(
|
||||
10000, 0.6, hbm_fkey, hbm_fval,
|
||||
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free },
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 0; i < 10000; i++)
|
||||
{
|
||||
cmc_assert(hbm_insert(map, i, i));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 10000; i++)
|
||||
{
|
||||
cmc_assert(hbm_remove_by_val(map, i, NULL, NULL));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < map->capacity; i++)
|
||||
{
|
||||
struct hashbidimap_entry *k = map->buffer[i][0];
|
||||
struct hashbidimap_entry *v = map->buffer[i][1];
|
||||
|
||||
cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && (v == NULL || v == CMC_ENTRY_DELETED));
|
||||
}
|
||||
|
||||
hbm_free(map);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(copy_of, {
|
||||
struct hashbidimap *map1 = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(hbm_insert(map1, i, i));
|
||||
|
||||
struct hashbidimap *map2 = hbm_copy_of(map1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map2);
|
||||
cmc_assert_equals(size_t, hbm_count(map1), hbm_count(map2));
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
{
|
||||
cmc_assert(hbm_contains_key(map2, i) && hbm_contains_key(map1, i));
|
||||
cmc_assert(hbm_contains_val(map2, i) && hbm_contains_val(map1, i));
|
||||
}
|
||||
|
||||
hbm_free(map1);
|
||||
hbm_free(map2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(equals, {
|
||||
struct hashbidimap *map1 = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
struct hashbidimap *map2 = hbm_new(1000, 0.9, hbm_fkey, hbm_fval);
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
{
|
||||
cmc_assert(hbm_insert(map1, i, i));
|
||||
cmc_assert(hbm_insert(map2, 99 - i, 99 - i));
|
||||
}
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map1);
|
||||
cmc_assert_not_equals(ptr, NULL, map2);
|
||||
cmc_assert_equals(size_t, hbm_count(map1), hbm_count(map2));
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
{
|
||||
cmc_assert(hbm_contains_key(map2, i) && hbm_contains_key(map1, i));
|
||||
cmc_assert(hbm_contains_val(map2, i) && hbm_contains_val(map1, i));
|
||||
}
|
||||
|
||||
hbm_free(map1);
|
||||
hbm_free(map2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(equals[from copy], {
|
||||
struct hashbidimap *map1 = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(hbm_insert(map1, i, i));
|
||||
|
||||
struct hashbidimap *map2 = hbm_copy_of(map1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map2);
|
||||
|
||||
cmc_assert(hbm_equals(map1, map2));
|
||||
|
||||
hbm_free(map1);
|
||||
hbm_free(map2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(flags, {
|
||||
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey, hbm_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
// customize
|
||||
hbm_customize(map, &cmc_alloc_node_default, &(struct cmc_callbacks){ 0 });
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
// Insert
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
cmc_assert(!hbm_insert(map, 1, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(!hbm_insert(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
|
||||
// clear
|
||||
hbm_clear(map);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
// hbm_update_key
|
||||
cmc_assert(!hbm_update_key(map, 1, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
cmc_assert(!hbm_update_key(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_insert(map, 2, 2));
|
||||
cmc_assert(!hbm_update_key(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(!hbm_update_key(map, 1, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
|
||||
// clear
|
||||
hbm_clear(map);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
// hbm_update_val
|
||||
cmc_assert(!hbm_update_val(map, 1, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 1));
|
||||
cmc_assert(!hbm_update_val(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_insert(map, 2, 2));
|
||||
cmc_assert(!hbm_update_val(map, 2, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(!hbm_update_val(map, 1, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_DUPLICATE, hbm_flag(map));
|
||||
|
||||
// remove_by_key
|
||||
cmc_assert(hbm_remove_by_key(map, 1, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
cmc_assert(!hbm_remove_by_key(map, 1, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_remove_by_key(map, 2, NULL, NULL));
|
||||
cmc_assert(!hbm_remove_by_key(map, 2, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, hbm_flag(map));
|
||||
|
||||
// remove_by_val
|
||||
cmc_assert(hbm_insert(map, 1, 1) && hbm_insert(map, 2, 2));
|
||||
|
||||
cmc_assert(hbm_remove_by_val(map, 1, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
cmc_assert(!hbm_remove_by_val(map, 1, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
cmc_assert(hbm_remove_by_val(map, 2, NULL, NULL));
|
||||
cmc_assert(!hbm_remove_by_val(map, 2, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, hbm_flag(map));
|
||||
|
||||
// get_key and get_val
|
||||
cmc_assert(hbm_insert(map, 2, 1));
|
||||
|
||||
cmc_assert_equals(size_t, 2, hbm_get_key(map, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert_equals(size_t, 1, hbm_get_val(map, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
cmc_assert_equals(size_t, 0, hbm_get_key(map, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert_equals(size_t, 0, hbm_get_val(map, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
|
||||
// contains_key
|
||||
cmc_assert(hbm_contains_key(map, 2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
// contains_val
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(hbm_contains_val(map, 1));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
|
||||
// copy_of
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
struct hashbidimap *map2 = hbm_copy_of(map);
|
||||
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map2));
|
||||
|
||||
size_t tmp = map->capacity;
|
||||
map->capacity = 0;
|
||||
|
||||
struct hashbidimap *map3 = hbm_copy_of(map);
|
||||
cmc_assert_equals(ptr, NULL, map3);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_ERROR, hbm_flag(map));
|
||||
|
||||
map->capacity = tmp;
|
||||
|
||||
// equals
|
||||
hbm_get_key(map, 100);
|
||||
hbm_get_key(map2, 100);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_NOT_FOUND, hbm_flag(map2));
|
||||
map->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(hbm_equals(map, map2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map2));
|
||||
|
||||
hbm_free(map);
|
||||
hbm_free(map2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct hashbidimap *map = hbm_new_custom(100, 0.7, hbm_fkey, hbm_fval, NULL, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_equals(ptr, callbacks, map->callbacks);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(hbm_insert(map, 10, 10));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(hbm_update_key(map, 10, 5));
|
||||
cmc_assert_equals(int32_t, 1, total_update);
|
||||
|
||||
cmc_assert(hbm_update_val(map, 5, 5));
|
||||
cmc_assert_equals(int32_t, 2, total_update);
|
||||
|
||||
cmc_assert(hbm_insert(map, 10, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert(hbm_remove_by_key(map, 5, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(hbm_remove_by_val(map, 10, NULL, NULL));
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
|
||||
cmc_assert(hbm_insert(map, 1, 2));
|
||||
cmc_assert_equals(int32_t, 3, total_create);
|
||||
|
||||
cmc_assert_equals(size_t, 1, hbm_get_key(map, 2));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert_equals(size_t, 2, hbm_get_val(map, 1));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(hbm_resize(map, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(hbm_resize(map, 200));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert(hbm_contains_key(map, 1));
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
|
||||
cmc_assert(hbm_contains_val(map, 2));
|
||||
cmc_assert_equals(int32_t, 4, total_read);
|
||||
|
||||
cmc_assert_equals(int32_t, 3, total_create);
|
||||
cmc_assert_equals(int32_t, 4, total_read);
|
||||
cmc_assert_equals(int32_t, 2, total_update);
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
hbm_customize(map, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, map->callbacks);
|
||||
|
||||
hbm_clear(map);
|
||||
cmc_assert(hbm_insert(map, 10, 10));
|
||||
cmc_assert(hbm_update_key(map, 10, 5));
|
||||
cmc_assert(hbm_update_val(map, 5, 5));
|
||||
cmc_assert(hbm_insert(map, 10, 10));
|
||||
cmc_assert(hbm_remove_by_key(map, 5, NULL, NULL));
|
||||
cmc_assert(hbm_remove_by_val(map, 10, NULL, NULL));
|
||||
cmc_assert(hbm_insert(map, 1, 2));
|
||||
cmc_assert_equals(size_t, 1, hbm_get_key(map, 2));
|
||||
cmc_assert_equals(size_t, 2, hbm_get_val(map, 1));
|
||||
cmc_assert(hbm_resize(map, 1000));
|
||||
cmc_assert(hbm_resize(map, 200));
|
||||
cmc_assert(hbm_contains_key(map, 1));
|
||||
cmc_assert(hbm_contains_val(map, 2));
|
||||
|
||||
cmc_assert_equals(int32_t, 3, total_create);
|
||||
cmc_assert_equals(int32_t, 4, total_read);
|
||||
cmc_assert_equals(int32_t, 2, total_update);
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, map->callbacks);
|
||||
|
||||
hbm_free(map);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
});
|
||||
});
|
||||
|
||||
CMC_CREATE_UNIT(CMCHashBidiMapIter, true, {
|
||||
CMC_CREATE_TEST(PFX##_iter_start, {
|
||||
// WIP
|
||||
cmc_assert(true);
|
||||
cmc_assert(true);
|
||||
});
|
||||
});
|
||||
|
||||
#endif /* CMC_TESTS_UNT_HASHBIDIMAP_H */
|
||||
+1333
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -9,6 +9,7 @@
|
||||
#define CMC_EXT_LIST_INIT
|
||||
#define CMC_EXT_LIST_ITER
|
||||
#define CMC_EXT_LIST_SEQ
|
||||
#define CMC_EXT_LIST_STR
|
||||
#include "cmc/list.h"
|
||||
|
||||
struct list_fval *l_fval = &(struct list_fval){
|
||||
|
||||
@@ -0,0 +1,958 @@
|
||||
#ifndef CMC_TESTS_UNT_QUEUE_H
|
||||
#define CMC_TESTS_UNT_QUEUE_H
|
||||
|
||||
#include "utl.h"
|
||||
|
||||
#define V size_t
|
||||
#define PFX q
|
||||
#define SNAME queue
|
||||
#define CMC_EXT_QUEUE_INIT
|
||||
#define CMC_EXT_QUEUE_ITER
|
||||
#define CMC_EXT_QUEUE_STR
|
||||
#include "cmc/queue.h"
|
||||
|
||||
struct queue_fval *q_fval = &(struct queue_fval){
|
||||
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
|
||||
};
|
||||
|
||||
CMC_CREATE_UNIT(CMCQueue, true, {
|
||||
CMC_CREATE_TEST(new, {
|
||||
struct queue *q = q_new(1000000, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
cmc_assert_not_equals(ptr, NULL, q->buffer);
|
||||
cmc_assert_equals(size_t, 1000000, q_capacity(q));
|
||||
cmc_assert_equals(size_t, 0, q_count(q));
|
||||
cmc_assert_not_equals(ptr, NULL, q->buffer);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(new[capacity = 0], {
|
||||
struct queue *q = q_new(0, q_fval);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(new[capacity = UINT64_MAX], {
|
||||
struct queue *q = q_new(UINT64_MAX, q_fval);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(clear[count capacity], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 0; i < 50; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert_equals(size_t, 50, q_count(q));
|
||||
|
||||
q_clear(q);
|
||||
|
||||
cmc_assert_equals(size_t, 0, q_count(q));
|
||||
cmc_assert_equals(size_t, 100, q_capacity(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(buffer_growth[capacity = 1], {
|
||||
struct queue *q = q_new(1, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 0; i < 50; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert_equals(size_t, 50, q_count(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(buffer_growth[item preservation], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 1; i <= 1200; i++)
|
||||
{
|
||||
q_enqueue(q, i);
|
||||
}
|
||||
|
||||
size_t sum = 0;
|
||||
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));
|
||||
cmc_assert_equals(size_t, 720600, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(enqueue[count capacity], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 0; i < 150; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert_equals(size_t, 150, q_count(q));
|
||||
cmc_assert_greater(size_t, 100, q_capacity(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(enqueue[item preservation], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
size_t sum = 0;
|
||||
|
||||
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, 5050, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(dequeue[count capacity], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
cmc_assert(q_dequeue(q));
|
||||
|
||||
cmc_assert_equals(size_t, 0, q_count(q));
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
cmc_assert(q_dequeue(q));
|
||||
|
||||
cmc_assert_equals(size_t, 0, q_count(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(dequeue[item preservation], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 200; i > 0; i--)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(q_dequeue(q));
|
||||
|
||||
size_t sum = 0;
|
||||
|
||||
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, 5050, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(peek[count = 0], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
size_t r = q_peek(q);
|
||||
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, r);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(peek[sum], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
size_t sum = 0;
|
||||
|
||||
for (size_t i = 0; i <= 100; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
while (!q_empty(q))
|
||||
{
|
||||
size_t r = q_peek(q);
|
||||
|
||||
sum += r;
|
||||
|
||||
cmc_assert(q_dequeue(q));
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 5050, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(contains, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 1; i <= 20; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
for (size_t i = 1; i <= 20; i++)
|
||||
cmc_assert(q_contains(q, i));
|
||||
|
||||
q_clear(q);
|
||||
|
||||
for (size_t i = 1; i <= 20; i++)
|
||||
cmc_assert(!q_contains(q, i));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(contains[count = 0], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert(!q_contains(q, 10));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(contains[count = 1], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert(!q_contains(q, 10));
|
||||
|
||||
cmc_assert(q_enqueue(q, 100));
|
||||
|
||||
cmc_assert(q_contains(q, 100));
|
||||
cmc_assert(!q_contains(q, 101));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(empty, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert(q_empty(q));
|
||||
|
||||
cmc_assert(q_enqueue(q, 1));
|
||||
|
||||
cmc_assert(!q_empty(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(full, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert(!q_full(q));
|
||||
|
||||
for (size_t i = 0; i != q_capacity(q); i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert(q_full(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(count, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert_equals(size_t, 0, q_count(q));
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert_equals(size_t, 100, q_count(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(capacity, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
cmc_assert_equals(size_t, 100, q_capacity(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(resize, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 0; i < 150; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert_equals(size_t, 150, q_count(q));
|
||||
cmc_assert(q_resize(q, q_count(q)));
|
||||
cmc_assert_equals(size_t, 150, q_capacity(q));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(resize[item preservation], {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 1; i <= 150; i++)
|
||||
cmc_assert(q_enqueue(q, i));
|
||||
|
||||
cmc_assert(q_resize(q, q_count(q)));
|
||||
|
||||
size_t sum = 0;
|
||||
|
||||
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, 11325, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(copy_of, {
|
||||
struct queue *q1 = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q1);
|
||||
|
||||
for (size_t i = 1; i <= 150; i++)
|
||||
cmc_assert(q_enqueue(q1, i));
|
||||
|
||||
struct queue *q2 = q_copy_of(q1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q2);
|
||||
cmc_assert_equals(size_t, q_count(q1), q_count(q2));
|
||||
|
||||
for (size_t i1 = q1->front, i2 = q2->front, j = 0; j < q1->count; j++)
|
||||
{
|
||||
cmc_assert_equals(size_t, q1->buffer[i1], q2->buffer[i2]);
|
||||
|
||||
i1 = (i1 + 1) % q1->capacity;
|
||||
i2 = (i2 + 1) % q2->capacity;
|
||||
}
|
||||
|
||||
q_free(q1);
|
||||
q_free(q2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(equals, {
|
||||
struct queue *q1 = q_new(100, q_fval);
|
||||
struct queue *q2 = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q1);
|
||||
cmc_assert_not_equals(ptr, NULL, q2);
|
||||
|
||||
for (size_t i = 1; i <= 150; i++)
|
||||
{
|
||||
cmc_assert(q_enqueue(q1, i));
|
||||
cmc_assert(q_enqueue(q2, i));
|
||||
}
|
||||
|
||||
cmc_assert(q_equals(q1, q2));
|
||||
|
||||
q_free(q1);
|
||||
q_free(q2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(equals[from copy], {
|
||||
struct queue *q1 = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q1);
|
||||
|
||||
for (size_t i = 1; i <= 150; i++)
|
||||
{
|
||||
cmc_assert(q_enqueue(q1, i));
|
||||
}
|
||||
|
||||
struct queue *q2 = q_copy_of(q1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q2);
|
||||
|
||||
cmc_assert(q_equals(q1, q2));
|
||||
|
||||
q_free(q1);
|
||||
q_free(q2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(equals[count = 0], {
|
||||
struct queue *q1 = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q1);
|
||||
|
||||
struct queue *q2 = q_copy_of(q1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q2);
|
||||
|
||||
cmc_assert(q_equals(q1, q2));
|
||||
|
||||
q_free(q1);
|
||||
q_free(q2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(flag, {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
// clear
|
||||
q->flag = CMC_FLAG_ERROR;
|
||||
q_clear(q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
// customize
|
||||
q->flag = CMC_FLAG_ERROR;
|
||||
q_customize(q, NULL, NULL);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
// enqueue
|
||||
q->flag = CMC_FLAG_ERROR;
|
||||
q_enqueue(q, 10);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
// dequeue
|
||||
q->flag = CMC_FLAG_ERROR;
|
||||
q_dequeue(q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
q_dequeue(q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, q_flag(q));
|
||||
|
||||
// peek
|
||||
q->flag = CMC_FLAG_ERROR;
|
||||
q_peek(q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, q_flag(q));
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
q_peek(q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
// contains
|
||||
q->flag = CMC_FLAG_ERROR;
|
||||
cmc_assert(q_contains(q, 10));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
|
||||
// copy_of
|
||||
q->flag = CMC_FLAG_OK;
|
||||
struct queue *q2 = q_copy_of(q);
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q2));
|
||||
|
||||
// equals
|
||||
q->flag = CMC_FLAG_OK;
|
||||
q2->flag = CMC_FLAG_OK;
|
||||
cmc_assert(q_equals(q, q2));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q));
|
||||
cmc_assert_equals(int32_t, CMC_FLAG_OK, q_flag(q2));
|
||||
|
||||
q_free(q);
|
||||
q_free(q2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct queue *q = q_new_custom(100, q_fval, NULL, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(q_dequeue(q));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert_equals(size_t, 10, q_peek(q));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert(q_contains(q, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(q_resize(q, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(q_resize(q, 100));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
cmc_assert_equals(int32_t, 0, total_update);
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
q_customize(q, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, q->callbacks);
|
||||
|
||||
q_clear(q);
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
cmc_assert(q_dequeue(q));
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
cmc_assert_equals(size_t, 10, q_peek(q));
|
||||
cmc_assert(q_contains(q, 10));
|
||||
cmc_assert(q_resize(q, 1000));
|
||||
cmc_assert(q_resize(q, 100));
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
cmc_assert_equals(int32_t, 0, total_update);
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, q->callbacks);
|
||||
|
||||
q_free(q);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
});
|
||||
});
|
||||
|
||||
CMC_CREATE_UNIT(CMCQueueIter, true, {
|
||||
CMC_CREATE_TEST(PFX##_iter_start(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
cmc_assert_equals(ptr, q, it.target);
|
||||
cmc_assert_equals(size_t, q->front, it.cursor);
|
||||
cmc_assert_equals(size_t, 0, it.index);
|
||||
cmc_assert_equals(bool, true, it.start);
|
||||
cmc_assert_equals(bool, true, it.end);
|
||||
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
|
||||
cmc_assert(q_enqueue(q, 1));
|
||||
cmc_assert(q_enqueue(q, 2));
|
||||
|
||||
it = q_iter_start(q);
|
||||
|
||||
cmc_assert_equals(size_t, 0, it.index);
|
||||
cmc_assert_equals(bool, false, it.end);
|
||||
cmc_assert_equals(size_t, q->front, it.cursor);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_end(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
|
||||
cmc_assert_equals(ptr, q, it.target);
|
||||
cmc_assert_equals(size_t, q->back, it.cursor);
|
||||
cmc_assert_equals(size_t, 0, it.index);
|
||||
cmc_assert_equals(bool, true, it.start);
|
||||
cmc_assert_equals(bool, true, it.end);
|
||||
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
|
||||
cmc_assert(q_enqueue(q, 1));
|
||||
cmc_assert(q_enqueue(q, 2));
|
||||
cmc_assert(q_enqueue(q, 3));
|
||||
|
||||
it = q_iter_end(q);
|
||||
|
||||
cmc_assert_equals(size_t, q->count - 1, it.index);
|
||||
cmc_assert_equals(size_t, q->back - 1, it.cursor);
|
||||
cmc_assert_equals(bool, false, it.start);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_at_start(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
// Empty checks
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
it = q_iter_end(q);
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
|
||||
// Non-empty check
|
||||
cmc_assert(q_enqueue(q, 1));
|
||||
it = q_iter_end(q);
|
||||
cmc_assert(!q_iter_at_start(&it));
|
||||
it = q_iter_start(q);
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_at_end(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
// Empty check
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
it = q_iter_end(q);
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
|
||||
// Non-empty check
|
||||
cmc_assert(q_enqueue(q, 1));
|
||||
it = q_iter_end(q);
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
it = q_iter_start(q);
|
||||
cmc_assert(!q_iter_at_end(&it));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_to_start(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
cmc_assert(!q_iter_to_start(&it));
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
cmc_assert_equals(size_t, 100, q->count);
|
||||
|
||||
it = q_iter_end(q);
|
||||
|
||||
cmc_assert(!q_iter_at_start(&it));
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 100, q_iter_value(&it));
|
||||
|
||||
cmc_assert(q_iter_to_start(&it));
|
||||
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
cmc_assert(!q_iter_at_end(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 1, q_iter_value(&it));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_to_end(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
|
||||
cmc_assert(!q_iter_to_end(&it));
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
it = q_iter_start(q);
|
||||
|
||||
cmc_assert(q_iter_at_start(&it));
|
||||
cmc_assert(!q_iter_at_end(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 1, q_iter_value(&it));
|
||||
|
||||
cmc_assert(q_iter_to_end(&it));
|
||||
|
||||
cmc_assert(!q_iter_at_start(&it));
|
||||
cmc_assert(q_iter_at_end(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 100, q_iter_value(&it));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_next(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
cmc_assert(!q_iter_next(&it));
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = q_iter_start(q); !q_iter_at_end(&it); q_iter_next(&it))
|
||||
{
|
||||
sum += q_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
|
||||
q_iter_to_start(&it);
|
||||
do
|
||||
{
|
||||
sum += q_iter_value(&it);
|
||||
} while (q_iter_next(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_prev(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
|
||||
cmc_assert(!q_iter_prev(&it));
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = q_iter_end(q); !q_iter_at_start(&it); q_iter_prev(&it))
|
||||
{
|
||||
sum += q_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
|
||||
q_iter_to_end(&it);
|
||||
do
|
||||
{
|
||||
sum += q_iter_value(&it);
|
||||
} while (q_iter_prev(&it));
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_advance(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
cmc_assert(!q_iter_advance(&it, 1));
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
it = q_iter_start(q);
|
||||
|
||||
cmc_assert(!q_iter_advance(&it, 0));
|
||||
cmc_assert(!q_iter_advance(&it, q->count));
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = q_iter_start(q);;)
|
||||
{
|
||||
sum += q_iter_value(&it);
|
||||
|
||||
if (!q_iter_advance(&it, 2))
|
||||
break;
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 250500, sum);
|
||||
|
||||
q_iter_to_start(&it);
|
||||
cmc_assert(q_iter_advance(&it, q->count - 1));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_rewind(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
|
||||
cmc_assert(!q_iter_rewind(&it, 1));
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
it = q_iter_end(q);
|
||||
|
||||
cmc_assert(!q_iter_rewind(&it, 0));
|
||||
cmc_assert(!q_iter_rewind(&it, q->count));
|
||||
|
||||
size_t sum = 0;
|
||||
for (it = q_iter_end(q);;)
|
||||
{
|
||||
sum += q_iter_value(&it);
|
||||
|
||||
if (!q_iter_rewind(&it, 2))
|
||||
break;
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 250500, sum);
|
||||
|
||||
q_iter_to_end(&it);
|
||||
cmc_assert(q_iter_rewind(&it, q->count - 1));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_go_to(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
cmc_assert(!q_iter_go_to(&it, 0));
|
||||
|
||||
it = q_iter_start(q);
|
||||
cmc_assert(!q_iter_go_to(&it, 0));
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
it = q_iter_start(q);
|
||||
|
||||
size_t sum = 0;
|
||||
for (size_t i = 0; i < 1001; i++)
|
||||
{
|
||||
q_iter_go_to(&it, i);
|
||||
|
||||
sum += q_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
for (size_t i = 1001; i > 0; i--)
|
||||
{
|
||||
cmc_assert(q_iter_go_to(&it, i - 1));
|
||||
|
||||
sum += q_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 500500, sum);
|
||||
|
||||
sum = 0;
|
||||
for (size_t i = 0; i < 1001; i += 100)
|
||||
{
|
||||
cmc_assert(q_iter_go_to(&it, i));
|
||||
cmc_assert_equals(size_t, i, q_iter_index(&it));
|
||||
|
||||
sum += q_iter_value(&it);
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, 5500, sum);
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_value(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, q_iter_value(&it));
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
|
||||
it = q_iter_start(q);
|
||||
|
||||
cmc_assert_equals(size_t, 10, q_iter_value(&it));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_rvalue(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
struct queue_iter it = q_iter_end(q);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, q_iter_rvalue(&it));
|
||||
|
||||
cmc_assert(q_enqueue(q, 10));
|
||||
|
||||
it = q_iter_start(q);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q_iter_rvalue(&it));
|
||||
cmc_assert_equals(size_t, 10, *q_iter_rvalue(&it));
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_iter_index(), {
|
||||
struct queue *q = q_new(100, q_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 0; i <= 1000; i++)
|
||||
q_enqueue(q, i);
|
||||
|
||||
struct queue_iter it = q_iter_start(q);
|
||||
|
||||
for (size_t i = 0; i < 1001; i++)
|
||||
{
|
||||
cmc_assert_equals(size_t, i, q_iter_index(&it));
|
||||
q_iter_next(&it);
|
||||
}
|
||||
|
||||
it = q_iter_end(q);
|
||||
for (size_t i = 1001; i > 0; i--)
|
||||
{
|
||||
cmc_assert_equals(size_t, i - 1, q_iter_index(&it));
|
||||
q_iter_prev(&it);
|
||||
}
|
||||
|
||||
q_free(q);
|
||||
});
|
||||
});
|
||||
|
||||
#endif /* CMC_TESTS_UNT_QUEUE_H */
|
||||
@@ -8,6 +8,7 @@
|
||||
#define SNAME stack
|
||||
#define CMC_EXT_STACK_INIT
|
||||
#define CMC_EXT_STACK_ITER
|
||||
#define CMC_EXT_STACK_STR
|
||||
#include "cmc/stack.h"
|
||||
|
||||
struct stack_fval *s_fval = &(struct stack_fval){
|
||||
|
||||
Reference in new issue
Block a user