Changed integer storage from positive/negative to signed/unsigned

This commit is contained in:
Benoit Blanchon
2021-04-14 11:42:53 +02:00
parent a002393716
commit fc4f5fd05f
16 changed files with 251 additions and 171 deletions

View File

@ -76,11 +76,28 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<std::string>() == "-42");
}
SECTION("set(42UL)") {
variant.set(42UL);
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<double>() == 42.0);
REQUIRE(variant.as<std::string>() == "42");
}
SECTION("set(0L)") {
variant.set(0L);
REQUIRE(variant.as<bool>() == false);
REQUIRE(variant.as<double>() == 0.0);
REQUIRE(variant.as<std::string>() == "0");
}
SECTION("set(0UL)") {
variant.set(0UL);
REQUIRE(variant.as<bool>() == false);
REQUIRE(variant.as<double>() == 0.0);
REQUIRE(variant.as<std::string>() == "0");
}
SECTION("set(null)") {

View File

@ -46,8 +46,14 @@ TEST_CASE("serialize MsgPack value") {
}
SECTION("positive fixint") {
checkVariant(0, "\x00");
checkVariant(127, "\x7F");
SECTION("signed") {
checkVariant(0, "\x00");
checkVariant(127, "\x7F");
}
SECTION("unsigned") {
checkVariant(0U, "\x00");
checkVariant(127U, "\x7F");
}
}
SECTION("uint 8") {

View File

@ -47,6 +47,7 @@ TEST_CASE("parseNumber<int32_t>()") {
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);

View File

@ -23,7 +23,27 @@ TEST_CASE("Test unsigned integer overflow") {
parseNumber("4294967296", second);
}
REQUIRE(first.type() == uint8_t(VALUE_IS_POSITIVE_INTEGER));
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));
}

View File

@ -4,6 +4,7 @@
add_executable(TextFormatterTests
writeFloat.cpp
writeInteger.cpp
writeString.cpp
)

View File

@ -0,0 +1,55 @@
// ArduinoJson - https://arduinojson.org
// Copyright Benoit Blanchon 2014-2021
// MIT License
#include <catch.hpp>
#include <limits>
#include <string>
#include <ArduinoJson/Json/TextFormatter.hpp>
#include <ArduinoJson/Serialization/Writer.hpp>
using namespace ARDUINOJSON_NAMESPACE;
template <typename T>
void checkWriteInteger(T value, std::string expected) {
char output[1024];
StaticStringWriter sb(output, sizeof(output));
TextFormatter<StaticStringWriter> writer(sb);
writer.writeInteger<T>(value);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
}
TEST_CASE("int8_t") {
checkWriteInteger<int8_t>(0, "0");
checkWriteInteger<int8_t>(-128, "-128");
checkWriteInteger<int8_t>(127, "127");
}
TEST_CASE("uint8_t") {
checkWriteInteger<uint8_t>(0, "0");
checkWriteInteger<uint8_t>(255, "255");
}
TEST_CASE("int16_t") {
checkWriteInteger<int16_t>(0, "0");
checkWriteInteger<int16_t>(-32768, "-32768");
checkWriteInteger<int16_t>(32767, "32767");
}
TEST_CASE("uint16_t") {
checkWriteInteger<uint16_t>(0, "0");
checkWriteInteger<uint16_t>(65535, "65535");
}
TEST_CASE("int32_t") {
checkWriteInteger<int32_t>(0, "0");
checkWriteInteger<int32_t>(-2147483647 - 1, "-2147483648");
checkWriteInteger<int32_t>(2147483647, "2147483647");
}
TEST_CASE("uint32_t") {
checkWriteInteger<uint32_t>(0, "0");
checkWriteInteger<uint32_t>(4294967295U, "4294967295");
}