File: test_json.c

package info (click to toggle)
yaz 4.2.30-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 15,120 kB
  • ctags: 13,367
  • sloc: xml: 119,746; ansic: 66,073; sh: 11,795; tcl: 2,125; makefile: 1,308; yacc: 371
file content (236 lines) | stat: -rw-r--r-- 5,987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* This file is part of the YAZ toolkit.
 * Copyright (C) 1995-2012 Index Data
 * See the file LICENSE for details.
 */
/**
 * \file 
 * \brief JSON test
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <yaz/test.h>
#include <yaz/json.h>
#include <string.h>
#include <yaz/log.h>

static int expect(json_parser_t p, const char *input, 
                  const char *output)
{
    int ret = 0;
    struct json_node *n;

    n = json_parser_parse(p, input);
    if (n == 0 && output == 0)
        ret = 1;
    else if (n && output)
    {
        WRBUF result = wrbuf_alloc();

        json_write_wrbuf(n, result);
        if (strcmp(wrbuf_cstr(result), output) == 0)
            ret = 1;
        else
        {
            yaz_log(YLOG_WARN, "expected '%s' but got '%s'",
                    output, wrbuf_cstr(result));
        }
        wrbuf_destroy(result);
    }
    else if (!n)
    {
        yaz_log(YLOG_WARN, "expected '%s' but got error '%s'",
                output, json_parser_get_errmsg(p));
    }
    json_remove_node(n);
    return ret;
}

static void tst1(void)
{
    json_parser_t p = json_parser_create();

    YAZ_CHECK(p);
    if (!p)
        return;

    YAZ_CHECK(expect(p, "", 0));

    YAZ_CHECK(expect(p, "1234", "1234"));

    YAZ_CHECK(expect(p, "[ 1234 ]", "[1234]"));

    YAZ_CHECK(expect(p, "{\"k\":tru}", 0));

    YAZ_CHECK(expect(p, "{\"k\":null", 0));

    YAZ_CHECK(expect(p, "{\"k\":nullx}", 0));

    YAZ_CHECK(expect(p, "{\"k\":-", 0));

    YAZ_CHECK(expect(p, "{\"k\":+", 0));

    YAZ_CHECK(expect(p, "{\"k\":\"a}", 0));

    YAZ_CHECK(expect(p, "{\"k\":\"a", 0));

    YAZ_CHECK(expect(p, "{\"k\":\"", 0));

    YAZ_CHECK(expect(p, "{", 0));

    YAZ_CHECK(expect(p, "{}", "{}"));

    YAZ_CHECK(expect(p, "{}  extra", 0));

    YAZ_CHECK(expect(p, "{\"a\":[1,2,3}", 0));
    
    YAZ_CHECK(expect(p, "{\"a\":[1,2,", 0));

    YAZ_CHECK(expect(p, "{\"k\":\"wa\"}", "{\"k\":\"wa\"}"));

    YAZ_CHECK(expect(p, "{\"k\":null}", "{\"k\":null}"));

    YAZ_CHECK(expect(p, "{\"k\":false}", "{\"k\":false}"));

    YAZ_CHECK(expect(p, "{\"k\":true}", "{\"k\":true}"));

    YAZ_CHECK(expect(p, "{\"k\":12}", "{\"k\":12}"));

    YAZ_CHECK(expect(p, "{\"k\":-12}", "{\"k\":-12}"));

    YAZ_CHECK(expect(p, "{\"k\":1.2e6}", "{\"k\":1.2e+06}"));

    YAZ_CHECK(expect(p, "{\"k\":1e3}", "{\"k\":1000}"));

    YAZ_CHECK(expect(p, "{\"k\":\"\"}", "{\"k\":\"\"}"));

    YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}"));

    YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2,\"c\":3}",
                     "{\"a\":1,\"b\":2,\"c\":3}"));

    YAZ_CHECK(expect(p, "{\"a\":[]}", "{\"a\":[]}"));

    YAZ_CHECK(expect(p, "{\"a\":[1]}", "{\"a\":[1]}"));

    YAZ_CHECK(expect(p, "{\"a\":[1,2]}", "{\"a\":[1,2]}"));

    YAZ_CHECK(expect(p, "{\"a\":[1,2,3]}", "{\"a\":[1,2,3]}"));

    YAZ_CHECK(expect(p, "{\"k\":\"\\t\"}", "{\"k\":\"\\t\"}"));
    YAZ_CHECK(expect(p, "{\"k\":\"\t\"}", "{\"k\":\"\\t\"}"));

    YAZ_CHECK(expect(p, "{\"k\":\"\\n\"}", "{\"k\":\"\\n\"}"));
    YAZ_CHECK(expect(p, "{\"k\":\"\n\"}", "{\"k\":\"\\n\"}"));

    YAZ_CHECK(expect(p, "{\"k\":\"\\r\"}", "{\"k\":\"\\r\"}"));
    YAZ_CHECK(expect(p, "{\"k\":\"\r\"}", "{\"k\":\"\\r\"}"));

    YAZ_CHECK(expect(p, "{\"k\":\"\\f\"}", "{\"k\":\"\\f\"}"));
    YAZ_CHECK(expect(p, "{\"k\":\"\f\"}", "{\"k\":\"\\f\"}"));

    YAZ_CHECK(expect(p, "{\"k\":\"\\b\"}", "{\"k\":\"\\b\"}"));
    YAZ_CHECK(expect(p, "{\"k\":\"\b\"}", "{\"k\":\"\\b\"}"));

    YAZ_CHECK(expect(p,
                     "{\"k\":\"\\u0001\\u0002\"}",
                     "{\"k\":\"\\u0001\\u0002\"}"));

    json_parser_destroy(p);
}

static void tst2(void)
{
    struct json_node *n, *n1;

    n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
    YAZ_CHECK(n);
    if (!n)
        return;

    YAZ_CHECK_EQ(json_count_children(n), 3);
    
    n1 = json_get_object(n, "a");
    YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
    YAZ_CHECK_EQ(json_count_children(n1), 0);

    n1 = json_get_object(n, "b");
    YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
    YAZ_CHECK_EQ(json_count_children(n1), 0);

    n1 = json_get_object(n, "b");
    YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
    YAZ_CHECK_EQ(json_count_children(n1), 0);

    n1 = json_get_object(n, "c");
    YAZ_CHECK(n1 && n1->type == json_node_array);
    YAZ_CHECK_EQ(json_count_children(n1), 3);

    n1 = json_get_elem(json_get_object(n, "c"), 0);
    YAZ_CHECK(n1 && n1->type == json_node_true);

    n1 = json_get_elem(json_get_object(n, "c"), 1);
    YAZ_CHECK(n1 && n1->type == json_node_false);

    n1 = json_get_elem(json_get_object(n, "c"), 2);
    YAZ_CHECK(n1 && n1->type == json_node_null);

    n1 = json_get_elem(json_get_object(n, "c"), 3);
    YAZ_CHECK(n1 == 0);

    json_remove_node(n);
}

static int append_check(const char *a, const char *b, const char *exp)
{
    WRBUF w = wrbuf_alloc();
    struct json_node *n_a, *n_b;
    int ret = 0;

    n_a = json_parse(a, 0);
    n_b = json_parse(b, 0);
    json_append_array(json_get_object(n_a, "a"),
                      json_detach_object(n_b, "b"));

    json_write_wrbuf(n_a, w);

    if (!strcmp(wrbuf_cstr(w), exp))
        ret = 1;
    wrbuf_destroy(w);
    json_remove_node(n_a);
    json_remove_node(n_b);
    return ret;
}

static void tst3(void)
{
    YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
                           "{\"a\":[1,2,3,5,6,7]}"));

    YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
                           "{\"a\":[5,6,7]}"));

    YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
                           "{\"a\":[1,2,3]}"));
}

int main (int argc, char **argv)
{
    YAZ_CHECK_INIT(argc, argv);
    tst1();
    tst2();
    tst3();
    YAZ_CHECK_TERM;
}

/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */