014a58cdd16485113c3f32ded5a2860661ec3c19
1 #include <stdlib.h> //malloc
2 #include <stdio.h> //print
3 #include <string.h> //memcpy
4 #include "../ston/ston.h"
6 /* ansi terminal colors */
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"
16 #define print_val(val) do { \
17 for (i = 0; i < (size_t)columns; i++) \
18 printf("["YELLOW"%x"CLRC"]",val[i]); \
22 #define check_ht(_HT) \
24 { fprintf(stderr,RED"Could not allocate ht32"CLRCN); \
28 printf("ht_size: [units:%i][bytes:%li]\n",ston_ht_size(ht),ston_ht32_size(ht)); \
31 int main(int argc, char* argv[])
33 uint32_t* htval
, previous_val
;
34 uint32_t val
[255], key
;
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
));
47 ht
= ston_ht32_new(columns
,elements
,0,malloc
);
52 printf("ht32_insertx: [%x] = ", key
);
54 ston_ht32_insertx(ht
,key
,val
,0,columns
);
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");
61 previous_val
= ston_ht32_insert(ht
,key
,1,val
[1]);
62 printf("ht32_insert: [%x] = ", key
);
64 printf("Previous value [%x] matches [5]: %s"CLRCN
, previous_val
,
65 (previous_val
== 0x5) ? GREEN
"PASS" : RED
"FAIL");
71 ht
= ston_ht32_new(columns
,elements
,0,malloc
);
73 printf("Filling hashtable with %i entries\n", (int)elements
);
74 for(key
= 0xCEED; elements
--; key
*= 7)
76 for(i
= 1; i
< columns
; i
++)
78 ston_ht32_insertx(ht
,key
,val
,0,columns
);
81 printf("Reading entries: ");
83 for(key
= 0xCEED; elements
--; key
*= 7)
84 { htval
= ston_ht32_row(ht
,key
);
87 for(i
= 1; i
< columns
; i
++)
88 if (htval
[i
] != (uint32_t)(i
* key
))
92 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
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)
100 for(i
= 1; i
< columns
; i
++)
102 ston_ht32_insertx(ht
,key
,val
,0,columns
);
104 printf("Reading entries: ");
107 for(key
= 0xCEED2; cap
--; key
*= 13)
108 { htval
= ston_ht32_row(ht
,key
);
111 for(i
= 1; i
< columns
; i
++)
112 if (htval
[i
] != (uint32_t)(key
* -i
))
116 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
118 printf(GREEN
"PASS"CLRCN
);
121 printf("Post-capacity insertion of %i\n",cap
);
122 for (key
= 0xCEED3; cap
--; key
*= 23)
124 for(i
= 1; i
< columns
; 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");
132 printf("Refilling hashtable with %i entries\n", max_capacity
);
134 for(key
= 0xCEED2; cap
--; key
*= 13)
136 for(i
= 1; i
< columns
; i
++)
138 ston_ht32_insertx(ht
,key
,val
,0,columns
);
140 printf("Reading entries: ");
143 for(key
= 0xCEED2; cap
--; key
*= 13)
144 { htval
= ston_ht32_row(ht
,key
);
147 for (i
= 1; i
< columns
; i
++)
148 if (htval
[i
] != (uint32_t)(key
* i
))
152 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
154 printf(GREEN
"PASS"CLRCN
);
159 printf("\n--------- DHT ----------\n\n");
164 dht
= ston_dht32_new(columns
, elements
, 0, malloc
, free
);
167 printf("Filling Dynamic hashtable with %i entries\n", (int)elements
);
168 for(key
= 0xCEED; elements
--; key
*= 7)
170 for(i
= 1; i
< columns
; i
++)
172 ston_dht32_insertx(dht
,key
,val
,0,columns
);
175 printf("Reading entries: ");
177 for(key
= 0xCEED; elements
--; key
*= 7)
178 { htval
= ston_dht32_row(dht
,key
);
181 for(i
= 1; i
< columns
; i
++)
182 if (htval
[i
] != (uint32_t)(i
* key
))
186 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
188 printf(GREEN
"PASS"CLRCN
);
189 max_capacity
= (ston_up2pow(50 << 1) * (ston_dht_pagemax(dht
) - ston_dht_pagestart(dht
))) - 50000;
191 printf("Overfilling hashtable with %i entries\n", max_capacity
);
192 for(key
= 0xCEED2; cap
--; key
*= 13)
194 for(i
= 1; i
< columns
; i
++)
196 ston_dht32_insertx(dht
,key
,val
,0,columns
);
198 printf("Reading entries: ");
201 for(key
= 0xCEED2; cap
--; key
*= 13)
202 { htval
= ston_dht32_row(dht
,key
);
205 for(i
= 1; i
< columns
; i
++)
206 if (htval
[i
] != (uint32_t)(key
* -i
))
210 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
212 printf(GREEN
"PASS"CLRCN
);
215 printf("Post-capacity insertion of %i\n",cap
);
216 for (key
= 0xCEED3; cap
--; key
*= 23)
218 for(i
= 1; i
< columns
; 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");
226 printf("Refilling hashtable with %i entries\n", max_capacity
);
228 for(key
= 0xCEED2; cap
--; key
*= 13)
230 for(i
= 1; i
< columns
; i
++)
232 ston_dht32_insertx(dht
,key
,val
,0,columns
);
234 printf("Reading entries: ");
237 for(key
= 0xCEED2; cap
--; key
*= 13)
238 { htval
= ston_dht32_row(dht
,key
);
241 for (i
= 1; i
< columns
; i
++)
242 if (htval
[i
] != (uint32_t)(key
* i
))
246 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
248 printf(GREEN
"PASS"CLRCN
);
250 ston_dht32_free(dht
);