-#include "../ston/ston.h"
#include <stdlib.h> //malloc
#include <stdio.h> //print
+#include <string.h> //memcpy
+#include <time.h> //clock
+#include "../ston/ston.h"
+#define XXH_PRIVATE_API
+#include "../xxHash/xxhash.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 startclock(_STR) do { \
+ start = clock(); \
+ printf(_STR": "); \
+ } while (0)
+
+#define startclockf(_STR,...) do { \
+ start = clock(); \
+ printf(_STR": ",__VA_ARGS__); \
+ } while (0)
+
+#define endclock() do { \
+ end = clock(); \
+ printf("%5f",(end - start)/(double)CLOCKS_PER_SEC); \
+ } while (0)
+
+#define endclockn() do { \
+ end = clock(); \
+ printf("%5f\n",(end - start)/(double)CLOCKS_PER_SEC); \
+ } while (0)
+
+#define $($)#$
+#define do_test(_COL,_COUNT,_HT,_FREE,_HTNEW,_HTINSERT,_HTROW,_SEED) do { \
+ printf("[ " $(_HT) " ] "); \
+ columns = _COL; \
+ count = _COUNT; \
+ _HT = _HTNEW; \
+ startclockf("Filling " $(_HT) " with %i entries",count); \
+ while (count--) \
+ { key = XXH32(&count,4,_SEED); \
+ val[0] = key; \
+ for (i = 0; i < columns; i++) \
+ val[i] = key - i; \
+ _HTINSERT(_HT,key,val,0,columns); \
+ } \
+ endclockn(); \
+ count = _COUNT; \
+ fail = 0; \
+ startclock("Reading entries"); \
+ while (count--) \
+ { key = XXH32(&count,4,_SEED); \
+ htval = _HTROW(_HT,key); \
+ if (htval == NULL) \
+ { fail++; \
+ printf("Row returned null\n"); \
+ break; \
+ } \
+ if (*htval != key) \
+ fail++; \
+ for (i = 1; i < columns; i++) \
+ if (htval[i] != key - i) \
+ fail++; \
+ } \
+ endclock(); \
+ if (fail) \
+ printf(RED"\nFAIL"CLRC"(%i)\n", fail); \
+ else \
+ printf(GREEN"\nPASS"CLRCN); \
+ _FREE(_HT); \
+ } while (0)
+
+void testiter(void*,void*,void*);
+void testiter
+( void* key,
+ void* val,
+ void* data
+)
+{ *(uint32_t*)ston_dht_val((ston_dht)data,key) = *(uint32_t*)val; }
int main(int argc, char* argv[])
-{ ston_ht ht;
+{ static ston_ht ht;
+ static ston_dht dht, dht2;
+ uint32_t val[255], key;
uint32_t* htval;
+ size_t i;
+ uint16_t columns;
+ int fail;
+ clock_t start, end;
+ int count;
+ printf("\nLow usage:\n");
+ do_test(2,200,ht,free,ston_ht32_new(2,200,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+ do_test(5,200,ht,free,ston_ht32_new(5,200,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+ do_test(2,200,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+ do_test(5,200,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+ printf("\nLow-mid usage:\n");
+ do_test(2,10000,ht,free,ston_ht32_new(2,10000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+ do_test(5,10000,ht,free,ston_ht32_new(5,10000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+ do_test(2,10000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+ do_test(5,10000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+ printf("\nMid usage:\n");
+ do_test(2,500000,ht,free,ston_ht32_new(2,500000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+ do_test(5,500000,ht,free,ston_ht32_new(5,500000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED);
+ do_test(2,500000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+ do_test(5,500000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED);
+
+ printf("\nIterator:\n");
+ dht = ston_dht_new(sizeof(uint32_t),sizeof(uint32_t),malloc,free);
+ dht2 = ston_dht_new(sizeof(uint32_t),sizeof(uint32_t),malloc,free);
+ for (count = 0; count < 50000; count++)
+ { key = XXH32(&count,4,0xCEED);
+ * (uint32_t*) ston_dht_val(dht,&key) = key * key;
+ }
+ startclock("Duplicating 50000 element DHT by iteration");
+ ston_dht_iterate(dht,testiter,dht2);
+ endclockn();
+ fail = 0;
+ startclock("Checking for correctness");
+ for (count = 0; count < 50000; count++)
+ { key = XXH32(&count,4,0xCEED);
+ if (*(uint32_t*)ston_dht_val(dht2,&key) != key * key)
+ fail++;
+ }
+ endclock();
+ if (fail)
+ printf(RED"\nFAIL"CLRC"(%i)\n", fail);
+ else
+ printf(GREEN"\nPASS"CLRCN);
+
+
+ /* printf("\nMid-high usage:\n"); */
+ /* do_test(2,9000000,ht,free,ston_ht32_new(2,9000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
+ /* do_test(5,9000000,ht,free,ston_ht32_new(5,9000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
+ /* do_test(2,9000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
+ /* do_test(2,9000000,dht,ston_dht_free,ston_dht32_new(5,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
+
+ /* printf("\nHigh usage:\n"); */
+ /* do_test(2,90000000,ht,free,ston_ht32_new(2,90000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
+ /* do_test(2,90000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
+
+ /* printf("\nHuge usage:\n"); */
+ /* do_test(2,100000000,ht,free,ston_ht32_new(2,100000000,0,malloc),ston_ht32_insertx,ston_ht32_row,0xCEED); */
+ /* do_test(2,100000000,dht,ston_dht_free,ston_dht32_new(2,malloc,free),ston_dht32_insertx,ston_dht32_row,0xCEED); */
- 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);
- free(ht);
- return 0;
+ printf("\nDONE\n");
}