2 * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
13 json_t *object, *string, *other_string, *value;
15 object = json_object();
16 string = json_string("test");
17 other_string = json_string("other");
20 fail("unable to create object");
22 fail("unable to create string");
24 fail("unable to create string");
26 if(json_object_get(object, "a"))
27 fail("value for nonexisting key");
29 if(json_object_set(object, "a", string))
30 fail("unable to set value");
32 iter = json_object_iter(object);
34 fail("unable to get iterator");
36 if(strcmp(json_object_iter_key(iter), "a"))
37 fail("iterating failed: wrong key");
38 if(json_object_iter_value(iter) != string)
39 fail("iterating failed: wrong value");
40 if(json_object_iter_next(object, iter) != NULL)
41 fail("able to iterate over the end");
43 /* invalid UTF-8 in key */
44 if(!json_object_set(object, "a\xefz", string))
45 fail("able to set invalid unicode key");
47 value = json_object_get(object, "a");
49 fail("no value for existing key");
51 fail("got different value than what was added");
53 /* "a", "lp" and "px" collide in a five-bucket hashtable */
54 if(json_object_set(object, "b", string) ||
55 json_object_set(object, "lp", string) ||
56 json_object_set(object, "px", string))
57 fail("unable to set value");
59 value = json_object_get(object, "a");
61 fail("no value for existing key");
63 fail("got different value than what was added");
65 if(json_object_set(object, "a", other_string))
66 fail("unable to replace an existing key");
68 value = json_object_get(object, "a");
70 fail("no value for existing key");
71 if(value != other_string)
72 fail("got different value than what was set");
74 if(!json_object_del(object, "nonexisting"))
75 fail("able to delete a nonexisting key");
77 if(json_object_del(object, "px"))
78 fail("unable to delete an existing key");
80 if(json_object_del(object, "a"))
81 fail("unable to delete an existing key");
83 if(json_object_del(object, "lp"))
84 fail("unable to delete an existing key");
87 /* add many keys to initiate rehashing */
89 if(json_object_set(object, "a", string))
90 fail("unable to set value");
92 if(json_object_set(object, "lp", string))
93 fail("unable to set value");
95 if(json_object_set(object, "px", string))
96 fail("unable to set value");
98 if(json_object_set(object, "c", string))
99 fail("unable to set value");
101 if(json_object_set(object, "d", string))
102 fail("unable to set value");
104 if(json_object_set(object, "e", string))
105 fail("unable to set value");
108 json_object_set_new(object, "foo", json_integer(123));
109 value = json_object_get(object, "foo");
110 if(!json_is_integer(value) || json_integer_value(value) != 123)
111 fail("json_object_set_new works incorrectly");
114 json_decref(other_string);