From be759c5051b828f77b59d5c67b9b53d7330e627e Mon Sep 17 00:00:00 2001 From: Dmitry Arkhipov Date: Sat, 22 Oct 2022 19:03:27 +0300 Subject: [PATCH] only throw system_error --- include/boost/json/array.hpp | 6 +- include/boost/json/detail/except.hpp | 18 +- include/boost/json/detail/impl/except.ipp | 44 +-- .../boost/json/detail/impl/string_impl.ipp | 50 +++- include/boost/json/detail/sbo_buffer.hpp | 5 +- include/boost/json/detail/value_to.hpp | 86 +++--- include/boost/json/error.hpp | 16 + include/boost/json/impl/array.hpp | 10 +- include/boost/json/impl/array.ipp | 15 +- include/boost/json/impl/error.ipp | 10 + include/boost/json/impl/null_resource.ipp | 4 +- include/boost/json/impl/object.hpp | 16 +- include/boost/json/impl/object.ipp | 10 +- include/boost/json/impl/static_resource.ipp | 4 +- include/boost/json/impl/string.ipp | 5 +- include/boost/json/impl/value.ipp | 4 + include/boost/json/object.hpp | 12 +- include/boost/json/string.hpp | 110 ++++--- include/boost/json/value.hpp | 134 +++++---- test/array.cpp | 30 +- test/error.cpp | 5 + test/limits.cpp | 108 +++---- test/object.cpp | 9 +- test/parse.cpp | 2 +- test/parser.cpp | 32 +- test/pointer.cpp | 22 +- test/serializer.cpp | 4 +- test/snippets.cpp | 4 +- test/stream_parser.cpp | 40 +-- test/string.cpp | 39 +-- test/value.cpp | 274 +++++++++--------- 31 files changed, 559 insertions(+), 569 deletions(-) diff --git a/include/boost/json/array.hpp b/include/boost/json/array.hpp index c511ec067..73b5e587e 100644 --- a/include/boost/json/array.hpp +++ b/include/boost/json/array.hpp @@ -593,14 +593,14 @@ class array Returns a reference to the element specified at location `pos`, with bounds checking. If `pos` is not within the range of the container, an exception - of type `std::out_of_range` is thrown. + of type @ref system_error is thrown. @par Complexity Constant. @param pos A zero-based index. - @throw std::out_of_range `pos >= size()` + @throw system_error `pos >= size()` */ /* @{ */ inline @@ -1070,7 +1070,7 @@ class array @param new_capacity The new capacity of the array. - @throw std::length_error `new_capacity > max_size()` + @throw system_error `new_capacity > max_size()` */ inline void diff --git a/include/boost/json/detail/except.hpp b/include/boost/json/detail/except.hpp index 8f23849a2..cdaf3f38f 100644 --- a/include/boost/json/detail/except.hpp +++ b/include/boost/json/detail/except.hpp @@ -20,11 +20,19 @@ namespace detail { BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; \ (ec).assign(e, &loc); -BOOST_JSON_DECL void BOOST_NORETURN throw_bad_alloc(source_location const& loc = BOOST_CURRENT_LOCATION); -BOOST_JSON_DECL void BOOST_NORETURN throw_invalid_argument(char const* what, source_location const& loc = BOOST_CURRENT_LOCATION); -BOOST_JSON_DECL void BOOST_NORETURN throw_length_error(char const* what, source_location const& loc = BOOST_CURRENT_LOCATION); -BOOST_JSON_DECL void BOOST_NORETURN throw_out_of_range(source_location const& loc = BOOST_CURRENT_LOCATION); -BOOST_JSON_DECL void BOOST_NORETURN throw_system_error(error_code const& ec, source_location const& loc = BOOST_CURRENT_LOCATION); +BOOST_JSON_DECL +void +BOOST_NORETURN +throw_system_error( + error_code const& ec, + source_location const& loc = BOOST_CURRENT_LOCATION); + +BOOST_JSON_DECL +void +BOOST_NORETURN +throw_system_error( + error e, + source_location const* loc); } // detail } // namespace json diff --git a/include/boost/json/detail/impl/except.ipp b/include/boost/json/detail/impl/except.ipp index 44ccaaa5f..72f7a7f8c 100644 --- a/include/boost/json/detail/impl/except.ipp +++ b/include/boost/json/detail/impl/except.ipp @@ -21,52 +21,26 @@ namespace json { namespace detail { void -throw_bad_alloc( - source_location const& loc) -{ - throw_exception( - std::bad_alloc(), - loc); -} - -void -throw_length_error( - char const* what, - source_location const& loc) -{ - throw_exception( - std::length_error(what), - loc); -} - -void -throw_invalid_argument( - char const* what, - source_location const& loc) -{ - throw_exception( - std::invalid_argument(what), - loc); -} - -void -throw_out_of_range( +throw_system_error( + error_code const& ec, source_location const& loc) { throw_exception( - std::out_of_range( - "out of range"), + system_error(ec), loc); } void throw_system_error( - error_code const& ec, - source_location const& loc) + error e, + source_location const* loc) { + error_code ec; + ec.assign(e, loc); + throw_exception( system_error(ec), - loc); + *loc); } } // detail diff --git a/include/boost/json/detail/impl/string_impl.ipp b/include/boost/json/detail/impl/string_impl.ipp index b9287e8e6..d6d5a984f 100644 --- a/include/boost/json/detail/impl/string_impl.ipp +++ b/include/boost/json/detail/impl/string_impl.ipp @@ -121,7 +121,10 @@ growth( std::size_t capacity) { if(new_size > max_size()) - detail::throw_length_error( "string too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::string_too_large, &loc ); + } // growth factor 2 if( capacity > max_size() - capacity) @@ -156,7 +159,10 @@ append( storage_ptr const& sp) { if(n > max_size() - size()) - detail::throw_length_error( "string too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::string_too_large, &loc ); + } if(n <= capacity() - size()) { term(size() + n); @@ -182,7 +188,10 @@ insert( { const auto curr_size = size(); if(pos > curr_size) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } const auto curr_data = data(); if(n <= capacity() - curr_size) { @@ -212,7 +221,10 @@ insert( else { if(n > max_size() - curr_size) - detail::throw_length_error( "string too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::string_too_large, &loc ); + } string_impl tmp(growth( curr_size + n, capacity()), sp); tmp.size(curr_size + n); @@ -242,7 +254,10 @@ insert_unchecked( { const auto curr_size = size(); if(pos > curr_size) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } const auto curr_data = data(); if(n <= capacity() - size()) { @@ -256,7 +271,10 @@ insert_unchecked( return dest; } if(n > max_size() - curr_size) - detail::throw_length_error( "string too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::string_too_large, &loc ); + } string_impl tmp(growth( curr_size + n, capacity()), sp); tmp.size(curr_size + n); @@ -284,7 +302,10 @@ replace( { const auto curr_size = size(); if (pos > curr_size) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } const auto curr_data = data(); n1 = (std::min)(n1, curr_size - pos); const auto delta = (std::max)(n1, n2) - @@ -332,7 +353,10 @@ replace( else { if (delta > max_size() - curr_size) - detail::throw_length_error( "string too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::string_too_large, &loc ); + } // would exceed capacity, reallocate string_impl tmp(growth( curr_size + delta, capacity()), sp); @@ -366,7 +390,10 @@ replace_unchecked( { const auto curr_size = size(); if(pos > curr_size) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } const auto curr_data = data(); const auto delta = (std::max)(n1, n2) - (std::min)(n1, n2); @@ -387,7 +414,10 @@ replace_unchecked( return replace_pos; } if(delta > max_size() - curr_size) - detail::throw_length_error( "string too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::string_too_large, &loc ); + } // would exceed capacity, reallocate string_impl tmp(growth( curr_size + delta, capacity()), sp); diff --git a/include/boost/json/detail/sbo_buffer.hpp b/include/boost/json/detail/sbo_buffer.hpp index d41ce1c02..5529fa5a1 100644 --- a/include/boost/json/detail/sbo_buffer.hpp +++ b/include/boost/json/detail/sbo_buffer.hpp @@ -144,7 +144,10 @@ class sbo_buffer return; if( max_size() - size_ < size ) - detail::throw_length_error( "buffer too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::number_too_large, &loc ); + } std::size_t const old_capacity = this->capacity(); std::size_t new_capacity = size_ + size; diff --git a/include/boost/json/detail/value_to.hpp b/include/boost/json/detail/value_to.hpp index dc2dc2769..3909cfdcc 100644 --- a/include/boost/json/detail/value_to.hpp +++ b/include/boost/json/detail/value_to.hpp @@ -36,40 +36,37 @@ using reserve_implementation = mp11::mp_cond< mp11::mp_true, mp11::mp_int<0>>; template -error_code +error try_reserve( T&, std::size_t size, mp11::mp_int<2>) { - error_code ec; constexpr std::size_t N = std::tuple_size>::value; if ( N != size ) - { - BOOST_JSON_FAIL(ec, error::size_mismatch); - } - return ec; + return error::size_mismatch; + return error(); } template -error_code +error try_reserve( T& cont, std::size_t size, mp11::mp_int<1>) { cont.reserve(size); - return error_code(); + return error(); } template -error_code +error try_reserve( T&, std::size_t, mp11::mp_int<0>) { - return error_code(); + return error(); } @@ -251,19 +248,23 @@ value_to_impl( value const& jv, Ctx const& ctx ) { - error_code ec; - object const* obj = jv.if_object(); if( !obj ) { + error_code ec; BOOST_JSON_FAIL(ec, error::not_object); return {boost::system::in_place_error, ec}; } T res; - ec = detail::try_reserve(res, obj->size(), reserve_implementation()); - if( ec.failed() ) + error const e = detail::try_reserve( + res, obj->size(), reserve_implementation()); + if( e != error() ) + { + error_code ec; + BOOST_JSON_FAIL( ec, e ); return {boost::system::in_place_error, ec}; + } auto ins = detail::inserter(res, inserter_implementation()); for( key_value_pair const& kv: *obj ) @@ -283,19 +284,21 @@ T value_to_impl( map_like_conversion_tag, value_to_tag, value const& jv, Ctx const& ctx ) { - error_code ec; - object const* obj = jv.if_object(); if( !obj ) { - BOOST_JSON_FAIL(ec, error::not_object); - throw_system_error( ec ); + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + throw_system_error( error::not_object, &loc ); } T result; - ec = detail::try_reserve(result, obj->size(), reserve_implementation()); - if( ec.failed() ) - throw_system_error( ec ); + error const e = detail::try_reserve( + result, obj->size(), reserve_implementation()); + if( e != error() ) + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + throw_system_error( e, &loc ); + } auto ins = detail::inserter(result, inserter_implementation()); for( key_value_pair const& kv: *obj ) @@ -314,19 +317,24 @@ value_to_impl( value const& jv, Ctx const& ctx ) { - error_code ec; array const* arr = jv.if_array(); if( !arr ) { + error_code ec; BOOST_JSON_FAIL(ec, error::not_array); return {boost::system::in_place_error, ec}; } T result; - ec = detail::try_reserve(result, arr->size(), reserve_implementation()); - if( ec.failed() ) + error const e = detail::try_reserve( + result, arr->size(), reserve_implementation()); + if( e != error() ) + { + error_code ec; + BOOST_JSON_FAIL( ec, e ); return {boost::system::in_place_error, ec}; + } auto ins = detail::inserter(result, inserter_implementation()); for( value const& val: *arr ) @@ -344,19 +352,21 @@ T value_to_impl( sequence_conversion_tag, value_to_tag, value const& jv, Ctx const& ctx ) { - error_code ec; - array const* arr = jv.if_array(); if( !arr ) { - BOOST_JSON_FAIL(ec, error::not_array); - throw_system_error( ec ); + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + throw_system_error( error::not_array, &loc ); } T result; - ec = detail::try_reserve(result, arr->size(), reserve_implementation()); - if( ec.failed() ) - throw_system_error( ec ); + error const e = detail::try_reserve( + result, arr->size(), reserve_implementation()); + if( e != error() ) + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + throw_system_error( e, &loc ); + } auto ins = detail::inserter(result, inserter_implementation()); for( value const& val: *arr ) @@ -435,20 +445,18 @@ T value_to_impl( tuple_conversion_tag, value_to_tag, value const& jv, Ctx const& ctx ) { - error_code ec; - array const* arr = jv.if_array(); if( !arr ) { - BOOST_JSON_FAIL(ec, error::not_array); - throw_system_error( ec ); + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + throw_system_error( error::not_array, &loc ); } constexpr std::size_t N = std::tuple_size>::value; if( N != arr->size() ) { - BOOST_JSON_FAIL(ec, error::size_mismatch); - throw_system_error( ec ); + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + throw_system_error( error::size_mismatch, &loc ); } return make_tuple_like( @@ -636,10 +644,13 @@ typename std::enable_if< value_to_impl( user_conversion_tag, try_value_to_tag, value const& jv, Ctx const& ) { +#ifndef BOOST_NO_EXCEPTIONS try { +#endif return { boost::system::in_place_value, tag_invoke(value_to_tag(), jv)}; +#ifndef BOOST_NO_EXCEPTIONS } catch( std::bad_alloc const&) { @@ -655,6 +666,7 @@ value_to_impl( BOOST_JSON_FAIL(ec, error::exception); return {boost::system::in_place_error, ec}; } +#endif } //---------------------------------------------------------- diff --git a/include/boost/json/error.hpp b/include/boost/json/error.hpp index 3915dd1ec..3c5d992ee 100644 --- a/include/boost/json/error.hpp +++ b/include/boost/json/error.hpp @@ -64,6 +64,9 @@ enum class error /// A string is too large string_too_large, + /// A number is too large + number_too_large, + /// error occured when trying to read input input_error, @@ -74,6 +77,9 @@ enum class error /// An exception was thrown during operation exception, + /// A requested element is outside of container's range + out_of_range, + /// test failure test_failure, @@ -127,6 +133,15 @@ enum class error /// JSON string was expected during conversion not_string, + /// std::int64_t was expected during conversion + not_int64, + + /// std::uint64_t was expected during conversion + not_uint64, + + /// `double` was expected during conversion + not_double, + /// JSON array has size incompatible with target size_mismatch, @@ -135,6 +150,7 @@ enum class error /// the key does not correspond to a known name unknown_name, + }; /** Error conditions corresponding to JSON errors diff --git a/include/boost/json/impl/array.hpp b/include/boost/json/impl/array.hpp index b9b3cdf28..b5e75ff91 100644 --- a/include/boost/json/impl/array.hpp +++ b/include/boost/json/impl/array.hpp @@ -206,9 +206,8 @@ value& array:: at(std::size_t pos) & { - if(pos >= t_->size) - detail::throw_out_of_range(); - return (*t_)[pos]; + auto const& self = *this; + return const_cast< value& >( self.at(pos) ); } value&& @@ -223,7 +222,10 @@ array:: at(std::size_t pos) const& { if(pos >= t_->size) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } return (*t_)[pos]; } diff --git a/include/boost/json/impl/array.ipp b/include/boost/json/impl/array.ipp index 5ed97143e..85abc61cd 100644 --- a/include/boost/json/impl/array.ipp +++ b/include/boost/json/impl/array.ipp @@ -40,7 +40,10 @@ allocate( { BOOST_ASSERT(capacity > 0); if(capacity > array::max_size()) - detail::throw_length_error( "array too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::array_too_large, &loc ); + } auto p = reinterpret_cast< table*>(sp->allocate( sizeof(table) + @@ -98,7 +101,10 @@ revert_insert( return; } if(n_ > max_size() - arr_->size()) - detail::throw_length_error( "array too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::array_too_large, &loc ); + } auto t = table::allocate( arr_->growth(arr_->size() + n_), arr_->sp_); @@ -622,7 +628,10 @@ growth( std::size_t new_size) const { if(new_size > max_size()) - detail::throw_length_error( "array too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::array_too_large, &loc ); + } std::size_t const old = capacity(); if(old > max_size() - old / 2) return new_size; diff --git a/include/boost/json/impl/error.ipp b/include/boost/json/impl/error.ipp index ff5dc24f0..71f28882d 100644 --- a/include/boost/json/impl/error.ipp +++ b/include/boost/json/impl/error.ipp @@ -56,9 +56,11 @@ case error::object_too_large: return "object too large"; case error::array_too_large: return "array too large"; case error::key_too_large: return "key too large"; case error::string_too_large: return "string too large"; +case error::number_too_large: return "number too large"; case error::input_error: return "input error"; case error::exception: return "got exception"; +case error::out_of_range: return "out of range"; case error::test_failure: return "test failure"; case error::missing_slash: return "missing slash character"; @@ -76,6 +78,9 @@ case error::not_bool: return "value is not boolean"; case error::not_array: return "value is not an array"; case error::not_object: return "value is not an object"; case error::not_string: return "value is not a string"; +case error::not_int64: return "value is not a std::int64_t number"; +case error::not_uint64: return "value is not a std::uint64_t number"; +case error::not_double: return "value is not a double"; case error::size_mismatch: return "array size does not match target size"; case error::exhausted_variants: return "exhausted all variants"; case error::unknown_name: return "unknown name"; @@ -109,6 +114,7 @@ case error::object_too_large: case error::array_too_large: case error::key_too_large: case error::string_too_large: +case error::number_too_large: case error::input_error: return condition::parse_error; @@ -130,12 +136,16 @@ case error::not_bool: case error::not_array: case error::not_object: case error::not_string: +case error::not_int64: +case error::not_uint64: +case error::not_double: case error::size_mismatch: case error::exhausted_variants: case error::unknown_name: return condition::conversion_error; case error::exception: +case error::out_of_range: return condition::generic_error; } } diff --git a/include/boost/json/impl/null_resource.ipp b/include/boost/json/impl/null_resource.ipp index 4c19a24a8..6a3544c9f 100644 --- a/include/boost/json/impl/null_resource.ipp +++ b/include/boost/json/impl/null_resource.ipp @@ -11,7 +11,7 @@ #define BOOST_JSON_IMPL_NULL_RESOURCE_IPP #include -#include +#include namespace boost { namespace json { @@ -66,7 +66,7 @@ protected: std::size_t, std::size_t) override { - detail::throw_bad_alloc(); + throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION ); } void diff --git a/include/boost/json/impl/object.hpp b/include/boost/json/impl/object.hpp index d4b9b3c3e..c0c960f10 100644 --- a/include/boost/json/impl/object.hpp +++ b/include/boost/json/impl/object.hpp @@ -340,10 +340,8 @@ object:: at(string_view key) & -> value& { - auto it = find(key); - if(it == end()) - detail::throw_out_of_range(); - return it->value(); + auto const& self = *this; + return const_cast< value& >( self.at(key) ); } auto @@ -361,7 +359,10 @@ at(string_view key) const& -> { auto it = find(key); if(it == end()) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } return it->value(); } @@ -497,7 +498,10 @@ insert( std::distance(first, last)); auto const n0 = size(); if(n > max_size() - n0) - detail::throw_length_error( "object too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::object_too_large, &loc ); + } reserve(n0 + n); revert_insert r(*this); while(first != last) diff --git a/include/boost/json/impl/object.ipp b/include/boost/json/impl/object.ipp index e3c8ac9a5..dcad164e2 100644 --- a/include/boost/json/impl/object.ipp +++ b/include/boost/json/impl/object.ipp @@ -448,7 +448,10 @@ insert( { auto const n0 = size(); if(init.size() > max_size() - n0) - detail::throw_length_error( "object too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::object_too_large, &loc ); + } reserve(n0 + init.size()); revert_insert r(*this); if(t_->is_small()) @@ -784,7 +787,10 @@ growth( std::size_t new_size) const { if(new_size > max_size()) - detail::throw_length_error( "object too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::object_too_large, &loc ); + } std::size_t const old = capacity(); if(old > max_size() - old / 2) return new_size; diff --git a/include/boost/json/impl/static_resource.ipp b/include/boost/json/impl/static_resource.ipp index 8593997d0..d1b8daeed 100644 --- a/include/boost/json/impl/static_resource.ipp +++ b/include/boost/json/impl/static_resource.ipp @@ -11,7 +11,7 @@ #define BOOST_JSON_IMPL_STATIC_RESOURCE_IPP #include -#include +#include #include #include @@ -49,7 +49,7 @@ do_allocate( auto p = alignment::align( align, n, p_, n_); if(! p) - detail::throw_bad_alloc(); + throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION ); p_ = reinterpret_cast(p) + n; n_ -= n; return p; diff --git a/include/boost/json/impl/string.ipp b/include/boost/json/impl/string.ipp index bdfe19f0e..63c25840e 100644 --- a/include/boost/json/impl/string.ipp +++ b/include/boost/json/impl/string.ipp @@ -317,7 +317,10 @@ erase( size_type count) { if(pos > impl_.size()) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } if( count > impl_.size() - pos) count = impl_.size() - pos; std::char_traits::move( diff --git a/include/boost/json/impl/value.ipp b/include/boost/json/impl/value.ipp index e3f4c6aee..b4e80f036 100644 --- a/include/boost/json/impl/value.ipp +++ b/include/boost/json/impl/value.ipp @@ -424,7 +424,9 @@ operator>>( char read_buf[BOOST_JSON_STACK_BUFFER_SIZE / 2]; std::streambuf& buf = *is.rdbuf(); std::ios::iostate err = std::ios::goodbit; +#ifndef BOOST_NO_EXCEPTIONS try +#endif { while( true ) { @@ -480,6 +482,7 @@ operator>>( break; } } +#ifndef BOOST_NO_EXCEPTIONS catch(...) { try @@ -493,6 +496,7 @@ operator>>( if( is.exceptions() & std::ios::badbit ) throw; } +#endif is.setstate(err | std::ios::failbit); return is; diff --git a/include/boost/json/object.hpp b/include/boost/json/object.hpp index eb7734a66..c388411c0 100644 --- a/include/boost/json/object.hpp +++ b/include/boost/json/object.hpp @@ -896,7 +896,7 @@ class object @param new_capacity The new minimum capacity. - @throw std::length_error `new_capacity > max_size()` + @throw system_error `new_capacity > max_size()` */ void reserve(std::size_t new_capacity) @@ -956,9 +956,9 @@ class object @param p The value to insert. - @throw std::length_error key is too long. + @throw system_error key is too long. - @throw std::length_error @ref size() >= max_size(). + @throw system_error @ref size() >= max_size(). @return A pair where `first` is an iterator to the existing or inserted element, and `second` @@ -1081,7 +1081,7 @@ class object @param m The value to insert or assign - @throw std::length_error if key is too long + @throw system_error if key is too long */ template std::pair @@ -1116,7 +1116,7 @@ class object This will be passed as `std::forward(arg)` to the @ref value constructor. - @throw std::length_error if key is too long + @throw system_error if key is too long */ template std::pair @@ -1315,7 +1315,7 @@ class object @param key The key of the element to find. - @throw std::out_of_range if no such element exists. + @throw system_error if no such element exists. */ /* @{ */ inline diff --git a/include/boost/json/string.hpp b/include/boost/json/string.hpp index 97e2e8e3c..ab7c2268c 100644 --- a/include/boost/json/string.hpp +++ b/include/boost/json/string.hpp @@ -253,7 +253,7 @@ class string ownership of the memory resource. The default argument for this parameter is `{}`. - @throw std::length_error `count > max_size()`. + @throw system_error `count > max_size()`. */ BOOST_JSON_DECL explicit @@ -286,7 +286,7 @@ class string ownership of the memory resource. The default argument for this parameter is `{}`. - @throw std::length_error `strlen(s) > max_size()`. + @throw system_error `strlen(s) > max_size()`. */ BOOST_JSON_DECL string( @@ -318,7 +318,7 @@ class string ownership of the memory resource. The default argument for this parameter is `{}`. - @throw std::length_error `count > max_size()`. + @throw system_error `count > max_size()`. */ BOOST_JSON_DECL explicit @@ -359,7 +359,7 @@ class string ownership of the memory resource. The default argument for this parameter is `{}`. - @throw std::length_error `std::distance(first, last) > max_size()`. + @throw system_error `std::distance(first, last) > max_size()`. */ template max_size()`. + @throw system_error `s.size() > max_size()`. */ BOOST_JSON_DECL string( @@ -591,7 +591,7 @@ class string @param s The null-terminated character string. - @throw std::length_error `std::strlen(s) > max_size()`. + @throw system_error `std::strlen(s) > max_size()`. */ BOOST_JSON_DECL string& @@ -616,7 +616,7 @@ class string @param s The string view to copy from. - @throw std::length_error `s.size() > max_size()`. + @throw system_error `s.size() > max_size()`. */ BOOST_JSON_DECL string& @@ -645,7 +645,7 @@ class string @param ch The value to initialize characters of the string with. - @throw std::length_error `count > max_size()`. + @throw system_error `count > max_size()`. */ BOOST_JSON_DECL string& @@ -734,7 +734,7 @@ class string @param s A pointer to a character string used to copy from. - @throw std::length_error `count > max_size()`. + @throw system_error `count > max_size()`. */ BOOST_JSON_DECL string& @@ -766,7 +766,7 @@ class string @param s A pointer to a character string used to copy from. - @throw std::length_error `strlen(s) > max_size()`. + @throw system_error `strlen(s) > max_size()`. */ BOOST_JSON_DECL string& @@ -802,7 +802,7 @@ class string @param last An input iterator pointing to the end of the range. - @throw std::length_error `std::distance(first, last) > max_size()`. + @throw system_error `std::distance(first, last) > max_size()`. */ template max_size()`. + @throw system_error `s.size() > max_size()`. */ string& assign(string_view s) @@ -903,40 +903,28 @@ class string @param pos A zero-based index to access. - @throw std::out_of_range `pos >= size()` + @throw system_error `pos >= size()` */ + /** @{ */ char& at(std::size_t pos) { - if(pos >= size()) - detail::throw_out_of_range(); - return impl_.data()[pos]; - } - - /** Return a character with bounds checking. - - Returns a reference to the character specified at - location `pos`. - - @par Complexity - Constant. - - @par Exception Safety - - Strong guarantee. - - @param pos A zero-based index to access. + auto const& self = *this; + return const_cast< char& >( self.at(pos) ); + } - @throw std::out_of_range `pos >= size()` - */ char const& at(std::size_t pos) const { if(pos >= size()) - detail::throw_out_of_range(); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::out_of_range, &loc ); + } return impl_.data()[pos]; } + /** @} */ /** Return a character without bounds checking. @@ -1486,7 +1474,7 @@ class string @param new_capacity The new capacity of the array. - @throw std::length_error `new_capacity > max_size()` + @throw system_error `new_capacity > max_size()` */ void reserve(std::size_t new_capacity) @@ -1560,9 +1548,9 @@ class string @param sv The `string_view` to insert. - @throw std::length_error `size() + s.size() > max_size()` + @throw system_error `size() + s.size() > max_size()` - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ BOOST_JSON_DECL string& @@ -1590,9 +1578,9 @@ class string @param ch The character to insert. - @throw std::length_error `size() + count > max_size()` + @throw system_error `size() + count > max_size()` - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ BOOST_JSON_DECL string& @@ -1620,9 +1608,9 @@ class string @param ch The character to insert. - @throw std::length_error `size() + 1 > max_size()` + @throw system_error `size() + 1 > max_size()` - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ string& insert( @@ -1663,9 +1651,9 @@ class string @param last The end of the character range. - @throw std::length_error `size() + insert_count > max_size()` + @throw system_error `size() + insert_count > max_size()` - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ template size()` + @throw system_error `pos > size()` */ BOOST_JSON_DECL string& @@ -1787,7 +1775,7 @@ class string @param ch The character to append. - @throw std::length_error `size() + 1 > max_size()` + @throw system_error `size() + 1 > max_size()` */ BOOST_JSON_DECL void @@ -1824,7 +1812,7 @@ class string @param ch The character to append. - @throw std::length_error `size() + count > max_size()` + @throw system_error `size() + count > max_size()` */ BOOST_JSON_DECL string& @@ -1844,7 +1832,7 @@ class string @param sv The `string_view` to append. - @throw std::length_error `size() + s.size() > max_size()` + @throw system_error `size() + s.size() > max_size()` */ BOOST_JSON_DECL string& @@ -1877,7 +1865,7 @@ class string @param last An iterator one past the last character to append. - @throw std::length_error `size() + insert_count > max_size()` + @throw system_error `size() + insert_count > max_size()` */ template max_size()` + @throw system_error `size() + sv.size() > max_size()` */ string& operator+=(string_view sv) @@ -1920,7 +1908,7 @@ class string @param ch The character to append. - @throw std::length_error `size() + 1 > max_size()` + @throw system_error `size() + 1 > max_size()` */ string& operator+=(char ch) @@ -2049,9 +2037,9 @@ class string @param sv The `string_view` to replace with. - @throw std::length_error `size() + (sv.size() - rcount) > max_size()` + @throw system_error `size() + (sv.size() - rcount) > max_size()` - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ BOOST_JSON_DECL string& @@ -2087,7 +2075,7 @@ class string @param sv The `string_view` to replace with. - @throw std::length_error `size() + (sv.size() - std::distance(first, last)) > max_size()` + @throw system_error `size() + (sv.size() - std::distance(first, last)) > max_size()` */ string& replace( @@ -2137,7 +2125,7 @@ class string @param last2 An iterator one past the end of the last character to replace with. - @throw std::length_error `size() + (inserted - std::distance(first, last)) > max_size()` + @throw system_error `size() + (inserted - std::distance(first, last)) > max_size()` */ template max_size()` + @throw system_error `size() + (count2 - rcount) > max_size()` - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ BOOST_JSON_DECL string& @@ -2218,7 +2206,7 @@ class string @param ch The character to replace with. - @throw std::length_error `size() + (count - std::distance(first, last)) > max_size()` + @throw system_error `size() + (count - std::distance(first, last)) > max_size()` */ string& replace( @@ -2249,7 +2237,7 @@ class string The default argument for this parameter is @ref npos. - @throw std::out_of_range `pos > size()` + @throw system_error `pos > size()` */ string_view subview( @@ -2294,7 +2282,7 @@ class string @param pos The index to begin copying from. The default argument for this parameter is `0`. - @throw std::out_of_range `pos > max_size()` + @throw system_error `pos > max_size()` */ std::size_t copy( @@ -2316,7 +2304,7 @@ class string @param count The size to resize the string to. - @throw std::out_of_range `count > max_size()` + @throw system_error `count > max_size()` */ void resize(std::size_t count) @@ -2336,7 +2324,7 @@ class string @param ch The characters to append if the size increases. - @throw std::out_of_range `count > max_size()` + @throw system_error `count > max_size()` */ BOOST_JSON_DECL void diff --git a/include/boost/json/value.hpp b/include/boost/json/value.hpp index 040209d8a..24df80a7e 100644 --- a/include/boost/json/value.hpp +++ b/include/boost/json/value.hpp @@ -2440,10 +2440,13 @@ class value #endif to_number() const { - error_code ec; - auto result = to_number(ec); - if(ec) - detail::throw_system_error( ec ); + error e; + auto result = to_number(e); + if( e != error() ) + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( e, &loc ); + } return result; } @@ -2502,15 +2505,14 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_object()` + @throw system_error `! this->is_object()` */ /* @{ */ object& as_object() & { - if(! is_object()) - detail::throw_invalid_argument( "not an object" ); - return obj_; + auto const& self = *this; + return const_cast( self.as_object() ); } object&& @@ -2522,9 +2524,10 @@ class value object const& as_object() const& { - if(! is_object()) - detail::throw_invalid_argument( "not an object" ); - return obj_; + if( is_object() ) + return obj_; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_object, &loc ); } /* @} */ @@ -2540,15 +2543,14 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_array()` + @throw system_error `! this->is_array()` */ /* @{ */ array& as_array() & { - if(! is_array()) - detail::throw_invalid_argument( "array required" ); - return arr_; + auto const& self = *this; + return const_cast( self.as_array() ); } array&& @@ -2560,9 +2562,10 @@ class value array const& as_array() const& { - if(! is_array()) - detail::throw_invalid_argument( "array required" ); - return arr_; + if( is_array() ) + return arr_; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_array, &loc ); } /* @} */ @@ -2578,15 +2581,14 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_string()` + @throw system_error `! this->is_string()` */ /* @{ */ string& as_string() & { - if(! is_string()) - detail::throw_invalid_argument( "not a string" ); - return str_; + auto const& self = *this; + return const_cast( self.as_string() ); } string&& @@ -2598,9 +2600,10 @@ class value string const& as_string() const& { - if(! is_string()) - detail::throw_invalid_argument( "not a string" ); - return str_; + if( is_string() ) + return str_; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_string, &loc ); } /* @} */ @@ -2616,14 +2619,15 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_int64()` + @throw system_error `! this->is_int64()` */ std::int64_t& as_int64() { - if(! is_int64()) - detail::throw_invalid_argument( "not an int64" ); - return sca_.i; + if( is_int64() ) + return sca_.i; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_int64, &loc ); } /** Return the underlying `std::int64_t`, or throw an exception. @@ -2638,14 +2642,15 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_int64()` + @throw system_error `! this->is_int64()` */ std::int64_t as_int64() const { - if(! is_int64()) - detail::throw_invalid_argument( "not an int64" ); - return sca_.i; + if( is_int64() ) + return sca_.i; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_int64, &loc ); } /** Return a reference to the underlying `std::uint64_t`, or throw an exception. @@ -2660,19 +2665,20 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_uint64()` + @throw system_error `! this->is_uint64()` */ std::uint64_t& as_uint64() { - if(! is_uint64()) - detail::throw_invalid_argument( "not a uint64" ); - return sca_.u; + if( is_uint64() ) + return sca_.u; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_uint64, &loc ); } /** Return the underlying `std::uint64_t`, or throw an exception. - If @ref is_int64() is `true`, returns + If @ref is_uint64() is `true`, returns the underlying `std::uint64_t`, otherwise throws an exception. @@ -2682,14 +2688,15 @@ class value @par Exception Safety Strong guarantee. - @throw std::length_error `! this->is_uint64()` + @throw system_error `! this->is_uint64()` */ std::uint64_t as_uint64() const { - if(! is_uint64()) - detail::throw_invalid_argument( "not a uint64" ); - return sca_.u; + if( is_uint64() ) + return sca_.u; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_uint64, &loc ); } /** Return a reference to the underlying `double`, or throw an exception. @@ -2704,19 +2711,20 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_double()` + @throw system_error `! this->is_double()` */ double& as_double() { - if(! is_double()) - detail::throw_invalid_argument( "not a double" ); - return sca_.d; + if( is_double() ) + return sca_.d; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_double, &loc ); } /** Return the underlying `double`, or throw an exception. - If @ref is_int64() is `true`, returns + If @ref is_double() is `true`, returns the underlying `double`, otherwise throws an exception. @@ -2726,14 +2734,15 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_double()` + @throw system_error `! this->is_double()` */ double as_double() const { - if(! is_double()) - detail::throw_invalid_argument( "not a double" ); - return sca_.d; + if( is_double() ) + return sca_.d; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_double, &loc ); } /** Return a reference to the underlying `bool`, or throw an exception. @@ -2748,14 +2757,15 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_bool()` + @throw system_error `! this->is_bool()` */ bool& as_bool() { - if(! is_bool()) - detail::throw_invalid_argument( "bool required" ); - return sca_.b; + if( is_bool() ) + return sca_.b; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_bool, &loc ); } /** Return the underlying `bool`, or throw an exception. @@ -2770,14 +2780,15 @@ class value @par Exception Safety Strong guarantee. - @throw std::invalid_argument `! this->is_bool()` + @throw system_error `! this->is_bool()` */ bool as_bool() const { - if(! is_bool()) - detail::throw_invalid_argument( "bool required" ); - return sca_.b; + if( is_bool() ) + return sca_.b; + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::not_bool, &loc ); } //------------------------------------------------------ @@ -3854,7 +3865,10 @@ class key_value_pair : value_(std::forward(args)...) { if(key.size() > string::max_size()) - detail::throw_length_error( "key too large" ); + { + BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; + detail::throw_system_error( error::key_too_large, &loc ); + } auto s = reinterpret_cast< char*>(value_.storage()-> allocate(key.size() + 1, alignof(char))); diff --git a/test/array.cpp b/test/array.cpp index 8399a89ae..ffd513020 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -457,15 +457,7 @@ class array_test BOOST_TEST(a.at(0).is_number()); BOOST_TEST(a.at(1).is_bool()); BOOST_TEST(a.at(2).is_string()); - try - { - a.at(3); - BOOST_TEST_FAIL(); - } - catch(std::out_of_range const&) - { - BOOST_TEST_PASS(); - } + BOOST_TEST_THROWS_WITH_LOCATION( a.at(3) ); } // at(pos) && @@ -474,15 +466,7 @@ class array_test BOOST_TEST(std::move(a).at(0).is_number()); BOOST_TEST(std::move(a).at(1).is_bool()); BOOST_TEST(std::move(a).at(2).is_string()); - try - { - std::move(a).at(3); - BOOST_TEST_FAIL(); - } - catch(std::out_of_range const&) - { - BOOST_TEST_PASS(); - } + BOOST_TEST_THROWS_WITH_LOCATION( a.at(3) ); value&& rv = std::move(a).at(0); (void)rv; } @@ -493,15 +477,7 @@ class array_test BOOST_TEST(a.at(0).is_number()); BOOST_TEST(a.at(1).is_bool()); BOOST_TEST(a.at(2).is_string()); - try - { - a.at(3); - BOOST_TEST_FAIL(); - } - catch(std::out_of_range const&) - { - BOOST_TEST_PASS(); - } + BOOST_TEST_THROWS_WITH_LOCATION( a.at(3) ); } // operator[&](size_type) & diff --git a/test/error.cpp b/test/error.cpp index 9d5257dfe..89241f805 100644 --- a/test/error.cpp +++ b/test/error.cpp @@ -61,6 +61,7 @@ class error_test check(condition::parse_error, error::array_too_large); check(condition::parse_error, error::key_too_large); check(condition::parse_error, error::string_too_large); + check(condition::parse_error, error::number_too_large); check(condition::parse_error, error::input_error); check(condition::pointer_parse_error, error::missing_slash); @@ -79,11 +80,15 @@ class error_test check(condition::conversion_error, error::not_array); check(condition::conversion_error, error::not_object); check(condition::conversion_error, error::not_string); + check(condition::conversion_error, error::not_int64); + check(condition::conversion_error, error::not_uint64); + check(condition::conversion_error, error::not_double); check(condition::conversion_error, error::size_mismatch); check(condition::conversion_error, error::exhausted_variants); check(condition::conversion_error, error::unknown_name); check(condition::generic_error, error::exception); + check(condition::generic_error, error::out_of_range); check(error::test_failure); // check std interop diff --git a/test/limits.cpp b/test/limits.cpp index bd4e3ba9b..81b7ff969 100644 --- a/test/limits.cpp +++ b/test/limits.cpp @@ -46,7 +46,7 @@ class limits_test {"26",26},{"27",27},{"28",28},{"29",29},{"30",30}, {"31",31}}; BOOST_TEST(init.size() > object::max_size()); - BOOST_TEST_THROWS(value{init}, std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( value{init} ); } } @@ -54,11 +54,7 @@ class limits_test testObject() { // max_size() - { - BOOST_TEST_THROWS( - object(object::max_size()+1), - std::length_error); - } + BOOST_TEST_THROWS_WITH_LOCATION( object(object::max_size()+1) ); // object(), max size { @@ -72,25 +68,19 @@ class limits_test {"26",26},{"27",27},{"28",28},{"29",29},{"30",30}, {"31",31}}; BOOST_TEST(init.size() > object::max_size()); - BOOST_TEST_THROWS( - object(init), - std::length_error); - BOOST_TEST_THROWS( - object(init.begin(), init.end()), - std::length_error); - BOOST_TEST_THROWS( + BOOST_TEST_THROWS_WITH_LOCATION( (object(init)) ); + BOOST_TEST_THROWS_WITH_LOCATION( + object(init.begin(), init.end()) ); + BOOST_TEST_THROWS_WITH_LOCATION( object( make_input_iterator(init.begin()), - make_input_iterator(init.end())), - std::length_error); + make_input_iterator(init.end())) ); } // reserve(), max size { object o; - BOOST_TEST_THROWS( - o.reserve(o.max_size() + 1), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( o.reserve(o.max_size() + 1) ); } // insert(), max size @@ -106,72 +96,60 @@ class limits_test {"31",31}}; BOOST_TEST(init.size() > object::max_size()); object o; - BOOST_TEST_THROWS( - o.insert(init), - std::length_error); - BOOST_TEST_THROWS( - o.insert(init.begin(), init.end()), - std::length_error); - BOOST_TEST_THROWS( + BOOST_TEST_THROWS_WITH_LOCATION( o.insert(init) ); + BOOST_TEST_THROWS_WITH_LOCATION( + o.insert(init.begin(), init.end()) ); + BOOST_TEST_THROWS_WITH_LOCATION( o.insert( make_input_iterator(init.begin()), - make_input_iterator(init.end())), - std::length_error); + make_input_iterator(init.end())) ); } // max key size { std::string const big( string::max_size() + 1, '*'); - BOOST_TEST_THROWS( - object({ {big, nullptr} }), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( object({ {big, nullptr} }) ); } // reserve { object obj; - BOOST_TEST_THROWS( - obj.reserve(object::max_size() + 1), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + obj.reserve(object::max_size() + 1) ); } } void testArray() { - { - BOOST_TEST_THROWS( - array( - array::max_size()+1, - value(nullptr)), - std::length_error); - } + BOOST_TEST_THROWS_WITH_LOCATION( + array( + array::max_size()+1, + value(nullptr)) ); { std::vector v( array::max_size()+1, 42); - BOOST_TEST_THROWS( - array(v.begin(), v.end()), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + array(v.begin(), v.end()) ); } { std::vector v( array::max_size()+1, 42); - BOOST_TEST_THROWS(array( - make_input_iterator(v.begin()), - make_input_iterator(v.end())), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + array( + make_input_iterator(v.begin()), + make_input_iterator(v.end())) ); } { array a; - BOOST_TEST_THROWS( + BOOST_TEST_THROWS_WITH_LOCATION( a.insert(a.begin(), array::max_size() + 1, - nullptr), - std::length_error); + nullptr) ); } } @@ -182,41 +160,36 @@ class limits_test { { string s; - BOOST_TEST_THROWS( - (s.resize(s.max_size() + 1)), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.resize(s.max_size() + 1)) ); } { string s; s.resize(100); - BOOST_TEST_THROWS( - (s.append(s.max_size() - 1, '*')), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.append(s.max_size() - 1, '*')) ); } { string s; s.resize(s.max_size() - 5); - BOOST_TEST_THROWS( - (s.replace(0, 1, s.subview(0, 10))), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.replace(0, 1, s.subview(0, 10))) ); } { string s; s.resize(s.max_size() - 5); - BOOST_TEST_THROWS( - (s.replace(0, 1, 10, 'a')), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.replace(0, 1, 10, 'a')) ); } { string s; s.resize(s.max_size() - 5); - BOOST_TEST_THROWS( - (s.insert(0, s.subview(0, 10))), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.insert(0, s.subview(0, 10))) ); } #if 0 @@ -387,9 +360,8 @@ class limits_test error_code ec; parse_options precise; precise.precise_parsing = true; - BOOST_TEST_THROWS( - parse( {buffer.data(), buffer.size()}, ec, {}, precise ), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + parse( {buffer.data(), buffer.size()}, ec, {}, precise )); BOOST_TEST( !ec ); // now we make the number one character shorter diff --git a/test/object.cpp b/test/object.cpp index 843392db2..b8da00aaa 100644 --- a/test/object.cpp +++ b/test/object.cpp @@ -1311,24 +1311,21 @@ class object_test { BOOST_TEST( o1.at("a").is_number()); - BOOST_TEST_THROWS((o1.at("d")), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( (o1.at("d")) ); } // at(key) const& { BOOST_TEST( co1.at("a").is_number()); - BOOST_TEST_THROWS((co1.at("d")), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( (co1.at("d")) ); } // at(key) && { BOOST_TEST( std::move(o1).at("a").is_number()); - BOOST_TEST_THROWS((std::move(o1).at("d")), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( (std::move(o1).at("d")) ); value&& rv = std::move(o1).at("a"); (void)rv; } diff --git a/test/parse.cpp b/test/parse.cpp index b79687118..b2e3348f0 100644 --- a/test/parse.cpp +++ b/test/parse.cpp @@ -201,7 +201,7 @@ class parse_test ss.clear(); ss.setstate(std::ios::failbit); - BOOST_TEST_THROWS( parse(ss), system_error ); + BOOST_TEST_THROWS_WITH_LOCATION( parse(ss) ); } void diff --git a/test/parser.cpp b/test/parser.cpp index eeee496f2..5ee3d9fc5 100644 --- a/test/parser.cpp +++ b/test/parser.cpp @@ -209,9 +209,7 @@ class parser_test // partial json { parser p; - BOOST_TEST_THROWS( - p.write_some("nul", 3), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write_some("nul", 3) ); } } @@ -228,9 +226,7 @@ class parser_test // partial json { parser p; - BOOST_TEST_THROWS( - p.write_some("nul"), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write_some("nul") ); } } @@ -309,17 +305,13 @@ class parser_test // valid json with invalid trailing char { parser p; - BOOST_TEST_THROWS( - p.write("null*", 5), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write("null*", 5) ); } // partial json { parser p; - BOOST_TEST_THROWS( - p.write("nul", 3), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write("nul", 3) ); } } @@ -336,17 +328,13 @@ class parser_test // valid json with invalid trailing char { parser p; - BOOST_TEST_THROWS( - p.write("null*"), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write("null*") ); } // partial json { parser p; - BOOST_TEST_THROWS( - p.write("nul"), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write("nul") ); } } @@ -363,9 +351,7 @@ class parser_test // release with no write { parser p; - BOOST_TEST_THROWS( - p.release(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.release() ); } // release after error @@ -375,9 +361,7 @@ class parser_test p.write("nul", ec); BOOST_TEST(ec); BOOST_TEST(hasLocation(ec)); - BOOST_TEST_THROWS( - p.release(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.release() ); } } } diff --git a/test/pointer.cpp b/test/pointer.cpp index 15d22933d..2b205590b 100644 --- a/test/pointer.cpp +++ b/test/pointer.cpp @@ -121,15 +121,15 @@ class pointer_test testErrors() { value const jv = testValue(); - BOOST_TEST_THROWS(jv.at_pointer("foo"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/fo"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/m~"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/m~n"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/foo/bar"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/foo/"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/foo/01"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/foo/2b"), system_error); - BOOST_TEST_THROWS(jv.at_pointer("/x/y/z"), system_error); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("foo") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/fo") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/m~") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/m~n") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/foo/bar") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/foo/") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/foo/01") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/foo/2b") ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.at_pointer("/x/y/z") ); } template @@ -276,7 +276,7 @@ class pointer_test array{3} } )); BOOST_TEST( *result == 3 ); - BOOST_TEST_THROWS(jv.set_at_pointer("/0/1", 1), system_error); + BOOST_TEST_THROWS_WITH_LOCATION( jv.set_at_pointer("/0/1", 1) ); jv = value(); result = &jv.set_at_pointer("/a/b/c/d/e/f/g/h/i/j/k/l", "m"); @@ -298,7 +298,7 @@ class pointer_test opts = {}; opts.max_created_elements = 5; jv = array(); - BOOST_TEST_THROWS( jv.set_at_pointer("/5", 1, opts), system_error ); + BOOST_TEST_THROWS_WITH_LOCATION( jv.set_at_pointer("/5", 1, opts) ); result = &jv.set_at_pointer( "/4", 0, opts ); BOOST_TEST(( jv == array{nullptr, nullptr, nullptr, nullptr, 0} )); BOOST_TEST( *result == 0 ); diff --git a/test/serializer.cpp b/test/serializer.cpp index 6875c132b..034b75194 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -382,7 +382,7 @@ class serializer_test char buf[1]; BOOST_TEST_THROWS( sr.read(buf), - std::exception); + std::bad_alloc); } } @@ -412,7 +412,7 @@ class serializer_test char buf[32]; BOOST_TEST_THROWS( sr.read(&buf[0], 1), - std::exception); + std::bad_alloc); } } diff --git a/test/snippets.cpp b/test/snippets.cpp index 508770d82..7d1897333 100644 --- a/test/snippets.cpp +++ b/test/snippets.cpp @@ -635,7 +635,7 @@ usingArrays() assert( arr[0].as_string() == "Hello" ); - // The following line throws std::out_of_range, since the index is out of range + // The following line throws system_error, since the index is out of range arr.at( 3 ) = nullptr; //] @@ -687,7 +687,7 @@ usingObjects() obj["key2"] = 42; obj["key3"] = false; - // The following line throws std::out_of_range, since the key does not exist + // The following line throws system_error, since the key does not exist obj.at( "key4" ); //] diff --git a/test/stream_parser.cpp b/test/stream_parser.cpp index d049c9846..36689d71d 100644 --- a/test/stream_parser.cpp +++ b/test/stream_parser.cpp @@ -185,9 +185,7 @@ class stream_parser_test } { stream_parser p; - BOOST_TEST_THROWS( - p.write_some("[*"), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write_some("[*") ); } } @@ -236,9 +234,7 @@ class stream_parser_test } { stream_parser p; - BOOST_TEST_THROWS( - p.write("[]*"), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.write("[]*") ); } } @@ -257,9 +253,7 @@ class stream_parser_test stream_parser p; BOOST_TEST(! p.done()); p.write("1."); - BOOST_TEST_THROWS( - p.finish(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.finish() ); } { stream_parser p; @@ -275,9 +269,7 @@ class stream_parser_test p.write("[1,2"); error_code ec; p.finish(ec); - BOOST_TEST_THROWS( - p.finish(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.finish() ); } { stream_parser p; @@ -291,9 +283,7 @@ class stream_parser_test p.write("[1,2"); std::error_code ec; p.finish(ec); - BOOST_TEST_THROWS( - p.finish(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.finish() ); } } @@ -304,9 +294,7 @@ class stream_parser_test BOOST_TEST( p.write_some("[") == 1); BOOST_TEST(! p.done()); - BOOST_TEST_THROWS( - p.release(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.release() ); } { stream_parser p; @@ -319,9 +307,7 @@ class stream_parser_test stream_parser p; p.write("["); BOOST_TEST(! p.done()); - BOOST_TEST_THROWS( - p.release(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.release() ); } { stream_parser p; @@ -331,9 +317,7 @@ class stream_parser_test ec == error::extra_data); BOOST_TEST(ec.has_location()); BOOST_TEST(! p.done()); - BOOST_TEST_THROWS( - p.release(), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( p.release() ); } } } @@ -1039,9 +1023,7 @@ class stream_parser_test { value jv; - BOOST_TEST_THROWS( - jv = parse("{,"), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( jv = parse("{,") ); } } @@ -1055,9 +1037,7 @@ class stream_parser_test { monotonic_resource mr; value jv; - BOOST_TEST_THROWS( - jv = parse("xxx", &mr), - system_error); + BOOST_TEST_THROWS_WITH_LOCATION( jv = parse("xxx", &mr) ); } } } diff --git a/test/string.cpp b/test/string.cpp index 22dfab687..1279c14c3 100644 --- a/test/string.cpp +++ b/test/string.cpp @@ -1038,8 +1038,7 @@ class string_test s2.at(1) = 'B'; BOOST_TEST(s2.at(1) == 'B'); - BOOST_TEST_THROWS(s1.at(s2.size()), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( s1.at(s2.size()) ); } // at(size_type) const @@ -1047,8 +1046,7 @@ class string_test BOOST_TEST(cs1.at(1) == 'b'); BOOST_TEST(cs2.at(1) == 'B'); - BOOST_TEST_THROWS(cs1.at(cs2.size()), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( cs1.at(cs2.size()) ); } // operator[&](size_type) @@ -1391,17 +1389,15 @@ class string_test // pos out of range { string s(t.v1); - BOOST_TEST_THROWS( - (s.insert(s.size() + 2, 1, '*')), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.insert(s.size() + 2, 1, '*')) ); } // size > max_size { string s(t.v1); - BOOST_TEST_THROWS( - (s.insert(1, s.max_size(), 'a')), - std::length_error); + BOOST_TEST_THROWS_WITH_LOCATION( + s.insert(1, s.max_size(), 'a') ); } } @@ -1426,9 +1422,8 @@ class string_test // pos out of range { string s(t.v1); - BOOST_TEST_THROWS( - (s.insert(s.size() + 2, "*")), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.insert(s.size() + 2, "*"))); } } @@ -1648,9 +1643,8 @@ class string_test // pos out of range { string s(t.v1); - BOOST_TEST_THROWS( - (s.insert(s.size() + 2, "*")), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.insert(s.size() + 2, "*"))); } } @@ -1757,9 +1751,8 @@ class string_test { string s(t.v1); - BOOST_TEST_THROWS( - (s.erase(t.v1.size() + 1, 1)), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( + (s.erase(t.v1.size() + 1, 1))); } } @@ -2175,8 +2168,8 @@ class string_test fail_loop([&](storage_ptr const& sp) { string s(t.v2, sp); - BOOST_TEST_THROWS(s.replace(s.size() + 1, 1, t.v2), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( + s.replace(s.size() + 1, 1, t.v2)); }); // outside, shrink @@ -2398,8 +2391,8 @@ class string_test fail_loop([&](storage_ptr const& sp) { string s(t.v2, sp); - BOOST_TEST_THROWS(s.replace(s.size() + 1, 1, 1, 'a'), - std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( + s.replace(s.size() + 1, 1, 1, 'a')); }); } diff --git a/test/value.cpp b/test/value.cpp index ead445133..66ce1c6f7 100644 --- a/test/value.cpp +++ b/test/value.cpp @@ -1722,222 +1722,222 @@ class value_test // as_object() & { - object& x = obj.as_object(); - BOOST_TEST_THROWS(arr.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(str.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(i64.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(u64.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(dub.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(boo.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(nul.as_object(), std::invalid_argument); + object& x = obj.as_object(); + BOOST_TEST_THROWS_WITH_LOCATION( arr.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( str.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( i64.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( u64.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( dub.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( boo.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_object() ); (void)x; } // as_object() const& { object const& x = cobj.as_object(); - BOOST_TEST_THROWS(carr.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(cstr.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(ci64.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(cu64.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(cdub.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(cboo.as_object(), std::invalid_argument); - BOOST_TEST_THROWS(cnul.as_object(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( carr.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( cstr.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( ci64.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( cu64.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( cdub.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( cboo.as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_object() ); (void)x; } // as_object() && { object&& x = std::move(obj).as_object(); - BOOST_TEST_THROWS(std::move(arr).as_object(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(str).as_object(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(i64).as_object(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(u64).as_object(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(dub).as_object(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(boo).as_object(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(nul).as_object(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(arr).as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(str).as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(i64).as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(u64).as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(dub).as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(boo).as_object() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(nul).as_object() ); (void)x; } // as_array() & { - BOOST_TEST_THROWS(obj.as_array(), std::invalid_argument); - array& x = arr.as_array(); - BOOST_TEST_THROWS(str.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(i64.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(u64.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(dub.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(boo.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(nul.as_array(), std::invalid_argument); + array& x = arr.as_array(); + BOOST_TEST_THROWS_WITH_LOCATION( obj.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( str.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( i64.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( u64.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( dub.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( boo.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_array() ); (void)x; } // as_array() const& { - BOOST_TEST_THROWS(cobj.as_array(), std::invalid_argument); - array const& x = carr.as_array(); - BOOST_TEST_THROWS(cstr.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(ci64.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(cu64.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(cdub.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(cboo.as_array(), std::invalid_argument); - BOOST_TEST_THROWS(cnul.as_array(), std::invalid_argument); + array const& x = carr.as_array(); + BOOST_TEST_THROWS_WITH_LOCATION( cobj.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( cstr.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( ci64.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( cu64.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( cdub.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( cboo.as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_array() ); (void)x; } // as_array() && { - BOOST_TEST_THROWS(std::move(obj).as_array(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(obj).as_array() ); array&& x = std::move(arr).as_array(); - BOOST_TEST_THROWS(std::move(str).as_array(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(i64).as_array(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(u64).as_array(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(dub).as_array(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(boo).as_array(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(nul).as_array(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(str).as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(i64).as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(u64).as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(dub).as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(boo).as_array() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(nul).as_array() ); (void)x; } // as_string() & { - BOOST_TEST_THROWS(obj.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(arr.as_string(), std::invalid_argument); - string& x = str.as_string(); - BOOST_TEST_THROWS(i64.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(u64.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(dub.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(boo.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(nul.as_string(), std::invalid_argument); + string& x = str.as_string(); + BOOST_TEST_THROWS_WITH_LOCATION( obj.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( arr.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( i64.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( u64.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( dub.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( boo.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_string() ); (void)x; } // as_string() const& { - BOOST_TEST_THROWS(cobj.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(carr.as_string(), std::invalid_argument); string const& x = cstr.as_string(); - BOOST_TEST_THROWS(ci64.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(cu64.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(cdub.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(cboo.as_string(), std::invalid_argument); - BOOST_TEST_THROWS(cnul.as_string(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( cobj.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( carr.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( ci64.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( cu64.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( cdub.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( cboo.as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_string() ); (void)x; } // as_string() const& { - BOOST_TEST_THROWS(std::move(obj).as_string(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(arr).as_string(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(obj).as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(arr).as_string() ); string&& x = std::move(str).as_string(); - BOOST_TEST_THROWS(std::move(i64).as_string(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(u64).as_string(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(dub).as_string(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(boo).as_string(), std::invalid_argument); - BOOST_TEST_THROWS(std::move(nul).as_string(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(i64).as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(u64).as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(dub).as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(boo).as_string() ); + BOOST_TEST_THROWS_WITH_LOCATION( std::move(nul).as_string() ); (void)x; } // as_int64() { - BOOST_TEST_THROWS(obj.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(arr.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(str.as_int64(), std::invalid_argument); std::int64_t& x = i64.as_int64(); - BOOST_TEST_THROWS(u64.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(dub.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(boo.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(nul.as_int64(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( obj.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( arr.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( str.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( u64.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( dub.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( boo.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_int64() ); (void)x; } // as_int64() const { - BOOST_TEST_THROWS(cobj.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(carr.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(cstr.as_int64(), std::invalid_argument); - std::int64_t const& x = ci64.as_int64(); - BOOST_TEST_THROWS(cu64.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(cdub.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(cboo.as_int64(), std::invalid_argument); - BOOST_TEST_THROWS(cnul.as_int64(), std::invalid_argument); + std::int64_t const& x = ci64.as_int64(); + BOOST_TEST_THROWS_WITH_LOCATION( cobj.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( carr.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cstr.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cu64.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cdub.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cboo.as_int64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_int64() ); (void)x; } // as_uint64() { - BOOST_TEST_THROWS(obj.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(arr.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(str.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(i64.as_uint64(), std::invalid_argument); - std::uint64_t& x = u64.as_uint64(); - BOOST_TEST_THROWS(dub.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(boo.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(nul.as_uint64(), std::invalid_argument); + std::uint64_t& x = u64.as_uint64(); + BOOST_TEST_THROWS_WITH_LOCATION( obj.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( arr.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( str.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( i64.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( dub.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( boo.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_uint64() ); (void)x; } // as_uint64() const { - BOOST_TEST_THROWS(cobj.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(carr.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(cstr.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(ci64.as_uint64(), std::invalid_argument); - std::uint64_t const& x = cu64.as_uint64(); - BOOST_TEST_THROWS(cdub.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(cboo.as_uint64(), std::invalid_argument); - BOOST_TEST_THROWS(cnul.as_uint64(), std::invalid_argument); + std::uint64_t const& x = cu64.as_uint64(); + BOOST_TEST_THROWS_WITH_LOCATION( cobj.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( carr.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cstr.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( ci64.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cdub.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cboo.as_uint64() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_uint64() ); (void)x; } // as_double() { - BOOST_TEST_THROWS(obj.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(arr.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(str.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(i64.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(u64.as_double(), std::invalid_argument); - double& x = dub.as_double(); - BOOST_TEST_THROWS(boo.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(nul.as_double(), std::invalid_argument); + double& x = dub.as_double(); + BOOST_TEST_THROWS_WITH_LOCATION( obj.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( arr.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( str.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( i64.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( u64.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( boo.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_double() ); (void)x; } // as_double() const { - BOOST_TEST_THROWS(cobj.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(carr.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(cstr.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(ci64.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(cu64.as_double(), std::invalid_argument); double const& x = cdub.as_double(); - BOOST_TEST_THROWS(cboo.as_double(), std::invalid_argument); - BOOST_TEST_THROWS(cnul.as_double(), std::invalid_argument); + BOOST_TEST_THROWS_WITH_LOCATION( cobj.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( carr.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( cstr.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( ci64.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( cu64.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( cboo.as_double() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_double() ); (void)x; } // as_bool() { - BOOST_TEST_THROWS(obj.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(arr.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(str.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(i64.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(u64.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(dub.as_bool(), std::invalid_argument); - bool& x = boo.as_bool(); - BOOST_TEST_THROWS(nul.as_bool(), std::invalid_argument); + bool& x = boo.as_bool(); + BOOST_TEST_THROWS_WITH_LOCATION( obj.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( arr.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( str.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( i64.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( u64.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( dub.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( nul.as_bool() ); (void)x; } // as_bool() const { - BOOST_TEST_THROWS(cobj.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(carr.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(cstr.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(ci64.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(cu64.as_bool(), std::invalid_argument); - BOOST_TEST_THROWS(cdub.as_bool(), std::invalid_argument); - bool const&x = cboo.as_bool(); - BOOST_TEST_THROWS(cnul.as_bool(), std::invalid_argument); + bool const&x = cboo.as_bool(); + BOOST_TEST_THROWS_WITH_LOCATION( cobj.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( carr.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( cstr.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( ci64.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( cu64.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( cdub.as_bool() ); + BOOST_TEST_THROWS_WITH_LOCATION( cnul.as_bool() ); (void)x; } } @@ -2041,7 +2041,7 @@ class value_test auto&& elem1 = std::move(jvo).at("k1"); BOOST_TEST( &elem1 == &jvo.at("k1") ); - BOOST_TEST_THROWS(cjvo.at("null"), std::out_of_range); + BOOST_TEST_THROWS_WITH_LOCATION( cjvo.at("null") ); // array value jva{true,2,"3"}; @@ -2054,13 +2054,13 @@ class value_test auto&& elem2 = std::move(jva).at(1); BOOST_TEST( &elem2 == &jva.at(1) ); - BOOST_TEST_THROWS( value({false,2,false}).at(4), std::out_of_range ); - BOOST_TEST_THROWS( value({false,2,"3"}).at(4), std::out_of_range ); - BOOST_TEST_THROWS( value({false,false}).at(4), std::out_of_range ); - BOOST_TEST_THROWS( value({false,2}).at(4), std::out_of_range ); - BOOST_TEST_THROWS( value({false,2,"3",nullptr}).at(4), std::out_of_range ); - BOOST_TEST_THROWS( value({2,false,"3"}).at(4), std::out_of_range ); - BOOST_TEST_THROWS( value({true,2,"3"}).at(4), std::out_of_range ); + BOOST_TEST_THROWS_WITH_LOCATION( value({false,2,false}).at(4) ); + BOOST_TEST_THROWS_WITH_LOCATION( value({false,2,"3"}).at(4) ); + BOOST_TEST_THROWS_WITH_LOCATION( value({false,false}).at(4) ); + BOOST_TEST_THROWS_WITH_LOCATION( value({false,2}).at(4) ); + BOOST_TEST_THROWS_WITH_LOCATION( value({false,2,"3",nullptr}).at(4) ); + BOOST_TEST_THROWS_WITH_LOCATION( value({2,false,"3"}).at(4) ); + BOOST_TEST_THROWS_WITH_LOCATION( value({true,2,"3"}).at(4) ); } //------------------------------------------------------