Renamed multiset to hashmultiset
This commit is contained in:
1 parent
c41ce371d3
commit
254bb3eacc
9 files changed
+1607
-525
No files matched your search
+1
-1
@@ -30,12 +30,12 @@ collections = [
|
||||
{'t': 'HASHBIDIMAP', 'h': '"cmc/hashbidimap.h"', 'pfx': 'hbm', 'sname': 'hashbidimap', 'key': 'size_t', 'val': 'size_t'},
|
||||
{'t': 'HASHMAP', 'h': '"cmc/hashmap.h"', 'pfx': 'hm', 'sname': 'hashmap', 'key': 'size_t', 'val': 'size_t'},
|
||||
{'t': 'HASHMULTIMAP', 'h': '"cmc/hashmultimap.h"', 'pfx': 'hmm', 'sname': 'hashmultimap', 'key': 'size_t', 'val': 'size_t'},
|
||||
{'t': 'HASHMULTISET', 'h': '"cmc/hashmultiset.h"', 'pfx': 'hms', 'sname': 'hashmultiset', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'HASHSET', 'h': '"cmc/hashset.h"', 'pfx': 'hs', 'sname': 'hashset', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'HEAP', 'h': '"cmc/heap.h"', 'pfx': 'h', 'sname': 'heap', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'INTERVALHEAP', 'h': '"cmc/intervalheap.h"', 'pfx': 'ih', 'sname': 'intervalheap', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'LINKEDLIST', 'h': '"cmc/linkedlist.h"', 'pfx': 'll', 'sname': 'linkedlist', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'LIST', 'h': '"cmc/list.h"', 'pfx': 'l', 'sname': 'list', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'MULTISET', 'h': '"cmc/multiset.h"', 'pfx': 'ms', 'sname': 'multiset', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'QUEUE', 'h': '"cmc/queue.h"', 'pfx': 'q', 'sname': 'queue', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'SORTEDLIST', 'h': '"cmc/sortedlist.h"', 'pfx': 'sl', 'sname': 'sortedlist', 'key': '', 'val': 'size_t'},
|
||||
{'t': 'STACK', 'h': '"cmc/stack.h"', 'pfx': 's', 'sname': 'stack', 'key': '', 'val': 'size_t'},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* multiset.h
|
||||
* hashmultiset.h
|
||||
*
|
||||
* Creation Date: 10/04/2019
|
||||
*
|
||||
@@ -9,17 +9,22 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* MultiSet
|
||||
* HashMultiSet
|
||||
*
|
||||
* In mathematics, a multiset is a modification of the concept of a set that,
|
||||
* unlike a set, allows for multiple instances for each of its elements. The
|
||||
* positive integer number of instances, given for each element is called the
|
||||
* multiplicity of this element in the multiset. A MultiSet also has a
|
||||
* multiplicity of this element in the multiset. A multiSet also has a
|
||||
* cardinality which equals the sum of the multiplicities of its elements.
|
||||
*
|
||||
* Implementation
|
||||
*
|
||||
* In this implementation, the values are stored in a flat hashtable and are
|
||||
* mapped to their multiplicity.
|
||||
*/
|
||||
|
||||
#ifndef CMC_MULTISET_H
|
||||
#define CMC_MULTISET_H
|
||||
#ifndef CMC_HASHMULTISET_H
|
||||
#define CMC_HASHMULTISET_H
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Core functionalities of the C Macro Collections Library
|
||||
@@ -32,37 +37,37 @@
|
||||
#include "../cor/hashtable.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* MultiSet specific
|
||||
* HashMultiSet specific
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* to_string format */
|
||||
static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
"at %p { "
|
||||
"buffer:%p, "
|
||||
"capacity:%" PRIuMAX ", "
|
||||
"count:%" PRIuMAX ", "
|
||||
"cardinality:%" PRIuMAX ", "
|
||||
"load:%lf, "
|
||||
"flag:%d, "
|
||||
"f_val:%p, "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
static const char *cmc_string_fmt_hashmultiset = "struct %s<%s> "
|
||||
"at %p { "
|
||||
"buffer:%p, "
|
||||
"capacity:%" PRIuMAX ", "
|
||||
"count:%" PRIuMAX ", "
|
||||
"cardinality:%" PRIuMAX ", "
|
||||
"load:%lf, "
|
||||
"flag:%d, "
|
||||
"f_val:%p, "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
#define CMC_GENERATE_MULTISET(PFX, SNAME, V) \
|
||||
CMC_GENERATE_MULTISET_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_MULTISET_SOURCE(PFX, SNAME, V)
|
||||
#define CMC_GENERATE_HASHMULTISET(PFX, SNAME, V) \
|
||||
CMC_GENERATE_HASHMULTISET_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_HASHMULTISET_SOURCE(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_MULTISET_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_MULTISET_HEADER(PFX, SNAME, V)
|
||||
#define CMC_WRAPGEN_HASHMULTISET_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_HASHMULTISET_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_MULTISET_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_MULTISET_SOURCE(PFX, SNAME, V)
|
||||
#define CMC_WRAPGEN_HASHMULTISET_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_HASHMULTISET_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Header
|
||||
* ------------------------------------------------------------------------- */
|
||||
#define CMC_GENERATE_MULTISET_HEADER(PFX, SNAME, V) \
|
||||
#define CMC_GENERATE_HASHMULTISET_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* Hashset Structure */ \
|
||||
/* HashMultiset Structure */ \
|
||||
struct SNAME \
|
||||
{ \
|
||||
/* Array of Entries */ \
|
||||
@@ -92,10 +97,10 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the hashset */ \
|
||||
/* Returns an iterator to the start of the hashmultiset */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the hashset */ \
|
||||
/* Returns an iterator to the end of the hashmultiset */ \
|
||||
struct SNAME##_iter (*it_end)(struct SNAME *); \
|
||||
}; \
|
||||
\
|
||||
@@ -107,8 +112,8 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
/* The element's multiplicity */ \
|
||||
size_t multiplicity; \
|
||||
\
|
||||
/* The distance of this node to its original position, used by \
|
||||
* robin-hood hashing */ \
|
||||
/* The distance of this node to its original position, used by */ \
|
||||
/* robin-hood hashing */ \
|
||||
size_t dist; \
|
||||
\
|
||||
/* The sate of this node (DELETED, EMPTY, FILLED) */ \
|
||||
@@ -137,10 +142,10 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
int (*pri)(V, V); \
|
||||
}; \
|
||||
\
|
||||
/* Hashset Iterator */ \
|
||||
/* HashMultiset Iterator */ \
|
||||
struct SNAME##_iter \
|
||||
{ \
|
||||
/* Target Hashset */ \
|
||||
/* Target HashMultiset */ \
|
||||
struct SNAME *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
@@ -240,7 +245,7 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
/* -------------------------------------------------------------------------
|
||||
* Source
|
||||
* ------------------------------------------------------------------------- */
|
||||
#define CMC_GENERATE_MULTISET_SOURCE(PFX, SNAME, V) \
|
||||
#define CMC_GENERATE_HASHMULTISET_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static size_t PFX##_impl_multiplicity_of(struct SNAME *_set_, V value); \
|
||||
@@ -867,7 +872,7 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
struct cmc_string str; \
|
||||
struct SNAME *s_ = _set_; \
|
||||
\
|
||||
int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_multiset, \
|
||||
int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_hashmultiset, \
|
||||
#SNAME, #V, s_, s_->buffer, s_->capacity, s_->count, \
|
||||
s_->cardinality, s_->load, s_->flag, s_->f_val, \
|
||||
s_->alloc, s_->callbacks); \
|
||||
@@ -1537,4 +1542,4 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_MULTISET_H */
|
||||
#endif /* CMC_HASHMULTISET_H */
|
||||
+2
-2
@@ -5,12 +5,12 @@
|
||||
#include "unt/hashbidimap.c"
|
||||
#include "unt/hashmap.c"
|
||||
#include "unt/hashmultimap.c"
|
||||
#include "unt/hashmultiset.c"
|
||||
#include "unt/hashset.c"
|
||||
#include "unt/heap.c"
|
||||
#include "unt/intervalheap.c"
|
||||
#include "unt/linkedlist.c"
|
||||
#include "unt/list.c"
|
||||
#include "unt/multiset.c"
|
||||
#include "unt/queue.c"
|
||||
#include "unt/sortedlist.c"
|
||||
#include "unt/stack.c"
|
||||
@@ -55,12 +55,12 @@ int main(void)
|
||||
cmc_run(HashMap, units, tests);
|
||||
cmc_run(HashMapIter, units, tests);
|
||||
cmc_run(HashMultiMap, units, tests);
|
||||
cmc_run(HashMultiSet, units, tests);
|
||||
cmc_run(HashSet, units, tests);
|
||||
cmc_run(Heap, units, tests);
|
||||
cmc_run(IntervalHeap, units, tests);
|
||||
cmc_run(LinkedList, units, tests);
|
||||
cmc_run(List, units, tests);
|
||||
cmc_run(MultiSet, units, tests);
|
||||
cmc_run(Queue, units, tests);
|
||||
cmc_run(SortedList, units, tests);
|
||||
cmc_run(Stack, units, tests);
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -215,7 +215,8 @@ CMC_CREATE_UNIT(BitSet, true, {
|
||||
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[bs_bit_to_index(word_bits * 20)]);
|
||||
cmc_assert_equals(uint32_t, 0,
|
||||
bs->buffer[bs_bit_to_index(word_bits * 20)]);
|
||||
|
||||
bs_free(bs);
|
||||
});
|
||||
|
||||
@@ -4,33 +4,37 @@
|
||||
|
||||
#include "../src/hashbidimap.c"
|
||||
|
||||
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_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_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_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 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
|
||||
@@ -83,8 +87,8 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new_custom(), {
|
||||
struct hashbidimap *map = hbm_new_custom(943722, 0.6, hbm_fkey, hbm_fval,
|
||||
hbm_alloc_node, callbacks);
|
||||
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);
|
||||
@@ -466,11 +470,11 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
|
||||
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 });
|
||||
&(struct cmc_alloc_node){ .malloc = malloc,
|
||||
.calloc = calloc,
|
||||
.realloc = realloc,
|
||||
.free = free },
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
@@ -499,11 +503,11 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
|
||||
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 });
|
||||
&(struct cmc_alloc_node){ .malloc = malloc,
|
||||
.calloc = calloc,
|
||||
.realloc = realloc,
|
||||
.free = free },
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
@@ -598,7 +602,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
|
||||
|
||||
// customize
|
||||
hbm_customize(map, &cmc_alloc_node_default,
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hbm_flag(map));
|
||||
|
||||
// Insert
|
||||
|
||||
@@ -4,35 +4,37 @@
|
||||
|
||||
#include "../src/hashmultimap.c"
|
||||
|
||||
struct hashmultimap_fkey *hmm_fkey = &(struct hashmultimap_fkey){ .cmp = cmc_size_cmp,
|
||||
.cpy = NULL,
|
||||
.str = cmc_size_str,
|
||||
.free = NULL,
|
||||
.hash = cmc_size_hash,
|
||||
.pri = cmc_size_cmp };
|
||||
struct hashmultimap_fkey *hmm_fkey =
|
||||
&(struct hashmultimap_fkey){ .cmp = cmc_size_cmp,
|
||||
.cpy = NULL,
|
||||
.str = cmc_size_str,
|
||||
.free = NULL,
|
||||
.hash = cmc_size_hash,
|
||||
.pri = cmc_size_cmp };
|
||||
|
||||
struct hashmultimap_fval *hmm_fval = &(struct hashmultimap_fval){ .cmp = cmc_size_cmp,
|
||||
.cpy = NULL,
|
||||
.str = cmc_size_str,
|
||||
.free = NULL,
|
||||
.hash = cmc_size_hash,
|
||||
.pri = cmc_size_cmp };
|
||||
struct hashmultimap_fval *hmm_fval =
|
||||
&(struct hashmultimap_fval){ .cmp = cmc_size_cmp,
|
||||
.cpy = NULL,
|
||||
.str = cmc_size_str,
|
||||
.free = NULL,
|
||||
.hash = cmc_size_hash,
|
||||
.pri = cmc_size_cmp };
|
||||
|
||||
struct hashmultimap_fkey *hmm_fkey_counter =
|
||||
&(struct hashmultimap_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 };
|
||||
.cpy = k_c_cpy,
|
||||
.str = k_c_str,
|
||||
.free = k_c_free,
|
||||
.hash = k_c_hash,
|
||||
.pri = k_c_pri };
|
||||
|
||||
struct hashmultimap_fval *hmm_fval_counter =
|
||||
&(struct hashmultimap_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 };
|
||||
.cpy = v_c_cpy,
|
||||
.str = v_c_str,
|
||||
.free = v_c_free,
|
||||
.hash = v_c_hash,
|
||||
.pri = v_c_pri };
|
||||
|
||||
struct cmc_alloc_node *hmm_alloc_node = &(struct cmc_alloc_node){
|
||||
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
|
||||
@@ -85,8 +87,8 @@ CMC_CREATE_UNIT(HashMultiMap, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new_custom(), {
|
||||
struct hashmultimap *map = hmm_new_custom(943722, 0.6, hmm_fkey, hmm_fval,
|
||||
hmm_alloc_node, callbacks);
|
||||
struct hashmultimap *map = hmm_new_custom(
|
||||
943722, 0.6, hmm_fkey, hmm_fval, hmm_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_not_equals(ptr, NULL, map->buffer);
|
||||
|
||||
@@ -0,0 +1,426 @@
|
||||
#include "utl.c"
|
||||
#include "utl/assert.h"
|
||||
#include "utl/test.h"
|
||||
|
||||
#include "../src/hashmultiset.c"
|
||||
|
||||
struct hashmultiset_fval *hms_fval =
|
||||
&(struct hashmultiset_fval){ .cmp = cmc_size_cmp,
|
||||
.cpy = NULL,
|
||||
.str = cmc_size_str,
|
||||
.free = NULL,
|
||||
.hash = cmc_size_hash,
|
||||
.pri = cmc_size_cmp };
|
||||
|
||||
struct hashmultiset_fval *hms_fval_counter =
|
||||
&(struct hashmultiset_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 *hms_alloc_node = &(struct cmc_alloc_node){
|
||||
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
|
||||
};
|
||||
|
||||
CMC_CREATE_UNIT(HashMultiSet, true, {
|
||||
CMC_CREATE_TEST(PFX##_new(), {
|
||||
struct hashmultiset *set = hms_new(943722, 0.6, hms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_not_equals(ptr, NULL, set->buffer);
|
||||
cmc_assert_equals(size_t, 0, set->count);
|
||||
cmc_assert_equals(double, 0.6, set->load);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, set->flag);
|
||||
cmc_assert_equals(ptr, hms_fval, set->f_val);
|
||||
cmc_assert_equals(ptr, &cmc_alloc_node_default, set->alloc);
|
||||
cmc_assert_equals(ptr, NULL, set->callbacks);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (943722 / 0.6), hms_capacity(set));
|
||||
|
||||
hms_free(set);
|
||||
|
||||
set = hms_new(0, 0.6, hms_fval);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = hms_new(UINT64_MAX, 0.99, hms_fval);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = hms_new(1000, 0.6, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = hms_new(1000, 0.6, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new_custom(), {
|
||||
struct hashmultiset *set =
|
||||
hms_new_custom(943722, 0.6, hms_fval, hms_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_not_equals(ptr, NULL, set->buffer);
|
||||
cmc_assert_equals(size_t, 0, set->count);
|
||||
cmc_assert_equals(double, 0.6, set->load);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, set->flag);
|
||||
cmc_assert_equals(ptr, hms_fval, set->f_val);
|
||||
cmc_assert_equals(ptr, hms_alloc_node, set->alloc);
|
||||
cmc_assert_equals(ptr, callbacks, set->callbacks);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (943722 / 0.6), hms_capacity(set));
|
||||
|
||||
hms_free(set);
|
||||
|
||||
set = hms_new_custom(0, 0.6, hms_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = hms_new_custom(UINT64_MAX, 0.99, hms_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = hms_new_custom(1000, 0.6, NULL, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_clear(), {
|
||||
v_total_free = 0;
|
||||
struct hashmultiset *set = hms_new(100, 0.6, hms_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
hms_insert(set, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1000, set->count);
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
hms_clear(set);
|
||||
|
||||
cmc_assert_equals(size_t, 0, set->count);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, set->flag);
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
|
||||
hms_free(set);
|
||||
v_total_free = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_free(), {
|
||||
v_total_free = 0;
|
||||
struct hashmultiset *set = hms_new(100, 0.6, hms_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
hms_insert(set, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1000, set->count);
|
||||
|
||||
hms_free(set);
|
||||
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
|
||||
set = hms_new(1000, 0.6, hms_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_not_equals(ptr, NULL, set->buffer);
|
||||
|
||||
hms_free(set);
|
||||
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
v_total_free = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[count cardinality multiplicity], {
|
||||
struct hashmultiset *set = hms_new(100, 0.6, hms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(hms_insert(set, 1));
|
||||
cmc_assert(hms_insert(set, 2));
|
||||
cmc_assert(hms_insert(set, 2));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, hms_count(set));
|
||||
cmc_assert_equals(size_t, 6, hms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i <= 3; i++)
|
||||
cmc_assert_equals(size_t, i, hms_multiplicity_of(set, i));
|
||||
|
||||
hms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[count cardinality multiplicity], {
|
||||
struct hashmultiset *set = hms_new(100, 0.6, hms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(hms_insert(set, 1));
|
||||
cmc_assert(hms_insert(set, 2));
|
||||
cmc_assert(hms_insert(set, 2));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, hms_count(set));
|
||||
cmc_assert_equals(size_t, 6, hms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i <= 3; i++)
|
||||
cmc_assert_equals(size_t, i, hms_multiplicity_of(set, i));
|
||||
|
||||
cmc_assert(hms_remove(set, 1));
|
||||
cmc_assert(hms_remove(set, 2));
|
||||
cmc_assert(hms_remove(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 2, hms_count(set));
|
||||
cmc_assert_equals(size_t, 3, hms_cardinality(set));
|
||||
|
||||
cmc_assert_equals(size_t, 0, hms_multiplicity_of(set, 0));
|
||||
cmc_assert_equals(size_t, 0, hms_multiplicity_of(set, 1));
|
||||
cmc_assert_equals(size_t, 1, hms_multiplicity_of(set, 2));
|
||||
cmc_assert_equals(size_t, 2, hms_multiplicity_of(set, 3));
|
||||
|
||||
hms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove_all[count cardinality multiplicity], {
|
||||
struct hashmultiset *set = hms_new(100, 0.6, hms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(hms_insert(set, 1));
|
||||
cmc_assert(hms_insert(set, 2));
|
||||
cmc_assert(hms_insert(set, 2));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
cmc_assert(hms_insert(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, hms_count(set));
|
||||
cmc_assert_equals(size_t, 6, hms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i <= 3; i++)
|
||||
cmc_assert_equals(size_t, i, hms_multiplicity_of(set, i));
|
||||
|
||||
cmc_assert(hms_remove_all(set, 1));
|
||||
cmc_assert(hms_remove_all(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 1, hms_count(set));
|
||||
cmc_assert_equals(size_t, 2, hms_cardinality(set));
|
||||
|
||||
cmc_assert_equals(size_t, 0, hms_multiplicity_of(set, 0));
|
||||
cmc_assert_equals(size_t, 0, hms_multiplicity_of(set, 1));
|
||||
cmc_assert_equals(size_t, 2, hms_multiplicity_of(set, 2));
|
||||
cmc_assert_equals(size_t, 0, hms_multiplicity_of(set, 3));
|
||||
|
||||
hms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(multiplicity, {
|
||||
struct hashmultiset *set = hms_new(50, 0.6, hms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 0; i < 1000; i++)
|
||||
cmc_assert(hms_insert(set, i % 20));
|
||||
|
||||
cmc_assert_equals(size_t, 20, hms_count(set));
|
||||
cmc_assert_equals(size_t, 1000, hms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i < 20; i++)
|
||||
cmc_assert_equals(size_t, 50, hms_multiplicity_of(set, i));
|
||||
|
||||
hms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(flags, {
|
||||
struct hashmultiset *set = hms_new(100, 0.6, hms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// clear
|
||||
set->flag = cmc_flags.ERROR;
|
||||
hms_clear(set);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// customize
|
||||
set->flag = cmc_flags.ERROR;
|
||||
hms_customize(set, NULL, NULL);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// insert
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_insert(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_insert(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// insert_many
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_insert_many(set, 1, 0));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_insert_many(set, 1, 2));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// update
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_update(set, 1, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_update(set, 10, 0));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// remove
|
||||
cmc_assert(!hms_remove(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.NOT_FOUND, hms_flag(set));
|
||||
|
||||
cmc_assert(hms_remove(set, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// remove_all
|
||||
cmc_assert_equals(size_t, 0, hms_remove_all(set, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, hms_flag(set));
|
||||
|
||||
cmc_assert(hms_insert_many(set, 1, 10));
|
||||
cmc_assert_equals(size_t, 0, hms_remove_all(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.NOT_FOUND, hms_flag(set));
|
||||
|
||||
cmc_assert_equals(size_t, 10, hms_remove_all(set, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// max min
|
||||
cmc_assert(!hms_max(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, hms_flag(set));
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!hms_min(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, hms_flag(set));
|
||||
|
||||
cmc_assert(hms_insert(set, 1));
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_max(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_min(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// multiplicity_of
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert_equals(size_t, 0, hms_multiplicity_of(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// contains
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!hms_contains(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
|
||||
// copy_of
|
||||
set->flag = cmc_flags.ERROR;
|
||||
struct hashmultiset *set2 = hms_copy_of(set);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set2));
|
||||
|
||||
// equals
|
||||
set->flag = cmc_flags.ERROR;
|
||||
set2->flag = cmc_flags.ERROR;
|
||||
cmc_assert(hms_equals(set, set2));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hms_flag(set2));
|
||||
|
||||
hms_free(set);
|
||||
hms_free(set2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct hashmultiset *set =
|
||||
hms_new_custom(100, 0.8, hms_fval, NULL, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(hms_insert(set, 1));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(hms_insert_many(set, 1, 3));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert(hms_update(set, 1, 0));
|
||||
cmc_assert_equals(int32_t, 1, total_update);
|
||||
|
||||
cmc_assert(hms_update(set, 1, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_update);
|
||||
|
||||
cmc_assert(hms_remove(set, 1));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(hms_remove_all(set, 1));
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
|
||||
cmc_assert(hms_update(set, 1, 10));
|
||||
cmc_assert_equals(int32_t, 3, total_update);
|
||||
|
||||
cmc_assert(hms_max(set, NULL));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert(hms_min(set, NULL));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(hms_contains(set, 1));
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
|
||||
cmc_assert(hms_resize(set, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(hms_resize(set, 100));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
cmc_assert_equals(int32_t, 3, total_update);
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
hms_customize(set, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, set->callbacks);
|
||||
|
||||
hms_clear(set);
|
||||
cmc_assert(hms_insert(set, 1));
|
||||
cmc_assert(hms_insert_many(set, 1, 3));
|
||||
cmc_assert(hms_update(set, 1, 0));
|
||||
cmc_assert(hms_update(set, 1, 10));
|
||||
cmc_assert(hms_remove(set, 1));
|
||||
cmc_assert(hms_remove_all(set, 1));
|
||||
cmc_assert(hms_update(set, 1, 10));
|
||||
cmc_assert(hms_max(set, NULL));
|
||||
cmc_assert(hms_min(set, NULL));
|
||||
cmc_assert(hms_contains(set, 1));
|
||||
cmc_assert(hms_resize(set, 1000));
|
||||
cmc_assert(hms_resize(set, 100));
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
cmc_assert_equals(int32_t, 3, total_update);
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, set->callbacks);
|
||||
|
||||
hms_free(set);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
});
|
||||
});
|
||||
@@ -1,425 +0,0 @@
|
||||
#include "utl.c"
|
||||
#include "utl/assert.h"
|
||||
#include "utl/test.h"
|
||||
|
||||
#include "../src/multiset.c"
|
||||
|
||||
struct multiset_fval *ms_fval = &(struct multiset_fval){ .cmp = cmc_size_cmp,
|
||||
.cpy = NULL,
|
||||
.str = cmc_size_str,
|
||||
.free = NULL,
|
||||
.hash = cmc_size_hash,
|
||||
.pri = cmc_size_cmp };
|
||||
|
||||
struct multiset_fval *ms_fval_counter =
|
||||
&(struct multiset_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 *ms_alloc_node = &(struct cmc_alloc_node){
|
||||
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
|
||||
};
|
||||
|
||||
CMC_CREATE_UNIT(MultiSet, true, {
|
||||
CMC_CREATE_TEST(PFX##_new(), {
|
||||
struct multiset *set = ms_new(943722, 0.6, ms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_not_equals(ptr, NULL, set->buffer);
|
||||
cmc_assert_equals(size_t, 0, set->count);
|
||||
cmc_assert_equals(double, 0.6, set->load);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, set->flag);
|
||||
cmc_assert_equals(ptr, ms_fval, set->f_val);
|
||||
cmc_assert_equals(ptr, &cmc_alloc_node_default, set->alloc);
|
||||
cmc_assert_equals(ptr, NULL, set->callbacks);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (943722 / 0.6), ms_capacity(set));
|
||||
|
||||
ms_free(set);
|
||||
|
||||
set = ms_new(0, 0.6, ms_fval);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = ms_new(UINT64_MAX, 0.99, ms_fval);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = ms_new(1000, 0.6, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = ms_new(1000, 0.6, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_new_custom(), {
|
||||
struct multiset *set =
|
||||
ms_new_custom(943722, 0.6, ms_fval, ms_alloc_node, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_not_equals(ptr, NULL, set->buffer);
|
||||
cmc_assert_equals(size_t, 0, set->count);
|
||||
cmc_assert_equals(double, 0.6, set->load);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, set->flag);
|
||||
cmc_assert_equals(ptr, ms_fval, set->f_val);
|
||||
cmc_assert_equals(ptr, ms_alloc_node, set->alloc);
|
||||
cmc_assert_equals(ptr, callbacks, set->callbacks);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (943722 / 0.6), ms_capacity(set));
|
||||
|
||||
ms_free(set);
|
||||
|
||||
set = ms_new_custom(0, 0.6, ms_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = ms_new_custom(UINT64_MAX, 0.99, ms_fval, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
|
||||
set = ms_new_custom(1000, 0.6, NULL, NULL, NULL);
|
||||
cmc_assert_equals(ptr, NULL, set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_clear(), {
|
||||
v_total_free = 0;
|
||||
struct multiset *set = ms_new(100, 0.6, ms_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
ms_insert(set, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1000, set->count);
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
ms_clear(set);
|
||||
|
||||
cmc_assert_equals(size_t, 0, set->count);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, set->flag);
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
|
||||
ms_free(set);
|
||||
v_total_free = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(PFX##_free(), {
|
||||
v_total_free = 0;
|
||||
struct multiset *set = ms_new(100, 0.6, ms_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i <= 1000; i++)
|
||||
ms_insert(set, i);
|
||||
|
||||
cmc_assert_equals(size_t, 1000, set->count);
|
||||
|
||||
ms_free(set);
|
||||
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
|
||||
set = ms_new(1000, 0.6, ms_fval_counter);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_not_equals(ptr, NULL, set->buffer);
|
||||
|
||||
ms_free(set);
|
||||
|
||||
cmc_assert_equals(int32_t, 1000, v_total_free);
|
||||
v_total_free = 0;
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[count cardinality multiplicity], {
|
||||
struct multiset *set = ms_new(100, 0.6, ms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(ms_insert(set, 1));
|
||||
cmc_assert(ms_insert(set, 2));
|
||||
cmc_assert(ms_insert(set, 2));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, ms_count(set));
|
||||
cmc_assert_equals(size_t, 6, ms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i <= 3; i++)
|
||||
cmc_assert_equals(size_t, i, ms_multiplicity_of(set, i));
|
||||
|
||||
ms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[count cardinality multiplicity], {
|
||||
struct multiset *set = ms_new(100, 0.6, ms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(ms_insert(set, 1));
|
||||
cmc_assert(ms_insert(set, 2));
|
||||
cmc_assert(ms_insert(set, 2));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, ms_count(set));
|
||||
cmc_assert_equals(size_t, 6, ms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i <= 3; i++)
|
||||
cmc_assert_equals(size_t, i, ms_multiplicity_of(set, i));
|
||||
|
||||
cmc_assert(ms_remove(set, 1));
|
||||
cmc_assert(ms_remove(set, 2));
|
||||
cmc_assert(ms_remove(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 2, ms_count(set));
|
||||
cmc_assert_equals(size_t, 3, ms_cardinality(set));
|
||||
|
||||
cmc_assert_equals(size_t, 0, ms_multiplicity_of(set, 0));
|
||||
cmc_assert_equals(size_t, 0, ms_multiplicity_of(set, 1));
|
||||
cmc_assert_equals(size_t, 1, ms_multiplicity_of(set, 2));
|
||||
cmc_assert_equals(size_t, 2, ms_multiplicity_of(set, 3));
|
||||
|
||||
ms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove_all[count cardinality multiplicity], {
|
||||
struct multiset *set = ms_new(100, 0.6, ms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(ms_insert(set, 1));
|
||||
cmc_assert(ms_insert(set, 2));
|
||||
cmc_assert(ms_insert(set, 2));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
cmc_assert(ms_insert(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 3, ms_count(set));
|
||||
cmc_assert_equals(size_t, 6, ms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i <= 3; i++)
|
||||
cmc_assert_equals(size_t, i, ms_multiplicity_of(set, i));
|
||||
|
||||
cmc_assert(ms_remove_all(set, 1));
|
||||
cmc_assert(ms_remove_all(set, 3));
|
||||
|
||||
cmc_assert_equals(size_t, 1, ms_count(set));
|
||||
cmc_assert_equals(size_t, 2, ms_cardinality(set));
|
||||
|
||||
cmc_assert_equals(size_t, 0, ms_multiplicity_of(set, 0));
|
||||
cmc_assert_equals(size_t, 0, ms_multiplicity_of(set, 1));
|
||||
cmc_assert_equals(size_t, 2, ms_multiplicity_of(set, 2));
|
||||
cmc_assert_equals(size_t, 0, ms_multiplicity_of(set, 3));
|
||||
|
||||
ms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(multiplicity, {
|
||||
struct multiset *set = ms_new(50, 0.6, ms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 0; i < 1000; i++)
|
||||
cmc_assert(ms_insert(set, i % 20));
|
||||
|
||||
cmc_assert_equals(size_t, 20, ms_count(set));
|
||||
cmc_assert_equals(size_t, 1000, ms_cardinality(set));
|
||||
|
||||
for (size_t i = 0; i < 20; i++)
|
||||
cmc_assert_equals(size_t, 50, ms_multiplicity_of(set, i));
|
||||
|
||||
ms_free(set);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(flags, {
|
||||
struct multiset *set = ms_new(100, 0.6, ms_fval);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// clear
|
||||
set->flag = cmc_flags.ERROR;
|
||||
ms_clear(set);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// customize
|
||||
set->flag = cmc_flags.ERROR;
|
||||
ms_customize(set, NULL, NULL);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// insert
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_insert(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_insert(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// insert_many
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_insert_many(set, 1, 0));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_insert_many(set, 1, 2));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// update
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_update(set, 1, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_update(set, 10, 0));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// remove
|
||||
cmc_assert(!ms_remove(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.NOT_FOUND, ms_flag(set));
|
||||
|
||||
cmc_assert(ms_remove(set, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// remove_all
|
||||
cmc_assert_equals(size_t, 0, ms_remove_all(set, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, ms_flag(set));
|
||||
|
||||
cmc_assert(ms_insert_many(set, 1, 10));
|
||||
cmc_assert_equals(size_t, 0, ms_remove_all(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.NOT_FOUND, ms_flag(set));
|
||||
|
||||
cmc_assert_equals(size_t, 10, ms_remove_all(set, 1));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// max min
|
||||
cmc_assert(!ms_max(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, ms_flag(set));
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!ms_min(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, ms_flag(set));
|
||||
|
||||
cmc_assert(ms_insert(set, 1));
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_max(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_min(set, NULL));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// multiplicity_of
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert_equals(size_t, 0, ms_multiplicity_of(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// contains
|
||||
set->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!ms_contains(set, 10));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
|
||||
// copy_of
|
||||
set->flag = cmc_flags.ERROR;
|
||||
struct multiset *set2 = ms_copy_of(set);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set2));
|
||||
|
||||
// equals
|
||||
set->flag = cmc_flags.ERROR;
|
||||
set2->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ms_equals(set, set2));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ms_flag(set2));
|
||||
|
||||
ms_free(set);
|
||||
ms_free(set2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct multiset *set =
|
||||
ms_new_custom(100, 0.8, ms_fval, NULL, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(ms_insert(set, 1));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(ms_insert_many(set, 1, 3));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert(ms_update(set, 1, 0));
|
||||
cmc_assert_equals(int32_t, 1, total_update);
|
||||
|
||||
cmc_assert(ms_update(set, 1, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_update);
|
||||
|
||||
cmc_assert(ms_remove(set, 1));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(ms_remove_all(set, 1));
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
|
||||
cmc_assert(ms_update(set, 1, 10));
|
||||
cmc_assert_equals(int32_t, 3, total_update);
|
||||
|
||||
cmc_assert(ms_max(set, NULL));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert(ms_min(set, NULL));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(ms_contains(set, 1));
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
|
||||
cmc_assert(ms_resize(set, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(ms_resize(set, 100));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
cmc_assert_equals(int32_t, 3, total_update);
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
ms_customize(set, NULL, NULL);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, set->callbacks);
|
||||
|
||||
ms_clear(set);
|
||||
cmc_assert(ms_insert(set, 1));
|
||||
cmc_assert(ms_insert_many(set, 1, 3));
|
||||
cmc_assert(ms_update(set, 1, 0));
|
||||
cmc_assert(ms_update(set, 1, 10));
|
||||
cmc_assert(ms_remove(set, 1));
|
||||
cmc_assert(ms_remove_all(set, 1));
|
||||
cmc_assert(ms_update(set, 1, 10));
|
||||
cmc_assert(ms_max(set, NULL));
|
||||
cmc_assert(ms_min(set, NULL));
|
||||
cmc_assert(ms_contains(set, 1));
|
||||
cmc_assert(ms_resize(set, 1000));
|
||||
cmc_assert(ms_resize(set, 100));
|
||||
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
cmc_assert_equals(int32_t, 3, total_update);
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, set->callbacks);
|
||||
|
||||
ms_free(set);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user