]> granicus.if.org Git - json-c/commitdiff
Add some basic tests for verifying json_object_equal behavior
authorHelmut Schaa <helmut.schaa@googlemail.com>
Wed, 13 Jan 2016 14:40:08 +0000 (15:40 +0100)
committerHelmut Schaa <helmut.schaa@googlemail.com>
Wed, 13 Jan 2016 14:56:39 +0000 (15:56 +0100)
Do some basic checks on ints, doubles, strings, arrays and "complex" objects.

.gitignore
tests/Makefile.am
tests/test_compare.c [new file with mode: 0644]
tests/test_compare.expected [new file with mode: 0644]
tests/test_compare.test [new file with mode: 0755]

index 7d3dbf95ac2b7c103b385a65e334ecdb711859cd..57aa6d1ebe508d44a8fafc8e97914d0b7d7d44cd 100644 (file)
@@ -45,6 +45,7 @@
 /tests/test_null
 /tests/test_printbuf
 /tests/test_set_serializer
+/tests/test_compare
 /tests/*.vg.out
 /tests/*.log
 /tests/*.trs
index dd343483cc97f45075508165587339b709b33324..79b196b6a34c137bdd548c21b39d57ee496530ce 100644 (file)
@@ -16,6 +16,7 @@ TESTS+= test_locale.test
 TESTS+= test_charcase.test
 TESTS+= test_printbuf.test
 TESTS+= test_set_serializer.test
+TESTS+= test_compare.test
 
 check_PROGRAMS=
 check_PROGRAMS += $(TESTS:.test=)
diff --git a/tests/test_compare.c b/tests/test_compare.c
new file mode 100644 (file)
index 0000000..30c6e50
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+* Tests if json_object_equal behaves correct.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+#include "json_inttypes.h"
+#include "json_object.h"
+#include "json_tokener.h"
+
+int main()
+{
+       /* integer tests */
+       struct json_object *int1 = json_object_new_int(0);
+       struct json_object *int2 = json_object_new_int(1);
+       struct json_object *int3 = json_object_new_int(1);
+
+       if (!json_object_equal(int1, int2))
+               printf("JSON integer comparision is correct\n");
+       else
+               printf("JSON integer comparision failed\n");
+
+       if (json_object_equal(int1, int1))
+               printf("JSON same object comparision is correct\n");
+       else
+               printf("JSON same object comparision failed\n");
+
+       if (json_object_equal(int2, int3))
+               printf("JSON same integer comparision is correct\n");
+       else
+               printf("JSON same integer comparision failed\n");
+
+       json_object_put(int1);
+       json_object_put(int2);
+       json_object_put(int3);
+
+       /* string tests */
+       struct json_object *str1 = json_object_new_string("TESTSTRING");
+       struct json_object *str2 = json_object_new_string("TESTSTRING");
+       struct json_object *str3 = json_object_new_string("DIFFERENT");
+
+       if (json_object_equal(str1, str2))
+               printf("Comparing equal strings is correct\n");
+       else
+               printf("Comparing equal strings failed\n");
+
+       if (!json_object_equal(str1, str3))
+               printf("Comparing different strings is correct\n");
+       else
+               printf("Comparing different strings failed\n");
+
+       json_object_put(str1);
+       json_object_put(str2);
+       json_object_put(str3);
+
+       /* double tests */
+       struct json_object *dbl1 = json_object_new_double(3.14159);
+       struct json_object *dbl2 = json_object_new_double(3.14159);
+       struct json_object *dbl3 = json_object_new_double(3.0);
+
+       if (json_object_equal(dbl1, dbl2))
+               printf("Comparing equal doubles is correct\n");
+       else
+               printf("Comparing equal doubles failed\n");
+
+       if (!json_object_equal(dbl1, dbl3))
+               printf("Comparing different doubles is correct\n");
+       else
+               printf("Comparing different doubles failed\n");
+
+       json_object_put(dbl1);
+       json_object_put(dbl2);
+       json_object_put(dbl3);
+
+       /* array tests */
+       struct json_object *ar1 = json_object_new_array();
+       struct json_object *ar2 = json_object_new_array();
+       struct json_object *ar3 = json_object_new_array();
+       struct json_object *ar4 = json_object_new_array();
+
+       json_object_array_add(ar1, json_object_new_int(1));
+       json_object_array_add(ar1, json_object_new_int(2));
+
+       json_object_array_add(ar2, json_object_new_int(1));
+       json_object_array_add(ar2, json_object_new_int(2));
+
+       json_object_array_add(ar3, json_object_new_int(1));
+       json_object_array_add(ar3, json_object_new_int(1));
+
+       if (json_object_equal(ar1, ar2))
+               printf("Comparing equal arrays is correct\n");
+       else
+               printf("Comparing equal arrays failed\n");
+
+       json_object_array_add(ar2, json_object_new_int(1));
+       if (!json_object_equal(ar1, ar2))
+               printf("Comparing arrays of different len is correct\n");
+       else
+               printf("Comparing arrays of different len failed\n");
+
+       if (!json_object_equal(ar1, ar3))
+               printf("Comparing different arrays is correct\n");
+       else
+               printf("Comparing different arrays failed\n");
+
+       if (!json_object_equal(ar1, ar4))
+               printf("Comparing different arrays (one empty) is correct\n");
+       else
+               printf("Comparing different arrays (one empty) failed\n");
+
+       json_object_put(ar1);
+       json_object_put(ar2);
+       json_object_put(ar3);
+       json_object_put(ar4);
+
+       /* object tests */
+       struct json_object *obj1 = json_object_new_object();
+       struct json_object *obj2 = json_object_new_object();
+
+       json_object_object_add(obj1, "test1", json_object_new_int(123));
+       json_object_object_add(obj1, "test2", json_object_new_int(321));
+
+       json_object_object_add(obj2, "test2", json_object_new_int(123));
+       json_object_object_add(obj2, "test1", json_object_new_int(321));
+
+       /* key-order is different between obj1 and obj2, should still be equal */
+       if (json_object_equal(obj1, obj2))
+               printf("Comparing JSON object with different key order is correct\n");
+       else
+               printf("Comparing JSON object with different key order is incorrect\n");
+
+       /* make obj2 look different to obj1 */
+       json_object_object_add(obj2, "test3", json_object_new_int(234));
+       if (!json_object_equal(obj1, obj2))
+               printf("Comparing different objects is correct\n");
+       else
+               printf("Comparing different objects is incorrect\n");
+
+       json_object_put(obj1);
+       json_object_put(obj2);
+
+       return 0;
+}
diff --git a/tests/test_compare.expected b/tests/test_compare.expected
new file mode 100644 (file)
index 0000000..46f03c4
--- /dev/null
@@ -0,0 +1,13 @@
+JSON integer comparision is correct
+JSON same object comparision is correct
+JSON same integer comparision is correct
+Comparing equal strings is correct
+Comparing different strings is correct
+Comparing equal doubles is correct
+Comparing different doubles is correct
+Comparing equal arrays is correct
+Comparing arrays of different len is correct
+Comparing different arrays is correct
+Comparing different arrays (one empty) is correct
+Comparing JSON object with different key order is correct
+Comparing different objects is correct
diff --git a/tests/test_compare.test b/tests/test_compare.test
new file mode 100755 (executable)
index 0000000..bcc4ff3
--- /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_compare
+exit $?