File: primitives.c

package info (click to toggle)
liblaxjson 1.0.5-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 172 kB
  • sloc: ansic: 1,202; makefile: 7
file content (458 lines) | stat: -rw-r--r-- 10,893 bytes parent folder | download | duplicates (3)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <laxjson.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static char out_buf[16384];
static int out_buf_index;

static void add_buf(const char *str, int len) {
    if (len == 0)
        len = strlen(str);
    memcpy(&out_buf[out_buf_index], str, len);
    out_buf_index += len;
}

static const char *type_to_str(enum LaxJsonType type) {
    switch (type) {
        case LaxJsonTypeString:
            return "string";
        case LaxJsonTypeProperty:
            return "property";
        case LaxJsonTypeNumber:
            return "number";
        case LaxJsonTypeObject:
            return "object";
        case LaxJsonTypeArray:
            return "array";
        case LaxJsonTypeTrue:
            return "true";
        case LaxJsonTypeFalse:
            return "false";
        case LaxJsonTypeNull:
            return "null";
    }
    exit(1);
}

static void feed(struct LaxJsonContext *context, const char *data) {
    int size = strlen(data);

    enum LaxJsonError err = lax_json_feed(context, size, data);

    if (!err)
        return;

    fprintf(stderr, "line %d column %d parse error: %s\n", context->line,
            context->column, lax_json_str_err(err));
    exit(1);
}

static int on_string_build(struct LaxJsonContext *context,
    enum LaxJsonType type, const char *value, int length)
{
    add_buf(type_to_str(type), 0);
    add_buf("\n", 0);
    add_buf(value, length);
    add_buf("\n", 0);
    return 0;
}

static int on_number_build(struct LaxJsonContext *context, double x)
{
    out_buf_index += snprintf(&out_buf[out_buf_index], 30, "number %g\n", x);
    return 0;
}

static int on_primitive_build(struct LaxJsonContext *context, enum LaxJsonType type)
{
    out_buf_index += snprintf(&out_buf[out_buf_index], 50, "%s\n", type_to_str(type));
    return 0;
}

static int on_begin_build(struct LaxJsonContext *context, enum LaxJsonType type)
{
    out_buf_index += snprintf(&out_buf[out_buf_index], 50, "begin %s\n", type_to_str(type));
    return 0;
}

static int on_end_build(struct LaxJsonContext *context, enum LaxJsonType type)
{
    out_buf_index += snprintf(&out_buf[out_buf_index], 50, "end %s\n", type_to_str(type));
    return 0;
}

static void check_build(struct LaxJsonContext *context, const char *output) {
    int expected_len = strlen(output);
    enum LaxJsonError err = lax_json_eof(context);
    if (err != LaxJsonErrorNone) {
        fprintf(stderr, "%s\n", lax_json_str_err(err));
        exit(1);
    }
    if (out_buf_index != expected_len) {
        fprintf(stderr, "\n"
                "EXPECTED:\n"
                "---------\n"
                "%s\n"
                "RECEIVED:\n"
                "---------\n"
                "%s\n", output, out_buf);
        exit(1);
    }
    if (memcmp(output, out_buf, expected_len) != 0) {
        fprintf(stderr,
                "EXPECTED:\n"
                "---------\n"
                "%s\n"
                "RECEIVED:\n"
                "---------\n"
                "%s\n", output, out_buf);
        exit(1);
    }
    lax_json_destroy(context);
}

static struct LaxJsonContext *init_for_build(void) {
    struct LaxJsonContext *context = lax_json_create();
    if (!context)
        exit(1);

    out_buf_index = 0;

    context->userdata = NULL;
    context->string = on_string_build;
    context->number = on_number_build;
    context->primitive = on_primitive_build;
    context->begin = on_begin_build;
    context->end = on_end_build;

    return context;
}

static void check_error(const char *input, enum LaxJsonError error, int line, int col) {
    struct LaxJsonContext *context = init_for_build();

    int size = strlen(input);
    enum LaxJsonError err = lax_json_feed(context, size, input);

    if (err == LaxJsonErrorNone)
        err = lax_json_eof(context);

    if (err != error) {
        fprintf(stderr, "Expected %s, received %s\n", lax_json_str_err(error), lax_json_str_err(err));
        exit(1);
    }

    if (context->line != line) {
        fprintf(stderr, "Expected error line %d, received error line %d\n", line, context->line);
        exit(1);
    }

    if (context->column != col) {
        fprintf(stderr, "Expected error column %d, received error column %d\n", col, context->column);
        exit(1);
    }

    lax_json_destroy(context);
}


static void test_false(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
        "// this is a comment\n"
        " false"
        );

    check_build(context,
            "false\n"
            );
}

static void test_true(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
        " /* before comment */true"
        );

    check_build(context,
            "true\n"
            );
}

static void test_null(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
        "null/* after comment*/ // line comment"
        );

    check_build(context,
            "null\n"
            );
}

