Fix reference counting on true, false and null
[jansson.git] / test / suites / api / test_simple.c
1 /*
2  * Copyright (c) 2009, 2010 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 <string.h>
9 #include <jansson.h>
10 #include "util.h"
11
12 /* Call the simple functions not covered by other tests of the public API */
13 int main()
14 {
15     json_t *value;
16
17     value = json_integer(1);
18     if(json_typeof(value) != JSON_INTEGER)
19         fail("json_typeof failed");
20
21     if(json_is_object(value))
22         fail("json_is_object failed");
23
24     if(json_is_array(value))
25         fail("json_is_array failed");
26
27     if(json_is_string(value))
28         fail("json_is_string failed");
29
30     if(!json_is_integer(value))
31         fail("json_is_integer failed");
32
33     if(json_is_real(value))
34         fail("json_is_real failed");
35
36     if(!json_is_number(value))
37         fail("json_is_number failed");
38
39     if(json_is_true(value))
40         fail("json_is_true failed");
41
42     if(json_is_false(value))
43         fail("json_is_false failed");
44
45     if(json_is_boolean(value))
46         fail("json_is_boolean failed");
47
48     if(json_is_null(value))
49         fail("json_is_null failed");
50
51     json_decref(value);
52
53
54     value = json_string("foo");
55     if(!value)
56         fail("json_string failed");
57     if(strcmp(json_string_value(value), "foo"))
58         fail("invalid string value");
59
60     if(json_string_set(value, "bar"))
61         fail("json_string_set failed");
62     if(strcmp(json_string_value(value), "bar"))
63         fail("invalid string value");
64
65     json_decref(value);
66
67     value = json_string(NULL);
68     if(value)
69         fail("json_string(NULL) failed");
70
71     /* invalid UTF-8  */
72     value = json_string("a\xefz");
73     if(value)
74         fail("json_string(<invalid utf-8>) failed");
75
76     value = json_string_nocheck("foo");
77     if(!value)
78         fail("json_string_nocheck failed");
79     if(strcmp(json_string_value(value), "foo"))
80         fail("invalid string value");
81
82     if(json_string_set_nocheck(value, "bar"))
83         fail("json_string_set_nocheck failed");
84     if(strcmp(json_string_value(value), "bar"))
85         fail("invalid string value");
86
87     json_decref(value);
88
89     /* invalid UTF-8 */
90     value = json_string_nocheck("qu\xff");
91     if(!value)
92         fail("json_string_nocheck failed");
93     if(strcmp(json_string_value(value), "qu\xff"))
94         fail("invalid string value");
95
96     if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
97         fail("json_string_set_nocheck failed");
98     if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
99         fail("invalid string value");
100
101     json_decref(value);
102
103
104     value = json_integer(123);
105     if(!value)
106         fail("json_integer failed");
107     if(json_integer_value(value) != 123)
108         fail("invalid integer value");
109     if(json_number_value(value) != 123.0)
110         fail("invalid number value");
111
112     if(json_integer_set(value, 321))
113         fail("json_integer_set failed");
114     if(json_integer_value(value) != 321)
115         fail("invalid integer value");
116     if(json_number_value(value) != 321.0)
117         fail("invalid number value");
118
119     json_decref(value);
120
121     value = json_real(123.123);
122     if(!value)
123         fail("json_real failed");
124     if(json_real_value(value) != 123.123)
125         fail("invalid integer value");
126     if(json_number_value(value) != 123.123)
127         fail("invalid number value");
128
129     if(json_real_set(value, 321.321))
130         fail("json_real_set failed");
131     if(json_real_value(value) != 321.321)
132         fail("invalid real value");
133     if(json_number_value(value) != 321.321)
134         fail("invalid number value");
135
136     json_decref(value);
137
138     value = json_true();
139     if(!value)
140         fail("json_true failed");
141     json_decref(value);
142
143     value = json_false();
144     if(!value)
145         fail("json_false failed");
146     json_decref(value);
147
148     value = json_null();
149     if(!value)
150         fail("json_null failed");
151     json_decref(value);
152
153     /* Test reference counting on singletons (true, false, null) */
154     value = json_true();
155     if(value->refcount != (unsigned int)-1)
156       fail("refcounting true works incorrectly");
157     json_decref(value);
158     if(value->refcount != (unsigned int)-1)
159       fail("refcounting true works incorrectly");
160     json_incref(value);
161     if(value->refcount != (unsigned int)-1)
162       fail("refcounting true works incorrectly");
163
164     value = json_false();
165     if(value->refcount != (unsigned int)-1)
166       fail("refcounting false works incorrectly");
167     json_decref(value);
168     if(value->refcount != (unsigned int)-1)
169       fail("refcounting false works incorrectly");
170     json_incref(value);
171     if(value->refcount != (unsigned int)-1)
172       fail("refcounting false works incorrectly");
173
174     value = json_null();
175     if(value->refcount != (unsigned int)-1)
176       fail("refcounting null works incorrectly");
177     json_decref(value);
178     if(value->refcount != (unsigned int)-1)
179       fail("refcounting null works incorrectly");
180     json_incref(value);
181     if(value->refcount != (unsigned int)-1)
182       fail("refcounting null works incorrectly");
183
184     return 0;
185 }