1 #include <stdlib.h> //malloc
2 #include <stdio.h> //print
3 #include <string.h> //memcpy
4 #include <time.h> //clock
5 #include "../ston/ston.h"
6 #define XXH_PRIVATE_API
7 #include "../xxHash/xxhash.h"
9 /* ansi terminal colors */
10 #define RED "\x1b[31m"
11 #define GREEN "\x1b[32m"
12 #define YELLOW "\x1b[33m"
13 #define BLUE "\x1b[34m"
14 #define MAGENTA "\x1b[35m"
15 #define CYAN "\x1b[36m"
16 #define CLRC "\x1b[0m" //clear current color
17 #define CLRCN CLRC"\n"
19 #define startclock(_STR) do { \
24 #define startclockf(_STR,...) do { \
26 printf(_STR": ",__VA_ARGS__); \
29 #define endclock() do { \
31 printf("%5f",(end - start)/(double)CLOCKS_PER_SEC); \
34 #define endclockn() do { \
36 printf("%5f\n",(end - start)/(double)CLOCKS_PER_SEC); \
40 #define do_test(_COL,_COUNT,_HT,_FREE,_HTNEW,_HTINSERT,_HTROW,_SEED) do { \
41 printf("[ " $(_HT) " ] "); \
45 startclockf("Filling " $(_HT) " with %i entries",count); \
47 { key = XXH32(&count,4,_SEED); \
49 for (i = 0; i < columns; i++) \
51 _HTINSERT(_HT,key,val,0,columns); \
56 startclock("Reading entries"); \
58 { key = XXH32(&count,4,_SEED); \
59 htval = _HTROW(_HT,key); \
62 printf("Row returned null\n"); \
67 for (i = 1; i < columns; i++) \
68 if (htval[i] != key - i) \
73 printf(RED"\nFAIL"CLRC"(%i)\n", fail); \
75 printf(GREEN"\nPASS"CLRCN); \
79 void testiter(void*,void*,void*);
85 { *(uint32_t*)ston_dht_val((ston_dht
)data
,key
) = *(uint32_t*)val
; }
87 int main(int argc
, char* argv
[])
89 static ston_dht dht
, dht2
;
90 uint32_t val
[255], key
;
97 printf("\nLow usage:\n");
98 do_test(2,200,ht
,free
,ston_ht32_new(2,200,0,malloc
),ston_ht32_insertx
,ston_ht32_row
,0xCEED);
99 do_test(5,200,ht
,free
,ston_ht32_new(5,200,0,malloc
),ston_ht32_insertx
,ston_ht32_row
,0xCEED);
100 do_test(2,200,dht
,ston_dht_free
,ston_dht32_new(2,malloc
,free
),ston_dht32_insertx
,ston_dht32_row
,0xCEED);
101 do_test(5,200,dht
,ston_dht_free
,ston_dht32_new(5,malloc
,free
),ston_dht32_insertx
,ston_dht32_row
,0xCEED);
103 printf("\nLow-mid usage:\n");
104 do_test(2,10000,ht
,free
,ston_ht32_new(2,10000,0,malloc
),ston_ht32_insertx
,ston_ht32_row
,0xCEED);
105 do_test(5,10000,ht
,free
,ston_ht32_new(5,10000,0,malloc
),ston_ht32_insertx
,ston_ht32_row
,0xCEED);
106 do_test(2,10000,dht
,ston_dht_free
,ston_dht32_new(2,malloc
,free
),ston_dht32_insertx
,ston_dht32_row
,0xCEED);
107 do_test(5,10000,dht
,ston_dht_free
,ston_dht32_new(5,malloc
,free
),ston_dht32_insertx
,ston_dht32_row
,0xCEED);
109 printf("\nMid usage:\n");
110 do_test(2,500000,ht
,free
,ston_ht32_new(2,500000,0,malloc
),ston_ht32_insertx
,ston_ht32_row
,0xCEED);
111 do_test(5,500000,ht
,free
,ston_ht32_new(5,500000,0,malloc
),ston_ht32_insertx
,ston_ht32_row
,0xCEED);
112 do_test(2,500000,dht
,ston_dht_free
,ston_dht32_new(2,malloc
,free
),ston_dht32_insertx
,ston_dht32_row
,0xCEED);
113 do_test(5,500000,dht
,ston_dht_free
,ston_dht32_new(5,malloc
,free
),ston_dht32_insertx
,ston_dht32_row
,0xCEED);
115 printf("\nIterator:\n");
116 dht
= ston_dht_new(sizeof(uint32_t),sizeof(uint32_t),malloc
,free
);
117 dht2
= ston_dht_new(sizeof(uint32_t),sizeof(uint32_t),malloc
,free
);
118 for (count
= 0; count
< 50000; count
++)
119 { key
= XXH32(&count
,4,0xCEED);
120 * (uint32_t*) ston_dht_val(dht
,&key
) = key
* key
;
122 startclock("Duplicating 50000 element DHT by iteration");
123 ston_dht_iterate(dht
,testiter
,dht2
);
126 startclock("Checking for correctness");
127 for (count
= 0; count
< 50000; count
++)
128 { key
= XXH32(&count
,4,0xCEED);
129 if (*(uint32_t*)ston_dht_val(dht2
,&key
) != key
* key
)
134 printf(RED
"\nFAIL"CLRC
"(%i)\n", fail
);
136 printf(GREEN
"\nPASS"CLRCN
);
139 /* printf("\nMid-high usage:\n"); */
140 /* do_test(2,9000000,ht,free,ston_ht32_new(2,9000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
141 /* do_test(5,9000000,ht,free,ston_ht32_new(5,9000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
142 /* do_test(2,9000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
143 /* do_test(2,9000000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
145 /* printf("\nHigh usage:\n"); */
146 /* do_test(2,90000000,ht,free,ston_ht32_new(2,90000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
147 /* do_test(2,90000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
149 /* printf("\nHuge usage:\n"); */
150 /* do_test(2,100000000,ht,free,ston_ht32_new(2,100000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
151 /* do_test(2,100000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */