diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 2f65d647a..b18893a4d 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -942,7 +942,7 @@ class lexer : public lexer_base if (str_end != nullptr) { - *str_end = (char*)p; + *str_end = const_cast(p); } return val; @@ -978,7 +978,7 @@ class lexer : public lexer_base if (str_end != nullptr) { - *str_end = (char*)p; + *str_end = const_cast(p); } return val; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8020f4c94..18ec30246 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9054,7 +9054,7 @@ class lexer : public lexer_base if (str_end != nullptr) { - *str_end = (char*)p; + *str_end = const_cast(p); } return val; @@ -9090,7 +9090,7 @@ class lexer : public lexer_base if (str_end != nullptr) { - *str_end = (char*)p; + *str_end = const_cast(p); } return val; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8434f78d7..c86f69d6c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -94,6 +94,7 @@ endif() set(files src/unit-algorithms.cpp src/unit-allocator.cpp + src/unit-alt-number.cpp src/unit-alt-string.cpp src/unit-bson.cpp src/unit-capacity.cpp diff --git a/test/src/unit-alt-number.cpp b/test/src/unit-alt-number.cpp new file mode 100644 index 000000000..c229c74d8 --- /dev/null +++ b/test/src/unit-alt-number.cpp @@ -0,0 +1,128 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ (test suite) +| | |__ | | | | | | version 3.8.0 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2018 Vitaliy Manushkin . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "doctest_compatibility.h" + +#include + +TEST_CASE("Alternative number types") +{ + SECTION("8 bit integers") + { + using json8 = nlohmann::basic_json; + + SECTION("unsigned") + { + std::uint8_t unsigned_max = 255; + json8 j_unsigned_max = json8::parse("255"); + CHECK(j_unsigned_max.is_number_unsigned()); + CHECK(j_unsigned_max.dump() == "255"); + CHECK((j_unsigned_max.get() == unsigned_max)); + CHECK(json8::parse(j_unsigned_max.dump()) == j_unsigned_max); + + json8 j_overflow = json8::parse("256"); + CHECK(j_overflow.is_number_float()); + CHECK(j_overflow.dump() == "256.0"); + } + + SECTION("signed") + { + std::int8_t signed_min = -128; + json8 j_signed_min = json8::parse("-128"); + CHECK(j_signed_min.is_number_integer()); + CHECK(j_signed_min.dump() == "-128"); + CHECK((j_signed_min.get() == signed_min)); + CHECK(json8::parse(j_signed_min.dump()) == j_signed_min); + + json8 j_underflow = json8::parse("-129"); + CHECK(j_underflow.is_number_float()); + CHECK(j_underflow.dump() == "-129.0"); + } + } + + SECTION("16 bit integers") + { + using json16 = nlohmann::basic_json; + + SECTION("unsigned") + { + std::uint16_t unsigned_max = 65535; + json16 j_unsigned_max = json16::parse("65535"); + CHECK(j_unsigned_max.is_number_unsigned()); + CHECK(j_unsigned_max.dump() == "65535"); + CHECK((j_unsigned_max.get() == unsigned_max)); + CHECK(json16::parse(j_unsigned_max.dump()) == j_unsigned_max); + + json16 j_overflow = json16::parse("65536"); + CHECK(j_overflow.is_number_float()); + CHECK(j_overflow.dump() == "65536.0"); + } + + SECTION("signed") + { + std::int16_t signed_min = -32768; + json16 j_signed_min = json16::parse("-32768"); + CHECK(j_signed_min.is_number_integer()); + CHECK(j_signed_min.dump() == "-32768"); + CHECK((j_signed_min.get() == signed_min)); + CHECK(json16::parse(j_signed_min.dump()) == j_signed_min); + + json16 j_underflow = json16::parse("-32769"); + CHECK(j_underflow.is_number_float()); + CHECK(j_underflow.dump() == "-32769.0"); + } + } + +#ifdef __SIZEOF_INT128__ + SECTION("128 bit integers") + { + using json128 = nlohmann::basic_json; + + SECTION("unsigned") + { + __uint128_t unsigned_max = (340282366920938463463.374607431768211455) * std::pow(10, 18); + json128 j_unsigned_max = json128::parse("340282366920938463463374607431768211455"); + CHECK(j_unsigned_max.is_number_unsigned()); + CHECK(j_unsigned_max.dump() == "340282366920938463463374607431768211455"); + CHECK((j_unsigned_max.get<__uint128_t>() == unsigned_max)); + CHECK(json128::parse(j_unsigned_max.dump()) == j_unsigned_max); + } + + SECTION("signed") + { + __int128_t signed_min = (-170141183460469231731.687303715884105728) * std::pow(10, 18); + json128 j_signed_min = json128::parse("-170141183460469231731687303715884105728"); + CHECK(j_signed_min.is_number_integer()); + CHECK(j_signed_min.dump() == "-170141183460469231731687303715884105728"); + CHECK((j_signed_min.get<__int128_t>() == signed_min)); + CHECK(json128::parse(j_signed_min.dump()) == j_signed_min); + } + } +#endif +}