aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2009-10-11 20:42:43 +0300
committerPetri Lehtinen <petri@digip.org>2009-10-11 20:44:01 +0300
commit1e00cd58a514a61e829e639f1e40dac94a334561 (patch)
tree030dc7824942dd289627d77b5f5891f6a0e73ba9 /test
parent40bb7bf4378e09570f3921cd76cdce6acc873259 (diff)
downloadjansson-1e00cd58a514a61e829e639f1e40dac94a334561.zip
jansson-1e00cd58a514a61e829e639f1e40dac94a334561.tar.gz
jansson-1e00cd58a514a61e829e639f1e40dac94a334561.tar.bz2
Extend object API
Added functions: json_object_size json_object_clear json_object_update
Diffstat (limited to 'test')
-rw-r--r--test/testprogs/test_object.c143
1 files changed, 139 insertions, 4 deletions
diff --git a/test/testprogs/test_object.c b/test/testprogs/test_object.c
index 1f9f83c..3be49e5 100644
--- a/test/testprogs/test_object.c
+++ b/test/testprogs/test_object.c
@@ -9,7 +9,137 @@
#include <string.h>
#include "util.h"
-int main()
+static void test_clear()
+{
+ json_t *object, *ten;
+
+ object = json_object();
+ ten = json_integer(10);
+
+ if(!object)
+ fail("unable to create object");
+ if(!ten)
+ fail("unable to create integer");
+
+ if(json_object_set(object, "a", ten) ||
+ json_object_set(object, "b", ten) ||
+ json_object_set(object, "c", ten) ||
+ json_object_set(object, "d", ten) ||
+ json_object_set(object, "e", ten))
+ fail("unable to set value");
+
+ if(json_object_size(object) != 5)
+ fail("invalid size");
+
+ json_object_clear(object);
+
+ if(json_object_size(object) != 0)
+ fail("invalid size after clear");
+
+ json_decref(ten);
+ json_decref(object);
+}
+
+static void test_update()
+{
+ json_t *object, *other, *nine, *ten;
+
+ object = json_object();
+ other = json_object();
+
+ nine = json_integer(9);
+ ten = json_integer(10);
+
+ if(!object || !other)
+ fail("unable to create object");
+ if(!nine || !ten)
+ fail("unable to create integer");
+
+
+ /* update an empty object with an empty object */
+
+ if(json_object_update(object, other))
+ fail("unable to update an emtpy object with an empty object");
+
+ if(json_object_size(object) != 0)
+ fail("invalid size after update");
+
+ if(json_object_size(other) != 0)
+ fail("invalid size for updater after update");
+
+
+ /* update an empty object with a nonempty object */
+
+ if(json_object_set(other, "a", ten) ||
+ json_object_set(other, "b", ten) ||
+ json_object_set(other, "c", ten) ||
+ json_object_set(other, "d", ten) ||
+ json_object_set(other, "e", ten))
+ fail("unable to set value");
+
+ if(json_object_update(object, other))
+ fail("unable to update an empty object");
+
+ if(json_object_size(object) != 5)
+ fail("invalid size after update");
+
+ if(json_object_get(object, "a") != ten ||
+ json_object_get(object, "b") != ten ||
+ json_object_get(object, "c") != ten ||
+ json_object_get(object, "d") != ten ||
+ json_object_get(object, "e") != ten)
+ fail("update works incorrectly");
+
+
+ /* perform the same update again */
+
+ if(json_object_update(object, other))
+ fail("unable to update an empty object");
+
+ if(json_object_size(object) != 5)
+ fail("invalid size after update");
+
+ if(json_object_get(object, "a") != ten ||
+ json_object_get(object, "b") != ten ||
+ json_object_get(object, "c") != ten ||
+ json_object_get(object, "d") != ten ||
+ json_object_get(object, "e") != ten)
+ fail("update works incorrectly");
+
+
+ /* update a nonempty object with a nonempty object with both old
+ and new keys */
+
+ if(json_object_clear(other))
+ fail("clear failed");
+
+ if(json_object_set(other, "a", nine) ||
+ json_object_set(other, "b", nine) ||
+ json_object_set(other, "f", nine) ||
+ json_object_set(other, "g", nine) ||
+ json_object_set(other, "h", nine))
+ fail("unable to set value");
+
+ if(json_object_update(object, other))
+ fail("unable to update a nonempty object");
+
+ if(json_object_size(object) != 8)
+ fail("invalid size after update");
+
+ if(json_object_get(object, "a") != nine ||
+ json_object_get(object, "b") != nine ||
+ json_object_get(object, "f") != nine ||
+ json_object_get(object, "g") != nine ||
+ json_object_get(object, "h") != nine)
+ fail("update works incorrectly");
+
+ json_decref(nine);
+ json_decref(ten);
+ json_decref(other);
+ json_decref(object);
+}
+
+static void test_misc()
{
json_t *object, *string, *other_string, *value;
void *iter;
@@ -20,9 +150,7 @@ int main()
if(!object)
fail("unable to create object");
- if(!string)
- fail("unable to create string");
- if(!other_string)
+ if(!string || !other_string)
fail("unable to create string");
if(json_object_get(object, "a"))
@@ -129,6 +257,13 @@ int main()
json_decref(string);
json_decref(other_string);
json_decref(object);
+}
+
+int main()
+{
+ test_misc();
+ test_clear();
+ test_update();
return 0;
}