1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#include <stdio.h>
#include "../ht.h"
int main(int UNUSED argc, char ** UNUSED argv)
{
ht *htp = new_ht(HT_INIT_CAP, 0);
NUM *temp = MYALLOC(NUM, 1), key = 1023;
*temp = 12345;
if (ht_insert(htp, &key, temp)) {
fleprintf0("Fail to insert\n");
free(temp);
destroy_ht(htp, DESTROY_EVERY_SELF);
return 1;
}
if ((temp = ht_find(htp, &key))) {
fleprintf("We found value %ld for key %ld\n",
*temp, key);
} else
fleprintf("We found no value for key %ld\n", key);
NUM size = ht_size(htp);
fleprintf("The size of the hash table is %ld\n", size);
for (NUM i = 0; i < size; i++)
fleprintf("The %ld-th element has key %ld and value %ld\n",
i,
*((NUM *)*(ht_keys(htp)+i)),
*((NUM *)*(ht_values(htp)+i)));
if (ht_delete(htp, &key, DELETE_VALUE)) {
fleprintf("Fail to delete key %ld\n", key);
destroy_ht(htp, DESTROY_VALUE_SELF);
return 1;
}
fleprintf0("After the deletion, ");
if ((temp = ht_find(htp, &key))) {
eprintf("We found value %ld for key %ld\n",
*temp, key);
destroy_ht(htp, DESTROY_VALUE_SELF);
return 1;
} else {
eprintf("We found no value for key %ld\n", key);
}
destroy_ht(htp, DESTROY_VALUE_SELF);
/* Testing hash tables of pairs of integers. */
htp = new_ht2(10);
pair2 *key2 = NULL;
for (int i = 0; i < 10; i++) {
if (i>=2 && i % 2) {
NEW_P2(key2, i<<1, i,
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;);
} else {
NEW_P2(key2, i, i<<1,
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;);
}
if (ht_insert(htp, key2, (void*)1)) {
fleprintf0("Fail to insert\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
}
}
printf("Success!\n");
pair2 testk2 = { .x = 0, .y = 0 };
if (ht_find(htp, &testk2) == NULL) {
fleprintf0("Fail to find zero zero pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for zero zero pair\n",
ht_find(htp, &testk2));
}
testk2.x = 1;
testk2.y = 1;
if (ht_find(htp, &testk2)) {
fleprintf0("Accidentally find one one pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for one one pair\n",
ht_find(htp, &testk2));
}
testk2.x = 1;
testk2.y = 2;
if (ht_find(htp, &testk2) == NULL) {
fleprintf0("Fail to find one two pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for one two pair\n",
ht_find(htp, &testk2));
}
destroy_ht(htp, DESTROY_KEY_SELF);
/* testing hash tables with pair3 keys */
htp = new_ht3(10);
pair3 *key3 = NULL;
for (int i = 0; i < 10; i++) {
NEW_P3(key3, i, i << 1, -i,
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;);
if (ht_insert(htp, key3, (void*)1)) {
fleprintf0("Fail to insert\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
}
}
printf("Success!\n");
pair3 testk3 = { .x = 0, .y = 0, .z = 0 };
if (ht_find(htp, &testk3) == NULL) {
fleprintf0("Fail to find zero pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for zero pair\n",
ht_find(htp, &testk3));
}
testk3.x = 1;
testk3.y = 2;
testk3.z = -1;
if (ht_find(htp, &testk3) == NULL) {
fleprintf0("Fail to find one two minus one pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for one two minus one pair\n",
ht_find(htp, &testk3));
}
testk3.x = 1;
testk3.y = 2;
testk3.z = 1;
if (ht_find(htp, &testk3)) {
fleprintf0("Accidentally find one two one pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for one two one pair\n",
ht_find(htp, &testk3));
}
destroy_ht(htp, DESTROY_KEY_SELF);
/* testing hash tables with pair4 keys */
htp = new_ht4(10);
pair4 *key4 = NULL;
for (int i = 0; i < 10; i++) {
NEW_P4(key4, i, i << 1, -i, i+1,
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;);
if (ht_insert(htp, key4, (void*)1)) {
fleprintf0("Fail to insert\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
}
}
printf("Success!\n");
pair4 testk4 = { .x = 0, .y = 0, .z = 0, .u = 1 };
if (ht_find(htp, &testk4) == NULL) {
fleprintf0("Fail to find zero pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for zero pair\n",
ht_find(htp, &testk4));
}
testk4.x = 1;
testk4.y = 2;
testk4.z = -1;
testk4.u = 2;
if (ht_find(htp, &testk4) == NULL) {
fleprintf0("Fail to find one two minus one pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for one two minus one two pair\n",
ht_find(htp, &testk4));
}
testk4.x = 1;
testk4.y = 2;
testk4.z = 1;
testk4.u = 2;
if (ht_find(htp, &testk4)) {
fleprintf0("Accidentally find one two one two pair\n");
destroy_ht(htp, DESTROY_KEY_SELF);
return 1;
} else {
printf("We find value %p for one two one two pair\n",
ht_find(htp, &testk4));
}
ht_reset(htp, DELETE_KEY);
printf("After reset the size is %ld\n", ht_size(htp));
destroy_ht(htp, DESTROY_KEY_SELF);
return 0;
}
|