summaryrefslogtreecommitdiff
path: root/jsmn_test.c
diff options
context:
space:
mode:
authorSerge A. Zaitsev <devnull@localhost>2012-02-01 12:38:51 +0200
committerSerge A. Zaitsev <devnull@localhost>2012-02-01 12:38:51 +0200
commit4b5c5ed66ac35f50b6c172a712883acb5f0cc62f (patch)
tree15845cf4501431c3a809a7a51705259cf7956ae2 /jsmn_test.c
parent2928f7ec0ebcd6ae9937a5689d8da2369c863f69 (diff)
switched to C unit-tests instead of bash
Diffstat (limited to 'jsmn_test.c')
-rw-r--r--jsmn_test.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/jsmn_test.c b/jsmn_test.c
new file mode 100644
index 000000000..a6e042944
--- /dev/null
+++ b/jsmn_test.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+
+#include "jsmn.c"
+
+static int test_passed = 0;
+static int test_failed = 0;
+
+/* Terminate current test with error */
+#define fail() return __LINE__
+
+/* Successfull end of the test case */
+#define done() return 0
+
+/* Check single condition */
+#define check(cond) do { if (!(cond)) fail(); } while (0)
+
+/* Test runner */
+static void test(int (*func)(void), const char *name) {
+ int r = func();
+ if (r == 0) {
+ test_passed++;
+ } else {
+ test_failed++;
+ printf("FAILED: %s (at line %d)\n", name, r);
+ }
+}
+
+#define TOKEN_EQ(t, tok_start, tok_end, tok_type) \
+ ((t).start == tok_start \
+ && (t).end == tok_end \
+ && (t).type == (tok_type))
+
+#define TOKEN_PRINT(t) \
+ printf("start: %d, end: %d, type: %d\n", (t).start, (t).end, (t).type)
+
+
+
+int test_primitive() {
+ int r;
+ jsmn_parser p;
+ jsmntok_t tokens[10];
+
+ jsmn_init_parser(&p, "{\"a\": 0}", tokens, 10);
+
+ r = jsmn_parse(&p);
+ check(r == JSMN_SUCCESS);
+ check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
+ check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
+ check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));
+
+ return 0;
+}
+
+int main() {
+ test(test_primitive, "test primitive values");
+ return 0;
+}