0f0f1f9af8aab63862924305c84dfdddd468906c
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)); \
30 #define check_dht(_HT) \
32 { fprintf(stderr,RED"Could not allocate dht32"CLRCN); \
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)); \
39 int main(int argc, char* argv[])
41 uint32_t* htval
, previous_val
;
42 uint32_t val
[255], key
;
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
));
55 ht
= ston_ht32_new(columns
,elements
,0,malloc
);
60 printf("ht32_insertx: [%x] = ", key
);
62 ston_ht32_insertx(ht
,key
,val
,0,columns
);
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");
69 previous_val
= ston_ht32_insert(ht
,key
,1,val
[1]);
70 printf("ht32_insert: [%x] = ", key
);
72 printf("Previous value [%x] matches [5]: %s"CLRCN
, previous_val
,
73 (previous_val
== 0x5) ? GREEN
"PASS" : RED
"FAIL");
79 ht
= ston_ht32_new(columns
,elements
,0,malloc
);
81 printf("Filling hashtable with %i entries\n", (int)elements
);
82 for(key
= 0xCEED; elements
--; key
*= 7)
84 for(i
= 1; i
< columns
; i
++)
86 ston_ht32_insertx(ht
,key
,val
,0,columns
);
89 printf("Reading entries: ");
91 for(key
= 0xCEED; elements
--; key
*= 7)
92 { htval
= ston_ht32_row(ht
,key
);
95 for(i
= 1; i
< columns
; i
++)
96 if (htval
[i
] != (uint32_t)(i
* key
))
100 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
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)
108 for(i
= 1; i
< columns
; i
++)
110 ston_ht32_insertx(ht
,key
,val
,0,columns
);
112 printf("Reading entries: ");
115 for(key
= 0xCEED2; cap
--; key
*= 13)
116 { htval
= ston_ht32_row(ht
,key
);
119 for(i
= 1; i
< columns
; i
++)
120 if (htval
[i
] != (uint32_t)(key
* -i
))
124 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
126 printf(GREEN
"PASS"CLRCN
);
129 printf("Post-capacity insertion of %i\n",cap
);
130 for (key
= 0xCEED3; cap
--; key
*= 23)
132 for(i
= 1; i
< columns
; 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");
140 printf("Refilling hashtable with %i entries\n", max_capacity
);
142 for(key
= 0xCEED2; cap
--; key
*= 13)
144 for(i
= 1; i
< columns
; i
++)
146 ston_ht32_insertx(ht
,key
,val
,0,columns
);
148 printf("Reading entries: ");
151 for(key
= 0xCEED2; cap
--; key
*= 13)
152 { htval
= ston_ht32_row(ht
,key
);
155 for (i
= 1; i
< columns
; i
++)
156 if (htval
[i
] != (uint32_t)(key
* i
))
160 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
162 printf(GREEN
"PASS"CLRCN
);
167 printf("\n--------- DHT ----------\n\n");
172 dht
= ston_dht32_new(columns
, malloc
, free
);
175 printf("Filling Dynamic hashtable with %i entries\n", (int)elements
);
176 for(key
= 0xCEED; elements
--; key
*= 7)
178 for(i
= 1; i
< columns
; i
++)
180 ston_dht32_insertx(dht
,key
,val
,0,columns
);
183 printf("Reading entries: ");
185 for(key
= 0xCEED; elements
--; key
*= 7)
186 { htval
= ston_dht32_row(dht
,key
);
189 for(i
= 1; i
< columns
; i
++)
190 if (htval
[i
] != (uint32_t)(i
* key
))
194 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
196 printf(GREEN
"PASS"CLRCN
);
197 max_capacity
= 100000;
199 printf("Overfilling hashtable with %i entries\n", max_capacity
);
200 for(key
= 0xCEED2; cap
--; key
*= 13)
202 for(i
= 1; i
< columns
; i
++)
204 ston_dht32_insertx(dht
,key
,val
,0,columns
);
206 printf("Reading entries: ");
209 for(key
= 0xCEED2; cap
--; key
*= 13)
210 { htval
= ston_dht32_row(dht
,key
);
213 for(i
= 1; i
< columns
; i
++)
214 if (htval
[i
] != (uint32_t)(key
* -i
))
218 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
220 printf(GREEN
"PASS"CLRCN
);
222 max_capacity
= 5000000;
223 printf("Refilling hashtable with %i entries\n", max_capacity
);
225 for(key
= 0xCEED2; cap
--; key
*= 13)
227 for(i
= 1; i
< columns
; i
++)
229 ston_dht32_insertx(dht
,key
,val
,0,columns
);
231 printf("Reading entries: ");
234 for(key
= 0xCEED2; cap
--; key
*= 13)
235 { htval
= ston_dht32_row(dht
,key
);
238 for (i
= 1; i
< columns
; i
++)
239 if (htval
[i
] != (uint32_t)(key
* i
))
243 printf(RED
"FAIL"CLRC
"(%i)\n", fail
);
245 printf(GREEN
"PASS"CLRCN
);