// ArduinoJson - https://arduinojson.org // Copyright © 2014-2025, Benoit BLANCHON // MIT License #include #include #include #include #include "Allocators.hpp" #include "Literals.hpp" template void checkReference(T& expected) { JsonVariant variant = expected; REQUIRE(expected == variant.as()); } template void checkNumericType() { JsonDocument docMin, docMax; JsonVariant variantMin = docMin.to(); JsonVariant variantMax = docMax.to(); T min = std::numeric_limits::min(); T max = std::numeric_limits::max(); variantMin.set(min); variantMax.set(max); REQUIRE(min == variantMin.as()); REQUIRE(max == variantMax.as()); } TEST_CASE("JsonVariant set()/get()") { SpyingAllocator spy; JsonDocument doc(&spy); JsonVariant variant = doc.to(); #if ARDUINOJSON_USE_LONG_LONG SECTION("SizeOfJsonInteger") { REQUIRE(8 == sizeof(JsonInteger)); } #endif // /!\ Most test were moved to `JsonVariant/set.cpp` // TODO: move the remaining tests too SECTION("False") { variant.set(false); REQUIRE(variant.as() == false); REQUIRE(spy.log() == AllocatorLog{}); } SECTION("True") { variant.set(true); REQUIRE(variant.as() == true); REQUIRE(spy.log() == AllocatorLog{}); } SECTION("Double") { checkNumericType(); } SECTION("Float") { checkNumericType(); } SECTION("SChar") { checkNumericType(); } SECTION("SInt") { checkNumericType(); } SECTION("SLong") { checkNumericType(); } SECTION("SShort") { checkNumericType(); } SECTION("UChar") { checkNumericType(); } SECTION("UInt") { checkNumericType(); } SECTION("ULong") { checkNumericType(); } SECTION("UShort") { checkNumericType(); } #if ARDUINOJSON_USE_LONG_LONG SECTION("LongLong") { checkNumericType(); } SECTION("ULongLong") { checkNumericType(); } #endif SECTION("Int8") { checkNumericType(); } SECTION("Uint8") { checkNumericType(); } SECTION("Int16") { checkNumericType(); } SECTION("Uint16") { checkNumericType(); } SECTION("Int32") { checkNumericType(); } SECTION("Uint32") { checkNumericType(); } #if ARDUINOJSON_USE_LONG_LONG SECTION("Int64") { checkNumericType(); } SECTION("Uint64") { checkNumericType(); } #endif SECTION("CanStoreObject") { JsonDocument doc2; JsonObject object = doc2.to(); variant.set(object); REQUIRE(variant.is()); REQUIRE(variant.as() == object); } } TEST_CASE("volatile") { JsonDocument doc; JsonVariant variant = doc.to(); SECTION("volatile bool") { // issue #2029 volatile bool f = true; variant.set(f); CHECK(variant.is() == true); CHECK(variant.as() == true); } SECTION("volatile int") { volatile int f = 42; variant.set(f); CHECK(variant.is() == true); CHECK(variant.as() == 42); } SECTION("volatile float") { // issue #1557 volatile float f = 3.14f; variant.set(f); CHECK(variant.is() == true); CHECK(variant.as() == 3.14f); } SECTION("volatile double") { volatile double f = 3.14; variant.set(f); CHECK(variant.is() == true); CHECK(variant.as() == 3.14); } }