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