5 #include "derp/common.h"
6 #include "derp/treemap.h"
9 #include <gc/leak_detector.h>
12 int ivals[] = {0,1,2,3,4,5,6,7,8,9};
14 int icmp(const void *a, const void *b, void *aux)
17 return *(int*)a - *(int*)b;
25 GC_debug_malloc_replacement,
26 GC_debug_realloc_replacement, GC_debug_free);
29 treemap *t = tm_new(derp_strcmp, NULL);
30 assert(tm_length(t) == 0);
31 assert(tm_is_empty(t));
33 assert(!tm_at(t, "zero"));
34 tm_insert(t, "zero", ivals);
35 assert(tm_length(t) == 1);
36 assert(*(int*)tm_at(t, "zero") == 0);
39 tm_insert(t, "zero", ivals+1);
40 assert(tm_length(t) == 1);
41 assert(*(int*)tm_at(t, "zero") == 1);
43 tm_insert(t, "zero", ivals);
44 assert(*(int*)tm_at(t, "zero") == 0);
46 tm_insert(t, "one", ivals+1);
47 assert(tm_length(t) == 2);
48 assert(*(int*)tm_at(t, "zero") == 0);
49 assert(*(int*)tm_at(t, "one") == 1);
50 assert(!tm_at(t, "flurgle"));
53 assert(!tm_at(t, "one"));
56 assert(tm_length(t) == 0);
57 assert(!tm_at(t, "zero"));
59 /* iterator should sort */
60 tm_insert(t, "d", NULL);
61 tm_insert(t, "a", NULL);
62 tm_insert(t, "e", NULL);
63 tm_insert(t, "c", NULL);
64 tm_insert(t, "b", NULL);
66 tm_iter *i = tm_iter_begin(t);
67 assert(*(char*)tm_iter_next(i)->k == 'a');
68 assert(*(char*)tm_iter_next(i)->k == 'b');
69 assert(*(char*)tm_iter_next(i)->k == 'c');
70 assert(*(char*)tm_iter_next(i)->k == 'd');
71 assert(*(char*)tm_iter_next(i)->k == 'e');
72 assert(!tm_iter_next(i));
76 /* test for memory leak */
77 tm_dtor(t, derp_free, derp_free, NULL);
78 char *key = malloc(5),
80 *otherkey = malloc(3);
81 int *val1 = malloc(sizeof *val1),
82 *val2 = malloc(sizeof *val2),
83 *val3 = malloc(sizeof *val3);
85 strcpy(dupkey, "life");
88 tm_insert(t, key, val1);
89 /* will free key, since dupkey is the same */
90 tm_insert(t, dupkey, val2);
91 /* safe to double-insert same key */
92 tm_insert(t, dupkey, val2);
94 /* tm_remove should free as needed */
96 strcpy(otherkey, "hi");
97 tm_insert(t, otherkey, val3);
98 tm_remove(t, otherkey);
102 treemap *t2 = tm_new(icmp, NULL);
103 /* insert in ascending order, inherently unbalanced,
104 * to exercise split/skew */
105 for (size_t i = 0; i < 10; i++)
106 tm_insert(t2, ivals+i, ivals+i);
107 assert(tm_length(t2) == 10);
110 for (size_t i = 0; i < 10; i++)
111 assert(*(int*)tm_at(t2, ivals+i) == (int)i);
113 /* remove odd ones */
114 for (size_t i = 1; i < 10; i+=2)
115 tm_remove(t2, ivals+i);
116 assert(tm_length(t2) == 5);
118 /* evens should still be there */
119 for (size_t i = 0; i < 10; i+=2)
120 assert((int)i == *(int*)tm_at(t2, ivals+i));
123 for (size_t i = 0; i < 10; i+=2)
124 tm_remove(t2, ivals+i);
125 assert(tm_is_empty(t2));