diff --git a/src/json.hpp b/src/json.hpp index f4ed9b88..2e700174 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -44,7 +44,7 @@ namespace nlohmann (@c bool by default) @tparam NumberIntegerType type for JSON integer numbers (@c int64_t by default) -@tparam NumberFloatType type for JSON floating point numbers +@tparam NumberFloatType type for JSON floating-point numbers (@c double by default) */ template < @@ -104,7 +104,7 @@ class basic_json using boolean_t = BooleanType; /// a type for a number (integer) using number_integer_t = NumberIntegerType; - /// a type for a number (floating point) + /// a type for a number (floating-point) using number_float_t = NumberFloatType; /// a type for list initialization using list_init_t = std::initializer_list; @@ -127,7 +127,7 @@ class basic_json boolean_t boolean; /// number (integer) number_integer_t number_integer; - /// number (floating point) + /// number (floating-point) number_float_t number_float; /// default constructor (for null values) @@ -142,7 +142,7 @@ class basic_json json_value(boolean_t v) : boolean(v) {} /// constructor for numbers (integer) json_value(number_integer_t v) : number_integer(v) {} - /// constructor for numbers (floating point) + /// constructor for numbers (floating-point) json_value(number_float_t v) : number_float(v) {} }; @@ -166,7 +166,7 @@ class basic_json boolean, /// number value (integer) number_integer, - /// number value (floating point) + /// number value (floating-point) number_float }; @@ -306,12 +306,12 @@ class basic_json : m_type(value_t::number_integer), m_value(number_integer_t(value)) {} - /// create a floating point number (explicit) + /// create a floating-point number (explicit) inline basic_json(const number_float_t& value) : m_type(value_t::number_float), m_value(value) {} - /// create a floating point number (implicit) + /// create a floating-point number (implicit) template::value and diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index b280246a..34fb06bf 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -44,7 +44,7 @@ namespace nlohmann (@c bool by default) @tparam NumberIntegerType type for JSON integer numbers (@c int64_t by default) -@tparam NumberFloatType type for JSON floating point numbers +@tparam NumberFloatType type for JSON floating-point numbers (@c double by default) */ template < @@ -104,7 +104,7 @@ class basic_json using boolean_t = BooleanType; /// a type for a number (integer) using number_integer_t = NumberIntegerType; - /// a type for a number (floating point) + /// a type for a number (floating-point) using number_float_t = NumberFloatType; /// a type for list initialization using list_init_t = std::initializer_list; @@ -127,7 +127,7 @@ class basic_json boolean_t boolean; /// number (integer) number_integer_t number_integer; - /// number (floating point) + /// number (floating-point) number_float_t number_float; /// default constructor (for null values) @@ -142,7 +142,7 @@ class basic_json json_value(boolean_t v) : boolean(v) {} /// constructor for numbers (integer) json_value(number_integer_t v) : number_integer(v) {} - /// constructor for numbers (floating point) + /// constructor for numbers (floating-point) json_value(number_float_t v) : number_float(v) {} }; @@ -166,7 +166,7 @@ class basic_json boolean, /// number value (integer) number_integer, - /// number value (floating point) + /// number value (floating-point) number_float }; @@ -306,12 +306,12 @@ class basic_json : m_type(value_t::number_integer), m_value(number_integer_t(value)) {} - /// create a floating point number (explicit) + /// create a floating-point number (explicit) inline basic_json(const number_float_t& value) : m_type(value_t::number_float), m_value(value) {} - /// create a floating point number (implicit) + /// create a floating-point number (implicit) template::value and diff --git a/test/unit.cpp b/test/unit.cpp index 878c3adf..3bb530b0 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -643,4 +643,254 @@ TEST_CASE("Constructors") CHECK(j == j_reference); } } + + SECTION("create a floating-point number (explicit)") + { + SECTION("uninitialized value") + { + json::number_float_t n{}; + json j(n); + CHECK(j.type() == json::value_t::number_float); + } + + SECTION("initialized value") + { + json::number_float_t n(42.23); + json j(n); + CHECK(j.type() == json::value_t::number_float); + } + } + + SECTION("create a floating-point number (implicit)") + { + // reference object + json::number_float_t n_reference = 42.23; + json j_reference(n_reference); + + SECTION("float") + { + float n = 42.23; + json j(n); + CHECK(j.type() == json::value_t::number_float); + CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); + } + + SECTION("double") + { + double n = 42.23; + json j(n); + CHECK(j.type() == json::value_t::number_float); + CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); + } + + SECTION("long double") + { + long double n = 42.23; + json j(n); + CHECK(j.type() == json::value_t::number_float); + CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); + } + + SECTION("floating-point literal without suffix") + { + json j(42.23); + CHECK(j.type() == json::value_t::number_float); + CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); + } + + SECTION("integer literal with f suffix") + { + json j(42.23f); + CHECK(j.type() == json::value_t::number_float); + CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); + } + + SECTION("integer literal with l suffix") + { + json j(42.23l); + CHECK(j.type() == json::value_t::number_float); + CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); + } + } + + SECTION("create a container (array or object) from an initializer list") + { + SECTION("empty initializer list") + { + SECTION("explicit") + { + std::initializer_list l; + json j(l); + CHECK(j.type() == json::value_t::object); + } + + SECTION("implicit") + { + json j {}; + CHECK(j.type() == json::value_t::null); + } + } + + SECTION("one element") + { + SECTION("array") + { + SECTION("explicit") + { + std::initializer_list l = {json(json::array_t())}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {json::array_t()}; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("object") + { + SECTION("explicit") + { + std::initializer_list l = {json(json::object_t())}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {json::object_t()}; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("string") + { + SECTION("explicit") + { + std::initializer_list l = {json("Hello world")}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {"Hello world"}; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("boolean") + { + SECTION("explicit") + { + std::initializer_list l = {json(true)}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {true}; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("number (integer)") + { + SECTION("explicit") + { + std::initializer_list l = {json(1)}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {1}; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("number (floating-point)") + { + SECTION("explicit") + { + std::initializer_list l = {json(42.23)}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {42.23}; + CHECK(j.type() == json::value_t::array); + } + } + } + + SECTION("more elements") + { + SECTION("explicit") + { + std::initializer_list l = {1, 42.23, true, nullptr, json::object_t(), json::array_t()}; + json j(l); + CHECK(j.type() == json::value_t::array); + } + + SECTION("implicit") + { + json j {1, 42.23, true, nullptr, json::object_t(), json::array_t()}; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("implicit type deduction") + { + SECTION("object") + { + json j { {"one", 1}, {"two", 2.2}, {"three", false} }; + CHECK(j.type() == json::value_t::object); + } + + SECTION("array") + { + json j { {"one", 1}, {"two", 2.2}, {"three", false}, 13 }; + CHECK(j.type() == json::value_t::array); + } + } + + SECTION("explicit type deduction") + { + SECTION("empty object") + { + json j = json::object(); + CHECK(j.type() == json::value_t::object); + } + + SECTION("object") + { + json j = json::object({ {"one", 1}, {"two", 2.2}, {"three", false} }); + CHECK(j.type() == json::value_t::object); + } + + SECTION("object with error") + { + CHECK_THROWS_AS(json::object({ {"one", 1}, {"two", 2.2}, {"three", false}, 13 }), std::logic_error); + } + + SECTION("empty array") + { + json j = json::array(); + CHECK(j.type() == json::value_t::array); + } + + SECTION("array") + { + json j = json::array({ {"one", 1}, {"two", 2.2}, {"three", false} }); + CHECK(j.type() == json::value_t::array); + } + } + } }