diff --git a/extras/tests/Helpers/Allocators.hpp b/extras/tests/Helpers/Allocators.hpp index 06984b82..027d5aa7 100644 --- a/extras/tests/Helpers/Allocators.hpp +++ b/extras/tests/Helpers/Allocators.hpp @@ -29,29 +29,79 @@ struct FailingAllocator : ArduinoJson::Allocator { } }; -class SpyingAllocator : public ArduinoJson::Allocator { +class AllocatorLog { public: - virtual ~SpyingAllocator() {} + struct Allocate { + Allocate(size_t s) : size(s) {} + size_t size; + }; - void* allocate(size_t n) override { - _log << "A" << n; - return malloc(n); + struct Reallocate { + Reallocate(size_t s) : size(s) {} + size_t size; + }; + + struct Deallocate {}; + + AllocatorLog& operator<<(const Allocate& a) { + _log << "allocate(" << a.size << ")\n"; + return *this; } - void deallocate(void* p) override { - _log << "F"; - free(p); + AllocatorLog& operator<<(const Deallocate&) { + _log << "deallocate()\n"; + return *this; } - void* reallocate(void* ptr, size_t n) override { - _log << "R" << n; - return realloc(ptr, n); + AllocatorLog& operator<<(const Reallocate& a) { + _log << "reallocate(" << a.size << ")\n"; + return *this; } - std::string log() const { - return _log.str(); + std::string str() const { + auto s = _log.str(); + if (s.empty()) + return "(empty)"; + s.pop_back(); // remove the trailing '\n' + return s; + } + + bool operator==(const AllocatorLog& other) const { + return str() == other.str(); + } + + friend std::ostream& operator<<(std::ostream& os, const AllocatorLog& log) { + os << log.str(); + return os; } private: std::ostringstream _log; }; + +class SpyingAllocator : public ArduinoJson::Allocator { + public: + virtual ~SpyingAllocator() {} + + void* allocate(size_t n) override { + _log << AllocatorLog::Allocate(n); + return malloc(n); + } + + void deallocate(void* p) override { + _log << AllocatorLog::Deallocate(); + free(p); + } + + void* reallocate(void* ptr, size_t n) override { + _log << AllocatorLog::Reallocate(n); + return realloc(ptr, n); + } + + const AllocatorLog& log() const { + return _log; + } + + private: + AllocatorLog _log; +}; diff --git a/extras/tests/JsonDocument/allocator.cpp b/extras/tests/JsonDocument/allocator.cpp index 920d1475..472b0830 100644 --- a/extras/tests/JsonDocument/allocator.cpp +++ b/extras/tests/JsonDocument/allocator.cpp @@ -42,7 +42,9 @@ TEST_CASE("JsonDocument's allocator") { SECTION("Construct/Destruct") { { JsonDocument doc(4096, &spyingAllocator); } - REQUIRE(spyingAllocator.log() == "A4096F"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(4096) + << AllocatorLog::Deallocate()); } SECTION("Copy construct") { @@ -56,7 +58,11 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(doc2.as() == "The size of this string is 32!!"); REQUIRE(doc2.capacity() == 4096); } - REQUIRE(spyingAllocator.log() == "A4096A4096FF"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(4096) + << AllocatorLog::Allocate(4096) + << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate()); } SECTION("Move construct") { @@ -71,7 +77,9 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(doc1.capacity() == 0); REQUIRE(doc2.capacity() == 4096); } - REQUIRE(spyingAllocator.log() == "A4096F"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(4096) + << AllocatorLog::Deallocate()); } SECTION("Copy assign larger") { @@ -86,7 +94,13 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(doc2.as() == "The size of this string is 32!!"); REQUIRE(doc2.capacity() == 4096); } - REQUIRE(spyingAllocator.log() == "A4096A8FA4096FF"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(4096) + << AllocatorLog::Allocate(8) + << AllocatorLog::Deallocate() + << AllocatorLog::Allocate(4096) + << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate()); } SECTION("Copy assign smaller") { @@ -101,7 +115,13 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(doc2.as() == "The size of this string is 32!!"); REQUIRE(doc2.capacity() == 1024); } - REQUIRE(spyingAllocator.log() == "A1024A4096FA1024FF"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(1024) + << AllocatorLog::Allocate(4096) + << AllocatorLog::Deallocate() + << AllocatorLog::Allocate(1024) + << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate()); } SECTION("Copy assign same size") { @@ -116,7 +136,11 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(doc2.as() == "The size of this string is 32!!"); REQUIRE(doc2.capacity() == 1024); } - REQUIRE(spyingAllocator.log() == "A1024A1024FF"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(1024) + << AllocatorLog::Allocate(1024) + << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate()); } SECTION("Move assign") { @@ -132,7 +156,11 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(doc1.capacity() == 0); REQUIRE(doc2.capacity() == 4096); } - REQUIRE(spyingAllocator.log() == "A4096A8FF"); + REQUIRE(spyingAllocator.log() == AllocatorLog() + << AllocatorLog::Allocate(4096) + << AllocatorLog::Allocate(8) + << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate()); } SECTION("garbageCollect()") {