mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 00:45:55 +08:00
添加依赖库
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# ArduinoJson - https://arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2021
|
||||
# MIT License
|
||||
|
||||
add_executable(NumbersTests
|
||||
parseFloat.cpp
|
||||
parseDouble.cpp
|
||||
parseInteger.cpp
|
||||
parseNumber.cpp
|
||||
)
|
||||
|
||||
|
||||
add_test(Numbers NumbersTests)
|
||||
|
||||
set_tests_properties(Numbers
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
@@ -0,0 +1,96 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// 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_NAMESPACE;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// 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_NAMESPACE;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("Test unsigned integer overflow") {
|
||||
VariantData first, second;
|
||||
first.init();
|
||||
second.init();
|
||||
|
||||
// Avoids MSVC warning C4127 (conditional expression is constant)
|
||||
size_t integerSize = sizeof(Integer);
|
||||
|
||||
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;
|
||||
first.init();
|
||||
second.init();
|
||||
|
||||
// Avoids MSVC warning C4127 (conditional expression is constant)
|
||||
size_t integerSize = sizeof(Integer);
|
||||
|
||||
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;
|
||||
result.init();
|
||||
|
||||
parseNumber("6a3", result);
|
||||
|
||||
REQUIRE(result.type() == uint8_t(VALUE_IS_NULL));
|
||||
}
|
||||
Reference in New Issue
Block a user