diff --git a/extras/tests/Helpers/Allocators.hpp b/extras/tests/Helpers/Allocators.hpp index 38766482..8bac44ed 100644 --- a/extras/tests/Helpers/Allocators.hpp +++ b/extras/tests/Helpers/Allocators.hpp @@ -187,15 +187,15 @@ class SpyingAllocator : public ArduinoJson::Allocator { size_t allocatedBytes_ = 0; }; -class ControllableAllocator : public ArduinoJson::Allocator { +class KillswitchAllocator : public ArduinoJson::Allocator { public: - ControllableAllocator( + KillswitchAllocator( Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance()) - : enabled_(true), upstream_(upstream) {} - virtual ~ControllableAllocator() {} + : working_(true), upstream_(upstream) {} + virtual ~KillswitchAllocator() {} void* allocate(size_t n) override { - return enabled_ ? upstream_->allocate(n) : 0; + return working_ ? upstream_->allocate(n) : 0; } void deallocate(void* p) override { @@ -203,15 +203,16 @@ class ControllableAllocator : public ArduinoJson::Allocator { } void* reallocate(void* ptr, size_t n) override { - return enabled_ ? upstream_->reallocate(ptr, n) : 0; + return working_ ? upstream_->reallocate(ptr, n) : 0; } - void disable() { - enabled_ = false; + // Turn the killswitch on, so all allocation fail + void on() { + working_ = false; } private: - bool enabled_; + bool working_; Allocator* upstream_; }; diff --git a/extras/tests/JsonDocument/MemberProxy.cpp b/extras/tests/JsonDocument/MemberProxy.cpp index e9ded841..6f2d5dea 100644 --- a/extras/tests/JsonDocument/MemberProxy.cpp +++ b/extras/tests/JsonDocument/MemberProxy.cpp @@ -373,12 +373,12 @@ TEST_CASE("Deduplicate keys") { } TEST_CASE("MemberProxy under memory constraints") { - ControllableAllocator allocator; - SpyingAllocator spy(&allocator); + KillswitchAllocator killswitch; + SpyingAllocator spy(&killswitch); JsonDocument doc(&spy); SECTION("key allocation fails") { - allocator.disable(); + killswitch.on(); doc[std::string("hello")] = "world"; diff --git a/extras/tests/JsonVariant/copy.cpp b/extras/tests/JsonVariant/copy.cpp index 71a29c56..585ce726 100644 --- a/extras/tests/JsonVariant/copy.cpp +++ b/extras/tests/JsonVariant/copy.cpp @@ -9,8 +9,8 @@ using ArduinoJson::detail::sizeofString; TEST_CASE("JsonVariant::set(JsonVariant)") { - ControllableAllocator allocator; - SpyingAllocator spyingAllocator(&allocator); + KillswitchAllocator killswitch; + SpyingAllocator spyingAllocator(&killswitch); JsonDocument doc1(&spyingAllocator); JsonDocument doc2(&spyingAllocator); JsonVariant var1 = doc1.to(); @@ -61,7 +61,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") { SECTION("fails gracefully if string allocation fails") { char str[] = "hello!!"; var1.set(str); - allocator.disable(); + killswitch.on(); spyingAllocator.clearLog(); var2.set(var1); @@ -114,7 +114,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") { SECTION("fails gracefully if raw string allocation fails") { var1.set(serialized(std::string("hello!!"))); - allocator.disable(); + killswitch.on(); spyingAllocator.clearLog(); var2.set(var1); diff --git a/extras/tests/ResourceManager/StringBuilder.cpp b/extras/tests/ResourceManager/StringBuilder.cpp index d0a893a4..ffb49dfa 100644 --- a/extras/tests/ResourceManager/StringBuilder.cpp +++ b/extras/tests/ResourceManager/StringBuilder.cpp @@ -10,8 +10,8 @@ using namespace ArduinoJson::detail; TEST_CASE("StringBuilder") { - ControllableAllocator controllableAllocator; - SpyingAllocator spyingAllocator(&controllableAllocator); + KillswitchAllocator killswitch; + SpyingAllocator spyingAllocator(&killswitch); ResourceManager resources(&spyingAllocator); SECTION("Empty string") { @@ -66,7 +66,7 @@ TEST_CASE("StringBuilder") { StringBuilder str(&resources); str.startString(); - controllableAllocator.disable(); + killswitch.on(); str.append( "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " "eiusmod tempor incididunt ut labore et dolore magna aliqua."); @@ -83,7 +83,7 @@ TEST_CASE("StringBuilder") { SECTION("Initial allocation fails") { StringBuilder str(&resources); - controllableAllocator.disable(); + killswitch.on(); str.startString(); REQUIRE(str.isValid() == false);