5 #include "ndbneb.h" /* for ARRAY_SIZE() */
6 #include "test_utils.h"
13 { "kalle", "penslar" },
17 { "andreas", "regerar" },
21 static struct test_data
{
25 static struct test_data
*ddup(int x
, int i
, int j
)
29 d
= malloc(sizeof(*d
));
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
))) {
64 return HASH_WALK_REMOVE
;
70 int main(int argc
, char **argv
)
72 hash_table
*ti
, *tj
, *tx
;
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 */
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));
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));
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
));
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
++) {
117 ok_int(s
.x
, hash_entries(tx
), "x table pre-delete");
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
++) {
125 ok_int(s
.i
, hash_entries(ti
), "i table pre-delete");
126 hash_walk_data(ti
, del_matching
);
128 ok_int(s
.i
, hash_entries(ti
), "i table post-delete");
130 for (j
= 0; j
< ARRAY_SIZE(keys
); j
++) {
132 ok_int(s
.j
, hash_entries(tj
), "j table pre-delete");
133 hash_walk_data(tj
, del_matching
);
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);