summaryrefslogtreecommitdiff
path: root/tests/map.c
blob: a370413c13bdb077919482180c0253fd391b5e55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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_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) {
		if (!ints_insert(&ints, i, i)) {
			fprintf(stderr, "failed inserting %d\n", i);
			ints_destroy(&ints);
			return -1;
		}
	}
	assert(ints_len(&ints) == 1000000);

	for (int i = 0; i < 1000000; ++i) {
		int *v = ints_find(&ints, i);
		assert(v && *v == i);
	}

	size_t count = 0;
	foreach(ints, iter, &ints) {
		assert(iter->key == iter->data);
		count++;
	}

	assert(count == 1000000);

	for (int i = 0; i < 1000000; ++i) {
		ints_remove(&ints, i);
	}

	assert(ints_len(&ints) == 0);
	ints_destroy(&ints);
}