blob: e6774d4f40010eaa02fd6552f5d7d26f949cb615 [file] [log] [blame]
Michal Vaskof20ad112023-02-10 15:12:12 +01001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_json.c
Radek Krejci50f0c6b2020-06-18 16:31:48 +02003 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for a generic JSON parser
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci50f0c6b2020-06-18 16:31:48 +020016
Radek Krejci50f0c6b2020-06-18 16:31:48 +020017#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "in_internal.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010019#include "json.h"
Radek Krejci50f0c6b2020-06-18 16:31:48 +020020static void
21test_general(void **state)
22{
23 struct lyjson_ctx *jsonctx;
24 struct ly_in *in;
25 const char *str;
26
Radek Krejci50f0c6b2020-06-18 16:31:48 +020027 /* empty */
28 str = "";
29 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010030 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020031 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
32 lyjson_ctx_free(jsonctx);
33
34 str = " \n\t \n";
35 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010036 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020037 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
38 lyjson_ctx_free(jsonctx);
39
40 /* constant values */
41 str = "true";
42 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010043 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020044 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
45 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
46 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
47 lyjson_ctx_free(jsonctx);
48
49 str = "false";
50 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010051 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020052 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
53 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
54 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
55 lyjson_ctx_free(jsonctx);
56
57 str = "null";
58 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010059 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020060 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
61 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
62 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
63 lyjson_ctx_free(jsonctx);
64
65 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020066}
67
68static void
69test_number(void **state)
70{
71 struct lyjson_ctx *jsonctx;
72 struct ly_in *in;
73 const char *str;
74
Radek Krejci50f0c6b2020-06-18 16:31:48 +020075 /* simple value */
76 str = "11";
77 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010078 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020079 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
80 assert_string_equal("11", jsonctx->value);
81 assert_int_equal(2, jsonctx->value_len);
82 assert_int_equal(0, jsonctx->dynamic);
83 lyjson_ctx_free(jsonctx);
84
85 /* fraction number */
86 str = "37.7668";
87 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010088 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020089 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
90 assert_string_equal("37.7668", jsonctx->value);
91 assert_int_equal(7, jsonctx->value_len);
92 assert_int_equal(0, jsonctx->dynamic);
93 lyjson_ctx_free(jsonctx);
94
95 /* negative number */
96 str = "-122.3959";
97 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010098 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020099 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
100 assert_string_equal("-122.3959", jsonctx->value);
101 assert_int_equal(9, jsonctx->value_len);
102 assert_int_equal(0, jsonctx->dynamic);
103 lyjson_ctx_free(jsonctx);
104
aPiecek76034c32021-06-08 15:03:11 +0200105 /* integer, positive exponent */
106 str = "550E3";
aPieceka40b4662021-05-13 15:47:03 +0200107 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100108 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200109 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200110 assert_string_equal("550000", jsonctx->value);
111 assert_int_equal(6, jsonctx->value_len);
112 assert_int_equal(1, jsonctx->dynamic);
aPieceka40b4662021-05-13 15:47:03 +0200113 lyjson_ctx_free(jsonctx);
114
aPiecek76034c32021-06-08 15:03:11 +0200115 str = "-550E3";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200116 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100117 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200118 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200119 assert_string_equal("-550000", jsonctx->value);
120 assert_int_equal(7, jsonctx->value_len);
121 assert_int_equal(1, jsonctx->dynamic);
122 lyjson_ctx_free(jsonctx);
123
124 /* integer, negative exponent */
125 str = "1E-1";
126 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100127 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200128 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
129 assert_string_equal("0.1", jsonctx->value);
130 assert_int_equal(3, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200131 assert_int_equal(1, jsonctx->dynamic);
132 lyjson_ctx_free(jsonctx);
133
134 str = "15E-1";
135 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100136 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200137 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
138 assert_string_equal("1.5", jsonctx->value);
139 assert_int_equal(3, jsonctx->value_len);
140 assert_int_equal(1, jsonctx->dynamic);
141 lyjson_ctx_free(jsonctx);
142
aPiecek76034c32021-06-08 15:03:11 +0200143 str = "-15E-1";
aPiecek350a6bf2021-05-25 07:59:10 +0200144 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100145 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek350a6bf2021-05-25 07:59:10 +0200146 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200147 assert_string_equal("-1.5", jsonctx->value);
aPiecek350a6bf2021-05-25 07:59:10 +0200148 assert_int_equal(4, jsonctx->value_len);
149 assert_int_equal(1, jsonctx->dynamic);
150 lyjson_ctx_free(jsonctx);
151
aPiecek76034c32021-06-08 15:03:11 +0200152 str = "16E-2";
aPiecek350a6bf2021-05-25 07:59:10 +0200153 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100154 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek350a6bf2021-05-25 07:59:10 +0200155 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200156 assert_string_equal("0.16", jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200157 assert_int_equal(4, jsonctx->value_len);
158 assert_int_equal(1, jsonctx->dynamic);
159 lyjson_ctx_free(jsonctx);
160
aPiecek76034c32021-06-08 15:03:11 +0200161 str = "-16E-2";
aPiecek58e46a52021-05-05 10:04:47 +0200162 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100163 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek58e46a52021-05-05 10:04:47 +0200164 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200165 assert_string_equal("-0.16", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200166 assert_int_equal(5, jsonctx->value_len);
167 assert_int_equal(1, jsonctx->dynamic);
168 lyjson_ctx_free(jsonctx);
169
aPiecek76034c32021-06-08 15:03:11 +0200170 str = "17E-3";
aPieceka40b4662021-05-13 15:47:03 +0200171 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100172 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200173 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200174 assert_string_equal("0.017", jsonctx->value);
175 assert_int_equal(5, jsonctx->value_len);
176 assert_int_equal(1, jsonctx->dynamic);
177 lyjson_ctx_free(jsonctx);
178
179 str = "-17E-3";
180 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100181 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200182 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
183 assert_string_equal("-0.017", jsonctx->value);
184 assert_int_equal(6, jsonctx->value_len);
185 assert_int_equal(1, jsonctx->dynamic);
186 lyjson_ctx_free(jsonctx);
187
188 str = "21000E-2";
189 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100190 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200191 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
192 assert_string_equal("210", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200193 assert_int_equal(3, jsonctx->value_len);
194 assert_int_equal(1, jsonctx->dynamic);
195 lyjson_ctx_free(jsonctx);
196
aPiecek76034c32021-06-08 15:03:11 +0200197 str = "21000E-4";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200198 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100199 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200200 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200201 assert_string_equal("2.1", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200202 assert_int_equal(3, jsonctx->value_len);
203 assert_int_equal(1, jsonctx->dynamic);
204 lyjson_ctx_free(jsonctx);
205
aPiecek76034c32021-06-08 15:03:11 +0200206 str = "21000E-7";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200207 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100208 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200209 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200210 assert_string_equal("0.0021", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200211 assert_int_equal(6, jsonctx->value_len);
212 assert_int_equal(1, jsonctx->dynamic);
213 lyjson_ctx_free(jsonctx);
214
aPiecek76034c32021-06-08 15:03:11 +0200215 /* decimal number, positive exponent */
216 str = "5.087E1";
aPieceka40b4662021-05-13 15:47:03 +0200217 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100218 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200219 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200220 assert_string_equal("50.87", jsonctx->value);
221 assert_int_equal(5, jsonctx->value_len);
aPieceka40b4662021-05-13 15:47:03 +0200222 assert_int_equal(1, jsonctx->dynamic);
223 lyjson_ctx_free(jsonctx);
224
aPiecek76034c32021-06-08 15:03:11 +0200225 str = "-5.087E1";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200226 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100227 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200228 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200229 assert_string_equal("-50.87", jsonctx->value);
230 assert_int_equal(6, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200231 assert_int_equal(1, jsonctx->dynamic);
232 lyjson_ctx_free(jsonctx);
233
aPiecek76034c32021-06-08 15:03:11 +0200234 str = "5.087E5";
aPiecek58e46a52021-05-05 10:04:47 +0200235 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100236 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek58e46a52021-05-05 10:04:47 +0200237 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200238 assert_string_equal("508700", jsonctx->value);
239 assert_int_equal(6, jsonctx->value_len);
240 assert_int_equal(1, jsonctx->dynamic);
241 lyjson_ctx_free(jsonctx);
242
243 str = "59.1e+1";
244 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100245 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200246 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
247 assert_string_equal("591", jsonctx->value);
248 assert_int_equal(3, jsonctx->value_len);
249 assert_int_equal(1, jsonctx->dynamic);
250 lyjson_ctx_free(jsonctx);
251
252 str = "0.005087E1";
253 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100254 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200255 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
256 assert_string_equal("0.05087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200257 assert_int_equal(7, jsonctx->value_len);
258 assert_int_equal(1, jsonctx->dynamic);
259 lyjson_ctx_free(jsonctx);
260
aPiecek76034c32021-06-08 15:03:11 +0200261 str = "0.005087E2";
aPiecek58e46a52021-05-05 10:04:47 +0200262 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek58e46a52021-05-05 10:04:47 +0200264 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200265 assert_string_equal("0.5087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200266 assert_int_equal(6, jsonctx->value_len);
267 assert_int_equal(1, jsonctx->dynamic);
268 lyjson_ctx_free(jsonctx);
269
aPiecek76034c32021-06-08 15:03:11 +0200270 str = "0.005087E6";
aPieceka40b4662021-05-13 15:47:03 +0200271 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100272 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200273 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200274 assert_string_equal("5087", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200275 assert_int_equal(4, jsonctx->value_len);
276 assert_int_equal(1, jsonctx->dynamic);
277 lyjson_ctx_free(jsonctx);
278
aPiecek76034c32021-06-08 15:03:11 +0200279 str = "0.05087E6";
280 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100281 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200282 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
283 assert_string_equal("50870", jsonctx->value);
284 assert_int_equal(5, jsonctx->value_len);
285 assert_int_equal(1, jsonctx->dynamic);
286 lyjson_ctx_free(jsonctx);
287
288 str = "0.005087E8";
289 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100290 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200291 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
292 assert_string_equal("508700", jsonctx->value);
293 assert_int_equal(6, jsonctx->value_len);
294 assert_int_equal(1, jsonctx->dynamic);
295 lyjson_ctx_free(jsonctx);
296
297 /* decimal number, negative exponent */
298 str = "35.94e-1";
299 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100300 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200301 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
302 assert_string_equal("3.594", jsonctx->value);
303 assert_int_equal(5, jsonctx->value_len);
304 assert_int_equal(1, jsonctx->dynamic);
305 lyjson_ctx_free(jsonctx);
306
307 str = "-35.94e-1";
308 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100309 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200310 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
311 assert_string_equal("-3.594", jsonctx->value);
312 assert_int_equal(6, jsonctx->value_len);
313 assert_int_equal(1, jsonctx->dynamic);
314 lyjson_ctx_free(jsonctx);
315
316 str = "35.94e-2";
317 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100318 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200319 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
320 assert_string_equal("0.3594", jsonctx->value);
321 assert_int_equal(6, jsonctx->value_len);
322 assert_int_equal(1, jsonctx->dynamic);
323 lyjson_ctx_free(jsonctx);
324
325 str = "35.94e-3";
326 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100327 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200328 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
329 assert_string_equal("0.03594", jsonctx->value);
330 assert_int_equal(7, jsonctx->value_len);
331 assert_int_equal(1, jsonctx->dynamic);
332 lyjson_ctx_free(jsonctx);
333
334 str = "0.3594e-1";
335 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100336 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200337 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
338 assert_string_equal("0.03594", jsonctx->value);
339 assert_int_equal(7, jsonctx->value_len);
340 assert_int_equal(1, jsonctx->dynamic);
341 lyjson_ctx_free(jsonctx);
342
343 str = "0.03594e-1";
344 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100345 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200346 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
347 assert_string_equal("0.003594", jsonctx->value);
348 assert_int_equal(8, jsonctx->value_len);
349 assert_int_equal(1, jsonctx->dynamic);
350 lyjson_ctx_free(jsonctx);
351
352 str = "0.003594e-1";
353 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100354 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200355 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
356 assert_string_equal("0.0003594", jsonctx->value);
357 assert_int_equal(9, jsonctx->value_len);
358 assert_int_equal(1, jsonctx->dynamic);
359 lyjson_ctx_free(jsonctx);
360
361 str = "0.3594e-2";
362 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100363 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200364 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
365 assert_string_equal("0.003594", jsonctx->value);
366 assert_int_equal(8, jsonctx->value_len);
367 assert_int_equal(1, jsonctx->dynamic);
368 lyjson_ctx_free(jsonctx);
369
370 str = "0.03594e-2";
371 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100372 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200373 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
374 assert_string_equal("0.0003594", jsonctx->value);
375 assert_int_equal(9, jsonctx->value_len);
376 assert_int_equal(1, jsonctx->dynamic);
377 lyjson_ctx_free(jsonctx);
378
379 str = "0.003594e-2";
380 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100381 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200382 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
383 assert_string_equal("0.00003594", jsonctx->value);
384 assert_int_equal(10, jsonctx->value_len);
385 assert_int_equal(1, jsonctx->dynamic);
386 lyjson_ctx_free(jsonctx);
387
388 /* zero */
389 str = "0";
390 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100391 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200392 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
393 assert_true(jsonctx->value[0] == '0');
394 assert_int_equal(1, jsonctx->value_len);
395 assert_int_equal(0, jsonctx->dynamic);
396 lyjson_ctx_free(jsonctx);
397
398 str = "-0";
399 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100400 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200401 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
402 assert_true(jsonctx->value[0] == '-');
403 assert_true(jsonctx->value[1] == '0');
404 assert_int_equal(2, jsonctx->value_len);
405 assert_int_equal(0, jsonctx->dynamic);
406 lyjson_ctx_free(jsonctx);
407
408 str = "94E0";
409 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100410 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200411 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
412 assert_true(jsonctx->value[0] == '9');
413 assert_true(jsonctx->value[1] == '4');
414 assert_int_equal(2, jsonctx->value_len);
415 assert_int_equal(0, jsonctx->dynamic);
416 lyjson_ctx_free(jsonctx);
417
418 str = "0E2";
419 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100420 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200421 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
422 assert_true(jsonctx->value[0] == '0');
423 assert_int_equal(1, jsonctx->value_len);
424 assert_int_equal(0, jsonctx->dynamic);
425 lyjson_ctx_free(jsonctx);
426
427 str = "-0E2";
428 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100429 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200430 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
431 assert_true(jsonctx->value[0] == '-');
432 assert_true(jsonctx->value[1] == '0');
433 assert_int_equal(2, jsonctx->value_len);
434 assert_int_equal(0, jsonctx->dynamic);
435 lyjson_ctx_free(jsonctx);
436
437 str = "5.320e+2";
438 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100439 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200440 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
441 assert_string_equal("532", jsonctx->value);
442 assert_int_equal(3, jsonctx->value_len);
443 assert_int_equal(1, jsonctx->dynamic);
444 lyjson_ctx_free(jsonctx);
445
446 str = "5.320e-1";
447 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100448 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200449 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
450 assert_string_equal("0.532", jsonctx->value);
451 assert_int_equal(5, jsonctx->value_len);
452 assert_int_equal(1, jsonctx->dynamic);
453 lyjson_ctx_free(jsonctx);
454
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200455 /* various invalid inputs */
456 str = "-x";
457 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100458 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200459 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200460
461 str = " -";
462 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100463 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200465
466 str = "--1";
467 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100468 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200469 CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200470
471 str = "+1";
472 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100473 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200474 CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200475
476 str = " 1.x ";
477 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100478 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200479 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200480
481 str = "1.";
482 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100483 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200484 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200485
486 str = " 1eo ";
487 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200490
491 str = "1e";
492 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100493 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200494 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200495
aPiecek76034c32021-06-08 15:03:11 +0200496 str = "1E1000";
497 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100498 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200499 CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", "Line number 1.");
500
501 str = "1e9999999999999999999";
502 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100503 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200504 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", "Line number 1.");
505
aPiecek0ba088e2021-06-15 12:53:17 +0200506 str = "1.1e66000";
507 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100508 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek0ba088e2021-06-15 12:53:17 +0200509 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", "Line number 1.");
510
511 str = "1.1e-66000";
512 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100513 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek0ba088e2021-06-15 12:53:17 +0200514 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", "Line number 1.");
515
aPiecek28e101a2021-06-10 09:09:31 +0200516 str = "-2.1e0.";
517 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100518 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek28e101a2021-06-10 09:09:31 +0200519 CHECK_LOG_CTX("Unexpected character \".\" after JSON number.", "Line number 1.");
520
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200521 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200522}
523
Radek Iša447abb82021-03-04 14:08:56 +0100524/* now string is tested in file ./tests/utests/types/string.c */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200525static void
526test_string(void **state)
527{
528 struct lyjson_ctx *jsonctx;
Radek Iša447abb82021-03-04 14:08:56 +0100529 struct ly_in *in = NULL;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200530 const char *str;
531
Radek Iša447abb82021-03-04 14:08:56 +0100532 str = "";
533 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
534
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200535 /* unterminated string */
536 str = "\"unterminated string";
537 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100538 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200539 CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1.");
Michal Vasko62af3692023-02-09 14:00:09 +0100540 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200541
542 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200543}
544
545static void
546test_object(void **state)
547{
548 struct lyjson_ctx *jsonctx;
549 struct ly_in *in;
550 const char *str;
551
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200552 /* empty */
553 str = " { } ";
554 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100555 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200556 assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0));
557 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
558 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
559 lyjson_ctx_free(jsonctx);
560
561 /* simple value */
562 str = "{\"name\" : \"Radek\"}";
563 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100564 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200565 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
566 assert_ptr_equal(&str[2], jsonctx->value);
567 assert_int_equal(4, jsonctx->value_len);
568 assert_int_equal(0, jsonctx->dynamic);
569 assert_string_equal("\"Radek\"}", jsonctx->in->current);
570 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
571 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
572 assert_string_equal("Radek\"}", jsonctx->value);
573 assert_int_equal(5, jsonctx->value_len);
574 assert_int_equal(0, jsonctx->dynamic);
575 assert_string_equal("}", jsonctx->in->current);
576 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
577 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
578 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
579 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
580 lyjson_ctx_free(jsonctx);
581
582 /* two values */
583 str = "{\"smart\" : true,\"handsom\":false}";
584 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100585 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200586 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
587 assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value);
588 assert_int_equal(5, jsonctx->value_len);
589 assert_int_equal(0, jsonctx->dynamic);
590 assert_string_equal("true,\"handsom\":false}", jsonctx->in->current);
591 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
592 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
593 assert_string_equal(",\"handsom\":false}", jsonctx->in->current);
594 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
595 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
596 assert_string_equal("handsom\":false}", jsonctx->value);
597 assert_int_equal(7, jsonctx->value_len);
598 assert_int_equal(0, jsonctx->dynamic);
599 assert_string_equal("false}", jsonctx->in->current);
600 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
601 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
602 assert_string_equal("}", jsonctx->in->current);
603 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
604 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
605 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
606 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
607 lyjson_ctx_free(jsonctx);
608
609 /* inherited objects */
610 str = "{\"person\" : {\"name\":\"Radek\"}}";
611 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100612 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200613 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
614 assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value);
615 assert_int_equal(6, jsonctx->value_len);
616 assert_int_equal(0, jsonctx->dynamic);
617 assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current);
618 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
619 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
620 assert_string_equal("name\":\"Radek\"}}", jsonctx->value);
621 assert_int_equal(4, jsonctx->value_len);
622 assert_int_equal(0, jsonctx->dynamic);
623 assert_string_equal("\"Radek\"}}", jsonctx->in->current);
624 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
625 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
626 assert_string_equal("Radek\"}}", jsonctx->value);
627 assert_int_equal(5, jsonctx->value_len);
628 assert_int_equal(0, jsonctx->dynamic);
629 assert_string_equal("}}", jsonctx->in->current);
630 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
631 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
632 assert_string_equal("}", jsonctx->in->current);
633 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
634 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
635 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
636 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
637 lyjson_ctx_free(jsonctx);
638
639 /* new line is allowed only as escaped character in JSON */
640 str = "{ unquoted : \"data\"}";
641 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100642 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200643 CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200644
645 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200646}
647
648static void
649test_array(void **state)
650{
651 struct lyjson_ctx *jsonctx;
652 struct ly_in *in;
653 const char *str;
654
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200655 /* empty */
656 str = " [ ] ";
657 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100658 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200659 assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0));
660 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
661 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
662 lyjson_ctx_free(jsonctx);
663
664 /* simple value */
665 str = "[ null]";
666 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100667 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200668 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
669 assert_null(jsonctx->value);
670 assert_int_equal(0, jsonctx->value_len);
671 assert_int_equal(0, jsonctx->dynamic);
672 assert_string_equal("null]", jsonctx->in->current);
673 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
674 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
675 assert_string_equal("]", jsonctx->in->current);
676 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
677 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
678 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
679 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
680 lyjson_ctx_free(jsonctx);
681
682 /* two values */
683 str = "[{\"a\":null},\"x\"]";
684 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100685 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200686 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
687 assert_null(jsonctx->value);
688 assert_int_equal(0, jsonctx->value_len);
689 assert_int_equal(0, jsonctx->dynamic);
690 assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current);
691 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
692 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
693 assert_string_equal("a\":null},\"x\"]", jsonctx->value);
694 assert_int_equal(1, jsonctx->value_len);
695 assert_int_equal(0, jsonctx->dynamic);
696 assert_string_equal("null},\"x\"]", jsonctx->in->current);
697 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
698 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
699 assert_string_equal("},\"x\"]", jsonctx->in->current);
700 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
701 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
702 assert_string_equal(",\"x\"]", jsonctx->in->current);
703 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
704 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
705 assert_string_equal("x\"]", jsonctx->value);
706 assert_int_equal(1, jsonctx->value_len);
707 assert_int_equal(0, jsonctx->dynamic);
708 assert_string_equal("]", jsonctx->in->current);
709 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
710 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
711 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
712 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
713 lyjson_ctx_free(jsonctx);
714
715 /* new line is allowed only as escaped character in JSON */
716 str = "[ , null]";
717 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100718 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200719 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200720 CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200721 lyjson_ctx_free(jsonctx);
722
723 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200724}
725
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100726int
727main(void)
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200728{
729 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200730 UTEST(test_general),
731 UTEST(test_number),
732 UTEST(test_string),
733 UTEST(test_object),
734 UTEST(test_array),
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200735 };
736
Radek Iša56ca9e42020-09-08 18:42:00 +0200737 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200738}