From 13248c2a164eb595cd28b5621f090d6361a8fd7d Mon Sep 17 00:00:00 2001 From: vraatikka Date: Mon, 5 Aug 2013 18:04:02 +0300 Subject: [PATCH] Added server/core/test directory, makefile and testhash.c for testing hashtable implementation. In makefile, there is target all, which compiles and executes tests. --- server/core/test/makefile | 29 ++++++++++ server/core/test/testhash.c | 108 ++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 server/core/test/makefile create mode 100644 server/core/test/testhash.c diff --git a/server/core/test/makefile b/server/core/test/makefile new file mode 100644 index 000000000..191dd25cf --- /dev/null +++ b/server/core/test/makefile @@ -0,0 +1,29 @@ +include ../../../build_gateway.inc +include ../../../makefile.inc + +CC=cc + +clean: + - $(DEL) *.o + - $(DEL) testhash + - $(DEL) *~ + +all: + $(MAKE) clean + $(MAKE) buildall + $(MAKE) runall + +buildall : + $(CC) $(CFLAGS) \ + -I$(ROOT_PATH)/server/include \ + -I$(ROOT_PATH)/utils \ + testhash.c ../hashtable.o ../atomic.o ../spinlock.o -o testhash \ + +runall: + - ./testhash 0 1 + - ./testhash 10 1 + - ./testhash 1000 10 + - ./testhash 10 0 + - ./testhash 1500 17 + - ./testhash 1 1 + diff --git a/server/core/test/testhash.c b/server/core/test/testhash.c new file mode 100644 index 000000000..3e98a0b2f --- /dev/null +++ b/server/core/test/testhash.c @@ -0,0 +1,108 @@ +#include +#include +#include + +#include "../../include/hashtable.h" + +static int hfun(void* key); +static int cmpfun (void *, void *); + +static int hfun( + void* key) +{ + return *(int *)key; +} + + +static int cmpfun( + void* v1, + void* v2) +{ + int i1; + int i2; + + i1 = *(int *)v1; + i2 = *(int *)v2; + + return (i1 < i2 ? -1 : (i1 > i2 ? 1 : 0)); +} + + + + +/** + * @node Simple test which creates hashtable and frees it. Size and number of entries + * sre specified by user and passed as arguments. + * + * Parameters: + * @param argc - + * + * + * @param argv - + * + * + * @return + * + * + * @details (write detailed description here) + * + */ +int main(int argc, char** argv) +{ + HASHTABLE* h; + int nelems; + int i; + int* val_arr; + int argsize; + int hsize; + int argelems; + int longest; + + if (argc != 3) { + fprintf(stderr, "\nWrong number of arguments. Usage " + ":\n\n\ttesthash <# of elements> <# hash size> " + " \n\n"); + return 1; + } + + argelems = strtol(argv[1], NULL, 10); + argsize = strtol(argv[2], NULL, 10); + + ss_dfprintf(stderr, + "testhash : creating hash table of size %d, including %d " + "elements in total.", + argsize, + argelems); + + val_arr = (int *)malloc(sizeof(void *)*argelems); + + h = hashtable_alloc(argsize, hfun, cmpfun); + + ss_dfprintf(stderr, "\t..done\nAdd %d elements to hash table.", argelems); + + for (i=0; i