aboutsummaryrefslogtreecommitdiff
path: root/test/test-htab.c
diff options
context:
space:
mode:
authorcomex2015-01-29 01:21:15 -0500
committercomex2015-01-29 01:21:15 -0500
commit3abf4c09ce3ea173ac9840e97788ca4c477a8aa1 (patch)
treebcd129df9b384bc4bb646445e44d0f46aa4a8902 /test/test-htab.c
parent***yawn*** (diff)
downloadsubstitute-3abf4c09ce3ea173ac9840e97788ca4c477a8aa1.tar.gz
port some old code
Diffstat (limited to '')
-rw-r--r--test/test-htab.c49
1 files changed, 49 insertions, 0 deletions
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 <string.h>
+#include <stdio.h>
+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);