Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 191 lines (156 sloc) 3.69 kB
97bda25 @tj Initial commit
tj authored
1
2 //
3 // hash.c
4 //
5 // Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
6 //
7
8 #include "hash.h"
9
10 /*
11 * Set hash `key` to `val`.
12 */
13
14 inline void
15 hash_set(hash_t *self, char *key, void *val) {
16 int ret;
17 khiter_t k = kh_put(ptr, self, key, &ret);
18 kh_value(self, k) = val;
19 }
20
21 /*
22 * Get hash `key`, or NULL.
23 */
24
25 inline void *
26 hash_get(hash_t *self, char *key) {
27 khiter_t k = kh_get(ptr, self, key);
28 return k == kh_end(self) ? NULL : kh_value(self, k);
29 }
30
31 /*
32 * Check if hash `key` exists.
33 */
34
35 inline int
36 hash_has(hash_t *self, char *key) {
37 khiter_t k = kh_get(ptr, self, key);
38 return kh_exist(self, k);
39 }
40
41 /*
42 * Remove hash `key`.
43 */
44
45 void
4dbca76 @tj change hash_remove() to hash_del()
tj authored
46 hash_del(hash_t *self, char *key) {
97bda25 @tj Initial commit
tj authored
47 khiter_t k = kh_get(ptr, self, key);
48 kh_del(ptr, self, k);
49 }
50
51 // tests
52
53 #ifdef TEST_HASH
54
55 #include <stdio.h>
56 #include <assert.h>
57 #include <string.h>
58
59 void
60 test_hash_set() {
61 hash_t *hash = hash_new();
62 assert(0 == hash_size(hash));
63
64 hash_set(hash, "name", "tobi");
65 hash_set(hash, "species", "ferret");
66 assert(2 == hash_size(hash));
67
68 assert(0 == strcmp("tobi", hash_get(hash, "name")));
69 assert(0 == strcmp("ferret", hash_get(hash, "species")));
70 }
71
72 void
73 test_hash_get() {
74 hash_t *hash = hash_new();
75 hash_set(hash, "foo", "bar");
76 assert(0 == strcmp("bar", hash_get(hash, "foo")));
77 assert(NULL == hash_get(hash, "bar"));
78 }
79
80 void
81 test_hash_has() {
82 hash_t *hash = hash_new();
83 hash_set(hash, "foo", "bar");
84 assert(1 == hash_has(hash, "foo"));
85 assert(0 == hash_has(hash, "bar"));
86 }
87
88 void
89 test_hash_size() {
90 hash_t *hash = hash_new();
91 assert(0 == hash_size(hash));
92 hash_set(hash, "foo", "bar");
93 assert(1 == hash_size(hash));
94 hash_set(hash, "bar", "baz");
95 assert(2 == hash_size(hash));
96 }
97
98 void
4dbca76 @tj change hash_remove() to hash_del()
tj authored
99 test_hash_del() {
97bda25 @tj Initial commit
tj authored
100 hash_t *hash = hash_new();
101 hash_set(hash, "foo", "bar");
102 assert(1 == hash_has(hash, "foo"));
103 assert(0 == hash_has(hash, "bar"));
4dbca76 @tj change hash_remove() to hash_del()
tj authored
104 hash_del(hash, "foo");
105 hash_del(hash, "bar");
97bda25 @tj Initial commit
tj authored
106 assert(0 == hash_has(hash, "foo"));
107 }
108
109 void
2f1105b @tj add hash_clear(hash)
tj authored
110 test_hash_clear() {
111 hash_t *hash = hash_new();
112 hash_set(hash, "foo", "bar");
113 hash_set(hash, "bar", "baz");
114 hash_set(hash, "raz", "jaz");
115 assert(3 == hash_size(hash));
116 hash_clear(hash);
117 assert(0 == hash_size(hash));
118 }
119
120 void
97bda25 @tj Initial commit
tj authored
121 test_hash_each() {
122 hash_t *hash = hash_new();
123 hash_set(hash, "name", "tj");
124 hash_set(hash, "age", "25");
125
126 const char *keys[2];
127 void *vals[2];
128 int n = 0;
129
130 hash_each(hash, {
131 keys[n] = key;
132 vals[n] = val;
133 n++;
134 });
135
a40b2ad @tj fix test asserts to account for randomness
tj authored
136 assert(0 == strcmp("age", keys[0]) || 0 == strcmp("name", keys[0]));
137 assert(0 == strcmp("age", keys[1]) || 0 == strcmp("name", keys[1]));
138 assert(0 == strcmp("25", vals[0]) || 0 == strcmp("tj", vals[0]));
139 assert(0 == strcmp("25", vals[1]) || 0 == strcmp("tj", vals[1]));
97bda25 @tj Initial commit
tj authored
140 }
141
142 void
143 test_hash_each_key() {
144 hash_t *hash = hash_new();
145 hash_set(hash, "name", "tj");
146 hash_set(hash, "age", "25");
147
148 const char *keys[2];
149 int n = 0;
150
151 hash_each_key(hash, {
152 keys[n++] = key;
153 });
154
a40b2ad @tj fix test asserts to account for randomness
tj authored
155 assert(0 == strcmp("age", keys[0]) || 0 == strcmp("name", keys[0]));
156 assert(0 == strcmp("age", keys[1]) || 0 == strcmp("name", keys[1]));
97bda25 @tj Initial commit
tj authored
157 }
158
159 void
160 test_hash_each_val() {
161 hash_t *hash = hash_new();
162 hash_set(hash, "name", "tj");
163 hash_set(hash, "age", "25");
164
165 void *vals[2];
166 int n = 0;
167
168 hash_each_val(hash, {
169 vals[n++] = val;
170 });
171
a40b2ad @tj fix test asserts to account for randomness
tj authored
172 assert(0 == strcmp("25", vals[0]) || 0 == strcmp("tj", vals[0]));
173 assert(0 == strcmp("25", vals[1]) || 0 == strcmp("tj", vals[1]));
97bda25 @tj Initial commit
tj authored
174 }
175
176 int
177 main(){
178 test_hash_set();
179 test_hash_get();
180 test_hash_has();
4dbca76 @tj change hash_remove() to hash_del()
tj authored
181 test_hash_del();
97bda25 @tj Initial commit
tj authored
182 test_hash_size();
2f1105b @tj add hash_clear(hash)
tj authored
183 test_hash_clear();
97bda25 @tj Initial commit
tj authored
184 test_hash_each();
185 test_hash_each_key();
186 test_hash_each_val();
187 printf("\n \e[32m\u2713 \e[90mok\e[0m\n\n");
188 return 0;
189 }
190
191 #endif
Something went wrong with that request. Please try again.