summaryrefslogtreecommitdiff
path: root/tests/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/map.c')
-rw-r--r--tests/map.c60
1 files changed, 51 insertions, 9 deletions
diff --git a/tests/map.c b/tests/map.c
index 6c42787..9af0b01 100644
--- a/tests/map.c
+++ b/tests/map.c
@@ -1,37 +1,79 @@
#include <assert.h>
#include <stdio.h>
+#include "test.h"
+/* required defs */
#define MAP_KEY int
#define MAP_TYPE int
-#define MAP_HASH(a) CONTS_MAP_NO_HASH(a)
+#define MAP_HASH(a) ints_generic_hash(&(a))
#define MAP_CMP(a, b) ((a) - (b))
#define MAP_NAME ints
+
+/* optional defs */
+#define MAP_MALLOC mallocc
+#define MAP_CALLOC callocc
+#define MAP_REALLOC reallocc
+#define MAP_FREE free
+
#include <conts/map.h>
int main()
{
+#if defined(COVERAGE)
+ assert(!covsrv_init());
+ atexit(covsrv_destroy);
+#endif
+
/* heuristic, but if we know how many elements we'll need, we should
* give it to the create function. */
- struct ints ints = ints_create(0);
- for (int i = 0; i < 1000000; ++i) {
- ints_insert(&ints, i, i);
+ struct ints ints = ints_create(10);
+
+ /* check that trying to search for something in an empty map doesn*t
+ * crash */
+ assert(ints_find(&ints, 0) == NULL);
+
+ /* similarly, iterating an empty map doesn't do anything */
+ foreach(ints, iter, &ints) {
+ assert(false && "iterating empty map");
}
- assert(ints_len(&ints) == 1000000);
- for (int i = 0; i < 1000000; ++i) {
+ for (int i = 0; i < ITER; ++i) {
+ if (!ints_insert(&ints, i, i)) {
+ fprintf(stderr, "failed inserting %d\n", i);
+ ints_destroy(&ints);
+ return -1;
+ }
+ }
+ assert(ints_len(&ints) == ITER);
+
+
+ for (int i = 0; i < ITER; ++i) {
int *v = ints_find(&ints, i);
assert(v && *v == i);
}
+ /* check that trying to find something that doesn't exist doesn't crash */
+ assert(ints_find(&ints, 123456789) == NULL);
+
+ /* check that trying to remove something that doesn't exist is a noop */
+ size_t len = ints_len(&ints);
+ ints_remove(&ints, 123456789);
+ assert(ints_len(&ints) == len);
+
+ /* check that trying to insert 0 again gets us the existing 0 */
+ int *orig = ints_find(&ints, 0);
+ int *new = ints_insert(&ints, 0, 0);
+ assert(orig == new);
+
size_t count = 0;
foreach(ints, iter, &ints) {
- assert(iter->key == iter->data);
+ assert(iter.t->key == iter.t->data);
count++;
}
- assert(count == 1000000);
+ assert(count == ITER);
- for (int i = 0; i < 1000000; ++i) {
+ for (int i = 0; i < ITER; ++i) {
ints_remove(&ints, i);
}