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 /* invalid UTF-8 in key */
33 if(!json_object_set(object, "a\xefz", string))
34 fail("able to set invalid unicode key");
36 value = json_object_get(object, "a");
38 fail("no value for existing key");
40 fail("got different value than what was added");
42 /* "a", "lp" and "px" collide with a five-bucket hashtable */
43 if(json_object_set(object, "b", string) ||
44 json_object_set(object, "lp", string) ||
45 json_object_set(object, "px", string))
46 fail("unable to set value");
48 value = json_object_get(object, "a");
50 fail("no value for existing key");
52 fail("got different value than what was added");
54 if(json_object_set(object, "a", other_string))
55 fail("unable to replace an existing key");
57 value = json_object_get(object, "a");
59 fail("no value for existing key");
60 if(value != other_string)
61 fail("got different value than what was set");
63 if(!json_object_del(object, "nonexisting"))
64 fail("able to delete a nonexisting key");
66 if(json_object_del(object, "px"))
67 fail("unable to delete an existing key");
69 if(json_object_del(object, "a"))
70 fail("unable to delete an existing key");
72 if(json_object_del(object, "lp"))
73 fail("unable to delete an existing key");
76 /* add many keys to initiate rehashing */
78 if(json_object_set(object, "a", string))
79 fail("unable to set value");
81 if(json_object_set(object, "lp", string))
82 fail("unable to set value");
84 if(json_object_set(object, "px", string))
85 fail("unable to set value");
87 if(json_object_set(object, "c", string))
88 fail("unable to set value");
90 if(json_object_set(object, "d", string))
91 fail("unable to set value");
93 if(json_object_set(object, "e", string))
94 fail("unable to set value");
97 json_object_set_new(object, "foo", json_integer(123));
98 value = json_object_get(object, "foo");
99 if(!json_is_integer(value) || json_integer_value(value) != 123)
100 fail("json_object_set_new works incorrectly");
103 json_decref(other_string);