test file for new json_dump_callback function
authorJKL <jkl@thirddefense.com>
Fri, 24 Jun 2011 17:40:20 +0000 (10:40 -0700)
committerPetri Lehtinen <petri@digip.org>
Thu, 30 Jun 2011 18:47:12 +0000 (21:47 +0300)
test/.gitignore
test/suites/api/Makefile.am
test/suites/api/check-exports
test/suites/api/test_dump_callback.c [new file with mode: 0644]

index 278e5f5..7b7203d 100644 (file)
@@ -4,6 +4,7 @@ suites/api/test_array
 suites/api/test_copy
 suites/api/test_cpp
 suites/api/test_dump
+suites/api/test_dump_callback
 suites/api/test_equal
 suites/api/test_load
 suites/api/test_loadb
index 58c31c6..5f96505 100644 (file)
@@ -4,6 +4,7 @@ check_PROGRAMS = \
        test_array \
        test_copy \
        test_dump \
+       test_dump_callback \
        test_equal \
        test_load \
        test_loadb \
@@ -17,6 +18,7 @@ check_PROGRAMS = \
 test_array_SOURCES = test_array.c util.h
 test_copy_SOURCES = test_copy.c util.h
 test_dump_SOURCES = test_dump.c util.h
+test_dump_callback_SOURCES = test_dump_callback.c util.h
 test_load_SOURCES = test_load.c util.h
 test_loadb_SOURCES = test_loadb.c util.h
 test_memory_funcs_SOURCES = test_memory_funcs.c util.h
index 6df5a48..802d683 100755 (executable)
@@ -47,6 +47,7 @@ json_object_iter_set_new
 json_dumps
 json_dumpf
 json_dump_file
+json_dump_callback
 json_loads
 json_loadf
 json_load_file
diff --git a/test/suites/api/test_dump_callback.c b/test/suites/api/test_dump_callback.c
new file mode 100644 (file)
index 0000000..cb71e1b
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <jansson.h>
+#include <string.h>
+#include <stdlib.h>
+#include "util.h"
+
+struct my_sink {
+    char *buf;
+    size_t off;
+    size_t cap;
+};
+
+static int my_writer(const char *buffer, size_t len, void *data) {
+    struct my_sink *s = data;
+    if (len > s->cap - s->off) {
+        return -1;
+    }
+    memcpy(s->buf + s->off, buffer, len);
+    s->off += len;
+    return 0;
+}
+
+int main(void)
+{
+    struct my_sink s;
+    json_t *json;
+    const char str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";
+    char *dumped_to_string;
+
+    json = json_loads(str, 0, NULL);
+    if(!json) {
+        fail("json_loads failed");
+    }
+
+    dumped_to_string = json_dumps(json, 0);
+    if (!dumped_to_string) {
+        json_decref(json);
+        fail("json_dumps failed");
+    }
+
+    s.off = 0;
+    s.cap = strlen(dumped_to_string);
+    s.buf = malloc(s.cap);
+    if (!s.buf) {
+        json_decref(json);
+        free(dumped_to_string);
+        fail("malloc failed");
+    }
+
+    if (json_dump_callback(json, my_writer, &s, 0) == -1) {
+        json_decref(json);
+        free(dumped_to_string);
+        free(s.buf);
+        fail("json_dump_callback failed on an exact-length sink buffer");
+    }
+
+    if (strncmp(dumped_to_string, s.buf, s.off) != 0) {
+        json_decref(json);
+        free(dumped_to_string);
+        free(s.buf);
+        fail("json_dump_callback and json_dumps did not produce identical output");
+    }
+
+    s.off = 1;
+    if (json_dump_callback(json, my_writer, &s, 0) != -1) {
+        json_decref(json);
+        free(dumped_to_string);
+        free(s.buf);
+        fail("json_dump_callback succeeded on a short buffer when it should have failed");
+    }
+
+    json_decref(json);
+    free(dumped_to_string);
+    free(s.buf);
+    return EXIT_SUCCESS;
+}
+