test.hpp 22.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//

#ifndef BOOST_JSON_TEST_HPP
#define BOOST_JSON_TEST_HPP

#include <boost/json/basic_parser.hpp>
#include <boost/json/value.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/storage_ptr.hpp>
#include <boost/json/detail/format.hpp>

#include <cstddef>
#include <iterator>
#include <memory>
#include <type_traits>

#include "test_suite.hpp"

BOOST_JSON_NS_BEGIN

//----------------------------------------------------------

struct test_failure : std::exception
{
    virtual
    char const*
    what() const noexcept override
    {
        return "test failure";
    }
};

struct fail_resource
    : memory_resource
{
    std::size_t fail_max = 0;
    std::size_t fail = 0;
    std::size_t nalloc = 0;
    std::size_t bytes = 0;

    ~fail_resource()
    {
        BOOST_TEST(nalloc == 0);
    }

    void*
    do_allocate(
        std::size_t n,
        std::size_t) override
    {
        if(++fail == fail_max)
        {
            ++fail_max;
            fail = 0;
            throw test_failure{};
        }
        auto p = ::operator new(n);
        ++nalloc;
        bytes += n;
        return p;
    }

    void
    do_deallocate(
        void* p,
        std::size_t n,
        std::size_t) noexcept override
    {
        if(BOOST_TEST(nalloc > 0))
            --nalloc;
        bytes -= n;
        ::operator delete(p);
    }

    bool
    do_is_equal(
        memory_resource const& mr) const noexcept override
    {
        return this == &mr;
    }
};

template<class F>
void
fail_loop(F&& f)
{
    fail_resource ss;
    ss.fail_max = 1;
    while(ss.fail < 200)
    {
        try
        {
            f(&ss);
        }
        catch(test_failure const&)
        {
            continue;
        }
        break;
    }
    BOOST_TEST(ss.fail < 200);
}

//----------------------------------------------------------

struct unique_resource
    : memory_resource
{
    unique_resource() = default;

    void*
    do_allocate(
        std::size_t n,
        std::size_t) override
    {
        return ::operator new(n);
    }

    void
    do_deallocate(
        void* p,
        std::size_t,
        std::size_t) noexcept override
    {
        return ::operator delete(p);
    }

    bool
    do_is_equal(
        memory_resource const& mr) const noexcept override
    {
        return this == &mr;
    }
};

//----------------------------------------------------------

// The null parser discards all the data

class null_parser
{
    struct handler
    {
        constexpr static std::size_t max_object_size = std::size_t(-1);
        constexpr static std::size_t max_array_size = std::size_t(-1);
        constexpr static std::size_t max_key_size = std::size_t(-1);
        constexpr static std::size_t max_string_size = std::size_t(-1);

        bool on_document_begin( error_code& ) { return true; }
        bool on_document_end( error_code& ) { return true; }
        bool on_object_begin( error_code& ) { return true; }
        bool on_object_end( std::size_t, error_code& ) { return true; }
        bool on_array_begin( error_code& ) { return true; }
        bool on_array_end( std::size_t, error_code& ) { return true; }
        bool on_key_part( string_view, std::size_t, error_code& ) { return true; }
        bool on_key( string_view, std::size_t, error_code& ) { return true; }
        bool on_string_part( string_view, std::size_t, error_code& ) { return true; }
        bool on_string( string_view, std::size_t, error_code& ) { return true; }
        bool on_number_part( string_view, error_code&) { return true; }
        bool on_int64( std::int64_t, string_view, error_code& ) { return true; }
        bool on_uint64( std::uint64_t, string_view, error_code& ) { return true; }
        bool on_double( double, string_view, error_code& ) { return true; }
        bool on_bool( bool, error_code& ) { return true; }
        bool on_null( error_code& ) { return true; }
        bool on_comment_part( string_view, error_code& ) { return true; }
        bool on_comment( string_view, error_code& ) { return true; }
    };

    basic_parser<handler> p_;

public:
    null_parser()
        : p_(parse_options())
    {
    }

    explicit
    null_parser(parse_options po)
        : p_(po)
    {
    }

    void
    reset()
    {
        p_.reset();
    }

    std::size_t
    write(
        char const* data,
        std::size_t size,
        error_code& ec)
    {
        auto const n = p_.write_some(
            false, data, size, ec);
        if(! ec && n < size)
            ec = error::extra_data;
        return n;
    }
};

//----------------------------------------------------------

