chiark / gitweb /
8777cf7a401d060837a0976edd19ac4bd52e8ed1
[elogind.git] / src / test / test-json.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "log.h"
23 #include "util.h"
24 #include "json.h"
25
26 static void test_one(const char *data, ...) {
27         void *state = NULL;
28         va_list ap;
29
30         va_start(ap, data);
31
32         for (;;) {
33                 _cleanup_free_ char *str = NULL;
34                 union json_value v = {};
35                 int t, tt;
36
37                 t = json_tokenize(&data, &str, &v, &state, NULL);
38                 tt = va_arg(ap, int);
39
40                 assert_se(t == tt);
41
42                 if (t == JSON_END || t < 0)
43                         break;
44
45                 else if (t == JSON_STRING) {
46                         const char *nn;
47
48                         nn = va_arg(ap, const char *);
49                         assert_se(streq_ptr(nn, str));
50
51                 } else if (t == JSON_REAL) {
52                         double d;
53
54                         d = va_arg(ap, double);
55                         assert_se(abs(d - v.real) < 0.001);
56
57                 } else if (t == JSON_INTEGER) {
58                         intmax_t i;
59
60                         i = va_arg(ap, intmax_t);
61                         assert_se(i == v.integer);
62
63                 } else if (t == JSON_BOOLEAN) {
64                         bool b;
65
66                         b = va_arg(ap, int);
67                         assert_se(b == v.boolean);
68                 }
69         }
70
71         va_end(ap);
72 }
73
74 int main(int argc, char *argv[]) {
75
76         test_one("x", -EINVAL);
77         test_one("", JSON_END);
78         test_one(" ", JSON_END);
79         test_one("0", JSON_INTEGER, (intmax_t) 0, JSON_END);
80         test_one("1234", JSON_INTEGER, (intmax_t) 1234, JSON_END);
81         test_one("3.141", JSON_REAL, 3.141, JSON_END);
82         test_one("0.0", JSON_REAL, 0.0, JSON_END);
83         test_one("7e3", JSON_REAL, 7e3, JSON_END);
84         test_one("-7e-3", JSON_REAL, -7e-3, JSON_END);
85         test_one("true", JSON_BOOLEAN, true, JSON_END);
86         test_one("false", JSON_BOOLEAN, false, JSON_END);
87         test_one("null", JSON_NULL, JSON_END);
88         test_one("{}", JSON_OBJECT_OPEN, JSON_OBJECT_CLOSE, JSON_END);
89         test_one("\t {\n} \n", JSON_OBJECT_OPEN, JSON_OBJECT_CLOSE, JSON_END);
90         test_one("[]", JSON_ARRAY_OPEN, JSON_ARRAY_CLOSE, JSON_END);
91         test_one("\t [] \n\n", JSON_ARRAY_OPEN, JSON_ARRAY_CLOSE, JSON_END);
92         test_one("\"\"", JSON_STRING, "", JSON_END);
93         test_one("\"foo\"", JSON_STRING, "foo", JSON_END);
94         test_one("\"foo\\nfoo\"", JSON_STRING, "foo\nfoo", JSON_END);
95         test_one("{\"foo\" : \"bar\"}", JSON_OBJECT_OPEN, JSON_STRING, "foo", JSON_COLON, JSON_STRING, "bar", JSON_OBJECT_CLOSE, JSON_END);
96         test_one("{\"foo\" : [true, false]}", JSON_OBJECT_OPEN, JSON_STRING, "foo", JSON_COLON, JSON_ARRAY_OPEN, JSON_BOOLEAN, true, JSON_COMMA, JSON_BOOLEAN, false, JSON_ARRAY_CLOSE, JSON_OBJECT_CLOSE, JSON_END);
97         test_one("\"\xef\xbf\xbd\"", JSON_STRING, "\xef\xbf\xbd", JSON_END);
98         test_one("\"\\ufffd\"", JSON_STRING, "\xef\xbf\xbd", JSON_END);
99
100         return 0;
101 }