Michal Vasko | f20ad11 | 2023-02-10 15:12:12 +0100 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_json.c |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 3 | * @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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 17 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 18 | #include "in_internal.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 19 | #include "json.h" |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 20 | static void |
| 21 | test_general(void **state) |
| 22 | { |
| 23 | struct lyjson_ctx *jsonctx; |
| 24 | struct ly_in *in; |
| 25 | const char *str; |
| 26 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 27 | /* empty */ |
| 28 | str = ""; |
| 29 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 30 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 31 | CHECK_LOG_CTX("Empty JSON file.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 32 | |
| 33 | str = " \n\t \n"; |
| 34 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 35 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 36 | CHECK_LOG_CTX("Empty JSON file.", "Line number 3."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 37 | |
| 38 | /* constant values */ |
| 39 | str = "true"; |
| 40 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 41 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 42 | assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx)); |
| 43 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 44 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 45 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 46 | lyjson_ctx_free(jsonctx); |
| 47 | |
| 48 | str = "false"; |
| 49 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 50 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 51 | assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx)); |
| 52 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 53 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 54 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 55 | lyjson_ctx_free(jsonctx); |
| 56 | |
| 57 | str = "null"; |
| 58 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 59 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 60 | assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx)); |
| 61 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 62 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 63 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 64 | lyjson_ctx_free(jsonctx); |
| 65 | |
| 66 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void |
| 70 | test_number(void **state) |
| 71 | { |
| 72 | struct lyjson_ctx *jsonctx; |
| 73 | struct ly_in *in; |
| 74 | const char *str; |
| 75 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 76 | /* simple value */ |
| 77 | str = "11"; |
| 78 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 79 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 80 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 81 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 89 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 90 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 91 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 99 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 100 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 101 | 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 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 106 | /* integer, positive exponent */ |
| 107 | str = "550E3"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 108 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 109 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 110 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 111 | assert_string_equal("550000", jsonctx->value); |
| 112 | assert_int_equal(6, jsonctx->value_len); |
| 113 | assert_int_equal(1, jsonctx->dynamic); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 114 | lyjson_ctx_free(jsonctx); |
| 115 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 116 | str = "-550E3"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 117 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 118 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 119 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 120 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 128 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 129 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 130 | assert_string_equal("0.1", jsonctx->value); |
| 131 | assert_int_equal(3, jsonctx->value_len); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 132 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 137 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 138 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 139 | 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 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 144 | str = "-15E-1"; |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 145 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 146 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 147 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 148 | assert_string_equal("-1.5", jsonctx->value); |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 149 | assert_int_equal(4, jsonctx->value_len); |
| 150 | assert_int_equal(1, jsonctx->dynamic); |
| 151 | lyjson_ctx_free(jsonctx); |
| 152 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 153 | str = "16E-2"; |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 154 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 155 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 156 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 157 | assert_string_equal("0.16", jsonctx->value); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 158 | assert_int_equal(4, jsonctx->value_len); |
| 159 | assert_int_equal(1, jsonctx->dynamic); |
| 160 | lyjson_ctx_free(jsonctx); |
| 161 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 162 | str = "-16E-2"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 163 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 164 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 165 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 166 | assert_string_equal("-0.16", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 167 | assert_int_equal(5, jsonctx->value_len); |
| 168 | assert_int_equal(1, jsonctx->dynamic); |
| 169 | lyjson_ctx_free(jsonctx); |
| 170 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 171 | str = "17E-3"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 172 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 173 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 174 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 175 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 182 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 183 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 184 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 191 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 192 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 193 | assert_string_equal("210", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 194 | assert_int_equal(3, jsonctx->value_len); |
| 195 | assert_int_equal(1, jsonctx->dynamic); |
| 196 | lyjson_ctx_free(jsonctx); |
| 197 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 198 | str = "21000E-4"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 199 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 200 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 201 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 202 | assert_string_equal("2.1", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 203 | assert_int_equal(3, jsonctx->value_len); |
| 204 | assert_int_equal(1, jsonctx->dynamic); |
| 205 | lyjson_ctx_free(jsonctx); |
| 206 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 207 | str = "21000E-7"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 208 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 209 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 210 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 211 | assert_string_equal("0.0021", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 212 | assert_int_equal(6, jsonctx->value_len); |
| 213 | assert_int_equal(1, jsonctx->dynamic); |
| 214 | lyjson_ctx_free(jsonctx); |
| 215 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 216 | /* decimal number, positive exponent */ |
| 217 | str = "5.087E1"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 218 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 219 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 220 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 221 | assert_string_equal("50.87", jsonctx->value); |
| 222 | assert_int_equal(5, jsonctx->value_len); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 223 | assert_int_equal(1, jsonctx->dynamic); |
| 224 | lyjson_ctx_free(jsonctx); |
| 225 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 226 | str = "-5.087E1"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 227 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 228 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 229 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 230 | assert_string_equal("-50.87", jsonctx->value); |
| 231 | assert_int_equal(6, jsonctx->value_len); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 232 | assert_int_equal(1, jsonctx->dynamic); |
| 233 | lyjson_ctx_free(jsonctx); |
| 234 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 235 | str = "5.087E5"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 236 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 237 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 238 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 239 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 246 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 247 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 248 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 255 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 256 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 257 | assert_string_equal("0.05087", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 258 | assert_int_equal(7, jsonctx->value_len); |
| 259 | assert_int_equal(1, jsonctx->dynamic); |
| 260 | lyjson_ctx_free(jsonctx); |
| 261 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 262 | str = "0.005087E2"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 263 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 264 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 265 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 266 | assert_string_equal("0.5087", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 267 | assert_int_equal(6, jsonctx->value_len); |
| 268 | assert_int_equal(1, jsonctx->dynamic); |
| 269 | lyjson_ctx_free(jsonctx); |
| 270 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 271 | str = "0.005087E6"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 272 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 273 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 274 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 275 | assert_string_equal("5087", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 276 | assert_int_equal(4, jsonctx->value_len); |
| 277 | assert_int_equal(1, jsonctx->dynamic); |
| 278 | lyjson_ctx_free(jsonctx); |
| 279 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 280 | str = "0.05087E6"; |
| 281 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 282 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 283 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 284 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 291 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 292 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 293 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 301 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 302 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 303 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 310 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 311 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 312 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 319 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 320 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 321 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 328 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 329 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 330 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 337 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 338 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 339 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 346 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 347 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 348 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 355 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 356 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 357 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 364 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 365 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 366 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 373 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 374 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 375 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 382 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 383 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 384 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 392 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 393 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 394 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 401 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 402 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 403 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 411 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 412 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 413 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 421 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 422 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 423 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 430 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 431 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 432 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 440 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 441 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 442 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 449 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 450 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 451 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 456 | /* various invalid inputs */ |
| 457 | str = "-x"; |
| 458 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 459 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 460 | CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 461 | |
| 462 | str = " -"; |
| 463 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 464 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 465 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 466 | |
| 467 | str = "--1"; |
| 468 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 469 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 470 | CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 471 | |
| 472 | str = "+1"; |
| 473 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 474 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 475 | CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 476 | |
| 477 | str = " 1.x "; |
| 478 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 479 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 480 | CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 481 | |
| 482 | str = "1."; |
| 483 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 484 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 485 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 486 | |
| 487 | str = " 1eo "; |
| 488 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 489 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 490 | CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 491 | |
| 492 | str = "1e"; |
| 493 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 494 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 495 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 496 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 497 | str = "1E1000"; |
| 498 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 499 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 500 | CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", "Line number 1."); |
| 501 | |
| 502 | str = "1e9999999999999999999"; |
| 503 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 504 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 505 | CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", "Line number 1."); |
| 506 | |
aPiecek | 0ba088e | 2021-06-15 12:53:17 +0200 | [diff] [blame] | 507 | str = "1.1e66000"; |
| 508 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 509 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
aPiecek | 0ba088e | 2021-06-15 12:53:17 +0200 | [diff] [blame] | 510 | CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", "Line number 1."); |
| 511 | |
| 512 | str = "1.1e-66000"; |
| 513 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 514 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
aPiecek | 0ba088e | 2021-06-15 12:53:17 +0200 | [diff] [blame] | 515 | CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", "Line number 1."); |
| 516 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 517 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 520 | /* now string is tested in file ./tests/utests/types/string.c */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 521 | static void |
| 522 | test_string(void **state) |
| 523 | { |
| 524 | struct lyjson_ctx *jsonctx; |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 525 | struct ly_in *in = NULL; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 526 | const char *str; |
| 527 | |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 528 | str = ""; |
| 529 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
| 530 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 531 | /* unterminated string */ |
| 532 | str = "\"unterminated string"; |
| 533 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 534 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 535 | CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1."); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 536 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 537 | |
| 538 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static void |
| 542 | test_object(void **state) |
| 543 | { |
| 544 | struct lyjson_ctx *jsonctx; |
| 545 | struct ly_in *in; |
| 546 | const char *str; |
| 547 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 548 | /* empty */ |
| 549 | str = " { } "; |
| 550 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 551 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 552 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); |
| 553 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 554 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 555 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 559 | lyjson_ctx_free(jsonctx); |
| 560 | |
| 561 | /* simple value */ |
| 562 | str = "{\"name\" : \"Radek\"}"; |
| 563 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 564 | |
| 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 570 | assert_ptr_equal(&str[2], jsonctx->value); |
| 571 | assert_int_equal(4, jsonctx->value_len); |
| 572 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 573 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 574 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 575 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 576 | assert_string_equal("Radek\"}", jsonctx->value); |
| 577 | assert_int_equal(5, jsonctx->value_len); |
| 578 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 579 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 580 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 581 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); |
| 582 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 583 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 584 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 585 | lyjson_ctx_free(jsonctx); |
| 586 | |
| 587 | /* two values */ |
| 588 | str = "{\"smart\" : true,\"handsom\":false}"; |
| 589 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 590 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 595 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 598 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 599 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 600 | assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx)); |
| 601 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 602 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 603 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 607 | assert_string_equal("handsom\":false}", jsonctx->value); |
| 608 | assert_int_equal(7, jsonctx->value_len); |
| 609 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 610 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 611 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 612 | assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx)); |
| 613 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 614 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 615 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); |
| 616 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 617 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 618 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 619 | lyjson_ctx_free(jsonctx); |
| 620 | |
| 621 | /* inherited objects */ |
| 622 | str = "{\"person\" : {\"name\":\"Radek\"}}"; |
| 623 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 624 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 629 | assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value); |
| 630 | assert_int_equal(6, jsonctx->value_len); |
| 631 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 632 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 633 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 634 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 638 | assert_string_equal("name\":\"Radek\"}}", jsonctx->value); |
| 639 | assert_int_equal(4, jsonctx->value_len); |
| 640 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 641 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 642 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 643 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 644 | assert_string_equal("Radek\"}}", jsonctx->value); |
| 645 | assert_int_equal(5, jsonctx->value_len); |
| 646 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 647 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 648 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 649 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); |
| 650 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 651 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 652 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); |
| 653 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 654 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 655 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 656 | lyjson_ctx_free(jsonctx); |
| 657 | |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 658 | /* unquoted string */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 659 | str = "{ unquoted : \"data\"}"; |
| 660 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 661 | 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)); |
| 665 | CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object name.", "Line number 1."); |
| 666 | lyjson_ctx_free(jsonctx); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 667 | |
| 668 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static void |
| 672 | test_array(void **state) |
| 673 | { |
| 674 | struct lyjson_ctx *jsonctx; |
| 675 | struct ly_in *in; |
| 676 | const char *str; |
| 677 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 678 | /* empty */ |
| 679 | str = " [ ] "; |
| 680 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 681 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 682 | assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx)); |
| 683 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 684 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 685 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 689 | lyjson_ctx_free(jsonctx); |
| 690 | |
| 691 | /* simple value */ |
| 692 | str = "[ null]"; |
| 693 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 694 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 695 | assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 696 | assert_null(jsonctx->value); |
| 697 | assert_int_equal(0, jsonctx->value_len); |
| 698 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 699 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 700 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 701 | assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx)); |
| 702 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 703 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 704 | assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx)); |
| 705 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 706 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 707 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 708 | lyjson_ctx_free(jsonctx); |
| 709 | |
| 710 | /* two values */ |
| 711 | str = "[{\"a\":null},\"x\"]"; |
| 712 | assert_non_null(ly_in_memory(in, str)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 713 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 714 | assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 715 | assert_null(jsonctx->value); |
| 716 | assert_int_equal(0, jsonctx->value_len); |
| 717 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 718 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 719 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 720 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 724 | assert_string_equal("a\":null},\"x\"]", jsonctx->value); |
| 725 | assert_int_equal(1, jsonctx->value_len); |
| 726 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 727 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 728 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 729 | assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx)); |
| 730 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 731 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 732 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); |
| 733 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 734 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 735 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 739 | assert_string_equal("x\"]", jsonctx->value); |
| 740 | assert_int_equal(1, jsonctx->value_len); |
| 741 | assert_int_equal(0, jsonctx->dynamic); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 742 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 743 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 744 | assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx)); |
| 745 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 746 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
Michal Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 747 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 748 | 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 Vasko | 09e0463 | 2023-03-22 14:34:10 +0100 | [diff] [blame] | 753 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 754 | assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 755 | CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 756 | lyjson_ctx_free(jsonctx); |
| 757 | |
| 758 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 761 | int |
| 762 | main(void) |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 763 | { |
| 764 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 765 | UTEST(test_general), |
| 766 | UTEST(test_number), |
| 767 | UTEST(test_string), |
| 768 | UTEST(test_object), |
| 769 | UTEST(test_array), |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 770 | }; |
| 771 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 772 | return cmocka_run_group_tests(tests, NULL, NULL); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 773 | } |