Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a test_cast test case. This checks that the casting that is impli…

…ed when calling the various json_object_get_FOO() functions on a differently typed object works correctly.

git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@68 327403b1-1117-474d-bef2-5cb71233fd97
  • Loading branch information...
commit 41e67d0f6f371baf1f3f5678c4f362e8c7f15e3b 1 parent 886c4fb
Eric Haszlakiewicz authored
Showing with 120 additions and 2 deletions.
  1. +5 −2 Makefile.am
  2. +64 −0 test_cast.c
  3. +39 −0 test_cast.expected
  4. +12 −0 test_cast.test
View
7 Makefile.am
@@ -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)
View
64 test_cast.c
@@ -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));
+}
View
39 test_cast.expected
@@ -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
View
12 test_cast.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 test_cast
+exit $?
Please sign in to comment.
Something went wrong with that request. Please try again.