summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/INDEX2
-rw-r--r--test/Makefile3
-rw-r--r--test/dict.c54
-rw-r--r--test/rbtree.c87
4 files changed, 145 insertions, 1 deletions
diff --git a/test/INDEX b/test/INDEX
index 772c82f..0042c36 100644
--- a/test/INDEX
+++ b/test/INDEX
@@ -6,6 +6,8 @@ charset Charset initialisation/finalisation
parserutils Library initialisation/finalisation
aliases Encoding alias handling
cscodec Charset codec implementation cscodec
+dict Dictionary handling
+rbtree Red-black tree implementation
filter Input stream filtering
inputstream Inputstream handling input
diff --git a/test/Makefile b/test/Makefile
index 2ed0b44..4c5caac 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -35,7 +35,8 @@ d := $(DIR)
override CFLAGS := $(CFLAGS) -I$(TOP)/src/ -I$(d)
# Tests
-TESTS_$(d) := aliases cscodec charset filter inputstream parserutils
+TESTS_$(d) := aliases cscodec charset dict filter inputstream parserutils \
+ rbtree
TESTS_$(d) := $(TESTS_$(d)) regression/cscodec-segv regression/filter-segv \
regression/stream-nomem
diff --git a/test/dict.c b/test/dict.c
new file mode 100644
index 0000000..ee8c597
--- /dev/null
+++ b/test/dict.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <parserutils/utils/dict.h>
+
+#include "testutils.h"
+
+extern void parserutils_dict_dump(parserutils_dict *dict);
+
+static void *myrealloc(void *ptr, size_t len, void *pw)
+{
+ UNUSED(pw);
+
+ return realloc(ptr, len);
+}
+
+int main(int argc, char **argv)
+{
+ parserutils_dict *dict;
+ uint8_t buf[256];
+
+ UNUSED(argc);
+ UNUSED(argv);
+
+ /* Seed buffer with printable ascii */
+ for (int i = 0; i < (int) sizeof(buf); i++) {
+ buf[i] = 97 + (int) (26.0 * (rand() / (RAND_MAX + 1.0)));
+ }
+ buf[sizeof(buf) - 1] = '\0';
+
+ dict = parserutils_dict_create(myrealloc, NULL);
+ assert(dict != NULL);
+
+ for (int i = 0; i < (int) sizeof(buf); i++) {
+ uint8_t *s = buf;
+
+ while (s - buf <= i) {
+ const parserutils_dict_entry *e;
+
+ parserutils_dict_insert(dict,
+ s, (size_t) (sizeof(buf) - i), &e);
+
+ s++;
+ }
+ }
+
+ parserutils_dict_destroy(dict);
+
+ printf("PASS\n");
+
+ return 0;
+}
+
diff --git a/test/rbtree.c b/test/rbtree.c
new file mode 100644
index 0000000..4179561
--- /dev/null
+++ b/test/rbtree.c
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "utils/rbtree.h"
+
+#include "testutils.h"
+
+static void *myrealloc(void *ptr, size_t len, void *pw)
+{
+ UNUSED(pw);
+
+ return realloc(ptr, len);
+}
+
+static int mycmp(const void *a, const void *b)
+{
+ return ((const uint32_t) a) - ((const uint32_t) b);
+}
+
+int main(int argc, char **argv)
+{
+ parserutils_rbtree *tree;
+
+ UNUSED(argc);
+ UNUSED(argv);
+
+ tree = parserutils_rbtree_create(mycmp, myrealloc, NULL);
+ assert(tree != NULL);
+
+#define N 40000
+#define G 307
+//#define N 400
+//#define G 7
+
+ printf("Inserting %d items\n", N);
+
+ for (int i = G, count = 1; i != 0; i = (i + G) % N, count++) {
+ void *old;
+ assert(parserutils_rbtree_insert(tree, (void *) i, (void *) i,
+ &old) == PARSERUTILS_OK);
+
+ if ((count % 10000) == 0)
+ printf("%d\n", count);
+ }
+
+ printf("Removing %d items\n", N/2);
+
+ for (int i = 1, count = 1; i < N; i += 2, count++) {
+ void *key, *value;
+ assert(parserutils_rbtree_delete(tree, (void *) i, &key,
+ &value) == PARSERUTILS_OK);
+ if ((count % 10000) == 0)
+ printf("%d\n", count);
+ }
+
+ printf("Finding %d items\n", N/2);
+
+ for (int i = 2, count = 1; i < N; i += 2, count++) {
+ void *value = NULL;
+ assert(parserutils_rbtree_find(tree, (void *) i, &value) ==
+ PARSERUTILS_OK);
+ assert(value != NULL && ((int) value) == i);
+ if ((count % 10000) == 0)
+ printf("%d\n", count);
+ }
+
+ printf("Verifying & removing %d items\n", N/2);
+
+ for (int i = 1, count = 1; i < N; i += 2, count++) {
+ void *key, *value = NULL;
+ assert(parserutils_rbtree_find(tree, (void *) i, &value) ==
+ PARSERUTILS_OK);
+ assert(value == NULL);
+ assert(parserutils_rbtree_delete(tree, (void *) i,
+ &key, &value) == PARSERUTILS_OK);
+ if ((count % 10000) == 0)
+ printf("%d\n", count);
+ }
+
+ parserutils_rbtree_destroy(tree, NULL, NULL);
+
+ printf("PASS\n");
+
+ return 0;
+}
+