00001
00002 #include <stdlib.h>
00003 #include <string.h>
00004
00005 #include "ghash.h"
00006
00007 #ifdef SELFTEST_MAIN
00008 #include "selftest.c"
00009
00010 static struct ghash dict;
00011 GHASH_DECL(test,const char*,int);
00012 GHASH_DEFN(test,const char*,int,adt_hashsp,adt_cmpsp,adt_copysp,0,adt_freesp,0);
00013
00014 static void print(struct test_entry* entry)
00015 {
00016 obuf_putf(&outbuf, "{[}s{] = }i{\n}", entry->key, entry->data);
00017 }
00018
00019 const char* keys[] = {
00020 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
00021 "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
00022 0
00023 };
00024
00025 static void testkeys(void)
00026 {
00027 int i;
00028 struct test_entry* p;
00029 for (i = 0; keys[i] != 0; ++i) {
00030 if ((p = test_get(&dict, &keys[i])) == 0)
00031 obuf_putf(&outbuf, "{Could not locate key [}s{]\n}", keys[i]);
00032 else if (p->data != i)
00033 obuf_putf(&outbuf,
00034 "{Result for key [}s{] is wrong (was }d{ should be }d{)\n}",
00035 keys[i], p->data, i);
00036 }
00037 }
00038
00039 MAIN
00040 {
00041 int i;
00042 struct test_entry* p;
00043 struct ghashiter iter;
00044
00045 test_init(&dict);
00046 obuf_putf(&outbuf, "{size=}d{ count=}d{\n}", dict.size, dict.count);
00047
00048 for (i = 0; keys[i] != 0; ++i) {
00049 test_add(&dict, &keys[i], &i);
00050 obuf_putf(&outbuf, "{size=}d{ count=}d{\n}", dict.size, dict.count);
00051 }
00052
00053 testkeys();
00054
00055 test_foreach(&dict, print);
00056
00057 ghash_remove(&dict, &keys[1]);
00058 ghash_remove(&dict, &keys[11]);
00059 ghash_remove(&dict, &keys[24]);
00060 testkeys();
00061
00062 ghashiter_loop(&iter, &dict) {
00063 p = iter.entry;
00064 obuf_putf(&outbuf, "{[}s{] = }i{\n}", p->key, p->data);
00065 }
00066
00067 test_free(&dict);
00068 }
00069 #endif
00070 #ifdef SELFTEST_EXP
00071 size=0 count=0
00072 size=31 count=1
00073 size=31 count=2
00074 size=31 count=3
00075 size=31 count=4
00076 size=31 count=5
00077 size=31 count=6
00078 size=31 count=7
00079 size=31 count=8
00080 size=31 count=9
00081 size=31 count=10
00082 size=31 count=11
00083 size=31 count=12
00084 size=31 count=13
00085 size=31 count=14
00086 size=31 count=15
00087 size=61 count=16
00088 size=61 count=17
00089 size=61 count=18
00090 size=61 count=19
00091 size=61 count=20
00092 size=61 count=21
00093 size=61 count=22
00094 size=61 count=23
00095 size=61 count=24
00096 size=61 count=25
00097 size=61 count=26
00098 [e] = 4
00099 [d] = 3
00100 [g] = 6
00101 [f] = 5
00102 [a] = 0
00103 [c] = 2
00104 [b] = 1
00105 [m] = 12
00106 [l] = 11
00107 [o] = 14
00108 [n] = 13
00109 [i] = 8
00110 [h] = 7
00111 [k] = 10
00112 [j] = 9
00113 [u] = 20
00114 [t] = 19
00115 [w] = 22
00116 [v] = 21
00117 [q] = 16
00118 [p] = 15
00119 [s] = 18
00120 [r] = 17
00121 [y] = 24
00122 [x] = 23
00123 [z] = 25
00124 Could not locate key [b]
00125 Could not locate key [l]
00126 Could not locate key [y]
00127 [e] = 4
00128 [d] = 3
00129 [g] = 6
00130 [f] = 5
00131 [a] = 0
00132 [c] = 2
00133 [m] = 12
00134 [o] = 14
00135 [n] = 13
00136 [i] = 8
00137 [h] = 7
00138 [k] = 10
00139 [j] = 9
00140 [u] = 20
00141 [t] = 19
00142 [w] = 22
00143 [v] = 21
00144 [q] = 16
00145 [p] = 15
00146 [s] = 18
00147 [r] = 17
00148 [x] = 23
00149 [z] = 25
00150 #endif