Better argument validation
[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     if(!json_object_set(object, NULL, string))
33         fail("able to set NULL key");
34
35     if(!json_object_set(object, "a", NULL))
36         fail("able to set NULL value");
37
38     iter = json_object_iter(object);
39     if(!iter)
40         fail("unable to get iterator");
41
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");
48
49     /* invalid UTF-8 in key */
50     if(!json_object_set(object, "a\xefz", string))
51         fail("able to set invalid unicode key");
52
53     value = json_object_get(object, "a");
54     if(!value)
55         fail("no value for existing key");
56     if(value != string)
57         fail("got different value than what was added");
58
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");
64
65     value = json_object_get(object, "a");
66     if(!value)
67         fail("no value for existing key");
68     if(value != string)
69         fail("got different value than what was added");
70
71     if(json_object_set(object, "a", other_string))
72         fail("unable to replace an existing key");
73
74     value = json_object_get(object, "a");
75     if(!value)
76         fail("no value for existing key");
77     if(value != other_string)
78         fail("got different value than what was set");
79
80     if(!json_object_del(object, "nonexisting"))
81         fail("able to delete a nonexisting key");
82
83     if(json_object_del(object, "px"))
84         fail("unable to delete an existing key");
85
86     if(json_object_del(object, "a"))
87         fail("unable to delete an existing key");
88
89     if(json_object_del(object, "lp"))
90         fail("unable to delete an existing key");
91
92
93     /* add many keys to initiate rehashing */
94
95     if(json_object_set(object, "a", string))
96         fail("unable to set value");
97
98     if(json_object_set(object, "lp", string))
99         fail("unable to set value");
100
101     if(json_object_set(object, "px", string))
102         fail("unable to set value");
103
104     if(json_object_set(object, "c", string))
105         fail("unable to set value");
106
107     if(json_object_set(object, "d", string))
108         fail("unable to set value");
109
110     if(json_object_set(object, "e", string))
111         fail("unable to set value");
112
113
114     if(json_object_set_new(object, "foo", json_integer(123)))
115         fail("unable to set new value");
116
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");
120
121     if(!json_object_set_new(object, NULL, json_integer(432)))
122         fail("able to set_new NULL key");
123
124     if(!json_object_set_new(object, "foo", NULL))
125         fail("able to set_new NULL value");
126
127     json_decref(string);
128     json_decref(other_string);
129     json_decref(object);
130
131     return 0;
132 }