aboutsummaryrefslogtreecommitdiff
path: root/src/tests/hashtest2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/hashtest2.cpp')
-rw-r--r--src/tests/hashtest2.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/tests/hashtest2.cpp b/src/tests/hashtest2.cpp
new file mode 100644
index 0000000..9ac75d5
--- /dev/null
+++ b/src/tests/hashtest2.cpp
@@ -0,0 +1,73 @@
1#include "hash.h"
2#include <string.h>
3
4typedef struct CC
5{
6 const char *sKey;
7 const char *sData;
8} CC;
9
10char *c(const char *s)
11{
12 int l = strlen(s);
13 char *o = new char[l+1];
14 o[l] = '\0';
15 memcpy(o, s, l);
16
17 return o;
18}
19
20CC *mkCC(const char *k, const char *d)
21{
22 CC *o = new CC;
23 o->sKey = c(k);
24 o->sData = c(d);
25 return o;
26}
27
28void klCC(CC *c)
29{
30 delete[] c->sKey;
31 delete[] c->sData;
32 delete c;
33}
34
35typedef Hash<const char *, CC *> CCHash;
36
37int main()
38{
39 char buf1[200];
40 char buf2[200];
41 CCHash h;
42 CC *tmp;
43
44 for(int i=0;i<10;i++)
45 {
46 sprintf(buf1, "key_%d", i);
47 sprintf(buf2, "data_%d", i);
48 tmp = mkCC(buf1, buf2);
49 h[tmp->sKey] = tmp;
50 }
51
52 for(int i=0;i<12;i++)
53 {
54 sprintf(buf1, "key_%d", i);
55 if(h.has(buf1))
56 {
57 tmp = h[buf1];
58 printf("GOT %s = %s\n", tmp->sKey, tmp->sData);
59 }
60 else
61 printf("%s is not in the table.\n", buf1);
62 }
63
64 CCHash::iterator it = h.begin();
65 for(;it!=h.end();it++)
66 {
67 tmp = (*it).second;
68 klCC(tmp);
69 }
70
71 return 0;
72}
73