Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [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" |
| 20 | #include "utests.h" |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 21 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 22 | static void |
| 23 | test_general(void **state) |
| 24 | { |
| 25 | struct lyjson_ctx *jsonctx; |
| 26 | struct ly_in *in; |
| 27 | const char *str; |
| 28 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 29 | /* empty */ |
| 30 | str = ""; |
| 31 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 32 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 33 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 34 | lyjson_ctx_free(jsonctx); |
| 35 | |
| 36 | str = " \n\t \n"; |
| 37 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 38 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 39 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 40 | lyjson_ctx_free(jsonctx); |
| 41 | |
| 42 | /* constant values */ |
| 43 | str = "true"; |
| 44 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 45 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 46 | assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0)); |
| 47 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 48 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 49 | lyjson_ctx_free(jsonctx); |
| 50 | |
| 51 | str = "false"; |
| 52 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 53 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 54 | assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0)); |
| 55 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 56 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 57 | lyjson_ctx_free(jsonctx); |
| 58 | |
| 59 | str = "null"; |
| 60 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 61 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 62 | assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0)); |
| 63 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 64 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 65 | lyjson_ctx_free(jsonctx); |
| 66 | |
| 67 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void |
| 71 | test_number(void **state) |
| 72 | { |
| 73 | struct lyjson_ctx *jsonctx; |
| 74 | struct ly_in *in; |
| 75 | const char *str; |
| 76 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 77 | /* simple value */ |
| 78 | str = "11"; |
| 79 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 80 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 81 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 82 | assert_string_equal("11", jsonctx->value); |
| 83 | assert_int_equal(2, jsonctx->value_len); |
| 84 | assert_int_equal(0, jsonctx->dynamic); |
| 85 | lyjson_ctx_free(jsonctx); |
| 86 | |
| 87 | /* fraction number */ |
| 88 | str = "37.7668"; |
| 89 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 90 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 91 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 92 | assert_string_equal("37.7668", jsonctx->value); |
| 93 | assert_int_equal(7, jsonctx->value_len); |
| 94 | assert_int_equal(0, jsonctx->dynamic); |
| 95 | lyjson_ctx_free(jsonctx); |
| 96 | |
| 97 | /* negative number */ |
| 98 | str = "-122.3959"; |
| 99 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 100 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 101 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 102 | assert_string_equal("-122.3959", jsonctx->value); |
| 103 | assert_int_equal(9, jsonctx->value_len); |
| 104 | assert_int_equal(0, jsonctx->dynamic); |
| 105 | lyjson_ctx_free(jsonctx); |
| 106 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 107 | /* integer, positive exponent */ |
| 108 | str = "550E3"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 109 | assert_non_null(ly_in_memory(in, str)); |
| 110 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 111 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 112 | assert_string_equal("550000", jsonctx->value); |
| 113 | assert_int_equal(6, jsonctx->value_len); |
| 114 | assert_int_equal(1, jsonctx->dynamic); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 115 | lyjson_ctx_free(jsonctx); |
| 116 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 117 | str = "-550E3"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 118 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 119 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 120 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 121 | assert_string_equal("-550000", jsonctx->value); |
| 122 | assert_int_equal(7, jsonctx->value_len); |
| 123 | assert_int_equal(1, jsonctx->dynamic); |
| 124 | lyjson_ctx_free(jsonctx); |
| 125 | |
| 126 | /* integer, negative exponent */ |
| 127 | str = "1E-1"; |
| 128 | assert_non_null(ly_in_memory(in, str)); |
| 129 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 130 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 131 | assert_string_equal("0.1", jsonctx->value); |
| 132 | assert_int_equal(3, jsonctx->value_len); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 133 | assert_int_equal(1, jsonctx->dynamic); |
| 134 | lyjson_ctx_free(jsonctx); |
| 135 | |
| 136 | str = "15E-1"; |
| 137 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 139 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 140 | assert_string_equal("1.5", jsonctx->value); |
| 141 | assert_int_equal(3, jsonctx->value_len); |
| 142 | assert_int_equal(1, jsonctx->dynamic); |
| 143 | lyjson_ctx_free(jsonctx); |
| 144 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 145 | str = "-15E-1"; |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 146 | assert_non_null(ly_in_memory(in, str)); |
| 147 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 148 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 149 | assert_string_equal("-1.5", jsonctx->value); |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 150 | assert_int_equal(4, jsonctx->value_len); |
| 151 | assert_int_equal(1, jsonctx->dynamic); |
| 152 | lyjson_ctx_free(jsonctx); |
| 153 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 154 | str = "16E-2"; |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 155 | assert_non_null(ly_in_memory(in, str)); |
| 156 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 157 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 158 | assert_string_equal("0.16", jsonctx->value); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 159 | assert_int_equal(4, jsonctx->value_len); |
| 160 | assert_int_equal(1, jsonctx->dynamic); |
| 161 | lyjson_ctx_free(jsonctx); |
| 162 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 163 | str = "-16E-2"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 164 | assert_non_null(ly_in_memory(in, str)); |
| 165 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 166 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 167 | assert_string_equal("-0.16", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 168 | assert_int_equal(5, jsonctx->value_len); |
| 169 | assert_int_equal(1, jsonctx->dynamic); |
| 170 | lyjson_ctx_free(jsonctx); |
| 171 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 172 | str = "17E-3"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 173 | assert_non_null(ly_in_memory(in, str)); |
| 174 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 175 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 176 | assert_string_equal("0.017", jsonctx->value); |
| 177 | assert_int_equal(5, jsonctx->value_len); |
| 178 | assert_int_equal(1, jsonctx->dynamic); |
| 179 | lyjson_ctx_free(jsonctx); |
| 180 | |
| 181 | str = "-17E-3"; |
| 182 | assert_non_null(ly_in_memory(in, str)); |
| 183 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 184 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 185 | assert_string_equal("-0.017", jsonctx->value); |
| 186 | assert_int_equal(6, jsonctx->value_len); |
| 187 | assert_int_equal(1, jsonctx->dynamic); |
| 188 | lyjson_ctx_free(jsonctx); |
| 189 | |
| 190 | str = "21000E-2"; |
| 191 | assert_non_null(ly_in_memory(in, str)); |
| 192 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 193 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 194 | assert_string_equal("210", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 195 | assert_int_equal(3, jsonctx->value_len); |
| 196 | assert_int_equal(1, jsonctx->dynamic); |
| 197 | lyjson_ctx_free(jsonctx); |
| 198 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 199 | str = "21000E-4"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 200 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 202 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 203 | assert_string_equal("2.1", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 204 | assert_int_equal(3, jsonctx->value_len); |
| 205 | assert_int_equal(1, jsonctx->dynamic); |
| 206 | lyjson_ctx_free(jsonctx); |
| 207 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 208 | str = "21000E-7"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 209 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 210 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 211 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 212 | assert_string_equal("0.0021", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 213 | assert_int_equal(6, jsonctx->value_len); |
| 214 | assert_int_equal(1, jsonctx->dynamic); |
| 215 | lyjson_ctx_free(jsonctx); |
| 216 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 217 | /* decimal number, positive exponent */ |
| 218 | str = "5.087E1"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 219 | assert_non_null(ly_in_memory(in, str)); |
| 220 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 221 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 222 | assert_string_equal("50.87", jsonctx->value); |
| 223 | assert_int_equal(5, jsonctx->value_len); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 224 | assert_int_equal(1, jsonctx->dynamic); |
| 225 | lyjson_ctx_free(jsonctx); |
| 226 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 227 | str = "-5.087E1"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 228 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 229 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 230 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 231 | assert_string_equal("-50.87", jsonctx->value); |
| 232 | assert_int_equal(6, jsonctx->value_len); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 233 | assert_int_equal(1, jsonctx->dynamic); |
| 234 | lyjson_ctx_free(jsonctx); |
| 235 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 236 | str = "5.087E5"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 237 | assert_non_null(ly_in_memory(in, str)); |
| 238 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 239 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 240 | assert_string_equal("508700", jsonctx->value); |
| 241 | assert_int_equal(6, jsonctx->value_len); |
| 242 | assert_int_equal(1, jsonctx->dynamic); |
| 243 | lyjson_ctx_free(jsonctx); |
| 244 | |
| 245 | str = "59.1e+1"; |
| 246 | assert_non_null(ly_in_memory(in, str)); |
| 247 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 248 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 249 | assert_string_equal("591", jsonctx->value); |
| 250 | assert_int_equal(3, jsonctx->value_len); |
| 251 | assert_int_equal(1, jsonctx->dynamic); |
| 252 | lyjson_ctx_free(jsonctx); |
| 253 | |
| 254 | str = "0.005087E1"; |
| 255 | assert_non_null(ly_in_memory(in, str)); |
| 256 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 257 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 258 | assert_string_equal("0.05087", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 259 | assert_int_equal(7, jsonctx->value_len); |
| 260 | assert_int_equal(1, jsonctx->dynamic); |
| 261 | lyjson_ctx_free(jsonctx); |
| 262 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 263 | str = "0.005087E2"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 264 | assert_non_null(ly_in_memory(in, str)); |
| 265 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 266 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 267 | assert_string_equal("0.5087", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 268 | assert_int_equal(6, jsonctx->value_len); |
| 269 | assert_int_equal(1, jsonctx->dynamic); |
| 270 | lyjson_ctx_free(jsonctx); |
| 271 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 272 | str = "0.005087E6"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 273 | assert_non_null(ly_in_memory(in, str)); |
| 274 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 275 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 276 | assert_string_equal("5087", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 277 | assert_int_equal(4, jsonctx->value_len); |
| 278 | assert_int_equal(1, jsonctx->dynamic); |
| 279 | lyjson_ctx_free(jsonctx); |
| 280 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 281 | str = "0.05087E6"; |
| 282 | assert_non_null(ly_in_memory(in, str)); |
| 283 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 284 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 285 | assert_string_equal("50870", jsonctx->value); |
| 286 | assert_int_equal(5, jsonctx->value_len); |
| 287 | assert_int_equal(1, jsonctx->dynamic); |
| 288 | lyjson_ctx_free(jsonctx); |
| 289 | |
| 290 | str = "0.005087E8"; |
| 291 | assert_non_null(ly_in_memory(in, str)); |
| 292 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 293 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 294 | assert_string_equal("508700", jsonctx->value); |
| 295 | assert_int_equal(6, jsonctx->value_len); |
| 296 | assert_int_equal(1, jsonctx->dynamic); |
| 297 | lyjson_ctx_free(jsonctx); |
| 298 | |
| 299 | /* decimal number, negative exponent */ |
| 300 | str = "35.94e-1"; |
| 301 | assert_non_null(ly_in_memory(in, str)); |
| 302 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 303 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 304 | assert_string_equal("3.594", jsonctx->value); |
| 305 | assert_int_equal(5, jsonctx->value_len); |
| 306 | assert_int_equal(1, jsonctx->dynamic); |
| 307 | lyjson_ctx_free(jsonctx); |
| 308 | |
| 309 | str = "-35.94e-1"; |
| 310 | assert_non_null(ly_in_memory(in, str)); |
| 311 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 312 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 313 | assert_string_equal("-3.594", jsonctx->value); |
| 314 | assert_int_equal(6, jsonctx->value_len); |
| 315 | assert_int_equal(1, jsonctx->dynamic); |
| 316 | lyjson_ctx_free(jsonctx); |
| 317 | |
| 318 | str = "35.94e-2"; |
| 319 | assert_non_null(ly_in_memory(in, str)); |
| 320 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 321 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 322 | assert_string_equal("0.3594", jsonctx->value); |
| 323 | assert_int_equal(6, jsonctx->value_len); |
| 324 | assert_int_equal(1, jsonctx->dynamic); |
| 325 | lyjson_ctx_free(jsonctx); |
| 326 | |
| 327 | str = "35.94e-3"; |
| 328 | assert_non_null(ly_in_memory(in, str)); |
| 329 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 330 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 331 | assert_string_equal("0.03594", jsonctx->value); |
| 332 | assert_int_equal(7, jsonctx->value_len); |
| 333 | assert_int_equal(1, jsonctx->dynamic); |
| 334 | lyjson_ctx_free(jsonctx); |
| 335 | |
| 336 | str = "0.3594e-1"; |
| 337 | assert_non_null(ly_in_memory(in, str)); |
| 338 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 339 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 340 | assert_string_equal("0.03594", jsonctx->value); |
| 341 | assert_int_equal(7, jsonctx->value_len); |
| 342 | assert_int_equal(1, jsonctx->dynamic); |
| 343 | lyjson_ctx_free(jsonctx); |
| 344 | |
| 345 | str = "0.03594e-1"; |
| 346 | assert_non_null(ly_in_memory(in, str)); |
| 347 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 348 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 349 | assert_string_equal("0.003594", jsonctx->value); |
| 350 | assert_int_equal(8, jsonctx->value_len); |
| 351 | assert_int_equal(1, jsonctx->dynamic); |
| 352 | lyjson_ctx_free(jsonctx); |
| 353 | |
| 354 | str = "0.003594e-1"; |
| 355 | assert_non_null(ly_in_memory(in, str)); |
| 356 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 357 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 358 | assert_string_equal("0.0003594", jsonctx->value); |
| 359 | assert_int_equal(9, jsonctx->value_len); |
| 360 | assert_int_equal(1, jsonctx->dynamic); |
| 361 | lyjson_ctx_free(jsonctx); |
| 362 | |
| 363 | str = "0.3594e-2"; |
| 364 | assert_non_null(ly_in_memory(in, str)); |
| 365 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 366 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 367 | assert_string_equal("0.003594", jsonctx->value); |
| 368 | assert_int_equal(8, jsonctx->value_len); |
| 369 | assert_int_equal(1, jsonctx->dynamic); |
| 370 | lyjson_ctx_free(jsonctx); |
| 371 | |
| 372 | str = "0.03594e-2"; |
| 373 | assert_non_null(ly_in_memory(in, str)); |
| 374 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 375 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 376 | assert_string_equal("0.0003594", jsonctx->value); |
| 377 | assert_int_equal(9, jsonctx->value_len); |
| 378 | assert_int_equal(1, jsonctx->dynamic); |
| 379 | lyjson_ctx_free(jsonctx); |
| 380 | |
| 381 | str = "0.003594e-2"; |
| 382 | assert_non_null(ly_in_memory(in, str)); |
| 383 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 384 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 385 | assert_string_equal("0.00003594", jsonctx->value); |
| 386 | assert_int_equal(10, jsonctx->value_len); |
| 387 | assert_int_equal(1, jsonctx->dynamic); |
| 388 | lyjson_ctx_free(jsonctx); |
| 389 | |
| 390 | /* zero */ |
| 391 | str = "0"; |
| 392 | assert_non_null(ly_in_memory(in, str)); |
| 393 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 394 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 395 | assert_true(jsonctx->value[0] == '0'); |
| 396 | assert_int_equal(1, jsonctx->value_len); |
| 397 | assert_int_equal(0, jsonctx->dynamic); |
| 398 | lyjson_ctx_free(jsonctx); |
| 399 | |
| 400 | str = "-0"; |
| 401 | assert_non_null(ly_in_memory(in, str)); |
| 402 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 403 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 404 | assert_true(jsonctx->value[0] == '-'); |
| 405 | assert_true(jsonctx->value[1] == '0'); |
| 406 | assert_int_equal(2, jsonctx->value_len); |
| 407 | assert_int_equal(0, jsonctx->dynamic); |
| 408 | lyjson_ctx_free(jsonctx); |
| 409 | |
| 410 | str = "94E0"; |
| 411 | assert_non_null(ly_in_memory(in, str)); |
| 412 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 413 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 414 | assert_true(jsonctx->value[0] == '9'); |
| 415 | assert_true(jsonctx->value[1] == '4'); |
| 416 | assert_int_equal(2, jsonctx->value_len); |
| 417 | assert_int_equal(0, jsonctx->dynamic); |
| 418 | lyjson_ctx_free(jsonctx); |
| 419 | |
| 420 | str = "0E2"; |
| 421 | assert_non_null(ly_in_memory(in, str)); |
| 422 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 423 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 424 | assert_true(jsonctx->value[0] == '0'); |
| 425 | assert_int_equal(1, jsonctx->value_len); |
| 426 | assert_int_equal(0, jsonctx->dynamic); |
| 427 | lyjson_ctx_free(jsonctx); |
| 428 | |
| 429 | str = "-0E2"; |
| 430 | assert_non_null(ly_in_memory(in, str)); |
| 431 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 432 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 433 | assert_true(jsonctx->value[0] == '-'); |
| 434 | assert_true(jsonctx->value[1] == '0'); |
| 435 | assert_int_equal(2, jsonctx->value_len); |
| 436 | assert_int_equal(0, jsonctx->dynamic); |
| 437 | lyjson_ctx_free(jsonctx); |
| 438 | |
| 439 | str = "5.320e+2"; |
| 440 | assert_non_null(ly_in_memory(in, str)); |
| 441 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 442 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 443 | assert_string_equal("532", jsonctx->value); |
| 444 | assert_int_equal(3, jsonctx->value_len); |
| 445 | assert_int_equal(1, jsonctx->dynamic); |
| 446 | lyjson_ctx_free(jsonctx); |
| 447 | |
| 448 | str = "5.320e-1"; |
| 449 | assert_non_null(ly_in_memory(in, str)); |
| 450 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 451 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
| 452 | assert_string_equal("0.532", jsonctx->value); |
| 453 | assert_int_equal(5, jsonctx->value_len); |
| 454 | assert_int_equal(1, jsonctx->dynamic); |
| 455 | lyjson_ctx_free(jsonctx); |
| 456 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 457 | /* various invalid inputs */ |
| 458 | str = "-x"; |
| 459 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 460 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 461 | 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] | 462 | |
| 463 | str = " -"; |
| 464 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 465 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 466 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 467 | |
| 468 | str = "--1"; |
| 469 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 470 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 471 | CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 472 | |
| 473 | str = "+1"; |
| 474 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 475 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 476 | 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] | 477 | |
| 478 | str = " 1.x "; |
| 479 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 480 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 481 | 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] | 482 | |
| 483 | str = "1."; |
| 484 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 485 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 486 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 487 | |
| 488 | str = " 1eo "; |
| 489 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 490 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 491 | 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] | 492 | |
| 493 | str = "1e"; |
| 494 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 495 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 496 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 497 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 498 | str = "1E1000"; |
| 499 | assert_non_null(ly_in_memory(in, str)); |
| 500 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 501 | CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", "Line number 1."); |
| 502 | |
| 503 | str = "1e9999999999999999999"; |
| 504 | assert_non_null(ly_in_memory(in, str)); |
| 505 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 506 | CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", "Line number 1."); |
| 507 | |
aPiecek | 0ba088e | 2021-06-15 12:53:17 +0200 | [diff] [blame] | 508 | str = "1.1e66000"; |
| 509 | assert_non_null(ly_in_memory(in, str)); |
| 510 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 511 | CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", "Line number 1."); |
| 512 | |
| 513 | str = "1.1e-66000"; |
| 514 | assert_non_null(ly_in_memory(in, str)); |
| 515 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 516 | CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", "Line number 1."); |
| 517 | |
aPiecek | 28e101a | 2021-06-10 09:09:31 +0200 | [diff] [blame] | 518 | str = "-2.1e0."; |
| 519 | assert_non_null(ly_in_memory(in, str)); |
| 520 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 521 | CHECK_LOG_CTX("Unexpected character \".\" after JSON number.", "Line number 1."); |
| 522 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 523 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 526 | /* now string is tested in file ./tests/utests/types/string.c */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 527 | static void |
| 528 | test_string(void **state) |
| 529 | { |
| 530 | struct lyjson_ctx *jsonctx; |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 531 | struct ly_in *in = NULL; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 532 | const char *str; |
| 533 | |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 534 | str = ""; |
| 535 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
| 536 | |
| 537 | #if 0 |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 538 | /* simple string */ |
| 539 | str = "\"hello\""; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 540 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 541 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); |
| 542 | assert_ptr_equal(&str[1], jsonctx->value); |
| 543 | assert_int_equal(5, jsonctx->value_len); |
| 544 | assert_int_equal(0, jsonctx->dynamic); |
| 545 | lyjson_ctx_free(jsonctx); |
| 546 | |
| 547 | /* 4-byte utf8 character */ |
| 548 | str = "\"\\t𠜎\""; |
| 549 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 550 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 551 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); |
| 552 | assert_string_equal("\t𠜎", jsonctx->value); |
| 553 | assert_int_equal(5, jsonctx->value_len); |
| 554 | assert_int_equal(1, jsonctx->dynamic); |
| 555 | lyjson_ctx_free(jsonctx); |
| 556 | |
| 557 | /* valid escape sequences - note that here it mixes valid JSON string characters (RFC 7159, sec. 7) and |
| 558 | * valid characters in YANG string type (RFC 7950, sec. 9.4). Since the latter is a subset of JSON string, |
| 559 | * the YANG string type's restrictions apply to the JSON escape sequences */ |
| 560 | str = "\"\\\" \\\\ \\r \\/ \\n \\t \\u20ac\""; |
| 561 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 562 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 563 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); |
| 564 | assert_string_equal("\" \\ \r / \n \t €", jsonctx->value); |
| 565 | assert_int_equal(15, jsonctx->value_len); |
| 566 | assert_int_equal(1, jsonctx->dynamic); |
| 567 | lyjson_ctx_free(jsonctx); |
| 568 | |
| 569 | /* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */ |
| 570 | str = "\"\\b\""; |
| 571 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 572 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 573 | CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 574 | |
| 575 | str = "\"\\f\""; |
| 576 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 577 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 578 | CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", "Line number 1."); |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 579 | #endif |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 580 | |
| 581 | /* unterminated string */ |
| 582 | str = "\"unterminated string"; |
| 583 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 584 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 585 | CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1."); |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 586 | #if 0 |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 587 | /* invalid escape sequence */ |
| 588 | str = "\"char \\x \""; |
| 589 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 590 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 591 | CHECK_LOG_CTX("Invalid character escape sequence \\x.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 592 | |
| 593 | /* new line is allowed only as escaped character in JSON */ |
| 594 | str = "\"\n\""; |
| 595 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 596 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 597 | CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", "Line number 1."); |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 598 | #endif |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 599 | |
| 600 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static void |
| 604 | test_object(void **state) |
| 605 | { |
| 606 | struct lyjson_ctx *jsonctx; |
| 607 | struct ly_in *in; |
| 608 | const char *str; |
| 609 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 610 | /* empty */ |
| 611 | str = " { } "; |
| 612 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 613 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 614 | assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0)); |
| 615 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 616 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 617 | lyjson_ctx_free(jsonctx); |
| 618 | |
| 619 | /* simple value */ |
| 620 | str = "{\"name\" : \"Radek\"}"; |
| 621 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 622 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 623 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); |
| 624 | assert_ptr_equal(&str[2], jsonctx->value); |
| 625 | assert_int_equal(4, jsonctx->value_len); |
| 626 | assert_int_equal(0, jsonctx->dynamic); |
| 627 | assert_string_equal("\"Radek\"}", jsonctx->in->current); |
| 628 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 629 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); |
| 630 | assert_string_equal("Radek\"}", jsonctx->value); |
| 631 | assert_int_equal(5, jsonctx->value_len); |
| 632 | assert_int_equal(0, jsonctx->dynamic); |
| 633 | assert_string_equal("}", jsonctx->in->current); |
| 634 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 635 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 636 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 637 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 638 | lyjson_ctx_free(jsonctx); |
| 639 | |
| 640 | /* two values */ |
| 641 | str = "{\"smart\" : true,\"handsom\":false}"; |
| 642 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 643 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 644 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); |
| 645 | assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value); |
| 646 | assert_int_equal(5, jsonctx->value_len); |
| 647 | assert_int_equal(0, jsonctx->dynamic); |
| 648 | assert_string_equal("true,\"handsom\":false}", jsonctx->in->current); |
| 649 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 650 | assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0)); |
| 651 | assert_string_equal(",\"handsom\":false}", jsonctx->in->current); |
| 652 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 653 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); |
| 654 | assert_string_equal("handsom\":false}", jsonctx->value); |
| 655 | assert_int_equal(7, jsonctx->value_len); |
| 656 | assert_int_equal(0, jsonctx->dynamic); |
| 657 | assert_string_equal("false}", jsonctx->in->current); |
| 658 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 659 | assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0)); |
| 660 | assert_string_equal("}", jsonctx->in->current); |
| 661 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 662 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 663 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 664 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 665 | lyjson_ctx_free(jsonctx); |
| 666 | |
| 667 | /* inherited objects */ |
| 668 | str = "{\"person\" : {\"name\":\"Radek\"}}"; |
| 669 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 670 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 671 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); |
| 672 | assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value); |
| 673 | assert_int_equal(6, jsonctx->value_len); |
| 674 | assert_int_equal(0, jsonctx->dynamic); |
| 675 | assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current); |
| 676 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 677 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); |
| 678 | assert_string_equal("name\":\"Radek\"}}", jsonctx->value); |
| 679 | assert_int_equal(4, jsonctx->value_len); |
| 680 | assert_int_equal(0, jsonctx->dynamic); |
| 681 | assert_string_equal("\"Radek\"}}", jsonctx->in->current); |
| 682 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 683 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); |
| 684 | assert_string_equal("Radek\"}}", jsonctx->value); |
| 685 | assert_int_equal(5, jsonctx->value_len); |
| 686 | assert_int_equal(0, jsonctx->dynamic); |
| 687 | assert_string_equal("}}", jsonctx->in->current); |
| 688 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 689 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 690 | assert_string_equal("}", jsonctx->in->current); |
| 691 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 692 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 693 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 694 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 695 | lyjson_ctx_free(jsonctx); |
| 696 | |
| 697 | /* new line is allowed only as escaped character in JSON */ |
| 698 | str = "{ unquoted : \"data\"}"; |
| 699 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 700 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 701 | CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 702 | |
| 703 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static void |
| 707 | test_array(void **state) |
| 708 | { |
| 709 | struct lyjson_ctx *jsonctx; |
| 710 | struct ly_in *in; |
| 711 | const char *str; |
| 712 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 713 | /* empty */ |
| 714 | str = " [ ] "; |
| 715 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 716 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 717 | assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0)); |
| 718 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 719 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 720 | lyjson_ctx_free(jsonctx); |
| 721 | |
| 722 | /* simple value */ |
| 723 | str = "[ null]"; |
| 724 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 725 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 726 | assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0)); |
| 727 | assert_null(jsonctx->value); |
| 728 | assert_int_equal(0, jsonctx->value_len); |
| 729 | assert_int_equal(0, jsonctx->dynamic); |
| 730 | assert_string_equal("null]", jsonctx->in->current); |
| 731 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 732 | assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0)); |
| 733 | assert_string_equal("]", jsonctx->in->current); |
| 734 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 735 | assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 736 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 737 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 738 | lyjson_ctx_free(jsonctx); |
| 739 | |
| 740 | /* two values */ |
| 741 | str = "[{\"a\":null},\"x\"]"; |
| 742 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 743 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 744 | assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0)); |
| 745 | assert_null(jsonctx->value); |
| 746 | assert_int_equal(0, jsonctx->value_len); |
| 747 | assert_int_equal(0, jsonctx->dynamic); |
| 748 | assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current); |
| 749 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 750 | assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); |
| 751 | assert_string_equal("a\":null},\"x\"]", jsonctx->value); |
| 752 | assert_int_equal(1, jsonctx->value_len); |
| 753 | assert_int_equal(0, jsonctx->dynamic); |
| 754 | assert_string_equal("null},\"x\"]", jsonctx->in->current); |
| 755 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 756 | assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0)); |
| 757 | assert_string_equal("},\"x\"]", jsonctx->in->current); |
| 758 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 759 | assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 760 | assert_string_equal(",\"x\"]", jsonctx->in->current); |
| 761 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 762 | assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); |
| 763 | assert_string_equal("x\"]", jsonctx->value); |
| 764 | assert_int_equal(1, jsonctx->value_len); |
| 765 | assert_int_equal(0, jsonctx->dynamic); |
| 766 | assert_string_equal("]", jsonctx->in->current); |
| 767 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 768 | assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0)); |
| 769 | assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); |
| 770 | assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); |
| 771 | lyjson_ctx_free(jsonctx); |
| 772 | |
| 773 | /* new line is allowed only as escaped character in JSON */ |
| 774 | str = "[ , null]"; |
| 775 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 776 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 777 | assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 778 | 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] | 779 | lyjson_ctx_free(jsonctx); |
| 780 | |
| 781 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 782 | } |
| 783 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 784 | int |
| 785 | main(void) |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 786 | { |
| 787 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 788 | UTEST(test_general), |
| 789 | UTEST(test_number), |
| 790 | UTEST(test_string), |
| 791 | UTEST(test_object), |
| 792 | UTEST(test_array), |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 793 | }; |
| 794 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 795 | return cmocka_run_group_tests(tests, NULL, NULL); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 796 | } |