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. */
5 #define should_be(var, expected) should_be_(var, #var, expected)
7 static void should_be_(const JsonNode *node, const char *name, const char *expected)
12 if (!json_check(node, errmsg)) {
13 fail("Invariants check failed: %s", errmsg);
17 encoded = json_encode(node);
19 if (strcmp(encoded, expected) == 0)
20 pass("%s is %s", name, expected);
22 fail("%s should be %s, but is actually %s", name, expected, encoded);
27 static void test_string(void)
31 str = json_mkstring("Hello\tworld!\n\001");
32 should_be(str, "\"Hello\\tworld!\\n\\u0001\"");
35 str = json_mkstring("\"\\\b\f\n\r\t");
36 should_be(str, "\"\\\"\\\\\\b\\f\\n\\r\\t\"");
40 static void test_number(void)
44 num = json_mknumber(5678901234.0);
45 should_be(num, "5678901234");
48 num = json_mknumber(-5678901234.0);
49 should_be(num, "-5678901234");
52 num = json_mknumber(0.0 / 0.0);
53 should_be(num, "null");
57 static void test_array(void)
60 JsonNode *children[5 + 1];
62 array = json_mkarray();
63 should_be(array, "[]");
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);
71 json_append_element(array, children[3]);
72 should_be(array, "[3]");
74 json_remove_from_parent(children[3]);
75 should_be(array, "[]");
77 json_prepend_element(array, children[3]);
78 should_be(array, "[3]");
80 json_prepend_element(array, children[2]);
81 should_be(array, "[2,3]");
83 json_append_element(array, children[4]);
84 should_be(array, "[2,3,4]");
86 json_delete(children[3]);
87 should_be(array, "[2,4]");
89 json_prepend_element(array, children[1]);
90 should_be(array, "[1,2,4]");
92 json_delete(children[1]);
93 should_be(array, "[2,4]");
95 json_delete(children[4]);
96 should_be(array, "[2]");
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);
102 json_append_element(array, children[5]);
103 should_be(array, "[2,5]");
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);
110 json_delete(children[2]);
111 json_delete(children[5]);
112 should_be(array, "[]");
114 ok1(json_find_element(array, -1) == NULL);
115 ok1(json_find_element(array, 0) == NULL);
116 ok1(json_find_element(array, 1) == NULL);
121 static void test_object(void)
124 JsonNode *children[5 + 1];
126 object = json_mkobject();
127 should_be(object, "{}");
129 children[1] = json_mknumber(1);
130 children[2] = json_mknumber(2);
131 children[3] = json_mknumber(3);
133 ok1(json_find_member(object, "one") == NULL);
134 ok1(json_find_member(object, "two") == NULL);
135 ok1(json_find_member(object, "three") == NULL);
137 json_append_member(object, "one", children[1]);
138 should_be(object, "{\"one\":1}");
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);
144 json_prepend_member(object, "two", children[2]);
145 should_be(object, "{\"two\":2,\"one\":1}");
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);
151 json_append_member(object, "three", children[3]);
152 should_be(object, "{\"two\":2,\"one\":1,\"three\":3}");
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]);
169 ok1(json_find_element(NULL, 0) == NULL);
170 ok1(json_find_member(NULL, "") == NULL);
171 ok1(json_first_child(NULL) == NULL);
173 node = json_mknull();
174 should_be(node, "null");
177 node = json_mkbool(false);
178 should_be(node, "false");
181 node = json_mkbool(true);
182 should_be(node, "true");
190 return exit_status();