diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-07-24 23:27:41 -0500 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-07-24 23:27:41 -0500 |
commit | 6988f53fcb05c13d99dd846494d79ea3bb3b1d4c (patch) | |
tree | 204aece682b23563bb42a7f5d2889b19d71e9c0c /tests | |
parent | 381f77c5bccf20234a2bddae2b6ac43997faf0a2 (diff) | |
download | json-c-6988f53fcb05c13d99dd846494d79ea3bb3b1d4c.tar.gz |
Rewrite json_object_object_add to replace just the value if the key already exists so keys remain valid.
This is particularly useful when replacing values in a loop, since it allows
the key used by json_object_object_foreach to continue to be used.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 5 | ||||
-rw-r--r-- | tests/testReplaceExisting.c | 56 | ||||
-rw-r--r-- | tests/testReplaceExisting.expected | 9 | ||||
-rwxr-xr-x | tests/testReplaceExisting.test | 12 |
4 files changed, 81 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index e2854dd..635ce55 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,6 +6,7 @@ LIBJSON_LA=$(top_builddir)/libjson.la check_PROGRAMS = test1 test1Formatted check_PROGRAMS += test2 test2Formatted check_PROGRAMS += test4 +check_PROGRAMS += testReplaceExisting check_PROGRAMS += test_parse_int64 check_PROGRAMS += test_null check_PROGRAMS += test_cast @@ -25,6 +26,8 @@ test2Formatted_CPPFLAGS = -DTEST_FORMATTED test4_LDADD = $(LIBJSON_LA) +testReplaceExisting_LDADD = $(LIBJSON_LA) + test_parse_int64_LDADD = $(LIBJSON_LA) test_null_LDADD = $(LIBJSON_LA) @@ -33,7 +36,7 @@ test_cast_LDADD = $(LIBJSON_LA) test_parse_LDADD = $(LIBJSON_LA) -TESTS = test1.test test2.test test4.test parse_int64.test test_null.test test_cast.test test_parse.test +TESTS = test1.test test2.test test4.test testReplaceExisting.test parse_int64.test test_null.test test_cast.test test_parse.test TESTS+= test_printbuf.test check_PROGRAMS+=test_printbuf diff --git a/tests/testReplaceExisting.c b/tests/testReplaceExisting.c new file mode 100644 index 0000000..8c8c4b2 --- /dev/null +++ b/tests/testReplaceExisting.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> + +#include "json.h" + +int main(int argc, char **argv) +{ + MC_SET_DEBUG(1); + + /* + * Check that replacing an existing object keeps the key valid, + * and that it keeps the order the same. + */ + json_object *my_object = json_object_new_object(); + json_object_object_add(my_object, "foo1", json_object_new_string("bar1")); + json_object_object_add(my_object, "foo2", json_object_new_string("bar2")); + json_object_object_add(my_object, "foo3", json_object_new_string("bar3")); + const char *original_key = NULL; + int orig_count = 0; + json_object_object_foreach(my_object, key, val) + { + printf("Key at index %d is [%s]\n", orig_count, key); + orig_count++; + if (strcmp(key, "foo2") != 0) + continue; + printf("replacing value for key [%s]\n", key); + original_key = key; + json_object_object_add(my_object, key, json_object_new_string("zzz")); + } + + printf("==== second loop starting ====\n"); + + int new_count = 0; + int retval = 0; + json_object_object_foreach(my_object, key2, val2) + { + printf("Key at index %d is [%s]\n", new_count, key2); + new_count++; + if (strcmp(key2, "foo2") != 0) + continue; + printf("pointer for key [%s] does %smatch\n", key2, + (key2 == original_key) ? "" : "NOT "); + if (key2 != original_key) + retval = 1; + } + if (new_count != orig_count) + { + printf("mismatch between original count (%d) and new count (%d)\n", + orig_count, new_count); + retval = 1; + } + + return 0; +} diff --git a/tests/testReplaceExisting.expected b/tests/testReplaceExisting.expected new file mode 100644 index 0000000..4d1c509 --- /dev/null +++ b/tests/testReplaceExisting.expected @@ -0,0 +1,9 @@ +Key at index 0 is [foo1] +Key at index 1 is [foo2] +replacing value for key [foo2] +Key at index 2 is [foo3] +==== second loop starting ==== +Key at index 0 is [foo1] +Key at index 1 is [foo2] +pointer for key [foo2] does match +Key at index 2 is [foo3] diff --git a/tests/testReplaceExisting.test b/tests/testReplaceExisting.test new file mode 100755 index 0000000..ec5cbf1 --- /dev/null +++ b/tests/testReplaceExisting.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 testReplaceExisting +exit $? |