dht implementation first round tests pass
[henge/apc.git] / src / testston.c
1 #include <stdlib.h> //malloc
2 #include <stdio.h> //print
3 #include <string.h> //memcpy
4 #include "../ston/ston.h"
5
6 /* ansi terminal colors */
7 #define RED "\x1b[31m"
8 #define GREEN "\x1b[32m"
9 #define YELLOW "\x1b[33m"
10 #define BLUE "\x1b[34m"
11 #define MAGENTA "\x1b[35m"
12 #define CYAN "\x1b[36m"
13 #define CLRC "\x1b[0m" //clear current color
14 #define CLRCN CLRC"\n"
15
16 #define print_val(val) do { \
17 for (i = 0; i < (size_t)columns; i++) \
18 printf("["YELLOW"%x"CLRC"]",val[i]); \
19 printf("\n"); \
20 } while(0)
21
22 #define check_ht(_HT) \
23 if ((_HT) == NULL) \
24 { fprintf(stderr,RED"Could not allocate ht32"CLRCN); \
25 return -1; \
26 } \
27 else \
28 printf("ht_size: [units:%i][bytes:%li]\n",ston_ht_size(ht),ston_ht32_size(ht)); \
29
30
31 int main(int argc, char* argv[])
32 { static ston_ht ht;
33 uint32_t* htval, previous_val;
34 uint32_t val[255], key;
35 size_t idx;
36 size_t i;
37 size_t elements;
38 uint16_t columns;
39 int fail;
40
41 printf("up2pow [5] = [%i]\n",ston_up2pow(5));
42 for (i = 0; i < 77; i += 7)
43 printf("trailing0 [%X] = [%x]\n",(uint32_t)i,ston_trailing0(i));
44
45 columns = 2;
46 elements = 1;
47 ht = ston_ht32_new(columns,elements,0,malloc);
48 check_ht(ht);
49 key = 0xFF;
50 val[0] = key;
51 val[1] = 0x5;
52 printf("ht32_insertx: [%x] = ", key);
53 print_val(val);
54 ston_ht32_insertx(ht,key,val,0,columns);
55 idx = 1;
56 htval = ston_ht32_row(ht,key);
57 printf("Read value [%x] matches: %s"CLRCN, htval[idx],
58 (htval[idx] == val[idx]) ? GREEN"PASS" : RED"FAIL");
59
60 val[1] = 0x2;
61 previous_val = ston_ht32_insert(ht,key,1,val[1]);
62 printf("ht32_insert: [%x] = ", key);
63 print_val(val);
64 printf("Previous value [%x] matches [5]: %s"CLRCN, previous_val,
65 (previous_val == 0x5) ? GREEN"PASS" : RED"FAIL");
66
67 free(ht);
68
69 columns = 4;
70 elements = 20;
71 ht = ston_ht32_new(columns,elements,0,malloc);
72 check_ht(ht);
73 printf("Filling hashtable with %i entries\n", (int)elements);
74 for(key = 0xCEED; elements--; key *= 7)
75 { val[0] = key;
76 for(i = 1; i < columns; i++)
77 val[i] = i * key;
78 ston_ht32_insertx(ht,key,val,0,columns);
79 }
80 elements = 20;
81 printf("Reading entries: ");
82 fail = 0;
83 for(key = 0xCEED; elements--; key *= 7)
84 { htval = ston_ht32_row(ht,key);
85 if (*htval != key)
86 fail++;
87 for(i = 1; i < columns; i++)
88 if (htval[i] != (uint32_t)(i * key))
89 fail++;
90 }
91 if (fail)
92 printf(RED"FAIL"CLRC"(%i)\n", fail);
93 else
94 printf(GREEN"PASS"CLRCN);
95 int max_capacity = ston_up2pow(20 << 1) - 20;
96 int cap = max_capacity;
97 printf("Overfilling hashtable with %i entries\n", max_capacity);
98 for(key = 0xCEED2; cap--; key *= 13)
99 { val[0] = key;
100 for(i = 1; i < columns; i++)
101 val[i] = key * -i;
102 ston_ht32_insertx(ht,key,val,0,columns);
103 }
104 printf("Reading entries: ");
105 cap = max_capacity;
106 fail = 0;
107 for(key = 0xCEED2; cap--; key *= 13)
108 { htval = ston_ht32_row(ht,key);
109 if (*htval != key)
110 fail++;
111 for(i = 1; i < columns; i++)
112 if (htval[i] != (uint32_t)(key * -i))
113 fail++;
114 }
115 if (fail)
116 printf(RED"FAIL"CLRC"(%i)\n", fail);
117 else
118 printf(GREEN"PASS"CLRCN);
119
120 cap = 20;
121 printf("Post-capacity insertion of %i\n",cap);
122 for (key = 0xCEED3; cap--; key *= 23)
123 { val[0] = key;
124 for(i = 1; i < columns; i++)
125 val[i] = key * -i;
126 size_t count = ston_ht32_insertx(ht,key,val,0,columns);
127 printf("Insertion %2i wrote %i bytes: %s"CLRCN, (int)cap, (int) count,
128 (count == 0) ? GREEN"PASS" : RED"FAIL");
129 }
130
131
132 printf("Refilling hashtable with %i entries\n", max_capacity);
133 cap = max_capacity;
134 for(key = 0xCEED2; cap--; key *= 13)
135 { val[0] = key;
136 for(i = 1; i < columns; i++)
137 val[i] = key * i;
138 ston_ht32_insertx(ht,key,val,0,columns);
139 }
140 printf("Reading entries: ");
141 cap = max_capacity;
142 fail = 0;
143 for(key = 0xCEED2; cap--; key *= 13)
144 { htval = ston_ht32_row(ht,key);
145 if (*htval != key)
146 fail ++;
147 for (i = 1; i < columns; i++)
148 if (htval[i] != (uint32_t)(key * i))
149 fail++;
150 }
151 if (fail)
152 printf(RED"FAIL"CLRC"(%i)\n", fail);
153 else
154 printf(GREEN"PASS"CLRCN);
155
156 free(ht);
157
158
159 printf("\n--------- DHT ----------\n\n");
160
161 ston_dht dht;
162 elements = 50;
163 columns = 6;
164 dht = ston_dht32_new(columns, elements, 0, malloc, free);
165 check_ht(dht);
166 elements = 50000;
167 printf("Filling Dynamic hashtable with %i entries\n", (int)elements);
168 for(key = 0xCEED; elements--; key *= 7)
169 { val[0] = key;
170 for(i = 1; i < columns; i++)
171 val[i] = i * key;
172 ston_dht32_insertx(dht,key,val,0,columns);
173 }
174 elements = 50000;
175 printf("Reading entries: ");
176 fail = 0;
177 for(key = 0xCEED; elements--; key *= 7)
178 { htval = ston_dht32_row(dht,key);
179 if (*htval != key)
180 fail++;
181 for(i = 1; i < columns; i++)
182 if (htval[i] != (uint32_t)(i * key))
183 fail++;
184 }
185 if (fail)
186 printf(RED"FAIL"CLRC"(%i)\n", fail);
187 else
188 printf(GREEN"PASS"CLRCN);
189 max_capacity = (ston_up2pow(50 << 1) * (ston_dht_pagemax(dht) - ston_dht_pagestart(dht))) - 50000;
190 cap = max_capacity;
191 printf("Overfilling hashtable with %i entries\n", max_capacity);
192 for(key = 0xCEED2; cap--; key *= 13)
193 { val[0] = key;
194 for(i = 1; i < columns; i++)
195 val[i] = key * -i;
196 ston_dht32_insertx(dht,key,val,0,columns);
197 }
198 printf("Reading entries: ");
199 cap = max_capacity;
200 fail = 0;
201 for(key = 0xCEED2; cap--; key *= 13)
202 { htval = ston_dht32_row(dht,key);
203 if (*htval != key)
204 fail++;
205 for(i = 1; i < columns; i++)
206 if (htval[i] != (uint32_t)(key * -i))
207 fail++;
208 }
209 if (fail)
210 printf(RED"FAIL"CLRC"(%i)\n", fail);
211 else
212 printf(GREEN"PASS"CLRCN);
213
214 cap = 20;
215 printf("Post-capacity insertion of %i\n",cap);
216 for (key = 0xCEED3; cap--; key *= 23)
217 { val[0] = key;
218 for(i = 1; i < columns; i++)
219 val[i] = key * -i;
220 size_t count = ston_dht32_insertx(dht,key,val,0,columns);
221 printf("Insertion %2i wrote %i bytes: %s"CLRCN, (int)cap, (int) count,
222 (count == 0) ? GREEN"PASS" : RED"FAIL");
223 }
224
225
226 printf("Refilling hashtable with %i entries\n", max_capacity);
227 cap = max_capacity;
228 for(key = 0xCEED2; cap--; key *= 13)
229 { val[0] = key;
230 for(i = 1; i < columns; i++)
231 val[i] = key * i;
232 ston_dht32_insertx(dht,key,val,0,columns);
233 }
234 printf("Reading entries: ");
235 cap = max_capacity;
236 fail = 0;
237 for(key = 0xCEED2; cap--; key *= 13)
238 { htval = ston_dht32_row(dht,key);
239 if (*htval != key)
240 fail ++;
241 for (i = 1; i < columns; i++)
242 if (htval[i] != (uint32_t)(key * i))
243 fail++;
244 }
245 if (fail)
246 printf(RED"FAIL"CLRC"(%i)\n", fail);
247 else
248 printf(GREEN"PASS"CLRCN);
249
250 ston_dht32_free(dht);
251
252 return 0;
253 }