1b42c028aeae3458c248302fd052017a844d248b
[jansson.git] / test / testprogs / test_object.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
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.
6  */
7
8 #include <jansson.h>
9 #include "util.h"
10
11 int main()
12 {
13     json_t *object, *string, *other_string, *value;
14
15     object = json_object();
16     string = json_string("test");
17     other_string = json_string("other");
18
19     if(!object)
20         fail("unable to create object");
21     if(!string)
22         fail("unable to create string");
23     if(!other_string)
24         fail("unable to create string");
25
26     if(json_object_get(object, "a"))
27         fail("value for nonexisting key");
28
29     if(json_object_set(object, "a", string))
30         fail("unable to set value");
31
32     /* invalid UTF-8 in key */
33     if(!json_object_set(object, "a\xefz", string))
34         fail("able to set invalid unicode key");
35
36     value = json_object_get(object, "a");
37     if(!value)
38         fail("no value for existing key");
39     if(value != string)
40         fail("got different value than what was added");
41
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");
47
48     value = json_object_get(object, "a");
49     if(!value)
50         fail("no value for existing key");
51     if(value != string)
52         fail("got different value than what was added");
53
54     if(json_object_set(object, "a", other_string))
55         fail("unable to replace an existing key");
56
57     value = json_object_get(object, "a");
58     if(!value)
59         fail("no value for existing key");
60     if(value != other_string)
61         fail("got different value than what was set");
62
63     if(!json_object_del(object, "nonexisting"))
64         fail("able to delete a nonexisting key");
65
66     if(json_object_del(object, "px"))
67         fail("unable to delete an existing key");
68
69     if(json_object_del(object, "a"))
70         fail("unable to delete an existing key");
71
72     if(json_object_del(object, "lp"))
73         fail("unable to delete an existing key");
74
75
76     /* add many keys to initiate rehashing */
77
78     if(json_object_set(object, "a", string))
79         fail("unable to set value");
80
81     if(json_object_set(object, "lp", string))
82         fail("unable to set value");
83
84     if(json_object_set(object, "px", string))
85         fail("unable to set value");
86
87     if(json_object_set(object, "c", string))
88         fail("unable to set value");
89
90     if(json_object_set(object, "d", string))
91         fail("unable to set value");
92
93     if(json_object_set(object, "e", string))
94         fail("unable to set value");
95
96
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");
101
102     json_decref(string);
103     json_decref(other_string);
104     json_decref(object);
105
106     return 0;
107 }