]> git.ozlabs.org Git - ccan/blob - ccan/json/test/run-construction.c
check_type: fix incorrect documentation.
[ccan] / ccan / json / test / run-construction.c
1 /* Build a list of numbers with various appends and prepends, verify them by testing against their encoded value, do pointer consistency checks each time, do element lookups, and remove items as well. */
2
3 #include "common.h"
4
5 #define should_be(var, expected) should_be_(var, #var, expected)
6
7 static void should_be_(const JsonNode *node, const char *name, const char *expected)
8 {
9         char errmsg[256];
10         char *encoded;
11         
12         if (!json_check(node, errmsg)) {
13                 fail("Invariants check failed: %s", errmsg);
14                 return;
15         }
16         
17         encoded = json_encode(node);
18         
19         if (strcmp(encoded, expected) == 0)
20                 pass("%s is %s", name, expected);
21         else
22                 fail("%s should be %s, but is actually %s", name, expected, encoded);
23         
24         free(encoded);
25 }
26
27 static void test_string(void)
28 {
29         JsonNode *str;
30         
31         str = json_mkstring("Hello\tworld!\n\001");
32         should_be(str, "\"Hello\\tworld!\\n\\u0001\"");
33         json_delete(str);
34         
35         str = json_mkstring("\"\\\b\f\n\r\t");
36         should_be(str, "\"\\\"\\\\\\b\\f\\n\\r\\t\"");
37         json_delete(str);
38 }
39
40 static void test_number(void)
41 {
42         JsonNode *num;
43         
44         num = json_mknumber(5678901234.0);
45         should_be(num, "5678901234");
46         json_delete(num);
47         
48         num = json_mknumber(-5678901234.0);
49         should_be(num, "-5678901234");
50         json_delete(num);
51         
52         num = json_mknumber(0.0 / 0.0);
53         should_be(num, "null");
54         json_delete(num);
55 }
56
57 static void test_array(void)
58 {
59         JsonNode *array;
60         JsonNode *children[5 + 1];
61         
62         array = json_mkarray();
63         should_be(array, "[]");
64         
65         children[1] = json_mknumber(1);
66         children[2] = json_mknumber(2);
67         children[3] = json_mknumber(3);
68         children[4] = json_mknumber(4);
69         children[5] = json_mknumber(5);
70         
71         json_append_element(array, children[3]);
72         should_be(array, "[3]");
73         
74         json_remove_from_parent(children[3]);
75         should_be(array, "[]");
76         
77         json_prepend_element(array, children[3]);
78         should_be(array, "[3]");
79         
80         json_prepend_element(array, children[2]);
81         should_be(array, "[2,3]");
82         
83         json_append_element(array, children[4]);
84         should_be(array, "[2,3,4]");
85         
86         json_delete(children[3]);
87         should_be(array, "[2,4]");
88         
89         json_prepend_element(array, children[1]);
90         should_be(array, "[1,2,4]");
91         
92         json_delete(children[1]);
93         should_be(array, "[2,4]");
94         
95         json_delete(children[4]);
96         should_be(array, "[2]");
97         
98         ok1(json_find_element(array, 0) == children[2]);
99         ok1(json_find_element(array, -1) == NULL);
100         ok1(json_find_element(array, 1) == NULL);
101         
102         json_append_element(array, children[5]);
103         should_be(array, "[2,5]");
104         
105         ok1(json_find_element(array, 0) == children[2]);
106         ok1(json_find_element(array, 1) == children[5]);
107         ok1(json_find_element(array, -1) == NULL);
108         ok1(json_find_element(array, 2) == NULL);
109         
110         json_delete(children[2]);
111         json_delete(children[5]);
112         should_be(array, "[]");
113         
114         ok1(json_find_element(array, -1) == NULL);
115         ok1(json_find_element(array, 0) == NULL);
116         ok1(json_find_element(array, 1) == NULL);
117         
118         json_delete(array);
119 }
120
121 static void test_object(void)
122 {
123         JsonNode *object;
124         JsonNode *children[5 + 1];
125         
126         object = json_mkobject();
127         should_be(object, "{}");
128         
129         children[1] = json_mknumber(1);
130         children[2] = json_mknumber(2);
131         children[3] = json_mknumber(3);
132         
133         ok1(json_find_member(object, "one") == NULL);
134         ok1(json_find_member(object, "two") == NULL);
135         ok1(json_find_member(object, "three") == NULL);
136         
137         json_append_member(object, "one", children[1]);
138         should_be(object, "{\"one\":1}");
139         
140         ok1(json_find_member(object, "one") == children[1]);
141         ok1(json_find_member(object, "two") == NULL);
142         ok1(json_find_member(object, "three") == NULL);
143         
144         json_prepend_member(object, "two", children[2]);
145         should_be(object, "{\"two\":2,\"one\":1}");
146         
147         ok1(json_find_member(object, "one") == children[1]);
148         ok1(json_find_member(object, "two") == children[2]);
149         ok1(json_find_member(object, "three") == NULL);
150         
151         json_append_member(object, "three", children[3]);
152         should_be(object, "{\"two\":2,\"one\":1,\"three\":3}");
153         
154         ok1(json_find_member(object, "one") == children[1]);
155         ok1(json_find_member(object, "two") == children[2]);
156         ok1(json_find_member(object, "three") == children[3]);
157         
158         json_delete(object);
159 }
160
161 int main(void)
162 {
163         JsonNode *node;
164         
165         (void) chomp;
166         
167         plan_tests(49);
168         
169         ok1(json_find_element(NULL, 0) == NULL);
170         ok1(json_find_member(NULL, "") == NULL);
171         ok1(json_first_child(NULL) == NULL);
172         
173         node = json_mknull();
174         should_be(node, "null");
175         json_delete(node);
176         
177         node = json_mkbool(false);
178         should_be(node, "false");
179         json_delete(node);
180         
181         node = json_mkbool(true);
182         should_be(node, "true");
183         json_delete(node);
184         
185         test_string();
186         test_number();
187         test_array();
188         test_object();
189         
190         return exit_status();
191 }