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