diff --git a/extras/tests/JsonVariant/set.cpp b/extras/tests/JsonVariant/set.cpp index bf4c951e..b10f9505 100644 --- a/extras/tests/JsonVariant/set.cpp +++ b/extras/tests/JsonVariant/set.cpp @@ -188,12 +188,12 @@ TEST_CASE("JsonVariant::set() when there is enough memory") { } SECTION("int64_t") { - bool result = variant.set(int64_t(-2147483649)); + bool result = variant.set(int64_t(-2147483649LL)); doc.shrinkToFit(); REQUIRE(result == true); REQUIRE(variant.is() == true); - REQUIRE(variant.as() == -2147483649); + REQUIRE(variant.as() == -2147483649LL); REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), @@ -278,6 +278,34 @@ TEST_CASE("JsonVariant::set() with not enough memory") { REQUIRE(result == false); REQUIRE(v.isNull()); } + + SECTION("int32_t") { + bool result = v.set(-42); + + REQUIRE(result == true); + REQUIRE(v.is()); + } + + SECTION("int64_t") { + bool result = v.set(-2147483649LL); + + REQUIRE(result == false); + REQUIRE(v.isNull()); + } + + SECTION("uint32_t") { + bool result = v.set(42); + + REQUIRE(result == true); + REQUIRE(v.is()); + } + + SECTION("uint64_t") { + bool result = v.set(4294967296U); + + REQUIRE(result == false); + REQUIRE(v.isNull()); + } } TEST_CASE("JsonVariant::set() releases the previous value") { @@ -325,29 +353,33 @@ TEST_CASE("JsonVariant::set() releases the previous value") { } } -TEST_CASE("Extension slots") { +TEST_CASE("JsonVariant::set() reuses extension slot") { SpyingAllocator spy; JsonDocument doc(&spy); + JsonVariant variant = doc.to(); - SECTION("double requires two slot") { - int i = ARDUINOJSON_POOL_CAPACITY - 1; + variant.set(1.2); + doc.shrinkToFit(); + spy.clearLog(); - // make sure the pool is full - doc[i] = 1; - REQUIRE(spy.log() == AllocatorLog{ - Allocate(sizeofPool()), - }); + SECTION("double") { + bool result = variant.set(3.4); - // replace with a float => no allocation - spy.clearLog(); - doc[i] = 1.2f; + REQUIRE(result == true); REQUIRE(spy.log() == AllocatorLog{}); + } - // replace with a double => new pool needed - spy.clearLog(); - doc[i] = 1.2; - REQUIRE(spy.log() == AllocatorLog{ - Allocate(sizeofPool()), - }); + SECTION("int64_t") { + bool result = variant.set(-2147483649LL); + + REQUIRE(result == true); + REQUIRE(spy.log() == AllocatorLog{}); + } + + SECTION("uint64_t") { + bool result = variant.set(4294967296U); + + REQUIRE(result == true); + REQUIRE(spy.log() == AllocatorLog{}); } } diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index 5d520acd..cd939595 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -65,8 +65,7 @@ struct Converter::value && return false; auto resources = getResourceManager(dst); data->clear(resources); - data->setInteger(src, resources); - return true; + return data->setInteger(src, resources); } static T fromJson(JsonVariantConst src) { diff --git a/src/ArduinoJson/Variant/VariantData.hpp b/src/ArduinoJson/Variant/VariantData.hpp index af49745a..30c5ba0a 100644 --- a/src/ArduinoJson/Variant/VariantData.hpp +++ b/src/ArduinoJson/Variant/VariantData.hpp @@ -427,12 +427,12 @@ class VariantData { enable_if_t setFloat(T value, ResourceManager*); template - enable_if_t::value> setInteger(T value, - ResourceManager* resources); + enable_if_t::value, bool> setInteger(T value, + ResourceManager* resources); template - enable_if_t::value> setInteger(T value, - ResourceManager* resources); + enable_if_t::value, bool> setInteger( + T value, ResourceManager* resources); void setRawString(StringNode* s) { ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL); // must call clear() first diff --git a/src/ArduinoJson/Variant/VariantImpl.hpp b/src/ArduinoJson/Variant/VariantImpl.hpp index 49dd32e1..11534673 100644 --- a/src/ArduinoJson/Variant/VariantImpl.hpp +++ b/src/ArduinoJson/Variant/VariantImpl.hpp @@ -92,7 +92,7 @@ enable_if_t VariantData::setFloat( } template -enable_if_t::value> VariantData::setInteger( +enable_if_t::value, bool> VariantData::setInteger( T value, ResourceManager* resources) { ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL); // must call clear() first (void)resources; // silence warning @@ -104,17 +104,18 @@ enable_if_t::value> VariantData::setInteger( #if ARDUINOJSON_USE_LONG_LONG else { auto extension = resources->allocExtension(); - if (extension) { - type_ = VALUE_IS_INT64; - content_.asSlotId = extension.id(); - extension->asInt64 = value; - } + if (!extension) + return false; + type_ = VALUE_IS_INT64; + content_.asSlotId = extension.id(); + extension->asInt64 = value; } #endif + return true; } template -enable_if_t::value> VariantData::setInteger( +enable_if_t::value, bool> VariantData::setInteger( T value, ResourceManager* resources) { ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL); // must call clear() first (void)resources; // silence warning @@ -126,13 +127,14 @@ enable_if_t::value> VariantData::setInteger( #if ARDUINOJSON_USE_LONG_LONG else { auto extension = resources->allocExtension(); - if (extension) { - type_ = VALUE_IS_UINT64; - content_.asSlotId = extension.id(); - extension->asUint64 = value; - } + if (!extension) + return false; + type_ = VALUE_IS_UINT64; + content_.asSlotId = extension.id(); + extension->asUint64 = value; } #endif + return true; } ARDUINOJSON_END_PRIVATE_NAMESPACE