// // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // Official repository: https://github.com/cppalliance/json // // Test that header file is self-contained. #include #include #include #include "test_suite.hpp" BOOST_JSON_NS_BEGIN namespace { template static T max_of() { return (std::numeric_limits::max)(); } template static T min_of() { return (std::numeric_limits::min)(); } } // (anon) class number_cast_test { public: void testNumberCast() { #define EQAL(T) BOOST_TEST(number_cast(jv) == V) #define EQUS(T) BOOST_TEST((V >= 0) && number_cast(jv) == static_cast(V)) #define EQUF(T) BOOST_TEST(static_cast(V) == static_cast(number_cast(jv))) #define THRO(T) BOOST_TEST_THROWS(number_cast(jv), system_error) BOOST_TEST_THROWS(number_cast(value(object_kind)), system_error); BOOST_TEST_THROWS(number_cast(value(array_kind)), system_error); BOOST_TEST_THROWS(number_cast(value(string_kind)), system_error); BOOST_TEST_THROWS(number_cast(value(false)), system_error); BOOST_TEST_THROWS(number_cast(value(nullptr)), system_error); { unsigned char V = 0; value const jv(V); EQAL(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); EQAL(std::uint8_t); EQAL(std::uint16_t); EQAL(std::uint32_t); EQAL(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); EQAL(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); EQAL(std::uint8_t); EQAL(std::uint16_t); EQUS(std::uint32_t); EQUS(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); THRO(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); EQAL(std::uint16_t); EQUS(std::uint32_t); EQUS(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); EQUS(std::uint32_t); EQUS(std::uint64_t); EQUF(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); THRO(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); EQUS(std::uint64_t); EQUF(float); EQUF(double); EQUF(long double); } //--- { auto V = max_of(); value const jv(V); THRO(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); EQAL(std::uint8_t); EQAL(std::uint16_t); EQAL(std::uint32_t); EQAL(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); EQAL(std::uint16_t); EQAL(std::uint32_t); EQAL(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); THRO(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); EQAL(std::uint32_t); EQAL(std::uint64_t); EQUF(float); EQAL(double); EQAL(long double); } { auto V = max_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); THRO(std::int32_t); THRO(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); EQAL(std::uint64_t); EQUF(float); EQUF(double); EQUF(long double); } //--- { auto V = min_of(); value const jv(V); EQAL(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); THRO(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = min_of(); value const jv(V); THRO(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); THRO(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = min_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); THRO(std::uint64_t); EQUF(float); EQAL(double); EQAL(long double); } { auto V = min_of(); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); THRO(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); THRO(std::uint64_t); EQUF(float); EQUF(double); EQUF(long double); } //--- { auto V = double(1.5); value const jv(V); THRO(std::int8_t); THRO(std::int16_t); THRO(std::int32_t); THRO(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); THRO(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = double(2.0); value const jv(V); EQAL(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); EQAL(std::uint8_t); EQAL(std::uint16_t); EQAL(std::uint32_t); EQAL(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } { auto V = double(-4.0); value const jv(V); EQAL(std::int8_t); EQAL(std::int16_t); EQAL(std::int32_t); EQAL(std::int64_t); THRO(std::uint8_t); THRO(std::uint16_t); THRO(std::uint32_t); THRO(std::uint64_t); EQAL(float); EQAL(double); EQAL(long double); } } void run() { testNumberCast(); } }; TEST_SUITE(number_cast_test, "boost.json.number_cast"); BOOST_JSON_NS_END