showlog: Let flapping and downtime imply EVT_HOST and EVT_SERVICE
[nagios-reports-module.git] / test-hash.c
blob635558f054b65f2235af06a22e3e51d45af26ce8
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 int del_matching(void *data)
43 struct test_data *d = (struct test_data *)data;
45 if (!memcmp(d, &del, sizeof(del))) {
46 removed++;
47 return HASH_WALK_REMOVE;
50 return 0;
53 int main(int argc, char **argv)
55 hash_table *ti, *tj, *tx;
56 int i, j, x;
57 struct test_data s;
59 t_set_colors(0);
60 t_start("testing hash_walk_data()");
61 memset(&s, 0, sizeof(s));
62 /* first we set up the hash-tables */
63 ti = hash_init(16);
64 tj = hash_init(16);
65 tx = hash_init(16);
66 x = i = j = 0;
67 for (x = 0; x < ARRAY_SIZE(keys); x++) {
68 hash_add(tx, keys[x].k1, ddup(x, 0, 0));
69 hash_add(tx, keys[x].k2, ddup(x, 0, 0));
70 hash_add2(tx, keys[x].k1, keys[x].k2, ddup(x, 0, 0));
71 s.x += 3;
72 ok_int(s.x, hash_entries(tx), "x table adding");
74 for (i = 0; i < ARRAY_SIZE(keys); i++) {
75 hash_add(ti, keys[i].k1, ddup(x, i, 0));
76 hash_add(ti, keys[i].k1, ddup(x, i, 0));
77 hash_add2(ti, keys[i].k1, keys[i].k2, ddup(x, i, 0));
78 s.i += 3;
79 ok_int(s.i, hash_entries(ti), "i table adding");
81 for (j = 0; j < ARRAY_SIZE(keys); j++) {
82 hash_add(tj, keys[j].k1, ddup(x, i, j));
83 hash_add(tj, keys[j].k2, ddup(x, i, j));
84 hash_add2(tj, keys[j].k1, keys[j].k2, ddup(x, i, j));
85 s.j += 3;
86 ok_int(s.j, hash_entries(tj), "j table adding");
91 ok_int(s.x, hash_entries(tx), "x table done adding");
92 ok_int(s.i, hash_entries(ti), "i table done adding");
93 ok_int(s.j, hash_entries(tj), "j table done adding");
95 for (x = 0; x < ARRAY_SIZE(keys); x++) {
96 del.x = x;
97 del.i = del.j = 0;
99 ok_int(s.x, hash_entries(tx), "x table pre-delete");
100 s.x -= 3;
101 hash_walk_data(tx, del_matching);
102 ok_int(s.x, hash_entries(tx), "x table post-delete");
104 for (i = 0; i < ARRAY_SIZE(keys); i++) {
105 del.i = i;
106 del.j = 0;
107 ok_int(s.i, hash_entries(ti), "i table pre-delete");
108 hash_walk_data(ti, del_matching);
109 s.i -= 3;
110 ok_int(s.i, hash_entries(ti), "i table post-delete");
112 for (j = 0; j < ARRAY_SIZE(keys); j++) {
113 del.j = j;
114 ok_int(s.j, hash_entries(tj), "j table pre-delete");
115 hash_walk_data(tj, del_matching);
116 s.j -= 3;
117 ok_int(s.j, hash_entries(tj), "j table post-delete");
122 ok_int(0, hash_entries(tx), "x table post all ops");
123 ok_int(0, hash_check_table(tx), "x table consistency post all ops");
124 ok_int(0, hash_entries(ti), "i table post all ops");
125 ok_int(0, hash_check_table(ti), "i table consistency post all ops");
126 ok_int(0, hash_entries(tj), "j table post all ops");
127 ok_int(0, hash_check_table(tj), "j table consistency post all ops");
128 hash_debug_table(tx, 0);
129 hash_debug_table(ti, 0);
130 hash_debug_table(tj, 0);
131 return t_end();