w.deng | e87b500 | 2025-08-20 10:43:03 +0800 | [diff] [blame^] | 1 | #ifdef NDEBUG |
| 2 | #undef NDEBUG |
| 3 | #endif |
| 4 | #include <assert.h> |
| 5 | #include <limits.h> |
| 6 | #include <stddef.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include "config.h" |
| 12 | |
| 13 | #include "json.h" |
| 14 | #include "parse_flags.h" |
| 15 | |
| 16 | static int sort_fn(const void *j1, const void *j2) |
| 17 | { |
| 18 | json_object *const *jso1, *const *jso2; |
| 19 | int i1, i2; |
| 20 | |
| 21 | jso1 = (json_object *const *)j1; |
| 22 | jso2 = (json_object *const *)j2; |
| 23 | if (!*jso1 && !*jso2) |
| 24 | return 0; |
| 25 | if (!*jso1) |
| 26 | return -1; |
| 27 | if (!*jso2) |
| 28 | return 1; |
| 29 | |
| 30 | i1 = json_object_get_int(*jso1); |
| 31 | i2 = json_object_get_int(*jso2); |
| 32 | |
| 33 | return i1 - i2; |
| 34 | } |
| 35 | |
| 36 | #ifdef TEST_FORMATTED |
| 37 | static const char *to_json_string(json_object *obj, int flags) |
| 38 | { |
| 39 | size_t length; |
| 40 | char *copy; |
| 41 | const char *result; |
| 42 | |
| 43 | result = json_object_to_json_string_length(obj, flags, &length); |
| 44 | copy = strdup(result); |
| 45 | if (copy == NULL) |
| 46 | printf("to_json_string: Allocation failed!\n"); |
| 47 | else |
| 48 | { |
| 49 | result = json_object_to_json_string_ext(obj, flags); |
| 50 | if (length != strlen(result)) |
| 51 | printf("to_json_string: Length mismatch!\n"); |
| 52 | if (strcmp(copy, result) != 0) |
| 53 | printf("to_json_string: Comparison Failed!\n"); |
| 54 | free(copy); |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | #define json_object_to_json_string(obj) to_json_string(obj, sflags) |
| 59 | #else |
| 60 | /* no special define */ |
| 61 | #endif |
| 62 | |
| 63 | json_object *make_array(void); |
| 64 | json_object *make_array(void) |
| 65 | { |
| 66 | json_object *my_array; |
| 67 | |
| 68 | my_array = json_object_new_array(); |
| 69 | json_object_array_add(my_array, json_object_new_int(1)); |
| 70 | json_object_array_add(my_array, json_object_new_int(2)); |
| 71 | json_object_array_add(my_array, json_object_new_int(3)); |
| 72 | json_object_array_put_idx(my_array, 4, json_object_new_int(5)); |
| 73 | json_object_array_put_idx(my_array, 3, json_object_new_int(4)); |
| 74 | json_object_array_put_idx(my_array, 6, json_object_new_int(7)); |
| 75 | |
| 76 | return my_array; |
| 77 | } |
| 78 | |
| 79 | void test_array_del_idx(void); |
| 80 | void test_array_del_idx(void) |
| 81 | { |
| 82 | int rc; |
| 83 | size_t ii; |
| 84 | size_t orig_array_len; |
| 85 | json_object *my_array; |
| 86 | #ifdef TEST_FORMATTED |
| 87 | int sflags = 0; |
| 88 | #endif |
| 89 | |
| 90 | my_array = make_array(); |
| 91 | orig_array_len = json_object_array_length(my_array); |
| 92 | |
| 93 | printf("my_array=\n"); |
| 94 | for (ii = 0; ii < json_object_array_length(my_array); ii++) |
| 95 | { |
| 96 | json_object *obj = json_object_array_get_idx(my_array, ii); |
| 97 | printf("\t[%d]=%s\n", (int)ii, json_object_to_json_string(obj)); |
| 98 | } |
| 99 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 100 | |
| 101 | for (ii = 0; ii < orig_array_len; ii++) |
| 102 | { |
| 103 | rc = json_object_array_del_idx(my_array, 0, 1); |
| 104 | printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", rc, |
| 105 | json_object_to_json_string(my_array)); |
| 106 | } |
| 107 | |
| 108 | /* One more time, with the empty array: */ |
| 109 | rc = json_object_array_del_idx(my_array, 0, 1); |
| 110 | printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", rc, |
| 111 | json_object_to_json_string(my_array)); |
| 112 | |
| 113 | json_object_put(my_array); |
| 114 | |
| 115 | /* Delete all array indexes at once */ |
| 116 | my_array = make_array(); |
| 117 | rc = json_object_array_del_idx(my_array, 0, orig_array_len); |
| 118 | printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)orig_array_len, rc, |
| 119 | json_object_to_json_string(my_array)); |
| 120 | |
| 121 | json_object_put(my_array); |
| 122 | |
| 123 | /* Delete *more* than all array indexes at once */ |
| 124 | my_array = make_array(); |
| 125 | rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1); |
| 126 | printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)(orig_array_len + 1), rc, |
| 127 | json_object_to_json_string(my_array)); |
| 128 | |
| 129 | json_object_put(my_array); |
| 130 | |
| 131 | /* Delete some array indexes, then add more */ |
| 132 | my_array = make_array(); |
| 133 | rc = json_object_array_del_idx(my_array, 0, orig_array_len - 1); |
| 134 | printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)(orig_array_len - 1), rc, |
| 135 | json_object_to_json_string(my_array)); |
| 136 | json_object_array_add(my_array, json_object_new_string("s1")); |
| 137 | json_object_array_add(my_array, json_object_new_string("s2")); |
| 138 | json_object_array_add(my_array, json_object_new_string("s3")); |
| 139 | |
| 140 | printf("after adding more entries, my_array.to_string()=%s\n", |
| 141 | json_object_to_json_string(my_array)); |
| 142 | json_object_put(my_array); |
| 143 | } |
| 144 | |
| 145 | void test_array_list_expand_internal(void); |
| 146 | void test_array_list_expand_internal(void) |
| 147 | { |
| 148 | int rc; |
| 149 | size_t ii; |
| 150 | size_t idx; |
| 151 | json_object *my_array; |
| 152 | #ifdef TEST_FORMATTED |
| 153 | int sflags = 0; |
| 154 | #endif |
| 155 | |
| 156 | my_array = make_array(); |
| 157 | printf("my_array=\n"); |
| 158 | for (ii = 0; ii < json_object_array_length(my_array); ii++) |
| 159 | { |
| 160 | json_object *obj = json_object_array_get_idx(my_array, ii); |
| 161 | printf("\t[%d]=%s\n", (int)ii, json_object_to_json_string(obj)); |
| 162 | } |
| 163 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 164 | |
| 165 | /* Put iNdex < array->size, no expand. */ |
| 166 | rc = json_object_array_put_idx(my_array, 5, json_object_new_int(6)); |
| 167 | printf("put_idx(5,6)=%d\n", rc); |
| 168 | |
| 169 | /* array->size < Put Index < array->size * 2 <= SIZE_T_MAX, the size = array->size * 2. */ |
| 170 | idx = ARRAY_LIST_DEFAULT_SIZE * 2 - 1; |
| 171 | rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0)); |
| 172 | printf("put_idx(%d,0)=%d\n", (int)(idx), rc); |
| 173 | |
| 174 | /* array->size * 2 < Put Index, the size = Put Index. */ |
| 175 | idx = ARRAY_LIST_DEFAULT_SIZE * 2 * 2 + 1; |
| 176 | rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0)); |
| 177 | printf("put_idx(%d,0)=%d\n", (int)(idx), rc); |
| 178 | |
| 179 | /* SIZE_T_MAX <= Put Index, it will fail and the size will no change. */ |
| 180 | idx = SIZE_MAX; // SIZE_MAX = SIZE_T_MAX |
| 181 | json_object *tmp = json_object_new_int(10); |
| 182 | rc = json_object_array_put_idx(my_array, idx, tmp); |
| 183 | printf("put_idx(SIZE_T_MAX,0)=%d\n", rc); |
| 184 | if (rc == -1) |
| 185 | { |
| 186 | json_object_put(tmp); |
| 187 | } |
| 188 | |
| 189 | json_object_put(my_array); |
| 190 | } |
| 191 | |
| 192 | int main(int argc, char **argv) |
| 193 | { |
| 194 | json_object *my_string, *my_int, *my_null, *my_object, *my_array; |
| 195 | size_t i; |
| 196 | #ifdef TEST_FORMATTED |
| 197 | int sflags = 0; |
| 198 | #endif |
| 199 | |
| 200 | MC_SET_DEBUG(1); |
| 201 | |
| 202 | #ifdef TEST_FORMATTED |
| 203 | sflags = parse_flags(argc, argv); |
| 204 | #endif |
| 205 | |
| 206 | my_string = json_object_new_string("\t"); |
| 207 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 208 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 209 | json_object_put(my_string); |
| 210 | |
| 211 | my_string = json_object_new_string("\\"); |
| 212 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 213 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 214 | json_object_put(my_string); |
| 215 | |
| 216 | my_string = json_object_new_string("/"); |
| 217 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 218 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 219 | printf("my_string.to_string(NOSLASHESCAPE)=%s\n", |
| 220 | json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE)); |
| 221 | json_object_put(my_string); |
| 222 | |
| 223 | my_string = json_object_new_string("/foo/bar/baz"); |
| 224 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 225 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 226 | printf("my_string.to_string(NOSLASHESCAPE)=%s\n", |
| 227 | json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE)); |
| 228 | json_object_put(my_string); |
| 229 | |
| 230 | my_string = json_object_new_string("foo"); |
| 231 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 232 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 233 | |
| 234 | my_int = json_object_new_int(9); |
| 235 | printf("my_int=%d\n", json_object_get_int(my_int)); |
| 236 | printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); |
| 237 | |
| 238 | my_null = json_object_new_null(); |
| 239 | printf("my_null.to_string()=%s\n", json_object_to_json_string(my_null)); |
| 240 | |
| 241 | my_array = json_object_new_array(); |
| 242 | json_object_array_add(my_array, json_object_new_int(1)); |
| 243 | json_object_array_add(my_array, json_object_new_int(2)); |
| 244 | json_object_array_add(my_array, json_object_new_int(3)); |
| 245 | json_object_array_put_idx(my_array, 4, json_object_new_int(5)); |
| 246 | printf("my_array=\n"); |
| 247 | for (i = 0; i < json_object_array_length(my_array); i++) |
| 248 | { |
| 249 | json_object *obj = json_object_array_get_idx(my_array, i); |
| 250 | printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj)); |
| 251 | } |
| 252 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 253 | |
| 254 | json_object_put(my_array); |
| 255 | |
| 256 | test_array_del_idx(); |
| 257 | test_array_list_expand_internal(); |
| 258 | |
| 259 | my_array = json_object_new_array_ext(5); |
| 260 | json_object_array_add(my_array, json_object_new_int(3)); |
| 261 | json_object_array_add(my_array, json_object_new_int(1)); |
| 262 | json_object_array_add(my_array, json_object_new_int(2)); |
| 263 | json_object_array_put_idx(my_array, 4, json_object_new_int(0)); |
| 264 | printf("my_array=\n"); |
| 265 | for (i = 0; i < json_object_array_length(my_array); i++) |
| 266 | { |
| 267 | json_object *obj = json_object_array_get_idx(my_array, i); |
| 268 | printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj)); |
| 269 | } |
| 270 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 271 | json_object_array_sort(my_array, sort_fn); |
| 272 | printf("my_array=\n"); |
| 273 | for (i = 0; i < json_object_array_length(my_array); i++) |
| 274 | { |
| 275 | json_object *obj = json_object_array_get_idx(my_array, i); |
| 276 | printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj)); |
| 277 | } |
| 278 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 279 | |
| 280 | json_object *one = json_object_new_int(1); |
| 281 | json_object *result = json_object_array_bsearch(one, my_array, sort_fn); |
| 282 | printf("find json_object(1) in my_array successfully: %s\n", |
| 283 | json_object_to_json_string(result)); |
| 284 | json_object_put(one); |
| 285 | |
| 286 | my_object = json_object_new_object(); |
| 287 | int rc = json_object_object_add(my_object, "abc", my_object); |
| 288 | if (rc != -1) |
| 289 | { |
| 290 | printf("ERROR: able to successfully add object to itself!\n"); |
| 291 | fflush(stdout); |
| 292 | } |
| 293 | json_object_object_add(my_object, "abc", json_object_new_int(12)); |
| 294 | json_object_object_add(my_object, "foo", json_object_new_string("bar")); |
| 295 | json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); |
| 296 | json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); |
| 297 | json_object_object_add(my_object, "baz", json_object_new_string("bang")); |
| 298 | |
| 299 | json_object *baz_obj = json_object_new_string("fark"); |
| 300 | json_object_get(baz_obj); |
| 301 | json_object_object_add(my_object, "baz", baz_obj); |
| 302 | json_object_object_del(my_object, "baz"); |
| 303 | |
| 304 | /* baz_obj should still be valid */ |
| 305 | printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj)); |
| 306 | json_object_put(baz_obj); |
| 307 | |
| 308 | /*json_object_object_add(my_object, "arr", my_array);*/ |
| 309 | printf("my_object=\n"); |
| 310 | json_object_object_foreach(my_object, key, val) |
| 311 | { |
| 312 | printf("\t%s: %s\n", key, json_object_to_json_string(val)); |
| 313 | } |
| 314 | |
| 315 | json_object *empty_array = json_object_new_array(); |
| 316 | json_object *empty_obj = json_object_new_object(); |
| 317 | json_object_object_add(my_object, "empty_array", empty_array); |
| 318 | json_object_object_add(my_object, "empty_obj", empty_obj); |
| 319 | printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object)); |
| 320 | |
| 321 | json_object_put(my_array); |
| 322 | my_array = json_object_new_array_ext(INT_MIN + 1); |
| 323 | if (my_array != NULL) |
| 324 | { |
| 325 | printf("ERROR: able to allocate an array of negative size!\n"); |
| 326 | fflush(stdout); |
| 327 | json_object_put(my_array); |
| 328 | my_array = NULL; |
| 329 | } |
| 330 | |
| 331 | #if SIZEOF_SIZE_T == SIZEOF_INT |
| 332 | my_array = json_object_new_array_ext(INT_MAX / 2 + 2); |
| 333 | if (my_array != NULL) |
| 334 | { |
| 335 | printf("ERROR: able to allocate an array of insufficient size!\n"); |
| 336 | fflush(stdout); |
| 337 | json_object_put(my_array); |
| 338 | my_array = NULL; |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | json_object_put(my_string); |
| 343 | json_object_put(my_int); |
| 344 | json_object_put(my_null); |
| 345 | json_object_put(my_object); |
| 346 | json_object_put(my_array); |
| 347 | |
| 348 | return EXIT_SUCCESS; |
| 349 | } |