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.
14 json_t *object, *string, *other_string, *value;
17 object = json_object();
18 string = json_string("test");
19 other_string = json_string("other");
22 fail("unable to create object");
24 fail("unable to create string");
26 fail("unable to create string");
28 if(json_object_get(object, "a"))
29 fail("value for nonexisting key");
31 if(json_object_set(object, "a", string))
32 fail("unable to set value");
34 if(!json_object_set(object, NULL, string))
35 fail("able to set NULL key");
37 if(!json_object_set(object, "a", NULL))
38 fail("able to set NULL value");
40 iter = json_object_iter(object);
42 fail("unable to get iterator");
44 if(strcmp(json_object_iter_key(iter), "a"))
45 fail("iterating failed: wrong key");
46 if(json_object_iter_value(iter) != string)
47 fail("iterating failed: wrong value");
48 if(json_object_iter_next(object, iter) != NULL)
49 fail("able to iterate over the end");
51 /* invalid UTF-8 in key */
52 if(!json_object_set(object, "a\xefz", string))
53 fail("able to set invalid unicode key");
55 value = json_object_get(object, "a");
57 fail("no value for existing key");
59 fail("got different value than what was added");
61 /* "a", "lp" and "px" collide in a five-bucket hashtable */
62 if(json_object_set(object, "b", string) ||
63 json_object_set(object, "lp", string) ||
64 json_object_set(object, "px", string))
65 fail("unable to set value");
67 value = json_object_get(object, "a");
69 fail("no value for existing key");
71 fail("got different value than what was added");
73 if(json_object_set(object, "a", other_string))
74 fail("unable to replace an existing key");
76 value = json_object_get(object, "a");
78 fail("no value for existing key");
79 if(value != other_string)
80 fail("got different value than what was set");
82 if(!json_object_del(object, "nonexisting"))
83 fail("able to delete a nonexisting key");
85 if(json_object_del(object, "px"))
86 fail("unable to delete an existing key");
88 if(json_object_del(object, "a"))
89 fail("unable to delete an existing key");
91 if(json_object_del(object, "lp"))
92 fail("unable to delete an existing key");
95 /* add many keys to initiate rehashing */
97 if(json_object_set(object, "a", string))
98 fail("unable to set value");
100 if(json_object_set(object, "lp", string))
101 fail("unable to set value");
103 if(json_object_set(object, "px", string))
104 fail("unable to set value");
106 if(json_object_set(object, "c", string))
107 fail("unable to set value");
109 if(json_object_set(object, "d", string))
110 fail("unable to set value");
112 if(json_object_set(object, "e", string))
113 fail("unable to set value");
116 if(json_object_set_new(object, "foo", json_integer(123)))
117 fail("unable to set new value");
119 value = json_object_get(object, "foo");
120 if(!json_is_integer(value) || json_integer_value(value) != 123)
121 fail("json_object_set_new works incorrectly");
123 if(!json_object_set_new(object, NULL, json_integer(432)))
124 fail("able to set_new NULL key");
126 if(!json_object_set_new(object, "foo", NULL))
127 fail("able to set_new NULL value");
130 json_decref(other_string);