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