mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 18:27:37 +02:00
Fix overflowed()
This commit is contained in:
@ -257,6 +257,20 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
|
|||||||
|
|
||||||
JsonVariant v = doc.to<JsonVariant>();
|
JsonVariant v = doc.to<JsonVariant>();
|
||||||
|
|
||||||
|
SECTION("string literal") {
|
||||||
|
bool result = v.set("hello world");
|
||||||
|
|
||||||
|
REQUIRE(result == false);
|
||||||
|
REQUIRE(v.isNull());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("static JsonString") {
|
||||||
|
bool result = v.set(JsonString("hello world", true));
|
||||||
|
|
||||||
|
REQUIRE(result == false);
|
||||||
|
REQUIRE(v.isNull());
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("std::string") {
|
SECTION("std::string") {
|
||||||
bool result = v.set("hello world!!"_s);
|
bool result = v.set("hello world!!"_s);
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
add_executable(ResourceManagerTests
|
add_executable(ResourceManagerTests
|
||||||
allocVariant.cpp
|
allocVariant.cpp
|
||||||
clear.cpp
|
clear.cpp
|
||||||
|
saveStaticString.cpp
|
||||||
saveString.cpp
|
saveString.cpp
|
||||||
shrinkToFit.cpp
|
shrinkToFit.cpp
|
||||||
size.cpp
|
size.cpp
|
||||||
|
47
extras/tests/ResourceManager/saveStaticString.cpp
Normal file
47
extras/tests/ResourceManager/saveStaticString.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// ArduinoJson - https://arduinojson.org
|
||||||
|
// Copyright © 2014-2025, Benoit BLANCHON
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
|
using namespace ArduinoJson::detail;
|
||||||
|
|
||||||
|
TEST_CASE("ResourceManager::saveStaticString() deduplicates strings") {
|
||||||
|
SpyingAllocator spy;
|
||||||
|
ResourceManager resources(&spy);
|
||||||
|
|
||||||
|
auto str1 = "hello";
|
||||||
|
auto str2 = "world";
|
||||||
|
|
||||||
|
auto id1 = resources.saveStaticString(str1);
|
||||||
|
auto id2 = resources.saveStaticString(str2);
|
||||||
|
REQUIRE(id1 != id2);
|
||||||
|
|
||||||
|
auto id3 = resources.saveStaticString(str1);
|
||||||
|
REQUIRE(id1 == id3);
|
||||||
|
|
||||||
|
resources.shrinkToFit();
|
||||||
|
REQUIRE(spy.log() ==
|
||||||
|
AllocatorLog{
|
||||||
|
Allocate(sizeofStaticStringPool()),
|
||||||
|
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
|
||||||
|
});
|
||||||
|
REQUIRE(resources.overflowed() == false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ResourceManager::saveStaticString() when allocation fails") {
|
||||||
|
SpyingAllocator spy(FailingAllocator::instance());
|
||||||
|
ResourceManager resources(&spy);
|
||||||
|
|
||||||
|
auto slotId = resources.saveStaticString("hello");
|
||||||
|
|
||||||
|
REQUIRE(slotId == NULL_SLOT);
|
||||||
|
REQUIRE(resources.overflowed() == true);
|
||||||
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
|
AllocateFail(sizeofStaticStringPool()),
|
||||||
|
});
|
||||||
|
}
|
@ -19,28 +19,6 @@ static StringNode* saveString(ResourceManager& resources, const char* s,
|
|||||||
return resources.saveString(adaptString(s, n));
|
return resources.saveString(adaptString(s, n));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("ResourceManager::saveStaticString()") {
|
|
||||||
SpyingAllocator spy;
|
|
||||||
ResourceManager resources(&spy);
|
|
||||||
|
|
||||||
auto str1 = "hello";
|
|
||||||
auto str2 = "world";
|
|
||||||
|
|
||||||
auto id1 = resources.saveStaticString(str1);
|
|
||||||
auto id2 = resources.saveStaticString(str2);
|
|
||||||
REQUIRE(id1 != id2);
|
|
||||||
|
|
||||||
auto id3 = resources.saveStaticString(str1);
|
|
||||||
REQUIRE(id1 == id3);
|
|
||||||
|
|
||||||
resources.shrinkToFit();
|
|
||||||
REQUIRE(spy.log() ==
|
|
||||||
AllocatorLog{
|
|
||||||
Allocate(sizeofStaticStringPool()),
|
|
||||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("ResourceManager::saveString()") {
|
TEST_CASE("ResourceManager::saveString()") {
|
||||||
ResourceManager resources;
|
ResourceManager resources;
|
||||||
|
|
||||||
|
@ -121,6 +121,8 @@ class ResourceManager {
|
|||||||
auto slot = staticStringsPools_.allocSlot(allocator_);
|
auto slot = staticStringsPools_.allocSlot(allocator_);
|
||||||
if (slot)
|
if (slot)
|
||||||
*slot = s;
|
*slot = s;
|
||||||
|
else
|
||||||
|
overflowed_ = true;
|
||||||
|
|
||||||
return slot.id();
|
return slot.id();
|
||||||
}
|
}
|
||||||
@ -130,8 +132,8 @@ class ResourceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
variantPools_.clear(allocator_);
|
|
||||||
overflowed_ = false;
|
overflowed_ = false;
|
||||||
|
variantPools_.clear(allocator_);
|
||||||
stringPool_.clear(allocator_);
|
stringPool_.clear(allocator_);
|
||||||
staticStringsPools_.clear(allocator_);
|
staticStringsPools_.clear(allocator_);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user