\hypertarget{adt_2ghash__test_8c-example}{
\section{adt/ghash\_\-test.c}
}


\begin{DocInclude}\begin{verbatim}1 /* $Id: ghash_test.c 704 2006-07-27 18:52:50Z bruce $ */
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "ghash.h"
6 
7 #ifdef SELFTEST_MAIN
8 #include "selftest.c"
9 
10 static struct ghash dict;
11 GHASH_DECL(test,const char*,int);
12 GHASH_DEFN(test,const char*,int,adt_hashsp,adt_cmpsp,adt_copysp,0,adt_freesp,0);
13 
14 static void print(struct test_entry* entry)
15 {
16   obuf_putf(&outbuf, "{[}s{] = }i{\n}", entry->key, entry->data);
17 }
18 
19 const char* keys[] = {
20   "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
21   "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
22   0
23 };
24 
25 static void testkeys(void)
26 {
27   int i;
28   struct test_entry* p;
29   for (i = 0; keys[i] != 0; ++i) {
30     if ((p = test_get(&dict, &keys[i])) == 0)
31       obuf_putf(&outbuf, "{Could not locate key [}s{]\n}", keys[i]);
32     else if (p->data != i)
33       obuf_putf(&outbuf,
34                 "{Result for key [}s{] is wrong (was }d{ should be }d{)\n}",
35                 keys[i], p->data, i);
36   }
37 }
38 
39 MAIN
40 {
41   int i;
42   struct test_entry* p;
43   struct ghashiter iter;
44 
45   test_init(&dict);
46   obuf_putf(&outbuf, "{size=}d{ count=}d{\n}", dict.size, dict.count);
47 
48   for (i = 0; keys[i] != 0; ++i) {
49     test_add(&dict, &keys[i], &i);
50     obuf_putf(&outbuf, "{size=}d{ count=}d{\n}", dict.size, dict.count);
51   }
52 
53   testkeys();
54 
55   test_foreach(&dict, print);
56 
57   ghash_remove(&dict, &keys[1]);
58   ghash_remove(&dict, &keys[11]);
59   ghash_remove(&dict, &keys[24]);
60   testkeys();
61 
62   ghashiter_loop(&iter, &dict) {
63     p = iter.entry;
64     obuf_putf(&outbuf, "{[}s{] = }i{\n}", p->key, p->data);
65   }
66 
67   test_free(&dict);
68 }
69 #endif
70 #ifdef SELFTEST_EXP
71 size=0 count=0
72 size=31 count=1
73 size=31 count=2
74 size=31 count=3
75 size=31 count=4
76 size=31 count=5
77 size=31 count=6
78 size=31 count=7
79 size=31 count=8
80 size=31 count=9
81 size=31 count=10
82 size=31 count=11
83 size=31 count=12
84 size=31 count=13
85 size=31 count=14
86 size=31 count=15
87 size=61 count=16
88 size=61 count=17
89 size=61 count=18
90 size=61 count=19
91 size=61 count=20
92 size=61 count=21
93 size=61 count=22
94 size=61 count=23
95 size=61 count=24
96 size=61 count=25
97 size=61 count=26
98 [e] = 4
99 [d] = 3
100 [g] = 6
101 [f] = 5
102 [a] = 0
103 [c] = 2
104 [b] = 1
105 [m] = 12
106 [l] = 11
107 [o] = 14
108 [n] = 13
109 [i] = 8
110 [h] = 7
111 [k] = 10
112 [j] = 9
113 [u] = 20
114 [t] = 19
115 [w] = 22
116 [v] = 21
117 [q] = 16
118 [p] = 15
119 [s] = 18
120 [r] = 17
121 [y] = 24
122 [x] = 23
123 [z] = 25
124 Could not locate key [b]
125 Could not locate key [l]
126 Could not locate key [y]
127 [e] = 4
128 [d] = 3
129 [g] = 6
130 [f] = 5
131 [a] = 0
132 [c] = 2
133 [m] = 12
134 [o] = 14
135 [n] = 13
136 [i] = 8
137 [h] = 7
138 [k] = 10
139 [j] = 9
140 [u] = 20
141 [t] = 19
142 [w] = 22
143 [v] = 21
144 [q] = 16
145 [p] = 15
146 [s] = 18
147 [r] = 17
148 [x] = 23
149 [z] = 25
150 #endif
\end{verbatim}
\end{DocInclude}
 