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 if(!json_object_set(object, NULL, string))
33 fail("able to set NULL key");
35 if(!json_object_set(object, "a", NULL))
36 fail("able to set NULL value");
38 iter = json_object_iter(object);
40 fail("unable to get iterator");
42 if(strcmp(json_object_iter_key(iter), "a"))
43 fail("iterating failed: wrong key");
44 if(json_object_iter_value(iter) != string)
45 fail("iterating failed: wrong value");
46 if(json_object_iter_next(object, iter) != NULL)
47 fail("able to iterate over the end");
49 /* invalid UTF-8 in key */
50 if(!json_object_set(object, "a\xefz", string))
51 fail("able to set invalid unicode key");
53 value = json_object_get(object, "a");
55 fail("no value for existing key");
57 fail("got different value than what was added");
59 /* "a", "lp" and "px" collide in a five-bucket hashtable */
60 if(json_object_set(object, "b", string) ||
61 json_object_set(object, "lp", string) ||
62 json_object_set(object, "px", string))
63 fail("unable to set value");
65 value = json_object_get(object, "a");
67 fail("no value for existing key");
69 fail("got different value than what was added");
71 if(json_object_set(object, "a", other_string))
72 fail("unable to replace an existing key");
74 value = json_object_get(object, "a");
76 fail("no value for existing key");
77 if(value != other_string)
78 fail("got different value than what was set");
80 if(!json_object_del(object, "nonexisting"))
81 fail("able to delete a nonexisting key");
83 if(json_object_del(object, "px"))
84 fail("unable to delete an existing key");
86 if(json_object_del(object, "a"))
87 fail("unable to delete an existing key");
89 if(json_object_del(object, "lp"))
90 fail("unable to delete an existing key");
93 /* add many keys to initiate rehashing */
95 if(json_object_set(object, "a", string))
96 fail("unable to set value");
98 if(json_object_set(object, "lp", string))
99 fail("unable to set value");
101 if(json_object_set(object, "px", string))
102 fail("unable to set value");
104 if(json_object_set(object, "c", string))
105 fail("unable to set value");
107 if(json_object_set(object, "d", string))
108 fail("unable to set value");
110 if(json_object_set(object, "e", string))
111 fail("unable to set value");
114 if(json_object_set_new(object, "foo", json_integer(123)))
115 fail("unable to set new value");
117 value = json_object_get(object, "foo");
118 if(!json_is_integer(value) || json_integer_value(value) != 123)
119 fail("json_object_set_new works incorrectly");
121 if(!json_object_set_new(object, NULL, json_integer(432)))
122 fail("able to set_new NULL key");
124 if(!json_object_set_new(object, "foo", NULL))
125 fail("able to set_new NULL value");
128 json_decref(other_string);