added library json
This commit is contained in:
18
libraries/ArduinoJson/extras/tests/Numbers/CMakeLists.txt
Normal file
18
libraries/ArduinoJson/extras/tests/Numbers/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
# ArduinoJson - https://arduinojson.org
|
||||
# Copyright © 2014-2023, Benoit BLANCHON
|
||||
# MIT License
|
||||
|
||||
add_executable(NumbersTests
|
||||
convertNumber.cpp
|
||||
parseFloat.cpp
|
||||
parseDouble.cpp
|
||||
parseInteger.cpp
|
||||
parseNumber.cpp
|
||||
)
|
||||
|
||||
add_test(Numbers NumbersTests)
|
||||
|
||||
set_tests_properties(Numbers
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
131
libraries/ArduinoJson/extras/tests/Numbers/convertNumber.cpp
Normal file
131
libraries/ArduinoJson/extras/tests/Numbers/convertNumber.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("canConvertNumber<TOut, TIn>()") {
|
||||
SECTION("int8_t -> int8_t") {
|
||||
CHECK(canConvertNumber<int8_t, int8_t>(0) == true);
|
||||
CHECK(canConvertNumber<int8_t, int8_t>(127) == true);
|
||||
CHECK(canConvertNumber<int8_t, int8_t>(-128) == true);
|
||||
}
|
||||
|
||||
SECTION("int8_t -> int16_t") {
|
||||
CHECK(canConvertNumber<int16_t, int8_t>(0) == true);
|
||||
CHECK(canConvertNumber<int16_t, int8_t>(127) == true);
|
||||
CHECK(canConvertNumber<int16_t, int8_t>(-128) == true);
|
||||
}
|
||||
|
||||
SECTION("int8_t -> uint8_t") {
|
||||
CHECK(canConvertNumber<uint8_t, int8_t>(0) == true);
|
||||
CHECK(canConvertNumber<uint8_t, int8_t>(127) == true);
|
||||
CHECK(canConvertNumber<uint8_t, int8_t>(-128) == false);
|
||||
}
|
||||
|
||||
SECTION("int8_t -> uint16_t") {
|
||||
CHECK(canConvertNumber<uint16_t, int8_t>(0) == true);
|
||||
CHECK(canConvertNumber<uint16_t, int8_t>(127) == true);
|
||||
CHECK(canConvertNumber<uint16_t, int8_t>(-128) == false);
|
||||
}
|
||||
|
||||
SECTION("int16_t -> int8_t") {
|
||||
CHECK(canConvertNumber<int8_t, int16_t>(0) == true);
|
||||
CHECK(canConvertNumber<int8_t, int16_t>(127) == true);
|
||||
CHECK(canConvertNumber<int8_t, int16_t>(128) == false);
|
||||
CHECK(canConvertNumber<int8_t, int16_t>(-128) == true);
|
||||
CHECK(canConvertNumber<int8_t, int16_t>(-129) == false);
|
||||
}
|
||||
|
||||
SECTION("int16_t -> uint8_t") {
|
||||
CHECK(canConvertNumber<uint8_t, int16_t>(0) == true);
|
||||
CHECK(canConvertNumber<uint8_t, int16_t>(255) == true);
|
||||
CHECK(canConvertNumber<uint8_t, int16_t>(256) == false);
|
||||
CHECK(canConvertNumber<uint8_t, int16_t>(-1) == false);
|
||||
}
|
||||
|
||||
SECTION("uint8_t -> int8_t") {
|
||||
CHECK(canConvertNumber<int8_t, uint8_t>(0) == true);
|
||||
CHECK(canConvertNumber<int8_t, uint8_t>(127) == true);
|
||||
CHECK(canConvertNumber<int8_t, uint8_t>(128) == false);
|
||||
CHECK(canConvertNumber<int8_t, uint8_t>(255) == false);
|
||||
}
|
||||
|
||||
SECTION("uint8_t -> int16_t") {
|
||||
CHECK(canConvertNumber<int16_t, uint8_t>(0) == true);
|
||||
CHECK(canConvertNumber<int16_t, uint8_t>(127) == true);
|
||||
CHECK(canConvertNumber<int16_t, uint8_t>(128) == true);
|
||||
CHECK(canConvertNumber<int16_t, uint8_t>(255) == true);
|
||||
}
|
||||
|
||||
SECTION("uint8_t -> uint8_t") {
|
||||
CHECK(canConvertNumber<uint8_t, uint8_t>(0) == true);
|
||||
CHECK(canConvertNumber<uint8_t, uint8_t>(127) == true);
|
||||
CHECK(canConvertNumber<uint8_t, uint8_t>(128) == true);
|
||||
CHECK(canConvertNumber<uint8_t, uint8_t>(255) == true);
|
||||
}
|
||||
|
||||
SECTION("uint8_t -> uint16_t") {
|
||||
CHECK(canConvertNumber<uint16_t, uint8_t>(0) == true);
|
||||
CHECK(canConvertNumber<uint16_t, uint8_t>(127) == true);
|
||||
CHECK(canConvertNumber<uint16_t, uint8_t>(128) == true);
|
||||
CHECK(canConvertNumber<uint16_t, uint8_t>(255) == true);
|
||||
}
|
||||
|
||||
SECTION("float -> int32_t") {
|
||||
CHECK(canConvertNumber<int32_t, float>(0) == true);
|
||||
CHECK(canConvertNumber<int32_t, float>(-2.147483904e9f) == false);
|
||||
CHECK(canConvertNumber<int32_t, float>(-2.147483648e+9f) == true);
|
||||
CHECK(canConvertNumber<int32_t, float>(2.14748352e+9f) == true);
|
||||
CHECK(canConvertNumber<int32_t, float>(2.14748365e+9f) == false);
|
||||
}
|
||||
|
||||
SECTION("double -> int32_t") {
|
||||
CHECK(canConvertNumber<int32_t, double>(0) == true);
|
||||
CHECK(canConvertNumber<int32_t, double>(-2.147483649e+9) == false);
|
||||
CHECK(canConvertNumber<int32_t, double>(-2.147483648e+9) == true);
|
||||
CHECK(canConvertNumber<int32_t, double>(2.147483647e+9) == true);
|
||||
CHECK(canConvertNumber<int32_t, double>(2.147483648e+9) == false);
|
||||
}
|
||||
|
||||
SECTION("float -> uint32_t") {
|
||||
CHECK(canConvertNumber<uint32_t, float>(0) == true);
|
||||
CHECK(canConvertNumber<uint32_t, float>(-1.401298e-45f) == false);
|
||||
CHECK(canConvertNumber<uint32_t, float>(4.29496704e+9f) == true);
|
||||
CHECK(canConvertNumber<uint32_t, float>(4.294967296e+9f) == false);
|
||||
}
|
||||
|
||||
SECTION("float -> int64_t") {
|
||||
CHECK(canConvertNumber<int64_t, float>(0) == true);
|
||||
CHECK(canConvertNumber<int64_t, float>(-9.22337204e+18f) == true);
|
||||
CHECK(canConvertNumber<int64_t, float>(9.22337149e+18f) == true);
|
||||
CHECK(canConvertNumber<int64_t, float>(9.22337204e+18f) == false);
|
||||
}
|
||||
|
||||
SECTION("double -> int64_t") {
|
||||
CHECK(canConvertNumber<int64_t, double>(0) == true);
|
||||
CHECK(canConvertNumber<int64_t, double>(-9.2233720368547758e+18) == true);
|
||||
CHECK(canConvertNumber<int64_t, double>(9.2233720368547748e+18) == true);
|
||||
CHECK(canConvertNumber<int64_t, double>(9.2233720368547758e+18) == false);
|
||||
}
|
||||
|
||||
SECTION("float -> uint64_t") {
|
||||
CHECK(canConvertNumber<uint64_t, float>(0) == true);
|
||||
CHECK(canConvertNumber<uint64_t, float>(-1.401298e-45f) == false);
|
||||
CHECK(canConvertNumber<uint64_t, float>(1.84467429741979238e+19f) == true);
|
||||
CHECK(canConvertNumber<uint64_t, float>(1.844674407370955161e+19f) ==
|
||||
false);
|
||||
}
|
||||
|
||||
SECTION("double -> uint64_t") {
|
||||
CHECK(canConvertNumber<uint64_t, double>(0) == true);
|
||||
CHECK(canConvertNumber<uint64_t, double>(-4.9406564584124e-324) == false);
|
||||
CHECK(canConvertNumber<uint64_t, double>(1.844674407370954958e+19) == true);
|
||||
CHECK(canConvertNumber<uint64_t, double>(1.844674407370955166e+19) ==
|
||||
false);
|
||||
}
|
||||
}
|
||||
96
libraries/ArduinoJson/extras/tests/Numbers/parseDouble.cpp
Normal file
96
libraries/ArduinoJson/extras/tests/Numbers/parseDouble.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_USE_DOUBLE 1
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
void checkDouble(const char* input, double expected) {
|
||||
CAPTURE(input);
|
||||
REQUIRE(parseNumber<double>(input) == Approx(expected));
|
||||
}
|
||||
|
||||
void checkDoubleNaN(const char* input) {
|
||||
CAPTURE(input);
|
||||
double result = parseNumber<double>(input);
|
||||
REQUIRE(result != result);
|
||||
}
|
||||
|
||||
void checkDoubleInf(const char* input, bool negative) {
|
||||
CAPTURE(input);
|
||||
double x = parseNumber<double>(input);
|
||||
if (negative)
|
||||
REQUIRE(x < 0);
|
||||
else
|
||||
REQUIRE(x > 0);
|
||||
REQUIRE(x == x); // not a NaN
|
||||
REQUIRE(x * 2 == x); // a property of infinity
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<double>()") {
|
||||
SECTION("Short_NoExponent") {
|
||||
checkDouble("3.14", 3.14);
|
||||
checkDouble("-3.14", -3.14);
|
||||
checkDouble("+3.14", +3.14);
|
||||
}
|
||||
|
||||
SECTION("Short_NoDot") {
|
||||
checkDouble("1E+308", 1E+308);
|
||||
checkDouble("-1E+308", -1E+308);
|
||||
checkDouble("+1E-308", +1E-308);
|
||||
checkDouble("+1e+308", +1e+308);
|
||||
checkDouble("-1e-308", -1e-308);
|
||||
}
|
||||
|
||||
SECTION("Max") {
|
||||
checkDouble(".017976931348623147e+310", 1.7976931348623147e+308);
|
||||
checkDouble(".17976931348623147e+309", 1.7976931348623147e+308);
|
||||
checkDouble("1.7976931348623147e+308", 1.7976931348623147e+308);
|
||||
checkDouble("17.976931348623147e+307", 1.7976931348623147e+308);
|
||||
checkDouble("179.76931348623147e+306", 1.7976931348623147e+308);
|
||||
}
|
||||
|
||||
SECTION("Min") {
|
||||
checkDouble(".022250738585072014e-306", 2.2250738585072014e-308);
|
||||
checkDouble(".22250738585072014e-307", 2.2250738585072014e-308);
|
||||
checkDouble("2.2250738585072014e-308", 2.2250738585072014e-308);
|
||||
checkDouble("22.250738585072014e-309", 2.2250738585072014e-308);
|
||||
checkDouble("222.50738585072014e-310", 2.2250738585072014e-308);
|
||||
}
|
||||
|
||||
SECTION("VeryLong") {
|
||||
checkDouble("0.00000000000000000000000000000001", 1e-32);
|
||||
checkDouble("100000000000000000000000000000000.0", 1e+32);
|
||||
checkDouble(
|
||||
"100000000000000000000000000000000.00000000000000000000000000000",
|
||||
1e+32);
|
||||
}
|
||||
|
||||
SECTION("MantissaTooLongToFit") {
|
||||
checkDouble("0.179769313486231571111111111111", 0.17976931348623157);
|
||||
checkDouble("17976931348623157.11111111111111", 17976931348623157.0);
|
||||
checkDouble("1797693.134862315711111111111111", 1797693.1348623157);
|
||||
|
||||
checkDouble("-0.179769313486231571111111111111", -0.17976931348623157);
|
||||
checkDouble("-17976931348623157.11111111111111", -17976931348623157.0);
|
||||
checkDouble("-1797693.134862315711111111111111", -1797693.1348623157);
|
||||
}
|
||||
|
||||
SECTION("ExponentTooBig") {
|
||||
checkDoubleInf("1e309", false);
|
||||
checkDoubleInf("-1e309", true);
|
||||
checkDoubleInf("1e65535", false);
|
||||
checkDouble("1e-65535", 0.0);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
checkDoubleNaN("NaN");
|
||||
checkDoubleNaN("nan");
|
||||
}
|
||||
}
|
||||
101
libraries/ArduinoJson/extras/tests/Numbers/parseFloat.cpp
Normal file
101
libraries/ArduinoJson/extras/tests/Numbers/parseFloat.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_USE_DOUBLE 0
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
void checkFloat(const char* input, float expected) {
|
||||
CAPTURE(input);
|
||||
REQUIRE(parseNumber<float>(input) == Approx(expected));
|
||||
}
|
||||
|
||||
void checkFloatNaN(const char* input) {
|
||||
CAPTURE(input);
|
||||
float result = parseNumber<float>(input);
|
||||
REQUIRE(result != result);
|
||||
}
|
||||
|
||||
void checkFloatInf(const char* input, bool negative) {
|
||||
CAPTURE(input);
|
||||
float x = parseNumber<float>(input);
|
||||
if (negative)
|
||||
REQUIRE(x < 0);
|
||||
else
|
||||
REQUIRE(x > 0);
|
||||
REQUIRE(x == x); // not a NaN
|
||||
REQUIRE(x * 2 == x); // a property of infinity
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<float>()") {
|
||||
SECTION("Float_Short_NoExponent") {
|
||||
checkFloat("3.14", 3.14f);
|
||||
checkFloat("-3.14", -3.14f);
|
||||
checkFloat("+3.14", +3.14f);
|
||||
}
|
||||
|
||||
SECTION("Short_NoDot") {
|
||||
checkFloat("1E+38", 1E+38f);
|
||||
checkFloat("-1E+38", -1E+38f);
|
||||
checkFloat("+1E-38", +1E-38f);
|
||||
checkFloat("+1e+38", +1e+38f);
|
||||
checkFloat("-1e-38", -1e-38f);
|
||||
}
|
||||
|
||||
SECTION("Max") {
|
||||
checkFloat("340.2823e+36", 3.402823e+38f);
|
||||
checkFloat("34.02823e+37", 3.402823e+38f);
|
||||
checkFloat("3.402823e+38", 3.402823e+38f);
|
||||
checkFloat("0.3402823e+39", 3.402823e+38f);
|
||||
checkFloat("0.03402823e+40", 3.402823e+38f);
|
||||
checkFloat("0.003402823e+41", 3.402823e+38f);
|
||||
}
|
||||
|
||||
SECTION("VeryLong") {
|
||||
checkFloat("0.00000000000000000000000000000001", 1e-32f);
|
||||
checkFloat("100000000000000000000000000000000.0", 1e+32f);
|
||||
checkFloat(
|
||||
"100000000000000000000000000000000.00000000000000000000000000000",
|
||||
1e+32f);
|
||||
}
|
||||
|
||||
SECTION("MantissaTooLongToFit") {
|
||||
checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
|
||||
checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
|
||||
checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
|
||||
|
||||
checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
|
||||
checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
|
||||
checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
|
||||
}
|
||||
|
||||
SECTION("ExponentTooBig") {
|
||||
checkFloatInf("1e39", false);
|
||||
checkFloatInf("-1e39", true);
|
||||
checkFloatInf("1e255", false);
|
||||
checkFloat("1e-255", 0.0f);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
checkFloatNaN("NaN");
|
||||
checkFloatNaN("nan");
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
checkFloatInf("Infinity", false);
|
||||
checkFloatInf("+Infinity", false);
|
||||
checkFloatInf("-Infinity", true);
|
||||
checkFloatInf("inf", false);
|
||||
checkFloatInf("+inf", false);
|
||||
checkFloatInf("-inf", true);
|
||||
|
||||
checkFloatInf("1e300", false);
|
||||
checkFloatInf("-1e300", true);
|
||||
}
|
||||
}
|
||||
68
libraries/ArduinoJson/extras/tests/Numbers/parseInteger.cpp
Normal file
68
libraries/ArduinoJson/extras/tests/Numbers/parseInteger.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
template <typename T>
|
||||
void checkInteger(const char* input, T expected) {
|
||||
CAPTURE(input);
|
||||
T actual = parseNumber<T>(input);
|
||||
REQUIRE(expected == actual);
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<int8_t>()") {
|
||||
checkInteger<int8_t>("-128", -128);
|
||||
checkInteger<int8_t>("127", 127);
|
||||
checkInteger<int8_t>("+127", 127);
|
||||
checkInteger<int8_t>("3.14", 3);
|
||||
checkInteger<int8_t>("x42", 0);
|
||||
checkInteger<int8_t>("128", 0); // overflow
|
||||
checkInteger<int8_t>("-129", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<int16_t>()") {
|
||||
checkInteger<int16_t>("-32768", -32768);
|
||||
checkInteger<int16_t>("32767", 32767);
|
||||
checkInteger<int16_t>("+32767", 32767);
|
||||
checkInteger<int16_t>("3.14", 3);
|
||||
checkInteger<int16_t>("x42", 0);
|
||||
checkInteger<int16_t>("-32769", 0); // overflow
|
||||
checkInteger<int16_t>("32768", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<int32_t>()") {
|
||||
checkInteger<int32_t>("-2147483648", (-2147483647 - 1));
|
||||
checkInteger<int32_t>("2147483647", 2147483647);
|
||||
checkInteger<int32_t>("+2147483647", 2147483647);
|
||||
checkInteger<int32_t>("3.14", 3);
|
||||
checkInteger<int32_t>("x42", 0);
|
||||
checkInteger<int32_t>("-2147483649", 0); // overflow
|
||||
checkInteger<int32_t>("2147483648", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<uint8_t>()") {
|
||||
checkInteger<uint8_t>("0", 0);
|
||||
checkInteger<uint8_t>("-0", 0);
|
||||
checkInteger<uint8_t>("255", 255);
|
||||
checkInteger<uint8_t>("+255", 255);
|
||||
checkInteger<uint8_t>("3.14", 3);
|
||||
checkInteger<uint8_t>("x42", 0);
|
||||
checkInteger<uint8_t>("-1", 0);
|
||||
checkInteger<uint8_t>("256", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<uint16_t>()") {
|
||||
checkInteger<uint16_t>("0", 0);
|
||||
checkInteger<uint16_t>("65535", 65535);
|
||||
checkInteger<uint16_t>("+65535", 65535);
|
||||
checkInteger<uint16_t>("3.14", 3);
|
||||
// checkInteger<uint16_t>(" 42", 0);
|
||||
checkInteger<uint16_t>("x42", 0);
|
||||
checkInteger<uint16_t>("-1", 0);
|
||||
checkInteger<uint16_t>("65536", 0);
|
||||
}
|
||||
53
libraries/ArduinoJson/extras/tests/Numbers/parseNumber.cpp
Normal file
53
libraries/ArduinoJson/extras/tests/Numbers/parseNumber.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("Test unsigned integer overflow") {
|
||||
VariantData first, second;
|
||||
|
||||
// Avoids MSVC warning C4127 (conditional expression is constant)
|
||||
size_t integerSize = sizeof(JsonInteger);
|
||||
|
||||
if (integerSize == 8) {
|
||||
parseNumber("18446744073709551615", first);
|
||||
parseNumber("18446744073709551616", second);
|
||||
} else {
|
||||
parseNumber("4294967295", first);
|
||||
parseNumber("4294967296", second);
|
||||
}
|
||||
|
||||
REQUIRE(first.type() == uint8_t(VALUE_IS_UNSIGNED_INTEGER));
|
||||
REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
|
||||
}
|
||||
|
||||
TEST_CASE("Test signed integer overflow") {
|
||||
VariantData first, second;
|
||||
|
||||
// Avoids MSVC warning C4127 (conditional expression is constant)
|
||||
size_t integerSize = sizeof(JsonInteger);
|
||||
|
||||
if (integerSize == 8) {
|
||||
parseNumber("-9223372036854775808", first);
|
||||
parseNumber("-9223372036854775809", second);
|
||||
} else {
|
||||
parseNumber("-2147483648", first);
|
||||
parseNumber("-2147483649", second);
|
||||
}
|
||||
|
||||
REQUIRE(first.type() == uint8_t(VALUE_IS_SIGNED_INTEGER));
|
||||
REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
|
||||
}
|
||||
|
||||
TEST_CASE("Invalid value") {
|
||||
VariantData result;
|
||||
|
||||
parseNumber("6a3", result);
|
||||
|
||||
REQUIRE(result.type() == uint8_t(VALUE_IS_NULL));
|
||||
}
|
||||
Reference in New Issue
Block a user