1 /* 2 __ _____ _____ _____ 3 __| | __| | | | JSON for Modern C++ (test suite) 4 | | |__ | | | | | | version 3.9.1 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 DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") 32 33 #define private public 34 #include <nlohmann/json.hpp> 35 using nlohmann::json; 36 #undef private 37 38 #include <deque> 39 #include <forward_list> 40 #include <fstream> 41 #include <list> 42 #include <set> 43 #include <unordered_map> 44 #include <unordered_set> 45 #include <valarray> 46 47 TEST_CASE("constructors") 48 { 49 SECTION("create an empty value with a given type") 50 { 51 SECTION("null") 52 { 53 auto t = json::value_t::null; 54 json j(t); 55 CHECK(j.type() == t); 56 } 57 58 SECTION("discarded") 59 { 60 auto t = json::value_t::discarded; 61 json j(t); 62 CHECK(j.type() == t); 63 } 64 65 SECTION("object") 66 { 67 auto t = json::value_t::object; 68 json j(t); 69 CHECK(j.type() == t); 70 } 71 72 SECTION("array") 73 { 74 auto t = json::value_t::array; 75 json j(t); 76 CHECK(j.type() == t); 77 } 78 79 SECTION("boolean") 80 { 81 auto t = json::value_t::boolean; 82 json j(t); 83 CHECK(j.type() == t); 84 CHECK(j == false); 85 } 86 87 SECTION("string") 88 { 89 auto t = json::value_t::string; 90 json j(t); 91 CHECK(j.type() == t); 92 CHECK(j == ""); 93 } 94 95 SECTION("number_integer") 96 { 97 auto t = json::value_t::number_integer; 98 json j(t); 99 CHECK(j.type() == t); 100 CHECK(j == 0); 101 } 102 103 SECTION("number_unsigned") 104 { 105 auto t = json::value_t::number_unsigned; 106 json j(t); 107 CHECK(j.type() == t); 108 CHECK(j == 0); 109 } 110 111 SECTION("number_float") 112 { 113 auto t = json::value_t::number_float; 114 json j(t); 115 CHECK(j.type() == t); 116 CHECK(j == 0.0); 117 } 118 119 SECTION("binary") 120 { 121 auto t = json::value_t::binary; 122 json j(t); 123 CHECK(j.type() == t); 124 CHECK(j == json::binary({})); 125 } 126 } 127 128 SECTION("create a null object (implicitly)") 129 { 130 SECTION("no parameter") 131 { 132 json j{}; 133 CHECK(j.type() == json::value_t::null); 134 } 135 } 136 137 SECTION("create a null object (explicitly)") 138 { 139 SECTION("parameter") 140 { 141 json j(nullptr); 142 CHECK(j.type() == json::value_t::null); 143 } 144 } 145 146 SECTION("create an object (explicit)") 147 { 148 SECTION("empty object") 149 { 150 json::object_t o; 151 json j(o); 152 CHECK(j.type() == json::value_t::object); 153 } 154 155 SECTION("filled object") 156 { 157 json::object_t o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 158 json j(o); 159 CHECK(j.type() == json::value_t::object); 160 } 161 } 162 163 SECTION("create an object (implicit)") 164 { 165 // reference object 166 json::object_t o_reference {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 167 json j_reference(o_reference); 168 169 SECTION("std::map<json::string_t, json>") 170 { 171 std::map<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 172 json j(o); 173 CHECK(j.type() == json::value_t::object); 174 CHECK(j == j_reference); 175 } 176 177 SECTION("std::map<std::string, std::string> #600") 178 { 179 std::map<std::string, std::string> m; 180 m["a"] = "b"; 181 m["c"] = "d"; 182 m["e"] = "f"; 183 184 json j(m); 185 CHECK((j.get<decltype(m)>() == m)); 186 } 187 188 SECTION("std::map<const char*, json>") 189 { 190 std::map<const char*, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 191 json j(o); 192 CHECK(j.type() == json::value_t::object); 193 CHECK(j == j_reference); 194 } 195 196 197 SECTION("std::multimap<json::string_t, json>") 198 { 199 std::multimap<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 200 json j(o); 201 CHECK(j.type() == json::value_t::object); 202 CHECK(j == j_reference); 203 } 204 205 SECTION("std::unordered_map<json::string_t, json>") 206 { 207 std::unordered_map<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 208 json j(o); 209 CHECK(j.type() == json::value_t::object); 210 CHECK(j == j_reference); 211 } 212 213 SECTION("std::unordered_multimap<json::string_t, json>") 214 { 215 std::unordered_multimap<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}; 216 json j(o); 217 CHECK(j.type() == json::value_t::object); 218 CHECK(j == j_reference); 219 } 220 221 SECTION("associative container literal") 222 { 223 json j({{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}}); 224 CHECK(j.type() == json::value_t::object); 225 CHECK(j == j_reference); 226 } 227 } 228 229 SECTION("create an array (explicit)") 230 { 231 SECTION("empty array") 232 { 233 json::array_t a; 234 json j(a); 235 CHECK(j.type() == json::value_t::array); 236 } 237 238 SECTION("filled array") 239 { 240 json::array_t a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 241 json j(a); 242 CHECK(j.type() == json::value_t::array); 243 } 244 } 245 246 SECTION("create an array (implicit)") 247 { 248 // reference array 249 json::array_t a_reference {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 250 json j_reference(a_reference); 251 252 SECTION("std::list<json>") 253 { 254 std::list<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 255 json j(a); 256 CHECK(j.type() == json::value_t::array); 257 CHECK(j == j_reference); 258 } 259 260 SECTION("std::pair") 261 { 262 std::pair<float, std::string> p{1.0f, "string"}; 263 json j(p); 264 265 CHECK(j.type() == json::value_t::array); 266 CHECK(j.get<decltype(p)>() == p); 267 REQUIRE(j.size() == 2); 268 CHECK(j[0] == std::get<0>(p)); 269 CHECK(j[1] == std::get<1>(p)); 270 } 271 272 SECTION("std::pair with discarded values") 273 { 274 json j{1, 2.0, "string"}; 275 276 const auto p = j.get<std::pair<int, float>>(); 277 CHECK(p.first == j[0]); 278 CHECK(p.second == j[1]); 279 } 280 281 SECTION("std::tuple") 282 { 283 const auto t = std::make_tuple(1.0, std::string{"string"}, 42, std::vector<int> {0, 1}); 284 json j(t); 285 286 CHECK(j.type() == json::value_t::array); 287 REQUIRE(j.size() == 4); 288 CHECK(j.get<decltype(t)>() == t); 289 CHECK(j[0] == std::get<0>(t)); 290 CHECK(j[1] == std::get<1>(t)); 291 CHECK(j[2] == std::get<2>(t)); 292 CHECK(j[3][0] == 0); 293 CHECK(j[3][1] == 1); 294 } 295 296 SECTION("std::tuple with discarded values") 297 { 298 json j{1, 2.0, "string", 42}; 299 300 const auto t = j.get<std::tuple<int, float, std::string>>(); 301 CHECK(std::get<0>(t) == j[0]); 302 CHECK(std::get<1>(t) == j[1]); 303 CHECK(std::get<2>(t) == j[2]); 304 } 305 306 SECTION("std::pair/tuple/array failures") 307 { 308 json j{1}; 309 310 CHECK_THROWS_AS((j.get<std::pair<int, int>>()), json::out_of_range&); 311 CHECK_THROWS_WITH((j.get<std::pair<int, int>>()), "[json.exception.out_of_range.401] array index 1 is out of range"); 312 CHECK_THROWS_AS((j.get<std::tuple<int, int>>()), json::out_of_range&); 313 CHECK_THROWS_WITH((j.get<std::tuple<int, int>>()), "[json.exception.out_of_range.401] array index 1 is out of range"); 314 CHECK_THROWS_AS((j.get<std::array<int, 3>>()), json::out_of_range&); 315 CHECK_THROWS_WITH((j.get<std::array<int, 3>>()), "[json.exception.out_of_range.401] array index 1 is out of range"); 316 } 317 318 SECTION("std::forward_list<json>") 319 { 320 std::forward_list<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 321 json j(a); 322 CHECK(j.type() == json::value_t::array); 323 CHECK(j == j_reference); 324 } 325 326 SECTION("std::array<json, 6>") 327 { 328 std::array<json, 6> a {{json(1), json(1u), json(2.2), json(false), json("string"), json()}}; 329 json j(a); 330 CHECK(j.type() == json::value_t::array); 331 CHECK(j == j_reference); 332 333 const auto a2 = j.get<std::array<json, 6>>(); 334 CHECK(a2 == a); 335 } 336 337 SECTION("std::valarray<int>") 338 { 339 std::valarray<int> va = {1, 2, 3, 4, 5}; 340 json j(va); 341 CHECK(j.type() == json::value_t::array); 342 CHECK(j == json({1, 2, 3, 4, 5})); 343 344 auto jva = j.get<std::valarray<int>>(); 345 CHECK(jva.size() == va.size()); 346 for (size_t i = 0; i < jva.size(); ++i) 347 { 348 CHECK(va[i] == jva[i]); 349 } 350 } 351 352 SECTION("std::valarray<double>") 353 { 354 std::valarray<double> va = {1.2, 2.3, 3.4, 4.5, 5.6}; 355 json j(va); 356 CHECK(j.type() == json::value_t::array); 357 CHECK(j == json({1.2, 2.3, 3.4, 4.5, 5.6})); 358 359 auto jva = j.get<std::valarray<double>>(); 360 CHECK(jva.size() == va.size()); 361 for (size_t i = 0; i < jva.size(); ++i) 362 { 363 CHECK(va[i] == jva[i]); 364 } 365 } 366 367 SECTION("std::vector<json>") 368 { 369 std::vector<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 370 json j(a); 371 CHECK(j.type() == json::value_t::array); 372 CHECK(j == j_reference); 373 } 374 375 SECTION("std::deque<json>") 376 { 377 std::deque<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 378 json j(a); 379 CHECK(j.type() == json::value_t::array); 380 CHECK(j == j_reference); 381 } 382 383 SECTION("std::set<json>") 384 { 385 std::set<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 386 json j(a); 387 CHECK(j.type() == json::value_t::array); 388 // we cannot really check for equality here 389 } 390 391 SECTION("std::unordered_set<json>") 392 { 393 std::unordered_set<json> a {json(1), json(1u), json(2.2), json(false), json("string"), json()}; 394 json j(a); 395 CHECK(j.type() == json::value_t::array); 396 // we cannot really check for equality here 397 } 398 399 SECTION("sequence container literal") 400 { 401 json j({json(1), json(1u), json(2.2), json(false), json("string"), json()}); 402 CHECK(j.type() == json::value_t::array); 403 CHECK(j == j_reference); 404 } 405 } 406 407 SECTION("create a string (explicit)") 408 { 409 SECTION("empty string") 410 { 411 json::string_t s; 412 json j(s); 413 CHECK(j.type() == json::value_t::string); 414 } 415 416 SECTION("filled string") 417 { 418 json::string_t s {"Hello world"}; 419 json j(s); 420 CHECK(j.type() == json::value_t::string); 421 } 422 } 423 424 SECTION("create a string (implicit)") 425 { 426 // reference string 427 json::string_t s_reference {"Hello world"}; 428 json j_reference(s_reference); 429 430 SECTION("std::string") 431 { 432 std::string s {"Hello world"}; 433 json j(s); 434 CHECK(j.type() == json::value_t::string); 435 CHECK(j == j_reference); 436 } 437 438 SECTION("char[]") 439 { 440 char s[] {"Hello world"}; 441 json j(s); 442 CHECK(j.type() == json::value_t::string); 443 CHECK(j == j_reference); 444 } 445 446 SECTION("const char*") 447 { 448 const char* s {"Hello world"}; 449 json j(s); 450 CHECK(j.type() == json::value_t::string); 451 CHECK(j == j_reference); 452 } 453 454 SECTION("string literal") 455 { 456 json j("Hello world"); 457 CHECK(j.type() == json::value_t::string); 458 CHECK(j == j_reference); 459 } 460 } 461 462 SECTION("create a boolean (explicit)") 463 { 464 SECTION("empty boolean") 465 { 466 json::boolean_t b{}; 467 json j(b); 468 CHECK(j.type() == json::value_t::boolean); 469 } 470 471 SECTION("filled boolean (true)") 472 { 473 json j(true); 474 CHECK(j.type() == json::value_t::boolean); 475 } 476 477 SECTION("filled boolean (false)") 478 { 479 json j(false); 480 CHECK(j.type() == json::value_t::boolean); 481 } 482 } 483 484 SECTION("create a binary (explicit)") 485 { 486 SECTION("empty binary") 487 { 488 json::binary_t b{}; 489 json j(b); 490 CHECK(j.type() == json::value_t::binary); 491 } 492 493 SECTION("filled binary") 494 { 495 json::binary_t b({1, 2, 3}); 496 json j(b); 497 CHECK(j.type() == json::value_t::binary); 498 } 499 } 500 501 SECTION("create an integer number (explicit)") 502 { 503 SECTION("uninitialized value") 504 { 505 json::number_integer_t n{}; 506 json j(n); 507 CHECK(j.type() == json::value_t::number_integer); 508 } 509 510 SECTION("initialized value") 511 { 512 json::number_integer_t n(42); 513 json j(n); 514 CHECK(j.type() == json::value_t::number_integer); 515 } 516 } 517 518 SECTION("create an integer number (implicit)") 519 { 520 // reference objects 521 json::number_integer_t n_reference = 42; 522 json j_reference(n_reference); 523 json::number_unsigned_t n_unsigned_reference = 42; 524 json j_unsigned_reference(n_unsigned_reference); 525 526 SECTION("short") 527 { 528 short n = 42; 529 json j(n); 530 CHECK(j.type() == json::value_t::number_integer); 531 CHECK(j == j_reference); 532 } 533 534 SECTION("unsigned short") 535 { 536 unsigned short n = 42; 537 json j(n); 538 CHECK(j.type() == json::value_t::number_unsigned); 539 CHECK(j == j_unsigned_reference); 540 } 541 542 SECTION("int") 543 { 544 int n = 42; 545 json j(n); 546 CHECK(j.type() == json::value_t::number_integer); 547 CHECK(j == j_reference); 548 } 549 550 SECTION("unsigned int") 551 { 552 unsigned int n = 42; 553 json j(n); 554 CHECK(j.type() == json::value_t::number_unsigned); 555 CHECK(j == j_unsigned_reference); 556 } 557 558 SECTION("long") 559 { 560 long n = 42; 561 json j(n); 562 CHECK(j.type() == json::value_t::number_integer); 563 CHECK(j == j_reference); 564 } 565 566 SECTION("unsigned long") 567 { 568 unsigned long n = 42; 569 json j(n); 570 CHECK(j.type() == json::value_t::number_unsigned); 571 CHECK(j == j_unsigned_reference); 572 } 573 574 SECTION("long long") 575 { 576 long long n = 42; 577 json j(n); 578 CHECK(j.type() == json::value_t::number_integer); 579 CHECK(j == j_reference); 580 } 581 582 SECTION("unsigned long long") 583 { 584 unsigned long long n = 42; 585 json j(n); 586 CHECK(j.type() == json::value_t::number_unsigned); 587 CHECK(j == j_unsigned_reference); 588 } 589 590 SECTION("int8_t") 591 { 592 int8_t n = 42; 593 json j(n); 594 CHECK(j.type() == json::value_t::number_integer); 595 CHECK(j == j_reference); 596 } 597 598 SECTION("int16_t") 599 { 600 int16_t n = 42; 601 json j(n); 602 CHECK(j.type() == json::value_t::number_integer); 603 CHECK(j == j_reference); 604 } 605 606 SECTION("int32_t") 607 { 608 int32_t n = 42; 609 json j(n); 610 CHECK(j.type() == json::value_t::number_integer); 611 CHECK(j == j_reference); 612 } 613 614 SECTION("int64_t") 615 { 616 int64_t n = 42; 617 json j(n); 618 CHECK(j.type() == json::value_t::number_integer); 619 CHECK(j == j_reference); 620 } 621 622 SECTION("int_fast8_t") 623 { 624 int_fast8_t n = 42; 625 json j(n); 626 CHECK(j.type() == json::value_t::number_integer); 627 CHECK(j == j_reference); 628 } 629 630 SECTION("int_fast16_t") 631 { 632 int_fast16_t n = 42; 633 json j(n); 634 CHECK(j.type() == json::value_t::number_integer); 635 CHECK(j == j_reference); 636 } 637 638 SECTION("int_fast32_t") 639 { 640 int_fast32_t n = 42; 641 json j(n); 642 CHECK(j.type() == json::value_t::number_integer); 643 CHECK(j == j_reference); 644 } 645 646 SECTION("int_fast64_t") 647 { 648 int_fast64_t n = 42; 649 json j(n); 650 CHECK(j.type() == json::value_t::number_integer); 651 CHECK(j == j_reference); 652 } 653 654 SECTION("int_least8_t") 655 { 656 int_least8_t n = 42; 657 json j(n); 658 CHECK(j.type() == json::value_t::number_integer); 659 CHECK(j == j_reference); 660 } 661 662 SECTION("int_least16_t") 663 { 664 int_least16_t n = 42; 665 json j(n); 666 CHECK(j.type() == json::value_t::number_integer); 667 CHECK(j == j_reference); 668 } 669 670 SECTION("int_least32_t") 671 { 672 int_least32_t n = 42; 673 json j(n); 674 CHECK(j.type() == json::value_t::number_integer); 675 CHECK(j == j_reference); 676 } 677 678 SECTION("int_least64_t") 679 { 680 int_least64_t n = 42; 681 json j(n); 682 CHECK(j.type() == json::value_t::number_integer); 683 CHECK(j == j_reference); 684 } 685 686 SECTION("uint8_t") 687 { 688 uint8_t n = 42; 689 json j(n); 690 CHECK(j.type() == json::value_t::number_unsigned); 691 CHECK(j == j_unsigned_reference); 692 } 693 694 SECTION("uint16_t") 695 { 696 uint16_t n = 42; 697 json j(n); 698 CHECK(j.type() == json::value_t::number_unsigned); 699 CHECK(j == j_unsigned_reference); 700 } 701 702 SECTION("uint32_t") 703 { 704 uint32_t n = 42; 705 json j(n); 706 CHECK(j.type() == json::value_t::number_unsigned); 707 CHECK(j == j_unsigned_reference); 708 } 709 710 SECTION("uint64_t") 711 { 712 uint64_t n = 42; 713 json j(n); 714 CHECK(j.type() == json::value_t::number_unsigned); 715 CHECK(j == j_unsigned_reference); 716 } 717 718 SECTION("uint_fast8_t") 719 { 720 uint_fast8_t n = 42; 721 json j(n); 722 CHECK(j.type() == json::value_t::number_unsigned); 723 CHECK(j == j_unsigned_reference); 724 } 725 726 SECTION("uint_fast16_t") 727 { 728 uint_fast16_t n = 42; 729 json j(n); 730 CHECK(j.type() == json::value_t::number_unsigned); 731 CHECK(j == j_unsigned_reference); 732 } 733 734 SECTION("uint_fast32_t") 735 { 736 uint_fast32_t n = 42; 737 json j(n); 738 CHECK(j.type() == json::value_t::number_unsigned); 739 CHECK(j == j_unsigned_reference); 740 } 741 742 SECTION("uint_fast64_t") 743 { 744 uint_fast64_t n = 42; 745 json j(n); 746 CHECK(j.type() == json::value_t::number_unsigned); 747 CHECK(j == j_unsigned_reference); 748 } 749 750 SECTION("uint_least8_t") 751 { 752 uint_least8_t n = 42; 753 json j(n); 754 CHECK(j.type() == json::value_t::number_unsigned); 755 CHECK(j == j_unsigned_reference); 756 } 757 758 SECTION("uint_least16_t") 759 { 760 uint_least16_t n = 42; 761 json j(n); 762 CHECK(j.type() == json::value_t::number_unsigned); 763 CHECK(j == j_unsigned_reference); 764 } 765 766 SECTION("uint_least32_t") 767 { 768 uint_least32_t n = 42; 769 json j(n); 770 CHECK(j.type() == json::value_t::number_unsigned); 771 CHECK(j == j_unsigned_reference); 772 } 773 774 SECTION("uint_least64_t") 775 { 776 uint_least64_t n = 42; 777 json j(n); 778 CHECK(j.type() == json::value_t::number_unsigned); 779 CHECK(j == j_unsigned_reference); 780 } 781 782 SECTION("integer literal without suffix") 783 { 784 json j(42); 785 CHECK(j.type() == json::value_t::number_integer); 786 CHECK(j == j_reference); 787 } 788 789 SECTION("integer literal with u suffix") 790 { 791 json j(42u); 792 CHECK(j.type() == json::value_t::number_unsigned); 793 CHECK(j == j_unsigned_reference); 794 } 795 796 SECTION("integer literal with l suffix") 797 { 798 json j(42l); 799 CHECK(j.type() == json::value_t::number_integer); 800 CHECK(j == j_reference); 801 } 802 803 SECTION("integer literal with ul suffix") 804 { 805 json j(42ul); 806 CHECK(j.type() == json::value_t::number_unsigned); 807 CHECK(j == j_unsigned_reference); 808 } 809 810 SECTION("integer literal with ll suffix") 811 { 812 json j(42ll); 813 CHECK(j.type() == json::value_t::number_integer); 814 CHECK(j == j_reference); 815 } 816 817 SECTION("integer literal with ull suffix") 818 { 819 json j(42ull); 820 CHECK(j.type() == json::value_t::number_unsigned); 821 CHECK(j == j_unsigned_reference); 822 } 823 } 824 825 SECTION("create a floating-point number (explicit)") 826 { 827 SECTION("uninitialized value") 828 { 829 json::number_float_t n{}; 830 json j(n); 831 CHECK(j.type() == json::value_t::number_float); 832 } 833 834 SECTION("initialized value") 835 { 836 json::number_float_t n(42.23); 837 json j(n); 838 CHECK(j.type() == json::value_t::number_float); 839 } 840 841 SECTION("NaN") 842 { 843 // NaN is stored properly, but serialized to null 844 json::number_float_t n(std::numeric_limits<json::number_float_t>::quiet_NaN()); 845 json j(n); 846 CHECK(j.type() == json::value_t::number_float); 847 848 // check round trip of NaN 849 json::number_float_t d{j}; 850 CHECK((std::isnan(d) && std::isnan(n)) == true); 851 852 // check that NaN is serialized to null 853 CHECK(j.dump() == "null"); 854 } 855 856 SECTION("infinity") 857 { 858 // infinity is stored properly, but serialized to null 859 json::number_float_t n(std::numeric_limits<json::number_float_t>::infinity()); 860 json j(n); 861 CHECK(j.type() == json::value_t::number_float); 862 863 // check round trip of infinity 864 json::number_float_t d{j}; 865 CHECK(d == n); 866 867 // check that inf is serialized to null 868 CHECK(j.dump() == "null"); 869 } 870 } 871 872 SECTION("create a floating-point number (implicit)") 873 { 874 // reference object 875 json::number_float_t n_reference = 42.23; 876 json j_reference(n_reference); 877 878 SECTION("float") 879 { 880 float n = 42.23f; 881 json j(n); 882 CHECK(j.type() == json::value_t::number_float); 883 CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); 884 } 885 886 SECTION("double") 887 { 888 double n = 42.23; 889 json j(n); 890 CHECK(j.type() == json::value_t::number_float); 891 CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); 892 } 893 894 SECTION("long double") 895 { 896 long double n = 42.23l; 897 json j(n); 898 CHECK(j.type() == json::value_t::number_float); 899 CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); 900 } 901 902 SECTION("floating-point literal without suffix") 903 { 904 json j(42.23); 905 CHECK(j.type() == json::value_t::number_float); 906 CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); 907 } 908 909 SECTION("integer literal with f suffix") 910 { 911 json j(42.23f); 912 CHECK(j.type() == json::value_t::number_float); 913 CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); 914 } 915 916 SECTION("integer literal with l suffix") 917 { 918 json j(42.23l); 919 CHECK(j.type() == json::value_t::number_float); 920 CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); 921 } 922 } 923 924 SECTION("create a container (array or object) from an initializer list") 925 { 926 SECTION("empty initializer list") 927 { 928 SECTION("explicit") 929 { 930 json j(json::initializer_list_t {}); 931 CHECK(j.type() == json::value_t::object); 932 } 933 934 SECTION("implicit") 935 { 936 json j {}; 937 CHECK(j.type() == json::value_t::null); 938 } 939 } 940 941 SECTION("one element") 942 { 943 SECTION("array") 944 { 945 SECTION("explicit") 946 { 947 json j(json::initializer_list_t {json(json::array_t())}); 948 CHECK(j.type() == json::value_t::array); 949 } 950 951 SECTION("implicit") 952 { 953 json j {json::array_t()}; 954 CHECK(j.type() == json::value_t::array); 955 } 956 } 957 958 SECTION("object") 959 { 960 SECTION("explicit") 961 { 962 json j(json::initializer_list_t {json(json::object_t())}); 963 CHECK(j.type() == json::value_t::array); 964 } 965 966 SECTION("implicit") 967 { 968 json j {json::object_t()}; 969 CHECK(j.type() == json::value_t::array); 970 } 971 } 972 973 SECTION("string") 974 { 975 SECTION("explicit") 976 { 977 json j(json::initializer_list_t {json("Hello world")}); 978 CHECK(j.type() == json::value_t::array); 979 } 980 981 SECTION("implicit") 982 { 983 json j {"Hello world"}; 984 CHECK(j.type() == json::value_t::array); 985 } 986 } 987 988 SECTION("boolean") 989 { 990 SECTION("explicit") 991 { 992 json j(json::initializer_list_t {json(true)}); 993 CHECK(j.type() == json::value_t::array); 994 } 995 996 SECTION("implicit") 997 { 998 json j {true}; 999 CHECK(j.type() == json::value_t::array); 1000 } 1001 } 1002 1003 SECTION("number (integer)") 1004 { 1005 SECTION("explicit") 1006 { 1007 json j(json::initializer_list_t {json(1)}); 1008 CHECK(j.type() == json::value_t::array); 1009 } 1010 1011 SECTION("implicit") 1012 { 1013 json j {1}; 1014 CHECK(j.type() == json::value_t::array); 1015 } 1016 } 1017 1018 SECTION("number (unsigned)") 1019 { 1020 SECTION("explicit") 1021 { 1022 json j(json::initializer_list_t {json(1u)}); 1023 CHECK(j.type() == json::value_t::array); 1024 } 1025 1026 SECTION("implicit") 1027 { 1028 json j {1u}; 1029 CHECK(j.type() == json::value_t::array); 1030 } 1031 } 1032 1033 SECTION("number (floating-point)") 1034 { 1035 SECTION("explicit") 1036 { 1037 json j(json::initializer_list_t {json(42.23)}); 1038 CHECK(j.type() == json::value_t::array); 1039 } 1040 1041 SECTION("implicit") 1042 { 1043 json j {42.23}; 1044 CHECK(j.type() == json::value_t::array); 1045 } 1046 } 1047 } 1048 1049 SECTION("more elements") 1050 { 1051 SECTION("explicit") 1052 { 1053 json j(json::initializer_list_t {1, 1u, 42.23, true, nullptr, json::object_t(), json::array_t()}); 1054 CHECK(j.type() == json::value_t::array); 1055 } 1056 1057 SECTION("implicit") 1058 { 1059 json j {1, 1u, 42.23, true, nullptr, json::object_t(), json::array_t()}; 1060 CHECK(j.type() == json::value_t::array); 1061 } 1062 } 1063 1064 SECTION("implicit type deduction") 1065 { 1066 SECTION("object") 1067 { 1068 json j { {"one", 1}, {"two", 1u}, {"three", 2.2}, {"four", false} }; 1069 CHECK(j.type() == json::value_t::object); 1070 } 1071 1072 SECTION("array") 1073 { 1074 json j { {"one", 1}, {"two", 1u}, {"three", 2.2}, {"four", false}, 13 }; 1075 CHECK(j.type() == json::value_t::array); 1076 } 1077 } 1078 1079 SECTION("explicit type deduction") 1080 { 1081 SECTION("empty object") 1082 { 1083 json j = json::object(); 1084 CHECK(j.type() == json::value_t::object); 1085 } 1086 1087 SECTION("object") 1088 { 1089 json j = json::object({ {"one", 1}, {"two", 1u}, {"three", 2.2}, {"four", false} }); 1090 CHECK(j.type() == json::value_t::object); 1091 } 1092 1093 SECTION("object with error") 1094 { 1095 json _; 1096 CHECK_THROWS_AS(_ = json::object({ {"one", 1}, {"two", 1u}, {"three", 2.2}, {"four", false}, 13 }), 1097 json::type_error&); 1098 CHECK_THROWS_WITH(_ = json::object({ {"one", 1}, {"two", 1u}, {"three", 2.2}, {"four", false}, 13 }), 1099 "[json.exception.type_error.301] cannot create object from initializer list"); 1100 } 1101 1102 SECTION("empty array") 1103 { 1104 json j = json::array(); 1105 CHECK(j.type() == json::value_t::array); 1106 } 1107 1108 SECTION("array") 1109 { 1110 json j = json::array({ {"one", 1}, {"two", 1u}, {"three", 2.2}, {"four", false} }); 1111 CHECK(j.type() == json::value_t::array); 1112 } 1113 } 1114 1115 SECTION("move from initializer_list") 1116 { 1117 SECTION("string") 1118 { 1119 // This should break through any short string optimization in std::string 1120 std::string source(1024, '!'); 1121 const char* source_addr = source.data(); 1122 1123 SECTION("constructor with implicit types (array)") 1124 { 1125 json j = {std::move(source)}; 1126 CHECK(j[0].get_ref<std::string const&>().data() == source_addr); 1127 } 1128 1129 SECTION("constructor with implicit types (object)") 1130 { 1131 json j = {{"key", std::move(source)}}; 1132 CHECK(j["key"].get_ref<std::string const&>().data() == source_addr); 1133 } 1134 1135 SECTION("constructor with implicit types (object key)") 1136 { 1137 json j = {{std::move(source), 42}}; 1138 CHECK(j.get_ref<json::object_t&>().begin()->first.data() == source_addr); 1139 } 1140 } 1141 1142 SECTION("array") 1143 { 1144 json::array_t source = {1, 2, 3}; 1145 const json* source_addr = source.data(); 1146 1147 SECTION("constructor with implicit types (array)") 1148 { 1149 json j {std::move(source)}; 1150 CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr); 1151 } 1152 1153 SECTION("constructor with implicit types (object)") 1154 { 1155 json j {{"key", std::move(source)}}; 1156 CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr); 1157 } 1158 1159 SECTION("assignment with implicit types (array)") 1160 { 1161 json j = {std::move(source)}; 1162 CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr); 1163 } 1164 1165 SECTION("assignment with implicit types (object)") 1166 { 1167 json j = {{"key", std::move(source)}}; 1168 CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr); 1169 } 1170 } 1171 1172 SECTION("object") 1173 { 1174 json::object_t source = {{"hello", "world"}}; 1175 const json* source_addr = &source.at("hello"); 1176 1177 SECTION("constructor with implicit types (array)") 1178 { 1179 json j {std::move(source)}; 1180 CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr); 1181 } 1182 1183 SECTION("constructor with implicit types (object)") 1184 { 1185 json j {{"key", std::move(source)}}; 1186 CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr); 1187 } 1188 1189 SECTION("assignment with implicit types (array)") 1190 { 1191 json j = {std::move(source)}; 1192 CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr); 1193 } 1194 1195 SECTION("assignment with implicit types (object)") 1196 { 1197 json j = {{"key", std::move(source)}}; 1198 CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr); 1199 } 1200 } 1201 1202 SECTION("json") 1203 { 1204 json source {1, 2, 3}; 1205 const json* source_addr = &source[0]; 1206 1207 SECTION("constructor with implicit types (array)") 1208 { 1209 json j {std::move(source), {}}; 1210 CHECK(&j[0][0] == source_addr); 1211 } 1212 1213 SECTION("constructor with implicit types (object)") 1214 { 1215 json j {{"key", std::move(source)}}; 1216 CHECK(&j["key"][0] == source_addr); 1217 } 1218 1219 SECTION("assignment with implicit types (array)") 1220 { 1221 json j = {std::move(source), {}}; 1222 CHECK(&j[0][0] == source_addr); 1223 } 1224 1225 SECTION("assignment with implicit types (object)") 1226 { 1227 json j = {{"key", std::move(source)}}; 1228 CHECK(&j["key"][0] == source_addr); 1229 } 1230 } 1231 1232 } 1233 } 1234 1235 SECTION("create an array of n copies of a given value") 1236 { 1237 SECTION("cnt = 0") 1238 { 1239 json v = {1, "foo", 34.23, {1, 2, 3}, {{"A", 1}, {"B", 2u}}}; 1240 json arr(0, v); 1241 CHECK(arr.size() == 0); 1242 } 1243 1244 SECTION("cnt = 1") 1245 { 1246 json v = {1, "foo", 34.23, {1, 2, 3}, {{"A", 1}, {"B", 2u}}}; 1247 json arr(1, v); 1248 CHECK(arr.size() == 1); 1249 for (auto& x : arr) 1250 { 1251 CHECK(x == v); 1252 } 1253 } 1254 1255 SECTION("cnt = 3") 1256 { 1257 json v = {1, "foo", 34.23, {1, 2, 3}, {{"A", 1}, {"B", 2u}}}; 1258 json arr(3, v); 1259 CHECK(arr.size() == 3); 1260 for (auto& x : arr) 1261 { 1262 CHECK(x == v); 1263 } 1264 } 1265 } 1266 1267 SECTION("create a JSON container from an iterator range") 1268 { 1269 SECTION("object") 1270 { 1271 SECTION("json(begin(), end())") 1272 { 1273 { 1274 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}}; 1275 json j_new(jobject.begin(), jobject.end()); 1276 CHECK(j_new == jobject); 1277 } 1278 { 1279 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}}; 1280 json j_new(jobject.cbegin(), jobject.cend()); 1281 CHECK(j_new == jobject); 1282 } 1283 } 1284 1285 SECTION("json(begin(), begin())") 1286 { 1287 { 1288 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}}; 1289 json j_new(jobject.begin(), jobject.begin()); 1290 CHECK(j_new == json::object()); 1291 } 1292 { 1293 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}}; 1294 json j_new(jobject.cbegin(), jobject.cbegin()); 1295 CHECK(j_new == json::object()); 1296 } 1297 } 1298 1299 SECTION("construct from subrange") 1300 { 1301 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}}; 1302 json j_new(jobject.find("b"), jobject.find("e")); 1303 CHECK(j_new == json({{"b", 1}, {"c", 17u}, {"d", false}})); 1304 } 1305 1306 SECTION("incompatible iterators") 1307 { 1308 { 1309 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}}; 1310 json jobject2 = {{"a", "a"}, {"b", 1}, {"c", 17u}}; 1311 CHECK_THROWS_AS(json(jobject.begin(), jobject2.end()), json::invalid_iterator&); 1312 CHECK_THROWS_AS(json(jobject2.begin(), jobject.end()), json::invalid_iterator&); 1313 CHECK_THROWS_WITH(json(jobject.begin(), jobject2.end()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1314 CHECK_THROWS_WITH(json(jobject2.begin(), jobject.end()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1315 } 1316 { 1317 json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}}; 1318 json jobject2 = {{"a", "a"}, {"b", 1}, {"c", 17u}}; 1319 CHECK_THROWS_AS(json(jobject.cbegin(), jobject2.cend()), json::invalid_iterator&); 1320 CHECK_THROWS_AS(json(jobject2.cbegin(), jobject.cend()), json::invalid_iterator&); 1321 CHECK_THROWS_WITH(json(jobject.cbegin(), jobject2.cend()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1322 CHECK_THROWS_WITH(json(jobject2.cbegin(), jobject.cend()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1323 } 1324 } 1325 } 1326 1327 SECTION("array") 1328 { 1329 SECTION("json(begin(), end())") 1330 { 1331 { 1332 json jarray = {1, 2, 3, 4, 5}; 1333 json j_new(jarray.begin(), jarray.end()); 1334 CHECK(j_new == jarray); 1335 } 1336 { 1337 json jarray = {1, 2, 3, 4, 5}; 1338 json j_new(jarray.cbegin(), jarray.cend()); 1339 CHECK(j_new == jarray); 1340 } 1341 } 1342 1343 SECTION("json(begin(), begin())") 1344 { 1345 { 1346 json jarray = {1, 2, 3, 4, 5}; 1347 json j_new(jarray.begin(), jarray.begin()); 1348 CHECK(j_new == json::array()); 1349 } 1350 { 1351 json jarray = {1, 2, 3, 4, 5}; 1352 json j_new(jarray.cbegin(), jarray.cbegin()); 1353 CHECK(j_new == json::array()); 1354 } 1355 } 1356 1357 SECTION("construct from subrange") 1358 { 1359 { 1360 json jarray = {1, 2, 3, 4, 5}; 1361 json j_new(jarray.begin() + 1, jarray.begin() + 3); 1362 CHECK(j_new == json({2, 3})); 1363 } 1364 { 1365 json jarray = {1, 2, 3, 4, 5}; 1366 json j_new(jarray.cbegin() + 1, jarray.cbegin() + 3); 1367 CHECK(j_new == json({2, 3})); 1368 } 1369 } 1370 1371 SECTION("incompatible iterators") 1372 { 1373 { 1374 json jarray = {1, 2, 3, 4}; 1375 json jarray2 = {2, 3, 4, 5}; 1376 CHECK_THROWS_AS(json(jarray.begin(), jarray2.end()), json::invalid_iterator&); 1377 CHECK_THROWS_AS(json(jarray2.begin(), jarray.end()), json::invalid_iterator&); 1378 CHECK_THROWS_WITH(json(jarray.begin(), jarray2.end()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1379 CHECK_THROWS_WITH(json(jarray2.begin(), jarray.end()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1380 } 1381 { 1382 json jarray = {1, 2, 3, 4}; 1383 json jarray2 = {2, 3, 4, 5}; 1384 CHECK_THROWS_AS(json(jarray.cbegin(), jarray2.cend()), json::invalid_iterator&); 1385 CHECK_THROWS_AS(json(jarray2.cbegin(), jarray.cend()), json::invalid_iterator&); 1386 CHECK_THROWS_WITH(json(jarray.cbegin(), jarray2.cend()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1387 CHECK_THROWS_WITH(json(jarray2.cbegin(), jarray.cend()), "[json.exception.invalid_iterator.201] iterators are not compatible"); 1388 } 1389 } 1390 } 1391 1392 SECTION("other values") 1393 { 1394 SECTION("construct with two valid iterators") 1395 { 1396 SECTION("null") 1397 { 1398 { 1399 json j; 1400 CHECK_THROWS_AS(json(j.begin(), j.end()), json::invalid_iterator&); 1401 CHECK_THROWS_WITH(json(j.begin(), j.end()), 1402 "[json.exception.invalid_iterator.206] cannot construct with iterators from null"); 1403 } 1404 { 1405 json j; 1406 CHECK_THROWS_AS(json(j.cbegin(), j.cend()), json::invalid_iterator&); 1407 CHECK_THROWS_WITH(json(j.cbegin(), j.cend()), 1408 "[json.exception.invalid_iterator.206] cannot construct with iterators from null"); 1409 } 1410 } 1411 1412 SECTION("string") 1413 { 1414 { 1415 json j = "foo"; 1416 json j_new(j.begin(), j.end()); 1417 CHECK(j == j_new); 1418 } 1419 { 1420 json j = "bar"; 1421 json j_new(j.cbegin(), j.cend()); 1422 CHECK(j == j_new); 1423 } 1424 } 1425 1426 SECTION("number (boolean)") 1427 { 1428 { 1429 json j = false; 1430 json j_new(j.begin(), j.end()); 1431 CHECK(j == j_new); 1432 } 1433 { 1434 json j = true; 1435 json j_new(j.cbegin(), j.cend()); 1436 CHECK(j == j_new); 1437 } 1438 } 1439 1440 SECTION("number (integer)") 1441 { 1442 { 1443 json j = 17; 1444 json j_new(j.begin(), j.end()); 1445 CHECK(j == j_new); 1446 } 1447 { 1448 json j = 17; 1449 json j_new(j.cbegin(), j.cend()); 1450 CHECK(j == j_new); 1451 } 1452 } 1453 1454 SECTION("number (unsigned)") 1455 { 1456 { 1457 json j = 17u; 1458 json j_new(j.begin(), j.end()); 1459 CHECK(j == j_new); 1460 } 1461 { 1462 json j = 17u; 1463 json j_new(j.cbegin(), j.cend()); 1464 CHECK(j == j_new); 1465 } 1466 } 1467 1468 SECTION("number (floating point)") 1469 { 1470 { 1471 json j = 23.42; 1472 json j_new(j.begin(), j.end()); 1473 CHECK(j == j_new); 1474 } 1475 { 1476 json j = 23.42; 1477 json j_new(j.cbegin(), j.cend()); 1478 CHECK(j == j_new); 1479 } 1480 } 1481 1482 SECTION("binary") 1483 { 1484 { 1485 json j = json::binary({1, 2, 3}); 1486 json j_new(j.begin(), j.end()); 1487 CHECK((j == j_new)); 1488 } 1489 { 1490 json j = json::binary({1, 2, 3}); 1491 json j_new(j.cbegin(), j.cend()); 1492 CHECK((j == j_new)); 1493 } 1494 } 1495 } 1496 1497 SECTION("construct with two invalid iterators") 1498 { 1499 SECTION("string") 1500 { 1501 { 1502 json j = "foo"; 1503 CHECK_THROWS_AS(json(j.end(), j.end()), json::invalid_iterator&); 1504 CHECK_THROWS_AS(json(j.begin(), j.begin()), json::invalid_iterator&); 1505 CHECK_THROWS_WITH(json(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range"); 1506 CHECK_THROWS_WITH(json(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1507 } 1508 { 1509 json j = "bar"; 1510 CHECK_THROWS_AS(json(j.cend(), j.cend()), json::invalid_iterator&); 1511 CHECK_THROWS_AS(json(j.cbegin(), j.cbegin()), json::invalid_iterator&); 1512 CHECK_THROWS_WITH(json(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range"); 1513 CHECK_THROWS_WITH(json(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1514 } 1515 } 1516 1517 SECTION("number (boolean)") 1518 { 1519 { 1520 json j = false; 1521 CHECK_THROWS_AS(json(j.end(), j.end()), json::invalid_iterator&); 1522 CHECK_THROWS_AS(json(j.begin(), j.begin()), json::invalid_iterator&); 1523 CHECK_THROWS_WITH(json(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range"); 1524 CHECK_THROWS_WITH(json(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1525 } 1526 { 1527 json j = true; 1528 CHECK_THROWS_AS(json(j.cend(), j.cend()), json::invalid_iterator&); 1529 CHECK_THROWS_AS(json(j.cbegin(), j.cbegin()), json::invalid_iterator&); 1530 CHECK_THROWS_WITH(json(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range"); 1531 CHECK_THROWS_WITH(json(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1532 } 1533 } 1534 1535 SECTION("number (integer)") 1536 { 1537 { 1538 json j = 17; 1539 CHECK_THROWS_AS(json(j.end(), j.end()), json::invalid_iterator&); 1540 CHECK_THROWS_AS(json(j.begin(), j.begin()), json::invalid_iterator&); 1541 CHECK_THROWS_WITH(json(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range"); 1542 CHECK_THROWS_WITH(json(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1543 } 1544 { 1545 json j = 17; 1546 CHECK_THROWS_AS(json(j.cend(), j.cend()), json::invalid_iterator&); 1547 CHECK_THROWS_AS(json(j.cbegin(), j.cbegin()), json::invalid_iterator&); 1548 CHECK_THROWS_WITH(json(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range"); 1549 CHECK_THROWS_WITH(json(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1550 } 1551 } 1552 1553 SECTION("number (integer)") 1554 { 1555 { 1556 json j = 17u; 1557 CHECK_THROWS_AS(json(j.end(), j.end()), json::invalid_iterator&); 1558 CHECK_THROWS_AS(json(j.begin(), j.begin()), json::invalid_iterator&); 1559 CHECK_THROWS_WITH(json(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range"); 1560 CHECK_THROWS_WITH(json(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1561 } 1562 { 1563 json j = 17u; 1564 CHECK_THROWS_AS(json(j.cend(), j.cend()), json::invalid_iterator&); 1565 CHECK_THROWS_AS(json(j.cbegin(), j.cbegin()), json::invalid_iterator&); 1566 CHECK_THROWS_WITH(json(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range"); 1567 CHECK_THROWS_WITH(json(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1568 } 1569 } 1570 1571 SECTION("number (floating point)") 1572 { 1573 { 1574 json j = 23.42; 1575 CHECK_THROWS_AS(json(j.end(), j.end()), json::invalid_iterator&); 1576 CHECK_THROWS_AS(json(j.begin(), j.begin()), json::invalid_iterator&); 1577 CHECK_THROWS_WITH(json(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range"); 1578 CHECK_THROWS_WITH(json(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1579 } 1580 { 1581 json j = 23.42; 1582 CHECK_THROWS_AS(json(j.cend(), j.cend()), json::invalid_iterator&); 1583 CHECK_THROWS_AS(json(j.cbegin(), j.cbegin()), json::invalid_iterator&); 1584 CHECK_THROWS_WITH(json(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range"); 1585 CHECK_THROWS_WITH(json(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range"); 1586 } 1587 } 1588 } 1589 } 1590 } 1591 } 1592