From 3abf4c09ce3ea173ac9840e97788ca4c477a8aa1 Mon Sep 17 00:00:00 2001 From: comex Date: Thu, 29 Jan 2015 01:21:15 -0500 Subject: port some old code --- test/test-htab.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/test-htab.c (limited to 'test') diff --git a/test/test-htab.c b/test/test-htab.c new file mode 100644 index 0000000..2986de8 --- /dev/null +++ b/test/test-htab.c @@ -0,0 +1,49 @@ +#include "cbit/htab.h" +#include +#include +struct teststr { + bool valid; + const char *what; +}; +#define ts_null(ts) ({ if (0) printf("null? %p\n", *ts); !*ts; }) +#define ts_eq(ts, cp) ({ if (0) printf("eq? %p %p\n", *ts, *cp); !strcmp(*(ts), *(cp)); }) +#define ts_hash(strp) strlen(*(strp)) +DECL_EXTERN_HTAB_KEY(teststr, const char *); +DECL_HTAB(teststr_int, teststr, int); + +int main() { + struct htab_teststr_int *hp; + HTAB_STORAGE(teststr_int) stor; + HTAB_STORAGE_INIT(&stor, teststr_int); + hp = &stor.h; + for(int i = 0; i < 100; i++) { + const char *k; + asprintf((char **) &k, "foo%d", i); + *htab_setp_teststr_int(hp, &k) = i; + } + const char *to_remove = "foo31"; + htab_remove_teststr_int(hp, &to_remove); + HTAB_FOREACH(hp, const char **k, int *v, teststr_int) { + if(*v % 10 == 1) + printf("%s -> %d\n", *k, *v); + } + htab_free_teststr_int(hp); +} + +/* +expect-output<< +foo91 -> 91 +foo21 -> 21 +foo1 -> 1 +foo11 -> 11 +foo31 -> 31 +foo41 -> 41 +foo51 -> 51 +foo61 -> 61 +foo71 -> 71 +foo81 -> 81 +>> +expect-exit 0 +*/ + +IMPL_HTAB_KEY(teststr, ts_hash, ts_eq, ts_null, /*nil_byte*/ 0); -- cgit v1.2.3