Add a test_cast test case. This checks that the casting that is implied when calling...
authorEric Haszlakiewicz <ehaszla@transunion.com>
Tue, 3 May 2011 20:42:25 +0000 (20:42 +0000)
committerEric Haszlakiewicz <ehaszla@transunion.com>
Tue, 3 May 2011 20:42:25 +0000 (20:42 +0000)
git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@68 327403b1-1117-474d-bef2-5cb71233fd97

Makefile.am
test_cast.c [new file with mode: 0644]
test_cast.expected [new file with mode: 0644]
test_cast.test [new file with mode: 0755]

index dc8b348..97fedd6 100644 (file)
@@ -32,7 +32,7 @@ libjson_la_SOURCES = \
        linkhash.c \
        printbuf.c
 
-check_PROGRAMS = test1 test2 test4 test_parse_int64 test_null
+check_PROGRAMS = test1 test2 test4 test_parse_int64 test_null test_cast
 
 test1_SOURCES = test1.c
 test1_LDADD = $(lib_LTLIBRARIES)
@@ -49,7 +49,10 @@ test_parse_int64_LDADD = $(lib_LTLIBRARIES)
 test_null_SOURCES = test_null.c
 test_null_LDADD = $(lib_LTLIBRARIES)
 
-TESTS = test1.test test2.test test4.test parse_int64.test test_null.test
+test_cast_SOURCES = test_cast.c
+test_cast_LDADD = $(lib_LTLIBRARIES)
+
+TESTS = test1.test test2.test test4.test parse_int64.test test_null.test test_cast.test 
 EXTRA_DIST += $(TESTS)
 testsubdir=testSubDir
 TESTS_ENVIRONMENT       = top_builddir=$(top_builddir)
diff --git a/test_cast.c b/test_cast.c
new file mode 100644 (file)
index 0000000..6459250
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Tests if casting within the json_object_get_* functions work correctly.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "config.h"
+
+#include "json_inttypes.h"
+#include "json_object.h"
+#include "json_tokener.h"
+#include "json_util.h"
+
+static void getit(struct json_object *new_obj, const char *field);
+
+int main(int argc, char **argv)
+{
+       const char *input = "{\n\
+               \"string_of_digits\": \"123\",\n\
+               \"regular_number\": 222,\n\
+               \"decimal_number\": 99.55,\n\
+               \"boolean_true\": true,\n\
+               \"boolean_false\": false,\n\
+               \"big_number\": 2147483649,\n\
+       }";
+       /* Note: 2147483649 = INT_MAX + 2 */
+
+       struct json_object *new_obj;
+
+       new_obj = json_tokener_parse(input);
+       printf("Parsed input: %s\n", input);
+       printf("Result is %s\n", (new_obj == NULL) ? "NULL (error!)" : "not NULL");
+       if (!new_obj)
+               return 1; // oops, we failed.
+
+       getit(new_obj, "string_of_digits");
+       getit(new_obj, "regular_number");
+       getit(new_obj, "decimal_number");
+       getit(new_obj, "boolean_true");
+       getit(new_obj, "boolean_false");
+       getit(new_obj, "big_number");
+
+    json_object_put(new_obj);
+
+    return 0;
+}
+
+static void getit(struct json_object *new_obj, const char *field)
+{
+       struct json_object *o = json_object_object_get(new_obj, field);
+
+       enum json_type o_type = json_object_get_type(o);
+       printf("new_obj.%s json_object_get_type()=%s\n", field,
+              json_type_to_name(o_type));
+       printf("new_obj.%s json_object_get_int()=%d\n", field,
+              json_object_get_int(o));
+       printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field,
+              json_object_get_int64(o));
+       printf("new_obj.%s json_object_get_boolean()=%d\n", field,
+              json_object_get_boolean(o));
+       printf("new_obj.%s json_object_get_double()=%f\n", field,
+              json_object_get_double(o));
+}
diff --git a/test_cast.expected b/test_cast.expected
new file mode 100644 (file)
index 0000000..d079e04
--- /dev/null
@@ -0,0 +1,39 @@
+Parsed input: {
+               "string_of_digits": "123",
+               "regular_number": 222,
+               "decimal_number": 99.55,
+               "boolean_true": true,
+               "boolean_false": false,
+               "big_number": 2147483649,
+       }
+Result is not NULL
+new_obj.string_of_digits json_object_get_type()=string
+new_obj.string_of_digits json_object_get_int()=123
+new_obj.string_of_digits json_object_get_int64()=123
+new_obj.string_of_digits json_object_get_boolean()=1
+new_obj.string_of_digits json_object_get_double()=123.000000
+new_obj.regular_number json_object_get_type()=int
+new_obj.regular_number json_object_get_int()=222
+new_obj.regular_number json_object_get_int64()=222
+new_obj.regular_number json_object_get_boolean()=1
+new_obj.regular_number json_object_get_double()=222.000000
+new_obj.decimal_number json_object_get_type()=double
+new_obj.decimal_number json_object_get_int()=99
+new_obj.decimal_number json_object_get_int64()=99
+new_obj.decimal_number json_object_get_boolean()=1
+new_obj.decimal_number json_object_get_double()=99.550000
+new_obj.boolean_true json_object_get_type()=boolean
+new_obj.boolean_true json_object_get_int()=1
+new_obj.boolean_true json_object_get_int64()=1
+new_obj.boolean_true json_object_get_boolean()=1
+new_obj.boolean_true json_object_get_double()=1.000000
+new_obj.boolean_false json_object_get_type()=boolean
+new_obj.boolean_false json_object_get_int()=0
+new_obj.boolean_false json_object_get_int64()=0
+new_obj.boolean_false json_object_get_boolean()=0
+new_obj.boolean_false json_object_get_double()=0.000000
+new_obj.big_number json_object_get_type()=int
+new_obj.big_number json_object_get_int()=2147483647
+new_obj.big_number json_object_get_int64()=2147483649
+new_obj.big_number json_object_get_boolean()=1
+new_obj.big_number json_object_get_double()=2147483649.000000
diff --git a/test_cast.test b/test_cast.test
new file mode 100755 (executable)
index 0000000..2102467
--- /dev/null
@@ -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 test_cast
+exit $?