d402c0ebd716aa12714c5123d3cca17642d02966
[yaz-moved-to-github.git] / test / test_json.c
1 /**
2  * \file 
3  * \brief JSON test
4  */
5 #include <yaz/test.h>
6 #include <yaz/json.h>
7 #include <string.h>
8 #include <yaz/log.h>
9
10 static int expect(json_parser_t p, const char *input, 
11                   const char *output)
12 {
13     int ret = 0;
14     struct json_node *n;
15
16     n = json_parser_parse(p, input);
17     if (n == 0 && output == 0)
18         ret = 1;
19     else if (n && output)
20     {
21         WRBUF result = wrbuf_alloc();
22
23         json_write_wrbuf(n, result);
24         if (strcmp(wrbuf_cstr(result), output) == 0)
25             ret = 1;
26         else
27         {
28             yaz_log(YLOG_WARN, "expected '%s' but got '%s'",
29                     output, wrbuf_cstr(result));
30         }
31         wrbuf_destroy(result);
32     }
33     else if (!n)
34     {
35         yaz_log(YLOG_WARN, "expected '%s' but got error '%s'",
36                 output, json_parser_get_errmsg(p));
37     }
38     json_remove_node(n);
39     return ret;
40 }
41
42 static void tst1(void)
43 {
44     json_parser_t p = json_parser_create();
45
46     YAZ_CHECK(p);
47     if (!p)
48         return;
49
50     YAZ_CHECK(expect(p, "", 0));
51
52     YAZ_CHECK(expect(p, "1234", "1234"));
53
54     YAZ_CHECK(expect(p, "[ 1234 ]", "[1234]"));
55
56     YAZ_CHECK(expect(p, "{\"k\":tru}", 0));
57
58     YAZ_CHECK(expect(p, "{\"k\":null", 0));
59
60     YAZ_CHECK(expect(p, "{\"k\":nullx}", 0));
61
62     YAZ_CHECK(expect(p, "{\"k\":-", 0));
63
64     YAZ_CHECK(expect(p, "{\"k\":+", 0));
65
66     YAZ_CHECK(expect(p, "{\"k\":\"a}", 0));
67
68     YAZ_CHECK(expect(p, "{\"k\":\"a", 0));
69
70     YAZ_CHECK(expect(p, "{\"k\":\"", 0));
71
72     YAZ_CHECK(expect(p, "{", 0));
73
74     YAZ_CHECK(expect(p, "{}", "{}"));
75
76     YAZ_CHECK(expect(p, "{}  extra", 0));
77
78     YAZ_CHECK(expect(p, "{\"a\":[1,2,3}", 0));
79     
80     YAZ_CHECK(expect(p, "{\"a\":[1,2,", 0));
81
82     YAZ_CHECK(expect(p, "{\"k\":\"wa\"}", "{\"k\":\"wa\"}"));
83
84     YAZ_CHECK(expect(p, "{\"k\":null}", "{\"k\":null}"));
85
86     YAZ_CHECK(expect(p, "{\"k\":false}", "{\"k\":false}"));
87
88     YAZ_CHECK(expect(p, "{\"k\":true}", "{\"k\":true}"));
89
90     YAZ_CHECK(expect(p, "{\"k\":12}", "{\"k\":12}"));
91
92     YAZ_CHECK(expect(p, "{\"k\":-12}", "{\"k\":-12}"));
93
94     YAZ_CHECK(expect(p, "{\"k\":1.2e6}", "{\"k\":1.2e+06}"));
95
96     YAZ_CHECK(expect(p, "{\"k\":1e3}", "{\"k\":1000}"));
97
98     YAZ_CHECK(expect(p, "{\"k\":\"\"}", "{\"k\":\"\"}"));
99
100     YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}"));
101
102     YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2,\"c\":3}",
103                      "{\"a\":1,\"b\":2,\"c\":3}"));
104
105     YAZ_CHECK(expect(p, "{\"a\":[]}", "{\"a\":[]}"));
106
107     YAZ_CHECK(expect(p, "{\"a\":[1]}", "{\"a\":[1]}"));
108
109     YAZ_CHECK(expect(p, "{\"a\":[1,2]}", "{\"a\":[1,2]}"));
110
111     YAZ_CHECK(expect(p, "{\"a\":[1,2,3]}", "{\"a\":[1,2,3]}"));
112
113     YAZ_CHECK(expect(p, "{\"k\":\"\\t\"}", "{\"k\":\"\\t\"}"));
114     YAZ_CHECK(expect(p, "{\"k\":\"\t\"}", "{\"k\":\"\\t\"}"));
115
116     YAZ_CHECK(expect(p, "{\"k\":\"\\n\"}", "{\"k\":\"\\n\"}"));
117     YAZ_CHECK(expect(p, "{\"k\":\"\n\"}", "{\"k\":\"\\n\"}"));
118
119     YAZ_CHECK(expect(p, "{\"k\":\"\\r\"}", "{\"k\":\"\\r\"}"));
120     YAZ_CHECK(expect(p, "{\"k\":\"\r\"}", "{\"k\":\"\\r\"}"));
121
122     YAZ_CHECK(expect(p, "{\"k\":\"\\f\"}", "{\"k\":\"\\f\"}"));
123     YAZ_CHECK(expect(p, "{\"k\":\"\f\"}", "{\"k\":\"\\f\"}"));
124
125     YAZ_CHECK(expect(p, "{\"k\":\"\\b\"}", "{\"k\":\"\\b\"}"));
126     YAZ_CHECK(expect(p, "{\"k\":\"\b\"}", "{\"k\":\"\\b\"}"));
127
128     YAZ_CHECK(expect(p,
129                      "{\"k\":\"\\u0001\\u0002\"}",
130                      "{\"k\":\"\\u0001\\u0002\"}"));
131
132     json_parser_destroy(p);
133 }
134
135 static void tst2(void)
136 {
137     struct json_node *n, *n1;
138
139     n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
140     YAZ_CHECK(n);
141     if (!n)
142         return;
143
144     YAZ_CHECK_EQ(json_count_children(n), 3);
145     
146     n1 = json_get_object(n, "a");
147     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
148     YAZ_CHECK_EQ(json_count_children(n1), 0);
149
150     n1 = json_get_object(n, "b");
151     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
152     YAZ_CHECK_EQ(json_count_children(n1), 0);
153
154     n1 = json_get_object(n, "b");
155     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
156     YAZ_CHECK_EQ(json_count_children(n1), 0);
157
158     n1 = json_get_object(n, "c");
159     YAZ_CHECK(n1 && n1->type == json_node_array);
160     YAZ_CHECK_EQ(json_count_children(n1), 3);
161
162     n1 = json_get_elem(json_get_object(n, "c"), 0);
163     YAZ_CHECK(n1 && n1->type == json_node_true);
164
165     n1 = json_get_elem(json_get_object(n, "c"), 1);
166     YAZ_CHECK(n1 && n1->type == json_node_false);
167
168     n1 = json_get_elem(json_get_object(n, "c"), 2);
169     YAZ_CHECK(n1 && n1->type == json_node_null);
170
171     n1 = json_get_elem(json_get_object(n, "c"), 3);
172     YAZ_CHECK(n1 == 0);
173
174     json_remove_node(n);
175 }
176
177 static int append_check(const char *a, const char *b, const char *exp)
178 {
179     WRBUF w = wrbuf_alloc();
180     struct json_node *n_a, *n_b;
181     int ret = 0;
182
183     n_a = json_parse(a, 0);
184     n_b = json_parse(b, 0);
185     json_append_array(json_get_object(n_a, "a"),
186                       json_detach_object(n_b, "b"));
187
188     json_write_wrbuf(n_a, w);
189
190     if (!strcmp(wrbuf_cstr(w), exp))
191         ret = 1;
192     wrbuf_destroy(w);
193     json_remove_node(n_a);
194     json_remove_node(n_b);
195     return ret;
196 }
197
198 static void tst3(void)
199 {
200     YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
201                            "{\"a\":[1,2,3,5,6,7]}"));
202
203     YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
204                            "{\"a\":[5,6,7]}"));
205
206     YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
207                            "{\"a\":[1,2,3]}"));
208 }
209
210 int main (int argc, char **argv)
211 {
212     YAZ_CHECK_INIT(argc, argv);
213     tst1();
214     tst2();
215     tst3();
216     YAZ_CHECK_TERM;
217 }
218
219 /*
220  * Local variables:
221  * c-basic-offset: 4
222  * c-file-style: "Stroustrup"
223  * indent-tabs-mode: nil
224  * End:
225  * vim: shiftwidth=4 tabstop=8 expandtab
226  */
227
228