Added BasicJsonDocument to support custom allocator (issue #876)

This commit is contained in:
Benoit Blanchon
2019-03-17 21:48:10 +01:00
parent 576543c4b4
commit dee8c8e242
6 changed files with 149 additions and 52 deletions

View File

@ -0,0 +1,49 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <stdlib.h> // malloc, free
#include <catch.hpp>
#include <sstream>
using ARDUINOJSON_NAMESPACE::addPadding;
class SpyingAllocator {
public:
SpyingAllocator(std::ostream& log) : _log(log) {}
void* allocate(size_t n) {
_log << "A" << n;
return malloc(n);
}
void deallocate(void* p) {
_log << "F";
free(p);
}
private:
SpyingAllocator& operator=(const SpyingAllocator& src);
std::ostream& _log;
};
typedef BasicJsonDocument<SpyingAllocator> MyJsonDocument;
TEST_CASE("BasicJsonDocument") {
std::stringstream log;
SECTION("Construct/Destruct") {
{ MyJsonDocument doc(4096, log); }
REQUIRE(log.str() == "A4096F");
}
SECTION("Copy construct") {
{
MyJsonDocument doc1(4096, log);
doc1.set(std::string("The size of this string is 32!!"));
MyJsonDocument doc2(doc1);
}
REQUIRE(log.str() == "A4096A32FF");
}
}