X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Ftestston.c;fp=src%2Ftestston.c;h=32910b0bc2806c3ce715f8e236ececf534e0dfe2;hp=71c01102489e50ca383fc74fb361a09ff89fc49c;hb=30e125e6bcfa71297ef4d592b0c80346f5688a1d;hpb=b0013aa7f2237fb88daf3f2ba856c8cdfe222fa2 diff --git a/src/testston.c b/src/testston.c index 71c0110..32910b0 100644 --- a/src/testston.c +++ b/src/testston.c @@ -1,16 +1,109 @@ -#include "../ston/ston.h" #include //malloc #include //print +#include //memcpy +#include "../ston/ston.h" + +/* ansi terminal colors */ +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define YELLOW "\x1b[33m" +#define BLUE "\x1b[34m" +#define MAGENTA "\x1b[35m" +#define CYAN "\x1b[36m" +#define CLRC "\x1b[0m" //clear current color +#define CLRCN CLRC"\n" + +#define print_val(val) do { \ + for (i = 0; i < (size_t)columns; i++) \ + printf("["YELLOW"%x"CLRC"]",val[i]); \ + printf("\n"); \ + } while(0) + +#define check_ht(_HT) \ + if ((_HT) == NULL) \ + { fprintf(stderr,RED"Could not allocate ht32"CLRCN); \ + return -1; \ + } \ + else \ + printf("ht_size: [units:%i][bytes:%li]\n",ston_ht_size(ht),ston_ht32_size(ht)); \ + int main(int argc, char* argv[]) -{ ston_ht ht; - uint32_t* htval; - - if ((ht = ston_ht32_new(2,1,0,malloc)) == NULL) - fprintf(stderr,"Could not allocate ht32\n"); - ston_ht32_insertx(ht,50,1,200); - htval = ston_ht32_entry(ht,50,1); - printf("[50][1] = %i\n",*htval); +{ static ston_ht ht; + uint32_t* htval, previous_val; + uint32_t val[255], key; + size_t idx; + size_t i; + size_t elements; + uint16_t columns; + + printf("up2pow [5] = [%i]\n",ston_up2pow(5)); + for (i = 0; i < 255; i += 7) + printf("trailing0 [%X] = [%x]\n",(uint32_t)i,ston_trailing0(i)); + + columns = 2; + elements = 1; + ht = ston_ht32_new(columns,elements,0,malloc); + check_ht(ht); + key = 0xFF; + val[0] = key; + val[1] = 0x5; + printf("ht32_insertx: [%x] = ", key); + print_val(val); + ston_ht32_insertx(ht,key,val,0,columns); + idx = 1; + htval = ston_ht32_row(ht,key); + printf("Read value [%x] matches: %s"CLRCN, htval[idx], + (htval[idx] == val[idx]) ? GREEN"PASS" : RED"FAIL"); + + val[1] = 0x2; + previous_val = ston_ht32_insert(ht,key,1,val[1]); + printf("ht32_insert: [%x] = ", key); + print_val(val); + printf("Previous value [%x] matches [5]: %s"CLRCN, previous_val, + (previous_val == 0x5) ? GREEN"PASS" : RED"FAIL"); + + free(ht); + + columns = 4; + elements = 20; + ht = ston_ht32_new(columns,elements,0,malloc); + check_ht(ht); + printf("Filling hashtable with entries\n"); + for(key = 0xCEED; elements--; key *= 7) + { val[0] = key; + for(i = 1; i < columns; i++) + val[i] = i * key; + ston_ht32_insertx(ht,key,val,0,columns); + } + printf("Reading entries\n"); + elements = 20; + for(key = 0xCEED; elements--; key *= 7) + { htval = ston_ht32_row(ht,key); + printf("[%s%x"CLRC"]",(*htval == key) ? GREEN : RED,*htval); + for(i = 1; i < columns; i++) + printf("[%s%x"CLRC"]",(htval[i] == (uint32_t)(i * key)) ? GREEN : RED,htval[i]); + putchar('\n'); + } + int max_capacity = ston_up2pow(20 << 1) - 20; + printf("Overfilling hashtable with %i entries\n", max_capacity); + int cap = max_capacity; + for(key = 0xCEED2; cap--; key *= 13) + { val[0] = key; + for(i = 1; i < columns; i++) + val[i] = key * -i; + ston_ht32_insertx(ht,key,val,0,columns); + } + printf("Reading entries\n"); + cap = max_capacity; + for(key = 0xCEED2; cap--; key *= 13) + { htval = ston_ht32_row(ht,key); + printf("[%s%x"CLRC"]",(*htval == key) ? GREEN : RED,*htval); + for(i = 1; i < columns; i++) + printf("[%s%x"CLRC"]",(htval[i] == (uint32_t)(key * -i)) ? GREEN : RED,htval[i]); + printf("\n"); + } + free(ht); return 0; }