diff --git a/extras/tests/Helpers/Allocators.hpp b/extras/tests/Helpers/Allocators.hpp index 8bae2cd6..06984b82 100644 --- a/extras/tests/Helpers/Allocators.hpp +++ b/extras/tests/Helpers/Allocators.hpp @@ -6,6 +6,8 @@ #include +#include + struct FailingAllocator : ArduinoJson::Allocator { static FailingAllocator* instance() { static FailingAllocator allocator; @@ -26,3 +28,30 @@ struct FailingAllocator : ArduinoJson::Allocator { return nullptr; } }; + +class SpyingAllocator : public ArduinoJson::Allocator { + public: + virtual ~SpyingAllocator() {} + + void* allocate(size_t n) override { + _log << "A" << n; + return malloc(n); + } + + void deallocate(void* p) override { + _log << "F"; + free(p); + } + + void* reallocate(void* ptr, size_t n) override { + _log << "R" << n; + return realloc(ptr, n); + } + + std::string log() const { + return _log.str(); + } + + private: + std::ostringstream _log; +}; diff --git a/extras/tests/JsonDocument/allocator.cpp b/extras/tests/JsonDocument/allocator.cpp index 3a85f1a8..920d1475 100644 --- a/extras/tests/JsonDocument/allocator.cpp +++ b/extras/tests/JsonDocument/allocator.cpp @@ -5,38 +5,12 @@ #include #include // malloc, free #include -#include #include +#include "Allocators.hpp" + using ArduinoJson::detail::sizeofObject; -class SpyingAllocator : public Allocator { - public: - virtual ~SpyingAllocator() {} - - void* allocate(size_t n) override { - _log << "A" << n; - return malloc(n); - } - - void deallocate(void* p) override { - _log << "F"; - free(p); - } - - void* reallocate(void* ptr, size_t n) override { - _log << "R" << n; - return realloc(ptr, n); - } - - std::string log() const { - return _log.str(); - } - - private: - std::ostringstream _log; -}; - class ControllableAllocator : public Allocator { public: ControllableAllocator() : _enabled(true) {}