non-dynamic hashtables finished testing
[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
40 printf("up2pow [5] = [%i]\n",ston_up2pow(5));
41 for (i = 0; i < 255; i += 7)
42 printf("trailing0 [%X] = [%x]\n",(uint32_t)i,ston_trailing0(i));
43
44 columns = 2;
45 elements = 1;
46 ht = ston_ht32_new(columns,elements,0,malloc);
47 check_ht(ht);
48 key = 0xFF;
49 val[0] = key;
50 val[1] = 0x5;
51 printf("ht32_insertx: [%x] = ", key);
52 print_val(val);
53 ston_ht32_insertx(ht,key,val,0,columns);
54 idx = 1;
55 htval = ston_ht32_row(ht,key);
56 printf("Read value [%x] matches: %s"CLRCN, htval[idx],
57 (htval[idx] == val[idx]) ? GREEN"PASS" : RED"FAIL");
58
59 val[1] = 0x2;
60 previous_val = ston_ht32_insert(ht,key,1,val[1]);
61 printf("ht32_insert: [%x] = ", key);
62 print_val(val);
63 printf("Previous value [%x] matches [5]: %s"CLRCN, previous_val,
64 (previous_val == 0x5) ? GREEN"PASS" : RED"FAIL");
65
66 free(ht);
67
68 columns = 4;
69 elements = 20;
70 ht = ston_ht32_new(columns,elements,0,malloc);
71 check_ht(ht);
72 printf("Filling hashtable with entries\n");
73 for(key = 0xCEED; elements--; key *= 7)
74 { val[0] = key;
75 for(i = 1; i < columns; i++)
76 val[i] = i * key;
77 ston_ht32_insertx(ht,key,val,0,columns);
78 }
79 printf("Reading entries\n");
80 elements = 20;
81 for(key = 0xCEED; elements--; key *= 7)
82 { htval = ston_ht32_row(ht,key);
83 printf("[%s%x"CLRC"]",(*htval == key) ? GREEN : RED,*htval);
84 for(i = 1; i < columns; i++)
85 printf("[%s%x"CLRC"]",(htval[i] == (uint32_t)(i * key)) ? GREEN : RED,htval[i]);
86 putchar('\n');
87 }
88 int max_capacity = ston_up2pow(20 << 1) - 20;
89 printf("Overfilling hashtable with %i entries\n", max_capacity);
90 int cap = max_capacity;
91 for(key = 0xCEED2; cap--; key *= 13)
92 { val[0] = key;
93 for(i = 1; i < columns; i++)
94 val[i] = key * -i;
95 ston_ht32_insertx(ht,key,val,0,columns);
96 }
97 printf("Reading entries\n");
98 cap = max_capacity;
99 for(key = 0xCEED2; cap--; key *= 13)
100 { htval = ston_ht32_row(ht,key);
101 printf("[%s%x"CLRC"]",(*htval == key) ? GREEN : RED,*htval);
102 for(i = 1; i < columns; i++)
103 printf("[%s%x"CLRC"]",(htval[i] == (uint32_t)(key * -i)) ? GREEN : RED,htval[i]);
104 printf("\n");
105 }
106
107 free(ht);
108 return 0;
109 }