class fail_parser
{
    struct handler
    {
        constexpr static std::size_t max_object_size = std::size_t(-1);
        constexpr static std::size_t max_array_size = std::size_t(-1);
        constexpr static std::size_t max_key_size = std::size_t(-1);
        constexpr static std::size_t max_string_size = std::size_t(-1);

        std::size_t n;

        handler()
            : n(std::size_t(-1))
        {
        }

        bool
        maybe_fail(error_code& ec)
        {
            if(n && --n > 0)
                return true;
            ec = error::test_failure;
            return false;
        }

        bool
        on_document_begin(
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_document_end(
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_object_begin(
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_object_end(
            std::size_t,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_array_begin(
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_array_end(
            std::size_t,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_key_part(
            string_view,
            std::size_t,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_key(
            string_view,
            std::size_t,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_string_part(
            string_view,
            std::size_t,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_string(
            string_view,
            std::size_t,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_number_part(
            string_view,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_int64(
            int64_t,
            string_view,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_uint64(
            uint64_t,
            string_view,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_double(
            double,
            string_view,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_bool(
            bool,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_null(error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_comment_part(
            string_view,
            error_code& ec)
        {
            return maybe_fail(ec);
        }

        bool
        on_comment(
            string_view,
            error_code& ec)
        {
            return maybe_fail(ec);
        }
    };

    basic_parser<handler> p_;

public:
    fail_parser()
        : p_(parse_options())
    {
    }

    explicit
    fail_parser(
        std::size_t n,
        parse_options po = parse_options())
        : p_(po)
    {
        p_.handler().n = n;
    }

    explicit
    fail_parser(parse_options po)
        : p_(po)
    {
    }

    void
    reset()
    {
        p_.reset();
    }

    bool
    done() const noexcept
    {
        return p_.done();
    }

    std::size_t
    write_some(
        bool more,
        char const* data,
        std::size_t size,
        error_code& ec)
    {
        return p_.write_some(
            more, data, size, ec);
    }

    std::size_t
    write(
        bool more,
        char const* data,
        std::size_t size,
        error_code& ec)
    {
        auto const n = p_.write_some(
            more, data, size, ec);
        if(! ec && n < size)
            ec = error::extra_data;
        return n;
    }
};

//----------------------------------------------------------

struct test_exception
    : std::exception
{
    char const*
    what() const noexcept
    {
        return "test exception";
    }
};

// Exercises every exception path
class throw_parser
{
    struct handler
    {
        constexpr static std::size_t max_object_size = std::size_t(-1);
        constexpr static std::size_t max_array_size = std::size_t(-1);
        constexpr static std::size_t max_key_size = std::size_t(-1);
        constexpr static std::size_t max_string_size = std::size_t(-1);

        std::size_t n;

        handler()
            : n(std::size_t(-1))
        {
        }

        bool
        maybe_throw()
        {
            if(n && --n > 0)
                return true;
            throw test_exception{};
        }

        bool
        on_document_begin(
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_document_end(
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_object_begin(
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_object_end(
            std::size_t,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_array_begin(
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_array_end(
            std::size_t,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_key_part(
            string_view,
            std::size_t,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_key(
            string_view,
            std::size_t,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_string_part(
            string_view,
            std::size_t,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_string(
            string_view,
            std::size_t,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_number_part(
            string_view,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_int64(
            int64_t,
            string_view,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_uint64(
            uint64_t,
            string_view,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_double(
            double,
            string_view,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_bool(
            bool,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_null(error_code&)
        {
            return maybe_throw();
        }

        bool
        on_comment_part(
            string_view,
            error_code&)
        {
            return maybe_throw();
        }

        bool
        on_comment(
            string_view,
            error_code&)
        {
            return maybe_throw();
        }
    };

    basic_parser<handler> p_;

public:
    throw_parser()
        : p_(parse_options())
    {
    }

    explicit
    throw_parser(
        std::size_t n,
        parse_options po = parse_options())
        : p_(po)
    {
        p_.handler().n = n;
    }

    explicit
    throw_parser(parse_options po)
        : p_(po)
    {
    }

    void
    reset() noexcept
    {
        p_.reset();
    }

    std::size_t
    write(
        bool more,
        char const* data,
        std::size_t size,
        error_code& ec)
    {
        auto const n = p_.write_some(
            more, data, size, ec);
        if(! ec && n < size)
            ec = error::extra_data;
        return n;
    }
};

//----------------------------------------------------------

// wrap an iterator to make an input iterator
template<class FwdIt>
class input_iterator
{
    FwdIt it_;

public:
    using value_type = typename std::iterator_traits<FwdIt>::value_type;
    using pointer = typename std::iterator_traits<FwdIt>::pointer;
    using reference = typename std::iterator_traits<FwdIt>::reference;
    using difference_type = typename std::iterator_traits<FwdIt>::difference_type;
    using iterator_category = std::input_iterator_tag;

    input_iterator() = default;
    input_iterator(input_iterator const&) = default;
    input_iterator& operator=(
        input_iterator const&) = default;

    input_iterator(FwdIt it)
        : it_(it)
    {
    }

    input_iterator&
    operator++() noexcept
    {
        ++it_;
        return *this;
    }

    input_iterator
    operator++(int) noexcept
    {
        auto tmp = *this;
        ++*this;
        return tmp;
    }

    pointer
    operator->() const noexcept
    {
        return it_.operator->();
    }

    reference
    operator*() const noexcept
    {
        return *it_;
    }

    bool
    operator==(input_iterator other) const noexcept
    {
        return it_ == other.it_;
    }

    bool
    operator!=(input_iterator other) const noexcept
    {
        return it_ != other.it_;
    }
};

template<class FwdIt>
input_iterator<FwdIt>
make_input_iterator(FwdIt it)
{
    return input_iterator<FwdIt>(it);
}

//----------------------------------------------------------

inline
bool
equal_storage(
    value const& v,
    storage_ptr const& sp);

inline
bool
equal_storage(
    object const& o,
    storage_ptr const& sp)
{
    if(*o.storage() != *sp)
        return false;
    for(auto const& e : o)
        if(! equal_storage(e.value(), sp))
            return false;
    return true;
}

inline
bool
equal_storage(
    array const& a,
    storage_ptr const& sp)
{
    if(*a.storage() != *sp)
        return false;
    for(auto const& v : a)
        if(! equal_storage(v, sp))
            return false;
    return true;
}

bool
equal_storage(
    value const& v,
    storage_ptr const& sp)
{
    switch(v.kind())
    {
    case json::kind::object:
        if(*v.as_object().storage() != *sp)
            return false;
        return equal_storage(v.as_object(), sp);

    case json::kind::array:
        if(*v.as_array().storage() != *sp)
            return false;
        return equal_storage(v.as_array(), sp);

    case json::kind::string:
        return *v.as_string().storage() == *sp;

    case json::kind::int64:
    case json::kind::uint64:
    case json::kind::double_:
    case json::kind::bool_:
    case json::kind::null:
    break;
    }

    return *v.storage() == *sp;
}

inline
void
check_storage(
    object const& o,
    storage_ptr const& sp)
{
    BOOST_TEST(equal_storage(o, sp));
}

inline
void
check_storage(
    array const& a,
    storage_ptr const& sp)
{
    BOOST_TEST(equal_storage(a, sp));
}

inline
void
check_storage(
    value const& v,
    storage_ptr const& sp)
{
    BOOST_TEST(equal_storage(v, sp));
}

//----------------------------------------------------------

namespace detail {

inline
void
to_string_test(
    string& dest,
    json::value const& jv)
{
    switch(jv.kind())
    {
    case kind::object:
    {
        dest.push_back('{');
        auto const& obj(
            jv.get_object());
        auto it = obj.begin();
        if(it != obj.end())
        {
            goto obj_first;
            while(it != obj.end())
            {
                dest.push_back(',');
            obj_first:
                dest.push_back('\"');
                dest.append(it->key());
                dest.push_back('\"');
                dest.push_back(':');
                to_string_test(
                    dest, it->value());
                ++it;
            }
        }
        dest.push_back('}');
        break;
    }

    case kind::array:
    {
        dest.push_back('[');
        auto const& arr(
            jv.get_array());
        auto it = arr.begin();
        if(it != arr.end())
        {
            goto arr_first;
            while(it != arr.end())
            {
                dest.push_back(',');
            arr_first:
                to_string_test(
                    dest, *it);
                ++it;
            }
        }
        dest.push_back(']');
        break;
    }

    case kind::string:
    #if 1
        // safe, but doesn't handle escsapes
        dest.push_back('\"');
        dest.append(jv.get_string());
        dest.push_back('\"');
    #else
        dest.append(serialize(jv));
    #endif
        break;

    case kind::int64:
    {
        char buf[detail::max_number_chars];
        auto const n =
            detail::format_int64(
                buf, jv.as_int64());
        dest.append(string_view(buf).substr(0, n));
        break;
    }

    case kind::uint64:
    {
        char buf[detail::max_number_chars];
        auto const n =
            detail::format_uint64(
                buf, jv.as_uint64());
        dest.append(string_view(buf).substr(0, n));
        break;
    }

    case kind::double_:
    {
        char buf[detail::max_number_chars];
        auto const n =
            detail::format_double(
                buf, jv.as_double());
        dest.append(string_view(buf).substr(0, n));
        break;
    }

    case kind::bool_:
        if(jv.as_bool())
            dest.append("true");
        else
            dest.append("false");
        break;

    case kind::null:
        dest.append("null");
        break;

    default:
        // should never get here
        dest.append("?");
        break;
    }
}

} // detail

inline
string
to_string_test(
    json::value const& jv)
{
    string s;
    s.reserve(1024);
    detail::to_string_test(s, jv);
    return s;
}

//----------------------------------------------------------

inline
bool
equal(
    value const& lhs,
    value const& rhs)
{
    if(lhs.kind() != rhs.kind())
        return false;
    switch(lhs.kind())
    {
    case kind::object:
    {
        auto const& obj1 =
            lhs.get_object();
        auto const& obj2 =
            rhs.get_object();
        auto n = obj1.size();
        if(obj2.size() != n)
            return false;
        auto it1 = obj1.begin();
        auto it2 = obj2.begin();
        while(n--)
        {
            if( it1->key() !=
                it2->key())
                return false;
            if(! equal(
                it1->value(),
                it2->value()))
                return false;
            ++it1;
            ++it2;
        }
        return true;
    }

    case kind::array:
    {
        auto const& arr1 =
            lhs.get_array();
        auto const& arr2 =
            rhs.get_array();
        auto n = arr1.size();
        if(arr2.size() != n)
            return false;
        auto it1 = arr1.begin();
        auto it2 = arr2.begin();
        while(n--)
            if(! equal(*it1++, *it2++))
                return false;
        return true;
    }

    case kind::string:
        return
            lhs.get_string() ==
            rhs.get_string();

    case kind::double_:
        return
            lhs.get_double() ==
            rhs.get_double();

    case kind::int64:
        return
            lhs.get_int64() ==
            rhs.get_int64();

    case kind::uint64:
        return
            lhs.get_uint64() ==
            rhs.get_uint64();

    case kind::bool_:
        return
            lhs.get_bool() ==
            rhs.get_bool();

    case kind::null:
        return true;
    }

    return false;
}

template<typename T>
inline
bool
check_hash_equal(
    T const& lhs,
    T const& rhs)
{
    if(lhs == rhs){
        return (std::hash<T>{}(lhs) == std::hash<T>{}(rhs));
    }
    return false; // ensure lhs == rhs intention
}

template<typename T, typename U>
inline
bool
check_hash_equal(
    T const& lhs,
    U const& rhs)
{
    if(lhs == rhs){
        return (std::hash<value>{}(lhs) == std::hash<value>{}(rhs));
    }
    return false; // ensure lhs == rhs intention
}

template<typename T>
inline
bool
expect_hash_not_equal(
    T const& lhs,
    T const& rhs)
{
    if(std::hash<T>{}(lhs) != std::hash<T>{}(rhs)){
        return lhs != rhs;
    }
    return true; // pass if hash values collide
}

template<typename T, typename U>
inline
bool
expect_hash_not_equal(
    T const& lhs,
    U const& rhs)
{
    if(std::hash<value>{}(lhs) != std::hash<value>{}(rhs)){
        return lhs != rhs;
    }
    return true; // pass if hash values collide
}
//----------------------------------------------------------

namespace detail {

inline
void
check_array_impl(int, value const&)
{
}

template<class Arg>
void
check_array_impl(
    int i, value const& jv,
    Arg const& arg)
{
    BOOST_TEST(equal(jv.at(i), arg));
}

template<
    class Arg0,
    class Arg1,
    class... Argn>
void
check_array_impl(
    int i, value const& jv,
    Arg0 const& arg0,
    Arg1 const& arg1,
    Argn const&... argn)
{
    BOOST_TEST(equal(jv.at(i), arg0));
    BOOST_TEST(equal(jv.at(i + 1), arg1));
    check_array_impl(i + 2, jv, argn...);
}

} // detail

template<class... Argn>
static
void
check_array(
    value const& jv,
    Argn const&... argn)
{
    if(! BOOST_TEST(jv.is_array()))
        return;
    if(! BOOST_TEST(sizeof...(argn) ==
        jv.get_array().size()))
        return;
    detail::check_array_impl(0, jv, argn...);
}

//----------------------------------------------------------

BOOST_JSON_NS_END

#endif