test: mdict tests
authorMarko Kreen <markokr@gmail.com>
Fri, 28 Jan 2011 22:02:05 +0000 (00:02 +0200)
committerMarko Kreen <markokr@gmail.com>
Fri, 28 Jan 2011 22:02:05 +0000 (00:02 +0200)
test/Makefile
test/test_common.c
test/test_common.h
test/test_mdict.c [new file with mode: 0644]

index eb07808f363b24dcd1e48382b8cf5a7792a15d02..6f1fb038594f61e1b7fdbd4ccabf9b855f2a296b 100644 (file)
@@ -4,7 +4,7 @@ SRCS = test_string.c test_crypto.c test_aatree.c test_heap.c \
        test_common.c test_list.c tinytest.c test_cbtree.c \
        test_utf8.c test_strpool.c test_pgutil.c test_regex.c \
        test_cxalloc.c test_bits.c test_base.c test_netdb.c \
-       test_cfparser.c test_endian.c test_hashtab.c
+       test_cfparser.c test_endian.c test_hashtab.c test_mdict.c
 OBJS = $(addprefix obj/, $(SRCS:.c=.o))
 HDRS = test_common.h test_config.h tinytest.h tinytest_macros.h
 LIBS =
index d387edcdf336363b381a2b9f4fa6528d05e55335..2f5c0d0cf40b33a7f7b16a3fbff95901b86f350b 100644 (file)
@@ -20,6 +20,7 @@ struct testgroup_t groups[] = {
        { "regex/", regex_tests },
        { "netdb/", netdb_tests },
        { "cfparser/", cfparser_tests },
+       { "mdict/", mdict_tests },
        END_OF_GROUPS
 };
 
index b4301be60200d4eb6e9effda906f2f34c606ac12..6a88a98c6c57bac8d91e7277e299c72e4ecdb0e0 100644 (file)
@@ -25,4 +25,5 @@ extern struct testcase_t netdb_tests[];
 extern struct testcase_t cfparser_tests[];
 extern struct testcase_t endian_tests[];
 extern struct testcase_t hashtab_tests[];
+extern struct testcase_t mdict_tests[];
 
diff --git a/test/test_mdict.c b/test/test_mdict.c
new file mode 100644 (file)
index 0000000..09c3616
--- /dev/null
@@ -0,0 +1,56 @@
+#include <usual/mdict.h>
+
+#include <usual/string.h>
+
+#include "test_common.h"
+
+static const char *xget(struct MDict *d, const char *k)
+{
+       const char *val = mdict_get(d, k);
+       return val ? val : "NULL";
+}
+
+static void test_mdict(void *p)
+{
+       struct MDict *d;
+       struct MBuf buf;
+       const char *s;
+
+       d = mdict_new(USUAL_ALLOC);
+       str_check(xget(d, "key"), "NULL");
+       int_check(mdict_put(d, "key", "val"), 1);
+       int_check(mdict_put(d, "key2", ""), 1);
+       int_check(mdict_put(d, "key3", NULL), 1);
+       str_check(xget(d, "key"), "val");
+       str_check(xget(d, "key2"), "");
+       str_check(xget(d, "key3"), "NULL");
+
+       mbuf_init_dynamic(&buf);
+       int_check(mdict_urlencode(d, &buf), 1);
+       int_check(mbuf_write_byte(&buf, 0), 1);
+       str_check(mbuf_data(&buf), "key=val&key2=&key3");
+       mbuf_free(&buf);
+
+       mdict_free(d);
+
+       d = mdict_new(USUAL_ALLOC);
+       s = "key=val&key2=&key3";
+       int_check(mdict_urldecode(d, s, strlen(s)), 1);
+       str_check(xget(d, "key"), "val");
+       str_check(xget(d, "key2"), "");
+       str_check(xget(d, "key3"), "NULL");
+       mdict_free(d);
+end:;
+}
+
+
+/*
+ * Describe
+ */
+
+struct testcase_t mdict_tests[] = {
+       { "basic", test_mdict },
+       END_OF_TESTCASES
+};
+
+