1 /* 2 __ _____ _____ _____ 3 __| | __| | | | JSON for Modern C++ (test suite) 4 | | |__ | | | | | | version 3.10.0 5 |_____|_____|_____|_|___| https://github.com/nlohmann/json 6 7 Licensed under the MIT License <http://opensource.org/licenses/MIT>. 8 SPDX-License-Identifier: MIT 9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>. 10 11 Permission is hereby granted, free of charge, to any person obtaining a copy 12 of this software and associated documentation files (the "Software"), to deal 13 in the Software without restriction, including without limitation the rights 14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 copies of the Software, and to permit persons to whom the Software is 16 furnished to do so, subject to the following conditions: 17 18 The above copyright notice and this permission notice shall be included in all 19 copies or substantial portions of the Software. 20 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 SOFTWARE. 28 */ 29 30 #include "doctest_compatibility.h" 31 32 #include <nlohmann/json.hpp> 33 using nlohmann::json; 34 35 TEST_CASE("capacity") 36 { 37 SECTION("empty()") 38 { 39 SECTION("boolean") 40 { 41 json j = true; 42 const json j_const(j); 43 44 SECTION("result of empty") 45 { 46 CHECK(j.empty() == false); 47 CHECK(j_const.empty() == false); 48 } 49 50 SECTION("definition of empty") 51 { 52 CHECK(j.empty() == (j.begin() == j.end())); 53 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 54 } 55 } 56 57 SECTION("string") 58 { 59 json j = "hello world"; 60 const json j_const(j); 61 62 SECTION("result of empty") 63 { 64 CHECK(j.empty() == false); 65 CHECK(j_const.empty() == false); 66 } 67 68 SECTION("definition of empty") 69 { 70 CHECK(j.empty() == (j.begin() == j.end())); 71 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 72 } 73 } 74 75 SECTION("array") 76 { 77 SECTION("empty array") 78 { 79 json j = json::array(); 80 const json j_const(j); 81 82 SECTION("result of empty") 83 { 84 CHECK(j.empty() == true); 85 CHECK(j_const.empty() == true); 86 } 87 88 SECTION("definition of empty") 89 { 90 CHECK(j.empty() == (j.begin() == j.end())); 91 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 92 } 93 } 94 95 SECTION("filled array") 96 { 97 json j = {1, 2, 3}; 98 const json j_const(j); 99 100 SECTION("result of empty") 101 { 102 CHECK(j.empty() == false); 103 CHECK(j_const.empty() == false); 104 } 105 106 SECTION("definition of empty") 107 { 108 CHECK(j.empty() == (j.begin() == j.end())); 109 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 110 } 111 } 112 } 113 114 SECTION("object") 115 { 116 SECTION("empty object") 117 { 118 json j = json::object(); 119 const json j_const(j); 120 121 SECTION("result of empty") 122 { 123 CHECK(j.empty() == true); 124 CHECK(j_const.empty() == true); 125 } 126 127 SECTION("definition of empty") 128 { 129 CHECK(j.empty() == (j.begin() == j.end())); 130 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 131 } 132 } 133 134 SECTION("filled object") 135 { 136 json j = {{"one", 1}, {"two", 2}, {"three", 3}}; 137 const json j_const(j); 138 139 SECTION("result of empty") 140 { 141 CHECK(j.empty() == false); 142 CHECK(j_const.empty() == false); 143 } 144 145 SECTION("definition of empty") 146 { 147 CHECK(j.empty() == (j.begin() == j.end())); 148 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 149 } 150 } 151 } 152 153 SECTION("number (integer)") 154 { 155 json j = -23; 156 const json j_const(j); 157 158 SECTION("result of empty") 159 { 160 CHECK(j.empty() == false); 161 CHECK(j_const.empty() == false); 162 } 163 164 SECTION("definition of empty") 165 { 166 CHECK(j.empty() == (j.begin() == j.end())); 167 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 168 } 169 } 170 171 SECTION("number (unsigned)") 172 { 173 json j = 23u; 174 const json j_const(j); 175 176 SECTION("result of empty") 177 { 178 CHECK(j.empty() == false); 179 CHECK(j_const.empty() == false); 180 } 181 182 SECTION("definition of empty") 183 { 184 CHECK(j.empty() == (j.begin() == j.end())); 185 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 186 } 187 } 188 189 SECTION("number (float)") 190 { 191 json j = 23.42; 192 const json j_const(j); 193 194 SECTION("result of empty") 195 { 196 CHECK(j.empty() == false); 197 CHECK(j_const.empty() == false); 198 } 199 200 SECTION("definition of empty") 201 { 202 CHECK(j.empty() == (j.begin() == j.end())); 203 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 204 } 205 } 206 207 SECTION("null") 208 { 209 json j = nullptr; 210 const json j_const(j); 211 212 SECTION("result of empty") 213 { 214 CHECK(j.empty() == true); 215 CHECK(j_const.empty() == true); 216 } 217 218 SECTION("definition of empty") 219 { 220 CHECK(j.empty() == (j.begin() == j.end())); 221 CHECK(j_const.empty() == (j_const.begin() == j_const.end())); 222 } 223 } 224 } 225 226 SECTION("size()") 227 { 228 SECTION("boolean") 229 { 230 json j = true; 231 const json j_const(j); 232 233 SECTION("result of size") 234 { 235 CHECK(j.size() == 1); 236 CHECK(j_const.size() == 1); 237 } 238 239 SECTION("definition of size") 240 { 241 CHECK(std::distance(j.begin(), j.end()) == j.size()); 242 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 243 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 244 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 245 } 246 } 247 248 SECTION("string") 249 { 250 json j = "hello world"; 251 const json j_const(j); 252 253 SECTION("result of size") 254 { 255 CHECK(j.size() == 1); 256 CHECK(j_const.size() == 1); 257 } 258 259 SECTION("definition of size") 260 { 261 CHECK(std::distance(j.begin(), j.end()) == j.size()); 262 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 263 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 264 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 265 } 266 } 267 268 SECTION("array") 269 { 270 SECTION("empty array") 271 { 272 json j = json::array(); 273 const json j_const(j); 274 275 SECTION("result of size") 276 { 277 CHECK(j.size() == 0); 278 CHECK(j_const.size() == 0); 279 } 280 281 SECTION("definition of size") 282 { 283 CHECK(std::distance(j.begin(), j.end()) == j.size()); 284 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 285 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 286 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 287 } 288 } 289 290 SECTION("filled array") 291 { 292 json j = {1, 2, 3}; 293 const json j_const(j); 294 295 SECTION("result of size") 296 { 297 CHECK(j.size() == 3); 298 CHECK(j_const.size() == 3); 299 } 300 301 SECTION("definition of size") 302 { 303 CHECK(std::distance(j.begin(), j.end()) == j.size()); 304 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 305 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 306 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 307 } 308 } 309 } 310 311 SECTION("object") 312 { 313 SECTION("empty object") 314 { 315 json j = json::object(); 316 const json j_const(j); 317 318 SECTION("result of size") 319 { 320 CHECK(j.size() == 0); 321 CHECK(j_const.size() == 0); 322 } 323 324 SECTION("definition of size") 325 { 326 CHECK(std::distance(j.begin(), j.end()) == j.size()); 327 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 328 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 329 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 330 } 331 } 332 333 SECTION("filled object") 334 { 335 json j = {{"one", 1}, {"two", 2}, {"three", 3}}; 336 const json j_const(j); 337 338 SECTION("result of size") 339 { 340 CHECK(j.size() == 3); 341 CHECK(j_const.size() == 3); 342 } 343 344 SECTION("definition of size") 345 { 346 CHECK(std::distance(j.begin(), j.end()) == j.size()); 347 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 348 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 349 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 350 } 351 } 352 } 353 354 SECTION("number (integer)") 355 { 356 json j = -23; 357 const json j_const(j); 358 359 SECTION("result of size") 360 { 361 CHECK(j.size() == 1); 362 CHECK(j_const.size() == 1); 363 } 364 365 SECTION("definition of size") 366 { 367 CHECK(std::distance(j.begin(), j.end()) == j.size()); 368 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 369 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 370 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 371 } 372 } 373 374 SECTION("number (unsigned)") 375 { 376 json j = 23u; 377 const json j_const(j); 378 379 SECTION("result of size") 380 { 381 CHECK(j.size() == 1); 382 CHECK(j_const.size() == 1); 383 } 384 385 SECTION("definition of size") 386 { 387 CHECK(std::distance(j.begin(), j.end()) == j.size()); 388 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 389 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 390 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 391 } 392 } 393 394 SECTION("number (float)") 395 { 396 json j = 23.42; 397 const json j_const(j); 398 399 SECTION("result of size") 400 { 401 CHECK(j.size() == 1); 402 CHECK(j_const.size() == 1); 403 } 404 405 SECTION("definition of size") 406 { 407 CHECK(std::distance(j.begin(), j.end()) == j.size()); 408 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 409 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 410 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 411 } 412 } 413 414 SECTION("null") 415 { 416 json j = nullptr; 417 const json j_const(j); 418 419 SECTION("result of size") 420 { 421 CHECK(j.size() == 0); 422 CHECK(j_const.size() == 0); 423 } 424 425 SECTION("definition of size") 426 { 427 CHECK(std::distance(j.begin(), j.end()) == j.size()); 428 CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); 429 CHECK(std::distance(j.rbegin(), j.rend()) == j.size()); 430 CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size()); 431 } 432 } 433 } 434 435 SECTION("max_size()") 436 { 437 SECTION("boolean") 438 { 439 json j = true; 440 const json j_const = true; 441 442 SECTION("result of max_size") 443 { 444 CHECK(j.max_size() == 1); 445 CHECK(j_const.max_size() == 1); 446 } 447 } 448 449 SECTION("string") 450 { 451 json j = "hello world"; 452 const json j_const = "hello world"; 453 454 SECTION("result of max_size") 455 { 456 CHECK(j.max_size() == 1); 457 CHECK(j_const.max_size() == 1); 458 } 459 } 460 461 SECTION("array") 462 { 463 SECTION("empty array") 464 { 465 json j = json::array(); 466 const json j_const = json::array(); 467 468 SECTION("result of max_size") 469 { 470 CHECK(j.max_size() >= j.size()); 471 CHECK(j_const.max_size() >= j_const.size()); 472 } 473 } 474 475 SECTION("filled array") 476 { 477 json j = {1, 2, 3}; 478 const json j_const = {1, 2, 3}; 479 480 SECTION("result of max_size") 481 { 482 CHECK(j.max_size() >= j.size()); 483 CHECK(j_const.max_size() >= j_const.size()); 484 } 485 } 486 } 487 488 SECTION("object") 489 { 490 SECTION("empty object") 491 { 492 json j = json::object(); 493 const json j_const = json::object(); 494 495 SECTION("result of max_size") 496 { 497 CHECK(j.max_size() >= j.size()); 498 CHECK(j_const.max_size() >= j_const.size()); 499 } 500 } 501 502 SECTION("filled object") 503 { 504 json j = {{"one", 1}, {"two", 2}, {"three", 3}}; 505 const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}}; 506 507 SECTION("result of max_size") 508 { 509 CHECK(j.max_size() >= j.size()); 510 CHECK(j_const.max_size() >= j_const.size()); 511 } 512 } 513 } 514 515 SECTION("number (integer)") 516 { 517 json j = -23; 518 const json j_const = -23; 519 520 SECTION("result of max_size") 521 { 522 CHECK(j.max_size() == 1); 523 CHECK(j_const.max_size() == 1); 524 } 525 } 526 527 SECTION("number (unsigned)") 528 { 529 json j = 23u; 530 const json j_const = 23u; 531 532 SECTION("result of max_size") 533 { 534 CHECK(j.max_size() == 1); 535 CHECK(j_const.max_size() == 1); 536 } 537 } 538 539 SECTION("number (float)") 540 { 541 json j = 23.42; 542 const json j_const = 23.42; 543 544 SECTION("result of max_size") 545 { 546 CHECK(j.max_size() == 1); 547 CHECK(j_const.max_size() == 1); 548 } 549 } 550 551 SECTION("null") 552 { 553 json j = nullptr; 554 const json j_const = nullptr; 555 556 SECTION("result of max_size") 557 { 558 CHECK(j.max_size() == 0); 559 CHECK(j_const.max_size() == 0); 560 } 561 } 562 } 563 } 564