static void test_string(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
        "\"foo\""
        );

    check_build(context,
            "string\n"
            "foo\n"
            );
}

static void test_basic_json(void) {
    struct LaxJsonContext *context;

    context = init_for_build();

    feed(context,
        "// comments are OK :)\n"
        "// single quotes, double quotes, and no quotes are OK\n"
        "{\n"
        "  textures: {\n"
        "    cockpit: {\n"
        "      images: {\n"
        "        arrow: {\n"
        "          path: \"img/arrow.png\",\n"
        "          anchor: \"left\"\n"
        "        },"
        "        'radar-circle': {\n"
        "          path: \"img/radar-circle.png\",\n"
        "          anchor: \"center\"\n"
        "        }\n"
        "      }\n"
        "    }\n"
        "  }\n"
        "}\n"
        );

    check_build(context,
            "begin object\n"
            "property\n"
            "textures\n"
            "begin object\n"
            "property\n"
            "cockpit\n"
            "begin object\n"
            "property\n"
            "images\n"
            "begin object\n"
            "property\n"
            "arrow\n"
            "begin object\n"
            "property\n"
            "path\n"
            "string\n"
            "img/arrow.png\n"
            "property\n"
            "anchor\n"
            "string\n"
            "left\n"
            "end object\n"
            "property\n"
            "radar-circle\n"
            "begin object\n"
            "property\n"
            "path\n"
            "string\n"
            "img/radar-circle.png\n"
            "property\n"
            "anchor\n"
            "string\n"
            "center\n"
            "end object\n"
            "end object\n"
            "end object\n"
            "end object\n"
            "end object\n"
            );
}

static void test_empty_object(void) {
    struct LaxJsonContext *context;

    context = init_for_build();

    feed(context, "{}");

    check_build(context,
            "begin object\n"
            "end object\n"
            );
}

static void test_float_value(void) {
    struct LaxJsonContext *context;

    context = init_for_build();

    feed(context,
            "{\n"
            "\"PI\": 3.141E-10"
            "}"
            );

    check_build(context,
            "begin object\n"
            "property\n"
            "PI\n"
            "number 3.141e-10\n"
            "end object\n"
            );
}

static void test_simple_digit_array(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
            "[ 1,2,3,4]"
            );

    check_build(context,
            "begin array\n"
            "number 1\n"
            "number 2\n"
            "number 3\n"
            "number 4\n"
            "end array\n"
            );
}

static void test_simple_string_array(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
            "[ \"1\",\"2\",\"3\",\"4\"]"
            );

    check_build(context,
            "begin array\n"
            "string\n"
            "1\n"
            "string\n"
            "2\n"
            "string\n"
            "3\n"
            "string\n"
            "4\n"
            "end array\n"
            );
}

static void test_array_of_empty_object(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
            "[ { }, { }, []]"
            );

    check_build(context,
            "begin array\n"
            "begin object\n"
            "end object\n"
            "begin object\n"
            "end object\n"
            "begin array\n"
            "end array\n"
            "end array\n"
            );
}

static void test_unclosed_value(void) {
    check_error(
            "{ foo: \"value\n"
            "}\n"
            , LaxJsonErrorUnexpectedEof, 3, 0);
}

static void test_unicode_text(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
            "{ \"v\":\"\\u2000\\u20ff\"}"
            );

    check_build(context,
            "begin object\n"
            "property\n"
            "v\n"
            "string\n"
            "\xe2\x80\x80\xe2\x83\xbf\n"
            "end object\n"
            );
}

static void test_escapes(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context,
            "{ prop: \"\\b\\n\\\"\\\\\\t\\f\\r\" }"
        );

    check_build(context,
            "begin object\n"
            "property\n"
            "prop\n"
            "string\n"
            "\b\n\"\\\t\f\r\n"
            "end object\n"
            );
}

static void test_decimals(void) {
    struct LaxJsonContext *context = init_for_build();

    feed(context, "{n: 0.3}");

    check_build(context,
            "begin object\n"
            "property\n"
            "n\n"
            "number 0.3\n"
            "end object\n"
            );
}


struct Test {
    const char *name;
    void (*fn)(void);
};

static struct Test tests[] = {
    {"false primitive", test_false},
    {"true primitive", test_true},
    {"null primitive", test_null},
    {"string primitive", test_string},
    {"basic json", test_basic_json},
    {"empty object", test_empty_object},
    {"float value", test_float_value},
    {"simple digit array", test_simple_digit_array},
    {"simple string array", test_simple_string_array},
    {"array of empty object", test_array_of_empty_object},
    {"unclosed value", test_unclosed_value},
    {"unicode text", test_unicode_text},
    {"escapes", test_escapes},
    {"decimal", test_decimals},
    {NULL, NULL},
};

int main(int argc, char *argv[]) {
    struct Test *test = &tests[0];

    while (test->name) {
        fprintf(stderr, "testing %s...", test->name);
        test->fn();
        fprintf(stderr, "OK\n");
        test += 1;
    }

    return 0;
}