Add test-hash.c, to test hash_walk_data()'s new removal option
[nagios-reports-module.git] / test-hash.c
blob08e9298cf277f63d18be0dcb7b8ef85d44ce020d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "hash.h"
5 #include "ndbneb.h" /* for ARRAY_SIZE() */
6 #include "test_utils.h"
8 static struct {
9 char *k1, *k2;
10 } keys[] = {
11 { "nisse", "banan" },
12 { "foo", "bar" },
13 { "kalle", "penslar" },
14 { "hello", "world" },
15 { "test", "fnurg" },
16 { "bar", "nitfol" },
17 { "andreas", "regerar" },
20 static int removed;
21 static struct test_data {
22 int x, i, j;
23 } del;
25 static struct test_data *ddup(int x, int i, int j)
27 struct test_data *d;
29 d = malloc(sizeof(*d));
30 d->x = x;
31 d->i = i;
32 d->j = j;
33 return d;
36 struct hash_check {
37 uint entries, count, max, added, removed;
38 int ent_delta, addrm_delta;
41 static void get_table_data(struct hash_check *c, hash_table *t)
43 c->count = hash_count_entries(t);
44 c->entries = hash_entries(t);
45 c->max = hash_entries_max(t);
46 c->added = hash_entries_added(t);
47 c->removed = hash_entries_removed(t);
48 c->ent_delta = hash_check_table(t);
49 c->addrm_delta = c->added - c->removed;
52 static void print_table_data(struct hash_check *c)
54 printf("entries: %u; counted: %u; delta: %d\n", c->entries, c->count, c->ent_delta);
55 printf("added: %u; removed: %u; delta: %d\n", c->added, c->removed, c->addrm_delta);
58 static int del_matching(void *data)
60 struct test_data *d = (struct test_data *)data;
62 if (!memcmp(d, &del, sizeof(del))) {
63 removed++;
64 return HASH_WALK_REMOVE;
67 return 0;
70 int main(int argc, char **argv)
72 hash_table *ti, *tj, *tx;
73 int i, j, x;
74 struct hash_check c;
75 struct test_data s;
77 t_set_colors(0);
78 printf("%sRunning hash_walk_data() tests%s\n", cyan, reset);
79 memset(&s, 0, sizeof(s));
80 /* first we set up the hash-tables */
81 ti = hash_init(16);
82 tj = hash_init(16);
83 tx = hash_init(16);
84 x = i = j = 0;
85 for (x = 0; x < ARRAY_SIZE(keys); x++) {
86 hash_add(tx, keys[x].k1, ddup(x, 0, 0));
87 hash_add(tx, keys[x].k2, ddup(x, 0, 0));
88 hash_add2(tx, keys[x].k1, keys[x].k2, ddup(x, 0, 0));
89 s.x += 3;
90 ok_int(s.x, hash_entries(tx), "x table adding");
92 for (i = 0; i < ARRAY_SIZE(keys); i++) {
93 hash_add(ti, keys[i].k1, ddup(x, i, 0));
94 hash_add(ti, keys[i].k1, ddup(x, i, 0));
95 hash_add2(ti, keys[i].k1, keys[i].k2, ddup(x, i, 0));
96 s.i += 3;
97 ok_int(s.i, hash_entries(ti), "i table adding");
99 for (j = 0; j < ARRAY_SIZE(keys); j++) {
100 hash_add(tj, keys[j].k1, ddup(x, i, j));
101 hash_add(tj, keys[j].k2, ddup(x, i, j));
102 hash_add2(tj, keys[j].k1, keys[j].k2, ddup(x, i, j));
103 s.j += 3;
104 ok_int(s.j, hash_entries(tj), "j table adding");
109 ok_int(s.x, hash_entries(tx), "x table done adding");
110 ok_int(s.i, hash_entries(ti), "i table done adding");
111 ok_int(s.j, hash_entries(tj), "j table done adding");
113 for (x = 0; x < ARRAY_SIZE(keys); x++) {
114 del.x = x;
115 del.i = del.j = 0;
117 ok_int(s.x, hash_entries(tx), "x table pre-delete");
118 s.x -= 3;
119 hash_walk_data(tx, del_matching);
120 ok_int(s.x, hash_entries(tx), "x table post-delete");
122 for (i = 0; i < ARRAY_SIZE(keys); i++) {
123 del.i = i;
124 del.j = 0;
125 ok_int(s.i, hash_entries(ti), "i table pre-delete");
126 hash_walk_data(ti, del_matching);
127 s.i -= 3;
128 ok_int(s.i, hash_entries(ti), "i table post-delete");
130 for (j = 0; j < ARRAY_SIZE(keys); j++) {
131 del.j = j;
132 ok_int(s.j, hash_entries(tj), "j table pre-delete");
133 hash_walk_data(tj, del_matching);
134 s.j -= 3;
135 ok_int(s.j, hash_entries(tj), "j table post-delete");
140 ok_int(0, hash_entries(tx), "x table post all ops");
141 ok_int(0, hash_check_table(tx), "x table consistency post all ops");
142 ok_int(0, hash_entries(ti), "i table post all ops");
143 ok_int(0, hash_check_table(ti), "i table consistency post all ops");
144 ok_int(0, hash_entries(tj), "j table post all ops");
145 ok_int(0, hash_check_table(tj), "j table consistency post all ops");
146 hash_debug_table(tx, 0);
147 hash_debug_table(ti, 0);
148 hash_debug_table(tj, 0);
149 return t_end();