Tests: rename ControllableAllocator to KillswitchAllocator

This commit is contained in:
Benoit Blanchon
2023-07-25 14:46:25 +02:00
parent a8329564e2
commit 7a76da3bc7
4 changed files with 21 additions and 20 deletions

View File

@ -187,15 +187,15 @@ class SpyingAllocator : public ArduinoJson::Allocator {
size_t allocatedBytes_ = 0; size_t allocatedBytes_ = 0;
}; };
class ControllableAllocator : public ArduinoJson::Allocator { class KillswitchAllocator : public ArduinoJson::Allocator {
public: public:
ControllableAllocator( KillswitchAllocator(
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance()) Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
: enabled_(true), upstream_(upstream) {} : working_(true), upstream_(upstream) {}
virtual ~ControllableAllocator() {} virtual ~KillswitchAllocator() {}
void* allocate(size_t n) override { void* allocate(size_t n) override {
return enabled_ ? upstream_->allocate(n) : 0; return working_ ? upstream_->allocate(n) : 0;
} }
void deallocate(void* p) override { void deallocate(void* p) override {
@ -203,15 +203,16 @@ class ControllableAllocator : public ArduinoJson::Allocator {
} }
void* reallocate(void* ptr, size_t n) override { void* reallocate(void* ptr, size_t n) override {
return enabled_ ? upstream_->reallocate(ptr, n) : 0; return working_ ? upstream_->reallocate(ptr, n) : 0;
} }
void disable() { // Turn the killswitch on, so all allocation fail
enabled_ = false; void on() {
working_ = false;
} }
private: private:
bool enabled_; bool working_;
Allocator* upstream_; Allocator* upstream_;
}; };

View File

@ -373,12 +373,12 @@ TEST_CASE("Deduplicate keys") {
} }
TEST_CASE("MemberProxy under memory constraints") { TEST_CASE("MemberProxy under memory constraints") {
ControllableAllocator allocator; KillswitchAllocator killswitch;
SpyingAllocator spy(&allocator); SpyingAllocator spy(&killswitch);
JsonDocument doc(&spy); JsonDocument doc(&spy);
SECTION("key allocation fails") { SECTION("key allocation fails") {
allocator.disable(); killswitch.on();
doc[std::string("hello")] = "world"; doc[std::string("hello")] = "world";

View File

@ -9,8 +9,8 @@
using ArduinoJson::detail::sizeofString; using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::set(JsonVariant)") { TEST_CASE("JsonVariant::set(JsonVariant)") {
ControllableAllocator allocator; KillswitchAllocator killswitch;
SpyingAllocator spyingAllocator(&allocator); SpyingAllocator spyingAllocator(&killswitch);
JsonDocument doc1(&spyingAllocator); JsonDocument doc1(&spyingAllocator);
JsonDocument doc2(&spyingAllocator); JsonDocument doc2(&spyingAllocator);
JsonVariant var1 = doc1.to<JsonVariant>(); JsonVariant var1 = doc1.to<JsonVariant>();
@ -61,7 +61,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
SECTION("fails gracefully if string allocation fails") { SECTION("fails gracefully if string allocation fails") {
char str[] = "hello!!"; char str[] = "hello!!";
var1.set(str); var1.set(str);
allocator.disable(); killswitch.on();
spyingAllocator.clearLog(); spyingAllocator.clearLog();
var2.set(var1); var2.set(var1);
@ -114,7 +114,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
SECTION("fails gracefully if raw string allocation fails") { SECTION("fails gracefully if raw string allocation fails") {
var1.set(serialized(std::string("hello!!"))); var1.set(serialized(std::string("hello!!")));
allocator.disable(); killswitch.on();
spyingAllocator.clearLog(); spyingAllocator.clearLog();
var2.set(var1); var2.set(var1);

View File

@ -10,8 +10,8 @@
using namespace ArduinoJson::detail; using namespace ArduinoJson::detail;
TEST_CASE("StringBuilder") { TEST_CASE("StringBuilder") {
ControllableAllocator controllableAllocator; KillswitchAllocator killswitch;
SpyingAllocator spyingAllocator(&controllableAllocator); SpyingAllocator spyingAllocator(&killswitch);
ResourceManager resources(&spyingAllocator); ResourceManager resources(&spyingAllocator);
SECTION("Empty string") { SECTION("Empty string") {
@ -66,7 +66,7 @@ TEST_CASE("StringBuilder") {
StringBuilder str(&resources); StringBuilder str(&resources);
str.startString(); str.startString();
controllableAllocator.disable(); killswitch.on();
str.append( str.append(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua."); "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
@ -83,7 +83,7 @@ TEST_CASE("StringBuilder") {
SECTION("Initial allocation fails") { SECTION("Initial allocation fails") {
StringBuilder str(&resources); StringBuilder str(&resources);
controllableAllocator.disable(); killswitch.on();
str.startString(); str.startString();
REQUIRE(str.isValid() == false); REQUIRE(str.isValid() == false);