blob: a760b382eca1fc551f5cd34791c9419c77c767e8 [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 Vasko09e04632023-03-22 14:34:10 +010030 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +010031 CHECK_LOG_CTX("Empty JSON file.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020032
33 str = " \n\t \n";
34 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +010035 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +010036 CHECK_LOG_CTX("Empty JSON file.", NULL, 3);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020037
38 /* constant values */
39 str = "true";
40 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +010041 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
42 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx));
43
Radek Krejci50f0c6b2020-06-18 16:31:48 +020044 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +010045 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020046 lyjson_ctx_free(jsonctx);
47
48 str = "false";
49 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +010050 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
51 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx));
52
Radek Krejci50f0c6b2020-06-18 16:31:48 +020053 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +010054 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020055 lyjson_ctx_free(jsonctx);
56
57 str = "null";
58 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +010059 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
60 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx));
61
Radek Krejci50f0c6b2020-06-18 16:31:48 +020062 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +010063 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020064 lyjson_ctx_free(jsonctx);
65
66 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020067}
68
69static void
70test_number(void **state)
71{
72 struct lyjson_ctx *jsonctx;
73 struct ly_in *in;
74 const char *str;
75
Radek Krejci50f0c6b2020-06-18 16:31:48 +020076 /* simple value */
77 str = "11";
78 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko09e04632023-03-22 14:34:10 +010079 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
80 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020081 assert_string_equal("11", jsonctx->value);
82 assert_int_equal(2, jsonctx->value_len);
83 assert_int_equal(0, jsonctx->dynamic);
84 lyjson_ctx_free(jsonctx);
85
86 /* fraction number */
87 str = "37.7668";
88 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +010089 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
90 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020091 assert_string_equal("37.7668", jsonctx->value);
92 assert_int_equal(7, jsonctx->value_len);
93 assert_int_equal(0, jsonctx->dynamic);
94 lyjson_ctx_free(jsonctx);
95
96 /* negative number */
97 str = "-122.3959";
98 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +010099 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
100 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200101 assert_string_equal("-122.3959", jsonctx->value);
102 assert_int_equal(9, jsonctx->value_len);
103 assert_int_equal(0, jsonctx->dynamic);
104 lyjson_ctx_free(jsonctx);
105
aPiecek76034c32021-06-08 15:03:11 +0200106 /* integer, positive exponent */
107 str = "550E3";
aPieceka40b4662021-05-13 15:47:03 +0200108 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100109 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
110 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200111 assert_string_equal("550000", jsonctx->value);
112 assert_int_equal(6, jsonctx->value_len);
113 assert_int_equal(1, jsonctx->dynamic);
aPieceka40b4662021-05-13 15:47:03 +0200114 lyjson_ctx_free(jsonctx);
115
aPiecek76034c32021-06-08 15:03:11 +0200116 str = "-550E3";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200117 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100118 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
119 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200120 assert_string_equal("-550000", jsonctx->value);
121 assert_int_equal(7, jsonctx->value_len);
122 assert_int_equal(1, jsonctx->dynamic);
123 lyjson_ctx_free(jsonctx);
124
125 /* integer, negative exponent */
126 str = "1E-1";
127 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100128 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
129 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200130 assert_string_equal("0.1", jsonctx->value);
131 assert_int_equal(3, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200132 assert_int_equal(1, jsonctx->dynamic);
133 lyjson_ctx_free(jsonctx);
134
135 str = "15E-1";
136 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100137 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
138 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200139 assert_string_equal("1.5", jsonctx->value);
140 assert_int_equal(3, jsonctx->value_len);
141 assert_int_equal(1, jsonctx->dynamic);
142 lyjson_ctx_free(jsonctx);
143
aPiecek76034c32021-06-08 15:03:11 +0200144 str = "-15E-1";
aPiecek350a6bf2021-05-25 07:59:10 +0200145 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100146 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
147 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200148 assert_string_equal("-1.5", jsonctx->value);
aPiecek350a6bf2021-05-25 07:59:10 +0200149 assert_int_equal(4, jsonctx->value_len);
150 assert_int_equal(1, jsonctx->dynamic);
151 lyjson_ctx_free(jsonctx);
152
aPiecek76034c32021-06-08 15:03:11 +0200153 str = "16E-2";
aPiecek350a6bf2021-05-25 07:59:10 +0200154 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100155 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
156 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200157 assert_string_equal("0.16", jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200158 assert_int_equal(4, jsonctx->value_len);
159 assert_int_equal(1, jsonctx->dynamic);
160 lyjson_ctx_free(jsonctx);
161
aPiecek76034c32021-06-08 15:03:11 +0200162 str = "-16E-2";
aPiecek58e46a52021-05-05 10:04:47 +0200163 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100164 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
165 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200166 assert_string_equal("-0.16", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200167 assert_int_equal(5, jsonctx->value_len);
168 assert_int_equal(1, jsonctx->dynamic);
169 lyjson_ctx_free(jsonctx);
170
aPiecek76034c32021-06-08 15:03:11 +0200171 str = "17E-3";
aPieceka40b4662021-05-13 15:47:03 +0200172 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100173 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
174 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200175 assert_string_equal("0.017", jsonctx->value);
176 assert_int_equal(5, jsonctx->value_len);
177 assert_int_equal(1, jsonctx->dynamic);
178 lyjson_ctx_free(jsonctx);
179
180 str = "-17E-3";
181 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100182 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
183 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200184 assert_string_equal("-0.017", jsonctx->value);
185 assert_int_equal(6, jsonctx->value_len);
186 assert_int_equal(1, jsonctx->dynamic);
187 lyjson_ctx_free(jsonctx);
188
189 str = "21000E-2";
190 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100191 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
192 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200193 assert_string_equal("210", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200194 assert_int_equal(3, jsonctx->value_len);
195 assert_int_equal(1, jsonctx->dynamic);
196 lyjson_ctx_free(jsonctx);
197
aPiecek76034c32021-06-08 15:03:11 +0200198 str = "21000E-4";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200199 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100200 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
201 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200202 assert_string_equal("2.1", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200203 assert_int_equal(3, jsonctx->value_len);
204 assert_int_equal(1, jsonctx->dynamic);
205 lyjson_ctx_free(jsonctx);
206
aPiecek76034c32021-06-08 15:03:11 +0200207 str = "21000E-7";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200208 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100209 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
210 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200211 assert_string_equal("0.0021", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200212 assert_int_equal(6, jsonctx->value_len);
213 assert_int_equal(1, jsonctx->dynamic);
214 lyjson_ctx_free(jsonctx);
215
aPiecek76034c32021-06-08 15:03:11 +0200216 /* decimal number, positive exponent */
217 str = "5.087E1";
aPieceka40b4662021-05-13 15:47:03 +0200218 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100219 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
220 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200221 assert_string_equal("50.87", jsonctx->value);
222 assert_int_equal(5, jsonctx->value_len);
aPieceka40b4662021-05-13 15:47:03 +0200223 assert_int_equal(1, jsonctx->dynamic);
224 lyjson_ctx_free(jsonctx);
225
aPiecek76034c32021-06-08 15:03:11 +0200226 str = "-5.087E1";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200227 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100228 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
229 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200230 assert_string_equal("-50.87", jsonctx->value);
231 assert_int_equal(6, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200232 assert_int_equal(1, jsonctx->dynamic);
233 lyjson_ctx_free(jsonctx);
234
aPiecek76034c32021-06-08 15:03:11 +0200235 str = "5.087E5";
aPiecek58e46a52021-05-05 10:04:47 +0200236 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100237 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
238 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200239 assert_string_equal("508700", jsonctx->value);
240 assert_int_equal(6, jsonctx->value_len);
241 assert_int_equal(1, jsonctx->dynamic);
242 lyjson_ctx_free(jsonctx);
243
244 str = "59.1e+1";
245 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100246 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
247 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200248 assert_string_equal("591", jsonctx->value);
249 assert_int_equal(3, jsonctx->value_len);
250 assert_int_equal(1, jsonctx->dynamic);
251 lyjson_ctx_free(jsonctx);
252
253 str = "0.005087E1";
254 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100255 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
256 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200257 assert_string_equal("0.05087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200258 assert_int_equal(7, jsonctx->value_len);
259 assert_int_equal(1, jsonctx->dynamic);
260 lyjson_ctx_free(jsonctx);
261
aPiecek76034c32021-06-08 15:03:11 +0200262 str = "0.005087E2";
aPiecek58e46a52021-05-05 10:04:47 +0200263 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100264 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
265 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200266 assert_string_equal("0.5087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200267 assert_int_equal(6, jsonctx->value_len);
268 assert_int_equal(1, jsonctx->dynamic);
269 lyjson_ctx_free(jsonctx);
270
aPiecek76034c32021-06-08 15:03:11 +0200271 str = "0.005087E6";
aPieceka40b4662021-05-13 15:47:03 +0200272 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100273 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
274 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200275 assert_string_equal("5087", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200276 assert_int_equal(4, jsonctx->value_len);
277 assert_int_equal(1, jsonctx->dynamic);
278 lyjson_ctx_free(jsonctx);
279
aPiecek76034c32021-06-08 15:03:11 +0200280 str = "0.05087E6";
281 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100282 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
283 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200284 assert_string_equal("50870", jsonctx->value);
285 assert_int_equal(5, jsonctx->value_len);
286 assert_int_equal(1, jsonctx->dynamic);
287 lyjson_ctx_free(jsonctx);
288
289 str = "0.005087E8";
290 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100291 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
292 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200293 assert_string_equal("508700", jsonctx->value);
294 assert_int_equal(6, jsonctx->value_len);
295 assert_int_equal(1, jsonctx->dynamic);
296 lyjson_ctx_free(jsonctx);
297
298 /* decimal number, negative exponent */
299 str = "35.94e-1";
300 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100301 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
302 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200303 assert_string_equal("3.594", jsonctx->value);
304 assert_int_equal(5, jsonctx->value_len);
305 assert_int_equal(1, jsonctx->dynamic);
306 lyjson_ctx_free(jsonctx);
307
308 str = "-35.94e-1";
309 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100310 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
311 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200312 assert_string_equal("-3.594", jsonctx->value);
313 assert_int_equal(6, jsonctx->value_len);
314 assert_int_equal(1, jsonctx->dynamic);
315 lyjson_ctx_free(jsonctx);
316
317 str = "35.94e-2";
318 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100319 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
320 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200321 assert_string_equal("0.3594", jsonctx->value);
322 assert_int_equal(6, jsonctx->value_len);
323 assert_int_equal(1, jsonctx->dynamic);
324 lyjson_ctx_free(jsonctx);
325
326 str = "35.94e-3";
327 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100328 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
329 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200330 assert_string_equal("0.03594", jsonctx->value);
331 assert_int_equal(7, jsonctx->value_len);
332 assert_int_equal(1, jsonctx->dynamic);
333 lyjson_ctx_free(jsonctx);
334
335 str = "0.3594e-1";
336 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100337 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
338 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200339 assert_string_equal("0.03594", jsonctx->value);
340 assert_int_equal(7, jsonctx->value_len);
341 assert_int_equal(1, jsonctx->dynamic);
342 lyjson_ctx_free(jsonctx);
343
344 str = "0.03594e-1";
345 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100346 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
347 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200348 assert_string_equal("0.003594", jsonctx->value);
349 assert_int_equal(8, jsonctx->value_len);
350 assert_int_equal(1, jsonctx->dynamic);
351 lyjson_ctx_free(jsonctx);
352
353 str = "0.003594e-1";
354 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100355 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
356 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200357 assert_string_equal("0.0003594", jsonctx->value);
358 assert_int_equal(9, jsonctx->value_len);
359 assert_int_equal(1, jsonctx->dynamic);
360 lyjson_ctx_free(jsonctx);
361
362 str = "0.3594e-2";
363 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100364 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
365 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200366 assert_string_equal("0.003594", jsonctx->value);
367 assert_int_equal(8, jsonctx->value_len);
368 assert_int_equal(1, jsonctx->dynamic);
369 lyjson_ctx_free(jsonctx);
370
371 str = "0.03594e-2";
372 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100373 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
374 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200375 assert_string_equal("0.0003594", jsonctx->value);
376 assert_int_equal(9, jsonctx->value_len);
377 assert_int_equal(1, jsonctx->dynamic);
378 lyjson_ctx_free(jsonctx);
379
380 str = "0.003594e-2";
381 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100382 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
383 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200384 assert_string_equal("0.00003594", jsonctx->value);
385 assert_int_equal(10, jsonctx->value_len);
386 assert_int_equal(1, jsonctx->dynamic);
387 lyjson_ctx_free(jsonctx);
388
389 /* zero */
390 str = "0";
391 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100392 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
393 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200394 assert_true(jsonctx->value[0] == '0');
395 assert_int_equal(1, jsonctx->value_len);
396 assert_int_equal(0, jsonctx->dynamic);
397 lyjson_ctx_free(jsonctx);
398
399 str = "-0";
400 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100401 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
402 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200403 assert_true(jsonctx->value[0] == '-');
404 assert_true(jsonctx->value[1] == '0');
405 assert_int_equal(2, jsonctx->value_len);
406 assert_int_equal(0, jsonctx->dynamic);
407 lyjson_ctx_free(jsonctx);
408
409 str = "94E0";
410 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100411 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
412 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200413 assert_true(jsonctx->value[0] == '9');
414 assert_true(jsonctx->value[1] == '4');
415 assert_int_equal(2, jsonctx->value_len);
416 assert_int_equal(0, jsonctx->dynamic);
417 lyjson_ctx_free(jsonctx);
418
419 str = "0E2";
420 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100421 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
422 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200423 assert_true(jsonctx->value[0] == '0');
424 assert_int_equal(1, jsonctx->value_len);
425 assert_int_equal(0, jsonctx->dynamic);
426 lyjson_ctx_free(jsonctx);
427
428 str = "-0E2";
429 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100430 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
431 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200432 assert_true(jsonctx->value[0] == '-');
433 assert_true(jsonctx->value[1] == '0');
434 assert_int_equal(2, jsonctx->value_len);
435 assert_int_equal(0, jsonctx->dynamic);
436 lyjson_ctx_free(jsonctx);
437
438 str = "5.320e+2";
439 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100440 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
441 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200442 assert_string_equal("532", jsonctx->value);
443 assert_int_equal(3, jsonctx->value_len);
444 assert_int_equal(1, jsonctx->dynamic);
445 lyjson_ctx_free(jsonctx);
446
447 str = "5.320e-1";
448 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100449 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
450 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200451 assert_string_equal("0.532", jsonctx->value);
452 assert_int_equal(5, jsonctx->value_len);
453 assert_int_equal(1, jsonctx->dynamic);
454 lyjson_ctx_free(jsonctx);
455
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200456 /* various invalid inputs */
457 str = "-x";
458 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100459 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100460 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200461
462 str = " -";
463 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100464 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100465 CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200466
467 str = "--1";
468 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100469 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100470 CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200471
472 str = "+1";
473 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100474 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100475 CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200476
477 str = " 1.x ";
478 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100479 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100480 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200481
482 str = "1.";
483 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100484 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100485 CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200486
487 str = " 1eo ";
488 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100489 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100490 CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200491
492 str = "1e";
493 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100494 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100495 CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200496
aPiecek76034c32021-06-08 15:03:11 +0200497 str = "1E1000";
498 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100499 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100500 CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", NULL, 1);
aPiecek76034c32021-06-08 15:03:11 +0200501
502 str = "1e9999999999999999999";
503 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100504 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100505 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", NULL, 1);
aPiecek76034c32021-06-08 15:03:11 +0200506
aPiecek0ba088e2021-06-15 12:53:17 +0200507 str = "1.1e66000";
508 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100509 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100510 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", NULL, 1);
aPiecek0ba088e2021-06-15 12:53:17 +0200511
512 str = "1.1e-66000";
513 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100514 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100515 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", NULL, 1);
aPiecek0ba088e2021-06-15 12:53:17 +0200516
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200517 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200518}
519
Radek Iša447abb82021-03-04 14:08:56 +0100520/* now string is tested in file ./tests/utests/types/string.c */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200521static void
522test_string(void **state)
523{
524 struct lyjson_ctx *jsonctx;
Radek Iša447abb82021-03-04 14:08:56 +0100525 struct ly_in *in = NULL;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200526 const char *str;
527
Radek Iša447abb82021-03-04 14:08:56 +0100528 str = "";
529 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
530
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200531 /* unterminated string */
532 str = "\"unterminated string";
533 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100534 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Michal Vasko7a266772024-01-23 11:02:38 +0100535 CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", NULL, 1);
536 CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200537
538 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200539}
540
541static void
542test_object(void **state)
543{
544 struct lyjson_ctx *jsonctx;
545 struct ly_in *in;
546 const char *str;
547
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200548 /* empty */
549 str = " { } ";
550 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko09e04632023-03-22 14:34:10 +0100551 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
552 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
553
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200554 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100555 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx));
556
557 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
558 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200559 lyjson_ctx_free(jsonctx);
560
561 /* simple value */
562 str = "{\"name\" : \"Radek\"}";
563 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100564
565 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
566 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
567
568 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
569 assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200570 assert_ptr_equal(&str[2], jsonctx->value);
571 assert_int_equal(4, jsonctx->value_len);
572 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100573
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200574 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100575 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200576 assert_string_equal("Radek\"}", jsonctx->value);
577 assert_int_equal(5, jsonctx->value_len);
578 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100579
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200580 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100581 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx));
582
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200583 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100584 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200585 lyjson_ctx_free(jsonctx);
586
587 /* two values */
588 str = "{\"smart\" : true,\"handsom\":false}";
589 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100590 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
591 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
592
593 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
594 assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200595 assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value);
596 assert_int_equal(5, jsonctx->value_len);
597 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100598
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200599 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100600 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx));
601
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200602 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100603 assert_int_equal(LYJSON_OBJECT_NEXT, lyjson_ctx_status(jsonctx));
604
605 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
606 assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200607 assert_string_equal("handsom\":false}", jsonctx->value);
608 assert_int_equal(7, jsonctx->value_len);
609 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100610
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200611 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100612 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx));
613
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200614 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100615 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx));
616
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200617 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100618 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200619 lyjson_ctx_free(jsonctx);
620
621 /* inherited objects */
622 str = "{\"person\" : {\"name\":\"Radek\"}}";
623 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100624 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
625 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
626
627 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
628 assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200629 assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value);
630 assert_int_equal(6, jsonctx->value_len);
631 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100632
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200633 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100634 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
635
636 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
637 assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200638 assert_string_equal("name\":\"Radek\"}}", jsonctx->value);
639 assert_int_equal(4, jsonctx->value_len);
640 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100641
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200642 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100643 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200644 assert_string_equal("Radek\"}}", jsonctx->value);
645 assert_int_equal(5, jsonctx->value_len);
646 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100647
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200648 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100649 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx));
650
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200651 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100652 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx));
653
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200654 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100655 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200656 lyjson_ctx_free(jsonctx);
657
Michal Vasko09e04632023-03-22 14:34:10 +0100658 /* unquoted string */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200659 str = "{ unquoted : \"data\"}";
660 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100661 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
662 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
663
664 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +0100665 CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object name.", NULL, 1);
Michal Vasko09e04632023-03-22 14:34:10 +0100666 lyjson_ctx_free(jsonctx);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200667
668 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200669}
670
671static void
672test_array(void **state)
673{
674 struct lyjson_ctx *jsonctx;
675 struct ly_in *in;
676 const char *str;
677
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200678 /* empty */
679 str = " [ ] ";
680 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko09e04632023-03-22 14:34:10 +0100681 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
682 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx));
683
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200684 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100685 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx));
686
687 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
688 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200689 lyjson_ctx_free(jsonctx);
690
691 /* simple value */
692 str = "[ null]";
693 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100694 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
695 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200696 assert_null(jsonctx->value);
697 assert_int_equal(0, jsonctx->value_len);
698 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100699
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200700 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100701 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx));
702
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200703 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100704 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx));
705
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200706 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100707 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200708 lyjson_ctx_free(jsonctx);
709
710 /* two values */
711 str = "[{\"a\":null},\"x\"]";
712 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100713 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
714 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200715 assert_null(jsonctx->value);
716 assert_int_equal(0, jsonctx->value_len);
717 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100718
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200719 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100720 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx));
721
722 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
723 assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200724 assert_string_equal("a\":null},\"x\"]", jsonctx->value);
725 assert_int_equal(1, jsonctx->value_len);
726 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100727
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200728 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100729 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx));
730
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200731 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100732 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx));
733
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200734 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100735 assert_int_equal(LYJSON_ARRAY_NEXT, lyjson_ctx_status(jsonctx));
736
737 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
738 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200739 assert_string_equal("x\"]", jsonctx->value);
740 assert_int_equal(1, jsonctx->value_len);
741 assert_int_equal(0, jsonctx->dynamic);
Michal Vasko09e04632023-03-22 14:34:10 +0100742
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200743 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100744 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx));
745
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200746 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100747 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200748 lyjson_ctx_free(jsonctx);
749
750 /* new line is allowed only as escaped character in JSON */
751 str = "[ , null]";
752 assert_non_null(ly_in_memory(in, str));
Michal Vasko09e04632023-03-22 14:34:10 +0100753 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200754 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +0100755 CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", NULL, 1);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200756 lyjson_ctx_free(jsonctx);
757
758 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200759}
760
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100761int
762main(void)
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200763{
764 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200765 UTEST(test_general),
766 UTEST(test_number),
767 UTEST(test_string),
768 UTEST(test_object),
769 UTEST(test_array),
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200770 };
771
Radek Iša56ca9e42020-09-08 18:42:00 +0200772 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200773}