Added move-constructor and move-assignment to BasicJsonDocument

This commit is contained in:
Benoit Blanchon
2020-03-01 17:24:29 +01:00
parent 2641697e0b
commit 2540b4e01b
5 changed files with 98 additions and 2 deletions

View File

@ -8,6 +8,7 @@
#include <sstream>
using ARDUINOJSON_NAMESPACE::addPadding;
using ARDUINOJSON_NAMESPACE::move;
class SpyingAllocator {
public:
@ -19,7 +20,7 @@ class SpyingAllocator {
return malloc(n);
}
void deallocate(void* p) {
_log << "F";
_log << (p ? "F" : "f");
free(p);
}
@ -43,8 +44,69 @@ TEST_CASE("BasicJsonDocument") {
{
MyJsonDocument doc1(4096, log);
doc1.set(std::string("The size of this string is 32!!"));
MyJsonDocument doc2(doc1);
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
}
REQUIRE(log.str() == "A4096A32FF");
}
SECTION("Move construct") {
{
MyJsonDocument doc1(4096, log);
doc1.set(std::string("The size of this string is 32!!"));
MyJsonDocument doc2(move(doc1));
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
REQUIRE(doc1.as<std::string>() == "null");
REQUIRE(doc1.capacity() == 0);
REQUIRE(doc2.capacity() == 4096);
#endif
}
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
REQUIRE(log.str() == "A4096Ff");
#else
REQUIRE(log.str() == "A4096A32FF");
#endif
}
SECTION("Copy assign") {
{
MyJsonDocument doc1(4096, log);
doc1.set(std::string("The size of this string is 32!!"));
MyJsonDocument doc2(8, log);
doc2 = doc1;
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
}
REQUIRE(log.str() == "A4096A8FA32FF");
}
SECTION("Move assign") {
{
MyJsonDocument doc1(4096, log);
doc1.set(std::string("The size of this string is 32!!"));
MyJsonDocument doc2(8, log);
doc2 = move(doc1);
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
REQUIRE(doc1.as<std::string>() == "null");
REQUIRE(doc1.capacity() == 0);
REQUIRE(doc2.capacity() == 4096);
#endif
}
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
REQUIRE(log.str() == "A4096A8FFf");
#else
REQUIRE(log.str() == "A4096A8FA32FF");
#endif
}
}