forked from bblanchon/ArduinoJson
Tests: replace constants with sizeofString(n)
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("ElementProxy::add()") {
|
||||
JsonDocument doc(4096);
|
||||
@ -199,7 +200,7 @@ TEST_CASE("ElementProxy::memoryUsage()") {
|
||||
|
||||
SECTION("returns size for string") {
|
||||
ep.set(std::string("hello"));
|
||||
REQUIRE(ep.memoryUsage() == 6);
|
||||
REQUIRE(ep.memoryUsage() == sizeofString(5));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
typedef ArduinoJson::detail::MemberProxy<JsonDocument&, const char*>
|
||||
MemberProxy;
|
||||
@ -248,7 +249,7 @@ TEST_CASE("MemberProxy::memoryUsage()") {
|
||||
|
||||
SECTION("return the size for a string") {
|
||||
mp.set(std::string("hello"));
|
||||
REQUIRE(mp.memoryUsage() == 6);
|
||||
REQUIRE(mp.memoryUsage() == sizeofString(5));
|
||||
}
|
||||
}
|
||||
|
||||
@ -342,7 +343,8 @@ TEST_CASE("Deduplicate keys") {
|
||||
doc[0][std::string("example")] = 1;
|
||||
doc[1][std::string("example")] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 2 * sizeofObject(1) + 8);
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
@ -354,7 +356,8 @@ TEST_CASE("Deduplicate keys") {
|
||||
doc[0][key] = 1;
|
||||
doc[1][key] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 2 * sizeofObject(1) + 8);
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
@ -365,7 +368,8 @@ TEST_CASE("Deduplicate keys") {
|
||||
doc[0][String("example")] = 1;
|
||||
doc[1][String("example")] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 2 * sizeofObject(1) + 8);
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
@ -376,7 +380,8 @@ TEST_CASE("Deduplicate keys") {
|
||||
doc[0][F("example")] = 1;
|
||||
doc[1][F("example")] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 2 * sizeofObject(1) + 8);
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument::add()") {
|
||||
JsonDocument doc(4096);
|
||||
@ -31,7 +32,7 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(std::string("example"));
|
||||
doc.add(std::string("example"));
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 8);
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
@ -40,7 +41,7 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(value);
|
||||
doc.add(value);
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 8);
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
@ -48,7 +49,7 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(String("example"));
|
||||
doc.add(String("example"));
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 8);
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
@ -56,7 +57,7 @@ TEST_CASE("JsonDocument::add()") {
|
||||
doc.add(F("example"));
|
||||
doc.add(F("example"));
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + 8);
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument::garbageCollect()") {
|
||||
ControllableAllocator controllableAllocator;
|
||||
@ -18,13 +19,13 @@ TEST_CASE("JsonDocument::garbageCollect()") {
|
||||
|
||||
SECTION("when allocation succeeds") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 2 * sizeofString(7));
|
||||
doc.remove("blanket");
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1) + 8);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(7));
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(4096)
|
||||
@ -34,14 +35,14 @@ TEST_CASE("JsonDocument::garbageCollect()") {
|
||||
|
||||
SECTION("when allocation fails") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 2 * sizeofString(7));
|
||||
doc.remove("blanket");
|
||||
controllableAllocator.disable();
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 2 * sizeofString(7));
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument::overflowed()") {
|
||||
SECTION("returns false on a fresh object") {
|
||||
@ -32,7 +33,7 @@ TEST_CASE("JsonDocument::overflowed()") {
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful string copy") {
|
||||
JsonDocument doc(sizeofArray(1) + 8);
|
||||
JsonDocument doc(sizeofArray(1) + sizeofString(7));
|
||||
doc.add(std::string("example"));
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
@ -50,7 +51,7 @@ TEST_CASE("JsonDocument::overflowed()") {
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful deserialization") {
|
||||
JsonDocument doc(sizeofArray(1) + 8);
|
||||
JsonDocument doc(sizeofArray(1) + sizeofString(7));
|
||||
deserializeJson(doc, "[\"example\"]");
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
@ -200,7 +200,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
|
||||
SECTION("unaligned") {
|
||||
doc.add(std::string("?")); // two bytes in the string pool
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1) + 2);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(1));
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
Reference in New Issue
Block a user