Coverage Report

Created: 2023-03-26 06:03

/src/clib/deps/hash/hash.c
Line
Count
Source (jump to first uncovered line)
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
0
hash_set(hash_t *self, char *key, void *val) {
16
0
  int ret;
17
0
  khiter_t k = kh_put(ptr, self, key, &ret);
18
0
  kh_value(self, k) = val;
19
0
}
20
21
/*
22
 * Get hash `key`, or NULL.
23
 */
24
25
inline void *
26
0
hash_get(hash_t *self, char *key) {
27
0
  khiter_t k = kh_get(ptr, self, key);
28
0
  return k == kh_end(self) ? NULL : kh_value(self, k);
29
0
}
30
31
/*
32
 * Check if hash `key` exists.
33
 */
34
35
inline int
36
0
hash_has(hash_t *self, char *key) {
37
0
  khiter_t k = kh_get(ptr, self, key);
38
0
  return kh_exist(self, k);
39
0
}
40
41
/*
42
 * Remove hash `key`.
43
 */
44
45
void
46
0
hash_del(hash_t *self, char *key) {
47
0
  khiter_t k = kh_get(ptr, self, key);
48
0
  kh_del(ptr, self, k);
49
0
}
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
99
test_hash_del() {
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"));
104
  hash_del(hash, "foo");
105
  hash_del(hash, "bar");
106
  assert(0 == hash_has(hash, "foo"));
107
}
108
109
void
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
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
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]));
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
155
  assert(0 == strcmp("age", keys[0]) || 0 == strcmp("name", keys[0]));
156
  assert(0 == strcmp("age", keys[1]) || 0 == strcmp("name", keys[1]));
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
172
  assert(0 == strcmp("25", vals[0]) || 0 == strcmp("tj", vals[0]));
173
  assert(0 == strcmp("25", vals[1]) || 0 == strcmp("tj", vals[1]));
174
}
175
176
int
177
main(){
178
  test_hash_set();
179
  test_hash_get();
180
  test_hash_has();
181
  test_hash_del();
182
  test_hash_size();
183
  test_hash_clear();
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