summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2012-04-22 10:27:50 -0500
committerEric Haszlakiewicz <erh+git@nimenees.com>2012-04-22 10:27:50 -0500
commit1e89ba68afbf8310b0fa34226a5e730e4118a465 (patch)
tree09b3ee9f80094f7aebaa571af54db543826f6399 /tests
parent0d79b534568f609a9444c3466ae1f5a2864f702f (diff)
downloadjson-c-1e89ba68afbf8310b0fa34226a5e730e4118a465.tar.gz
Create a tests subdirectory and move one of the test to there.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am22
-rw-r--r--tests/test1.c111
-rw-r--r--tests/test1.expected35
-rwxr-xr-xtests/test1.test12
4 files changed, 180 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..ed5d2a5
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,22 @@
+
+include ../Makefile.am.inc
+
+#lib_LTLIBRARIES = ../libjson.la
+
+#check_PROGRAMS = test1 test2 test4 test_parse_int64 test_null test_cast test_parse
+check_PROGRAMS = test1
+
+#test1_SOURCES = test1.c
+#test1_LDADD = $(lib_LTLIBRARIES)
+test1_LDADD = $(top_builddir)/libjson.la
+
+#TESTS = test1.test test2.test test4.test parse_int64.test test_null.test test_cast.test test_parse.test
+TESTS = test1.test
+EXTRA_DIST=
+EXTRA_DIST += $(TESTS)
+
+testsubdir=testSubDir
+TESTS_ENVIRONMENT = top_builddir=$(top_builddir)
+
+distclean-local:
+ -rm -rf $(testsubdir)
diff --git a/tests/test1.c b/tests/test1.c
new file mode 100644
index 0000000..e1e411d
--- /dev/null
+++ b/tests/test1.c
@@ -0,0 +1,111 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <assert.h>
+
+#include "json.h"
+
+static int sort_fn (const void *j1, const void *j2)
+{
+ json_object * const *jso1, * const *jso2;
+ int i1, i2;
+
+ jso1 = j1;
+ jso2 = j2;
+ if (!*jso1 && !*jso2) {
+ return 0;
+ }
+ if (!*jso1) {
+ return -1;
+ }
+ if (!*jso2) {
+ return 1;
+ }
+
+ i1 = json_object_get_int(*jso1);
+ i2 = json_object_get_int(*jso2);
+
+ return i1 - i2;
+}
+
+int main(int argc, char **argv)
+{
+ json_object *my_string, *my_int, *my_object, *my_array;
+ int i;
+
+ MC_SET_DEBUG(1);
+
+ my_string = json_object_new_string("\t");
+ printf("my_string=%s\n", json_object_get_string(my_string));
+ printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
+ json_object_put(my_string);
+
+ my_string = json_object_new_string("\\");
+ printf("my_string=%s\n", json_object_get_string(my_string));
+ printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
+ json_object_put(my_string);
+
+ my_string = json_object_new_string("foo");
+ printf("my_string=%s\n", json_object_get_string(my_string));
+ printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
+
+ my_int = json_object_new_int(9);
+ printf("my_int=%d\n", json_object_get_int(my_int));
+ printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
+
+ my_array = json_object_new_array();
+ json_object_array_add(my_array, json_object_new_int(1));
+ json_object_array_add(my_array, json_object_new_int(2));
+ json_object_array_add(my_array, json_object_new_int(3));
+ json_object_array_put_idx(my_array, 4, json_object_new_int(5));
+ printf("my_array=\n");
+ for(i=0; i < json_object_array_length(my_array); i++) {
+ json_object *obj = json_object_array_get_idx(my_array, i);
+ printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
+ }
+ printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
+
+ json_object_put(my_array);
+
+ my_array = json_object_new_array();
+ json_object_array_add(my_array, json_object_new_int(3));
+ json_object_array_add(my_array, json_object_new_int(1));
+ json_object_array_add(my_array, json_object_new_int(2));
+ json_object_array_put_idx(my_array, 4, json_object_new_int(0));
+ printf("my_array=\n");
+ for(i=0; i < json_object_array_length(my_array); i++) {
+ json_object *obj = json_object_array_get_idx(my_array, i);
+ printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
+ }
+ printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
+ json_object_array_sort(my_array, sort_fn);
+ printf("my_array=\n");
+ for(i=0; i < json_object_array_length(my_array); i++) {
+ json_object *obj = json_object_array_get_idx(my_array, i);
+ printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
+ }
+ printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
+
+ my_object = json_object_new_object();
+ json_object_object_add(my_object, "abc", json_object_new_int(12));
+ json_object_object_add(my_object, "foo", json_object_new_string("bar"));
+ json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
+ json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
+ json_object_object_add(my_object, "baz", json_object_new_string("bang"));
+ json_object_object_add(my_object, "baz", json_object_new_string("fark"));
+ json_object_object_del(my_object, "baz");
+ /*json_object_object_add(my_object, "arr", my_array);*/
+ printf("my_object=\n");
+ json_object_object_foreach(my_object, key, val) {
+ printf("\t%s: %s\n", key, json_object_to_json_string(val));
+ }
+ printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
+
+ json_object_put(my_string);
+ json_object_put(my_int);
+ json_object_put(my_object);
+ json_object_put(my_array);
+
+ return 0;
+}
diff --git a/tests/test1.expected b/tests/test1.expected
new file mode 100644
index 0000000..6653fe0
--- /dev/null
+++ b/tests/test1.expected
@@ -0,0 +1,35 @@
+my_string=
+my_string.to_string()="\t"
+my_string=\
+my_string.to_string()="\\"
+my_string=foo
+my_string.to_string()="foo"
+my_int=9
+my_int.to_string()=9
+my_array=
+ [0]=1
+ [1]=2
+ [2]=3
+ [3]=null
+ [4]=5
+my_array.to_string()=[ 1, 2, 3, null, 5 ]
+my_array=
+ [0]=3
+ [1]=1
+ [2]=2
+ [3]=null
+ [4]=0
+my_array.to_string()=[ 3, 1, 2, null, 0 ]
+my_array=
+ [0]=null
+ [1]=0
+ [2]=1
+ [3]=2
+ [4]=3
+my_array.to_string()=[ null, 0, 1, 2, 3 ]
+my_object=
+ abc: 12
+ foo: "bar"
+ bool0: false
+ bool1: true
+my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
diff --git a/tests/test1.test b/tests/test1.test
new file mode 100755
index 0000000..6074fac
--- /dev/null
+++ b/tests/test1.test
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+# Common definitions
+if test -z "$srcdir"; then
+ srcdir="${0%/*}"
+ test "$srcdir" = "$0" && srcdir=.
+ test -z "$srcdir" && srcdir=.
+fi
+. "$srcdir/test-defs.sh"
+
+run_output_test test1
+exit $?