mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Test: add AllocatorLog
This commit is contained in:
@ -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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user