2022-07-20 13:38:07 +03:00
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++ (supporting code)
2022-08-02 00:27:58 +03:00
// | | |__ | | | | | | version 3.11.1
2022-07-20 13:38:07 +03:00
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
2016-08-04 23:51:08 +03:00
2022-05-29 14:08:06 +03:00
// cmake/test.cmake selects the C++ standard versions with which to build a
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
// When using macros that are only defined for particular versions of the standard
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
// version macro in a comment close by, like this:
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
2019-01-13 19:41:21 +03:00
# include "doctest_compatibility.h"
2016-08-04 23:51:08 +03:00
2018-01-29 13:21:11 +03:00
# include <nlohmann/json.hpp>
2016-08-04 23:51:08 +03:00
using nlohmann : : json ;
2022-05-29 14:08:06 +03:00
# if JSON_HAS_RANGES
# include <algorithm>
# include <ranges>
# endif
2016-08-04 23:51:08 +03:00
TEST_CASE ( " iterators 2 " )
{
SECTION ( " iterator comparisons " )
{
json j_values = { nullptr , true , 42 , 42u , 23.23 , { { " one " , 1 } , { " two " , 2 } } , { 1 , 2 , 3 , 4 , 5 } , " Hello, world " } ;
for ( json & j : j_values )
{
auto it1 = j . begin ( ) ;
auto it2 = j . begin ( ) ;
auto it3 = j . begin ( ) ;
+ + it2 ;
+ + it3 ;
+ + it3 ;
auto it1_c = j . cbegin ( ) ;
auto it2_c = j . cbegin ( ) ;
auto it3_c = j . cbegin ( ) ;
+ + it2_c ;
+ + it3_c ;
+ + it3_c ;
// comparison: equal
{
CHECK ( it1 = = it1 ) ;
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1 = = it2 ) ) ;
CHECK ( ! ( it1 = = it3 ) ) ;
CHECK ( ! ( it2 = = it3 ) ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it1_c = = it1_c ) ;
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1_c = = it2_c ) ) ;
CHECK ( ! ( it1_c = = it3_c ) ) ;
CHECK ( ! ( it2_c = = it3_c ) ) ;
2016-08-04 23:51:08 +03:00
}
// comparison: not equal
{
// check definition
2020-06-03 22:22:07 +03:00
CHECK ( ( it1 ! = it1 ) = = ! ( it1 = = it1 ) ) ;
CHECK ( ( it1 ! = it2 ) = = ! ( it1 = = it2 ) ) ;
CHECK ( ( it1 ! = it3 ) = = ! ( it1 = = it3 ) ) ;
CHECK ( ( it2 ! = it3 ) = = ! ( it2 = = it3 ) ) ;
CHECK ( ( it1_c ! = it1_c ) = = ! ( it1_c = = it1_c ) ) ;
CHECK ( ( it1_c ! = it2_c ) = = ! ( it1_c = = it2_c ) ) ;
CHECK ( ( it1_c ! = it3_c ) = = ! ( it1_c = = it3_c ) ) ;
CHECK ( ( it2_c ! = it3_c ) = = ! ( it2_c = = it3_c ) ) ;
2016-08-04 23:51:08 +03:00
}
// comparison: smaller
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1 < it1 ) ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it1 < it2 ) ;
CHECK ( it1 < it3 ) ;
CHECK ( it2 < it3 ) ;
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1_c < it1_c ) ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it1_c < it2_c ) ;
CHECK ( it1_c < it3_c ) ;
CHECK ( it2_c < it3_c ) ;
}
}
// comparison: less than or equal
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < = it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < = it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
// check definition
2020-06-03 22:22:07 +03:00
CHECK ( ( it1 < = it1 ) = = ! ( it1 < it1 ) ) ;
CHECK ( ( it1 < = it2 ) = = ! ( it2 < it1 ) ) ;
CHECK ( ( it1 < = it3 ) = = ! ( it3 < it1 ) ) ;
CHECK ( ( it2 < = it3 ) = = ! ( it3 < it2 ) ) ;
CHECK ( ( it1_c < = it1_c ) = = ! ( it1_c < it1_c ) ) ;
CHECK ( ( it1_c < = it2_c ) = = ! ( it2_c < it1_c ) ) ;
CHECK ( ( it1_c < = it3_c ) = = ! ( it3_c < it1_c ) ) ;
CHECK ( ( it2_c < = it3_c ) = = ! ( it3_c < it2_c ) ) ;
2016-08-04 23:51:08 +03:00
}
}
// comparison: greater than
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
// check definition
CHECK ( ( it1 > it1 ) = = ( it1 < it1 ) ) ;
CHECK ( ( it1 > it2 ) = = ( it2 < it1 ) ) ;
CHECK ( ( it1 > it3 ) = = ( it3 < it1 ) ) ;
CHECK ( ( it2 > it3 ) = = ( it3 < it2 ) ) ;
CHECK ( ( it1_c > it1_c ) = = ( it1_c < it1_c ) ) ;
CHECK ( ( it1_c > it2_c ) = = ( it2_c < it1_c ) ) ;
CHECK ( ( it1_c > it3_c ) = = ( it3_c < it1_c ) ) ;
CHECK ( ( it2_c > it3_c ) = = ( it3_c < it2_c ) ) ;
}
}
// comparison: greater than or equal
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > = it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > = it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
// check definition
2020-06-03 22:22:07 +03:00
CHECK ( ( it1 > = it1 ) = = ! ( it1 < it1 ) ) ;
CHECK ( ( it1 > = it2 ) = = ! ( it1 < it2 ) ) ;
CHECK ( ( it1 > = it3 ) = = ! ( it1 < it3 ) ) ;
CHECK ( ( it2 > = it3 ) = = ! ( it2 < it3 ) ) ;
CHECK ( ( it1_c > = it1_c ) = = ! ( it1_c < it1_c ) ) ;
CHECK ( ( it1_c > = it2_c ) = = ! ( it1_c < it2_c ) ) ;
CHECK ( ( it1_c > = it3_c ) = = ! ( it1_c < it3_c ) ) ;
CHECK ( ( it2_c > = it3_c ) = = ! ( it2_c < it3_c ) ) ;
2016-08-04 23:51:08 +03:00
}
}
}
// check exceptions if different objects are compared
for ( auto j : j_values )
{
for ( auto k : j_values )
{
if ( j ! = k )
{
2021-01-02 23:36:11 +03:00
# if JSON_DIAGNOSTICS
// the output differs in each loop, so we cannot fix a string for the expected exception
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( j . begin ( ) = = k . begin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( j . cbegin ( ) = = k . cbegin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( j . begin ( ) < k . begin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( j . cbegin ( ) < k . cbegin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
2021-01-02 23:36:11 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
}
}
}
SECTION ( " iterator arithmetic " )
{
2017-08-20 21:44:44 +03:00
json j_object = { { " one " , 1 } , { " two " , 2 } , { " three " , 3 } } ;
2016-08-04 23:51:08 +03:00
json j_array = { 1 , 2 , 3 , 4 , 5 , 6 } ;
json j_null = nullptr ;
json j_value = 42 ;
SECTION ( " addition and subtraction " )
{
SECTION ( " object " )
{
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( 1 + it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( 1 + it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
SECTION ( " array " )
{
{
auto it = j_array . begin ( ) ;
it + = 3 ;
CHECK ( ( j_array . begin ( ) + 3 ) = = it ) ;
2017-06-20 23:58:02 +03:00
CHECK ( ( 3 + j_array . begin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_array . begin ( ) ) ;
CHECK ( ( it - j_array . begin ( ) ) = = 3 ) ;
CHECK ( * it = = json ( 4 ) ) ;
it - = 2 ;
CHECK ( * it = = json ( 2 ) ) ;
}
{
auto it = j_array . cbegin ( ) ;
it + = 3 ;
CHECK ( ( j_array . cbegin ( ) + 3 ) = = it ) ;
2017-06-20 23:58:02 +03:00
CHECK ( ( 3 + j_array . cbegin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_array . cbegin ( ) ) ;
CHECK ( ( it - j_array . cbegin ( ) ) = = 3 ) ;
CHECK ( * it = = json ( 4 ) ) ;
it - = 2 ;
CHECK ( * it = = json ( 2 ) ) ;
}
}
SECTION ( " null " )
{
{
auto it = j_null . begin ( ) ;
it + = 3 ;
CHECK ( ( j_null . begin ( ) + 3 ) = = it ) ;
2017-06-20 23:58:02 +03:00
CHECK ( ( 3 + j_null . begin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_null . begin ( ) ) ;
CHECK ( ( it - j_null . begin ( ) ) = = 3 ) ;
CHECK ( it ! = j_null . end ( ) ) ;
it - = 3 ;
CHECK ( it = = j_null . end ( ) ) ;
}
{
auto it = j_null . cbegin ( ) ;
it + = 3 ;
CHECK ( ( j_null . cbegin ( ) + 3 ) = = it ) ;
2017-06-20 23:58:02 +03:00
CHECK ( ( 3 + j_null . cbegin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_null . cbegin ( ) ) ;
CHECK ( ( it - j_null . cbegin ( ) ) = = 3 ) ;
CHECK ( it ! = j_null . cend ( ) ) ;
it - = 3 ;
CHECK ( it = = j_null . cend ( ) ) ;
}
}
SECTION ( " value " )
{
{
auto it = j_value . begin ( ) ;
it + = 3 ;
CHECK ( ( j_value . begin ( ) + 3 ) = = it ) ;
2017-06-20 23:58:02 +03:00
CHECK ( ( 3 + j_value . begin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_value . begin ( ) ) ;
CHECK ( ( it - j_value . begin ( ) ) = = 3 ) ;
CHECK ( it ! = j_value . end ( ) ) ;
it - = 3 ;
CHECK ( * it = = json ( 42 ) ) ;
}
{
auto it = j_value . cbegin ( ) ;
it + = 3 ;
CHECK ( ( j_value . cbegin ( ) + 3 ) = = it ) ;
2017-06-20 23:58:02 +03:00
CHECK ( ( 3 + j_value . cbegin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_value . cbegin ( ) ) ;
CHECK ( ( it - j_value . cbegin ( ) ) = = 3 ) ;
CHECK ( it ! = j_value . cend ( ) ) ;
it - = 3 ;
CHECK ( * it = = json ( 42 ) ) ;
}
}
}
SECTION ( " subscript operator " )
{
SECTION ( " object " )
{
{
auto it = j_object . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.208] cannot use operator[] for object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.208] cannot use operator[] for object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_object . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.208] cannot use operator[] for object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.208] cannot use operator[] for object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
SECTION ( " array " )
{
{
auto it = j_array . begin ( ) ;
CHECK ( it [ 0 ] = = json ( 1 ) ) ;
CHECK ( it [ 1 ] = = json ( 2 ) ) ;
CHECK ( it [ 2 ] = = json ( 3 ) ) ;
CHECK ( it [ 3 ] = = json ( 4 ) ) ;
CHECK ( it [ 4 ] = = json ( 5 ) ) ;
CHECK ( it [ 5 ] = = json ( 6 ) ) ;
}
{
auto it = j_array . cbegin ( ) ;
CHECK ( it [ 0 ] = = json ( 1 ) ) ;
CHECK ( it [ 1 ] = = json ( 2 ) ) ;
CHECK ( it [ 2 ] = = json ( 3 ) ) ;
CHECK ( it [ 3 ] = = json ( 4 ) ) ;
CHECK ( it [ 4 ] = = json ( 5 ) ) ;
CHECK ( it [ 5 ] = = json ( 6 ) ) ;
}
}
SECTION ( " null " )
{
{
auto it = j_null . begin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_null . cbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
SECTION ( " value " )
{
{
auto it = j_value . begin ( ) ;
CHECK ( it [ 0 ] = = json ( 42 ) ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_value . cbegin ( ) ;
CHECK ( it [ 0 ] = = json ( 42 ) ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
}
}
SECTION ( " reverse iterator comparisons " )
{
json j_values = { nullptr , true , 42 , 42u , 23.23 , { { " one " , 1 } , { " two " , 2 } } , { 1 , 2 , 3 , 4 , 5 } , " Hello, world " } ;
for ( json & j : j_values )
{
auto it1 = j . rbegin ( ) ;
auto it2 = j . rbegin ( ) ;
auto it3 = j . rbegin ( ) ;
+ + it2 ;
+ + it3 ;
+ + it3 ;
auto it1_c = j . crbegin ( ) ;
auto it2_c = j . crbegin ( ) ;
auto it3_c = j . crbegin ( ) ;
+ + it2_c ;
+ + it3_c ;
+ + it3_c ;
// comparison: equal
{
CHECK ( it1 = = it1 ) ;
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1 = = it2 ) ) ;
CHECK ( ! ( it1 = = it3 ) ) ;
CHECK ( ! ( it2 = = it3 ) ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it1_c = = it1_c ) ;
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1_c = = it2_c ) ) ;
CHECK ( ! ( it1_c = = it3_c ) ) ;
CHECK ( ! ( it2_c = = it3_c ) ) ;
2016-08-04 23:51:08 +03:00
}
// comparison: not equal
{
// check definition
2020-06-03 22:22:07 +03:00
CHECK ( ( it1 ! = it1 ) = = ! ( it1 = = it1 ) ) ;
CHECK ( ( it1 ! = it2 ) = = ! ( it1 = = it2 ) ) ;
CHECK ( ( it1 ! = it3 ) = = ! ( it1 = = it3 ) ) ;
CHECK ( ( it2 ! = it3 ) = = ! ( it2 = = it3 ) ) ;
CHECK ( ( it1_c ! = it1_c ) = = ! ( it1_c = = it1_c ) ) ;
CHECK ( ( it1_c ! = it2_c ) = = ! ( it1_c = = it2_c ) ) ;
CHECK ( ( it1_c ! = it3_c ) = = ! ( it1_c = = it3_c ) ) ;
CHECK ( ( it2_c ! = it3_c ) = = ! ( it2_c = = it3_c ) ) ;
2016-08-04 23:51:08 +03:00
}
// comparison: smaller
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1 < it1 ) ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it1 < it2 ) ;
CHECK ( it1 < it3 ) ;
CHECK ( it2 < it3 ) ;
2020-06-03 22:22:07 +03:00
CHECK ( ! ( it1_c < it1_c ) ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it1_c < it2_c ) ;
CHECK ( it1_c < it3_c ) ;
CHECK ( it2_c < it3_c ) ;
}
}
// comparison: less than or equal
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < = it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 < = it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 < = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 < = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c < = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c < = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
// check definition
2020-06-03 22:22:07 +03:00
CHECK ( ( it1 < = it1 ) = = ! ( it1 < it1 ) ) ;
CHECK ( ( it1 < = it2 ) = = ! ( it2 < it1 ) ) ;
CHECK ( ( it1 < = it3 ) = = ! ( it3 < it1 ) ) ;
CHECK ( ( it2 < = it3 ) = = ! ( it3 < it2 ) ) ;
CHECK ( ( it1_c < = it1_c ) = = ! ( it1_c < it1_c ) ) ;
CHECK ( ( it1_c < = it2_c ) = = ! ( it2_c < it1_c ) ) ;
CHECK ( ( it1_c < = it3_c ) = = ! ( it3_c < it1_c ) ) ;
CHECK ( ( it2_c < = it3_c ) = = ! ( it3_c < it2_c ) ) ;
2016-08-04 23:51:08 +03:00
}
}
// comparison: greater than
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
// check definition
CHECK ( ( it1 > it1 ) = = ( it1 < it1 ) ) ;
CHECK ( ( it1 > it2 ) = = ( it2 < it1 ) ) ;
CHECK ( ( it1 > it3 ) = = ( it3 < it1 ) ) ;
CHECK ( ( it2 > it3 ) = = ( it3 < it2 ) ) ;
CHECK ( ( it1_c > it1_c ) = = ( it1_c < it1_c ) ) ;
CHECK ( ( it1_c > it2_c ) = = ( it2_c < it1_c ) ) ;
CHECK ( ( it1_c > it3_c ) = = ( it3_c < it1_c ) ) ;
CHECK ( ( it2_c > it3_c ) = = ( it3_c < it2_c ) ) ;
}
}
// comparison: greater than or equal
{
if ( j . type ( ) = = json : : value_t : : object )
{
2021-01-02 18:13:04 +03:00
# if JSON_DIAGNOSTICS
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > = it1 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it2 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it3 , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it1_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it2_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it3_c , " [json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it1 > = it1 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it2 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2 > = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1 > = it3 , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it1_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it2_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it2_c > = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it1_c > = it3_c , " [json.exception.invalid_iterator.213] cannot compare order of object iterators " , json : : invalid_iterator & ) ;
2021-01-02 18:13:04 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
else
{
// check definition
2020-06-03 22:22:07 +03:00
CHECK ( ( it1 > = it1 ) = = ! ( it1 < it1 ) ) ;
CHECK ( ( it1 > = it2 ) = = ! ( it1 < it2 ) ) ;
CHECK ( ( it1 > = it3 ) = = ! ( it1 < it3 ) ) ;
CHECK ( ( it2 > = it3 ) = = ! ( it2 < it3 ) ) ;
CHECK ( ( it1_c > = it1_c ) = = ! ( it1_c < it1_c ) ) ;
CHECK ( ( it1_c > = it2_c ) = = ! ( it1_c < it2_c ) ) ;
CHECK ( ( it1_c > = it3_c ) = = ! ( it1_c < it3_c ) ) ;
CHECK ( ( it2_c > = it3_c ) = = ! ( it2_c < it3_c ) ) ;
2016-08-04 23:51:08 +03:00
}
}
}
// check exceptions if different objects are compared
for ( auto j : j_values )
{
for ( auto k : j_values )
{
if ( j ! = k )
{
2021-01-02 23:36:11 +03:00
# if JSON_DIAGNOSTICS
// the output differs in each loop, so we cannot fix a string for the expected exception
# else
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( j . rbegin ( ) = = k . rbegin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( j . crbegin ( ) = = k . crbegin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( j . rbegin ( ) < k . rbegin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( j . crbegin ( ) < k . crbegin ( ) , " [json.exception.invalid_iterator.212] cannot compare iterators of different containers " , json : : invalid_iterator & ) ;
2021-01-02 23:36:11 +03:00
# endif
2016-08-04 23:51:08 +03:00
}
}
}
}
SECTION ( " reverse iterator arithmetic " )
{
2017-08-20 21:44:44 +03:00
json j_object = { { " one " , 1 } , { " two " , 2 } , { " three " , 3 } } ;
2016-08-04 23:51:08 +03:00
json j_array = { 1 , 2 , 3 , 4 , 5 , 6 } ;
json j_null = nullptr ;
json j_value = 42 ;
SECTION ( " addition and subtraction " )
{
SECTION ( " object " )
{
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it + 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( 1 + it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( 1 + it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - = 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - 1 , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2017-08-20 21:44:44 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it - it , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
SECTION ( " array " )
{
{
auto it = j_array . rbegin ( ) ;
it + = 3 ;
CHECK ( ( j_array . rbegin ( ) + 3 ) = = it ) ;
2017-05-29 15:39:27 +03:00
CHECK ( json : : reverse_iterator ( 3 + j_array . rbegin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_array . rbegin ( ) ) ;
2017-05-29 11:52:53 +03:00
CHECK ( ( it - j_array . rbegin ( ) ) = = 3 ) ;
2016-08-04 23:51:08 +03:00
CHECK ( * it = = json ( 3 ) ) ;
it - = 2 ;
CHECK ( * it = = json ( 5 ) ) ;
}
{
auto it = j_array . crbegin ( ) ;
it + = 3 ;
CHECK ( ( j_array . crbegin ( ) + 3 ) = = it ) ;
2017-05-29 15:39:27 +03:00
CHECK ( json : : const_reverse_iterator ( 3 + j_array . crbegin ( ) ) = = it ) ;
2016-08-04 23:51:08 +03:00
CHECK ( ( it - 3 ) = = j_array . crbegin ( ) ) ;
2017-05-29 11:52:53 +03:00
CHECK ( ( it - j_array . crbegin ( ) ) = = 3 ) ;
2016-08-04 23:51:08 +03:00
CHECK ( * it = = json ( 3 ) ) ;
it - = 2 ;
CHECK ( * it = = json ( 5 ) ) ;
}
}
SECTION ( " null " )
{
{
auto it = j_null . rbegin ( ) ;
it + = 3 ;
2017-05-29 12:43:45 +03:00
CHECK ( ( j_null . rbegin ( ) + 3 ) = = it ) ;
2017-05-29 15:39:27 +03:00
CHECK ( json : : reverse_iterator ( 3 + j_null . rbegin ( ) ) = = it ) ;
2017-05-29 12:43:45 +03:00
CHECK ( ( it - 3 ) = = j_null . rbegin ( ) ) ;
2017-05-29 11:52:53 +03:00
CHECK ( ( it - j_null . rbegin ( ) ) = = 3 ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it ! = j_null . rend ( ) ) ;
it - = 3 ;
CHECK ( it = = j_null . rend ( ) ) ;
}
{
auto it = j_null . crbegin ( ) ;
it + = 3 ;
2017-05-29 12:43:45 +03:00
CHECK ( ( j_null . crbegin ( ) + 3 ) = = it ) ;
2017-05-29 15:39:27 +03:00
CHECK ( json : : const_reverse_iterator ( 3 + j_null . crbegin ( ) ) = = it ) ;
2017-05-29 12:43:45 +03:00
CHECK ( ( it - 3 ) = = j_null . crbegin ( ) ) ;
2017-05-29 11:52:53 +03:00
CHECK ( ( it - j_null . crbegin ( ) ) = = 3 ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it ! = j_null . crend ( ) ) ;
it - = 3 ;
CHECK ( it = = j_null . crend ( ) ) ;
}
}
SECTION ( " value " )
{
{
auto it = j_value . rbegin ( ) ;
it + = 3 ;
2017-05-29 12:43:45 +03:00
CHECK ( ( j_value . rbegin ( ) + 3 ) = = it ) ;
2017-05-29 15:39:27 +03:00
CHECK ( json : : reverse_iterator ( 3 + j_value . rbegin ( ) ) = = it ) ;
2017-05-29 12:43:45 +03:00
CHECK ( ( it - 3 ) = = j_value . rbegin ( ) ) ;
2017-05-29 11:52:53 +03:00
CHECK ( ( it - j_value . rbegin ( ) ) = = 3 ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it ! = j_value . rend ( ) ) ;
it - = 3 ;
CHECK ( * it = = json ( 42 ) ) ;
}
{
auto it = j_value . crbegin ( ) ;
it + = 3 ;
2017-05-29 12:43:45 +03:00
CHECK ( ( j_value . crbegin ( ) + 3 ) = = it ) ;
2017-05-29 15:39:27 +03:00
CHECK ( json : : const_reverse_iterator ( 3 + j_value . crbegin ( ) ) = = it ) ;
2017-05-29 12:43:45 +03:00
CHECK ( ( it - 3 ) = = j_value . crbegin ( ) ) ;
2017-05-29 11:52:53 +03:00
CHECK ( ( it - j_value . crbegin ( ) ) = = 3 ) ;
2016-08-04 23:51:08 +03:00
CHECK ( it ! = j_value . crend ( ) ) ;
it - = 3 ;
CHECK ( * it = = json ( 42 ) ) ;
}
}
}
SECTION ( " subscript operator " )
{
SECTION ( " object " )
{
{
auto it = j_object . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_object . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.209] cannot use offsets with object iterators " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
SECTION ( " array " )
{
{
auto it = j_array . rbegin ( ) ;
CHECK ( it [ 0 ] = = json ( 6 ) ) ;
CHECK ( it [ 1 ] = = json ( 5 ) ) ;
CHECK ( it [ 2 ] = = json ( 4 ) ) ;
CHECK ( it [ 3 ] = = json ( 3 ) ) ;
CHECK ( it [ 4 ] = = json ( 2 ) ) ;
CHECK ( it [ 5 ] = = json ( 1 ) ) ;
}
{
auto it = j_array . crbegin ( ) ;
CHECK ( it [ 0 ] = = json ( 6 ) ) ;
CHECK ( it [ 1 ] = = json ( 5 ) ) ;
CHECK ( it [ 2 ] = = json ( 4 ) ) ;
CHECK ( it [ 3 ] = = json ( 3 ) ) ;
CHECK ( it [ 4 ] = = json ( 2 ) ) ;
CHECK ( it [ 5 ] = = json ( 1 ) ) ;
}
}
SECTION ( " null " )
{
{
auto it = j_null . rbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_null . crbegin ( ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 0 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
SECTION ( " value " )
{
{
auto it = j_value . rbegin ( ) ;
CHECK ( it [ 0 ] = = json ( 42 ) ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
{
auto it = j_value . crbegin ( ) ;
CHECK ( it [ 0 ] = = json ( 42 ) ) ;
2022-03-24 17:55:35 +03:00
CHECK_THROWS_WITH_AS ( it [ 1 ] , " [json.exception.invalid_iterator.214] cannot get value " , json : : invalid_iterator & ) ;
2016-08-04 23:51:08 +03:00
}
}
}
}
2022-05-29 14:08:06 +03:00
# if JSON_HAS_RANGES
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
SECTION ( " ranges " )
{
SECTION ( " concepts " )
{
using nlohmann : : detail : : iteration_proxy_value ;
CHECK ( std : : bidirectional_iterator < json : : iterator > ) ;
CHECK ( std : : input_iterator < iteration_proxy_value < json : : iterator > > ) ;
CHECK ( std : : is_same < json : : iterator , std : : ranges : : iterator_t < json > > : : value ) ;
CHECK ( std : : ranges : : bidirectional_range < json > ) ;
using nlohmann : : detail : : iteration_proxy ;
using items_type = decltype ( std : : declval < json & > ( ) . items ( ) ) ;
CHECK ( std : : is_same < items_type , iteration_proxy < json : : iterator > > : : value ) ;
CHECK ( std : : is_same < iteration_proxy_value < json : : iterator > , std : : ranges : : iterator_t < items_type > > : : value ) ;
CHECK ( std : : ranges : : input_range < items_type > ) ;
}
// libstdc++ algorithms don't work with Clang 15 (04/2022)
2022-06-21 17:57:47 +03:00
# if !DOCTEST_CLANG || (DOCTEST_CLANG && defined(__GLIBCXX__))
2022-05-29 14:08:06 +03:00
SECTION ( " algorithms " )
{
SECTION ( " copy " )
{
json j { " foo " , " bar " } ;
auto j_copied = json : : array ( ) ;
std : : ranges : : copy ( j , std : : back_inserter ( j_copied ) ) ;
CHECK ( j = = j_copied ) ;
}
SECTION ( " find_if " )
{
json j { 1 , 3 , 2 , 4 } ;
auto j_even = json : : array ( ) ;
# if JSON_USE_IMPLICIT_CONVERSIONS
auto it = std : : ranges : : find_if ( j , [ ] ( int v ) noexcept
{
return ( v % 2 ) = = 0 ;
} ) ;
# else
auto it = std : : ranges : : find_if ( j , [ ] ( const json & j ) noexcept
{
int v ;
j . get_to ( v ) ;
return ( v % 2 ) = = 0 ;
} ) ;
# endif
CHECK ( * it = = 2 ) ;
}
}
# endif
// libstdc++ views don't work with Clang 15 (04/2022)
// libc++ hides limited ranges implementation behind guard macro
2022-06-21 17:57:47 +03:00
# if !(DOCTEST_CLANG && (defined(__GLIBCXX__) || defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)))
2022-05-29 14:08:06 +03:00
SECTION ( " views " )
{
SECTION ( " reverse " )
{
json j { 1 , 2 , 3 , 4 , 5 } ;
json j_expected { 5 , 4 , 3 , 2 , 1 } ;
auto reversed = j | std : : views : : reverse ;
CHECK ( reversed = = j_expected ) ;
}
SECTION ( " transform " )
{
json j
{
{ " a_key " , " a_value " } ,
{ " b_key " , " b_value " } ,
{ " c_key " , " c_value " } ,
} ;
json j_expected { " a_key " , " b_key " , " c_key " } ;
auto transformed = j . items ( ) | std : : views : : transform ( [ ] ( const auto & item )
{
return item . key ( ) ;
} ) ;
auto j_transformed = json : : array ( ) ;
std : : ranges : : copy ( transformed , std : : back_inserter ( j_transformed ) ) ;
CHECK ( j_transformed = = j_expected ) ;
}
}
# endif
}
# endif
2016-08-04 23:51:08 +03:00
}