Change string copy policy: only string literal are stored by pointer

This commit is contained in:
Benoit Blanchon
2024-11-25 11:32:03 +01:00
parent 5f8e3c0f0f
commit 594dc707cb
27 changed files with 456 additions and 197 deletions

View File

@ -17,6 +17,15 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
JsonDocument doc(&spy);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("string literal") {
bool result = variant.set("hello\0world");
REQUIRE(result == true);
CHECK(variant ==
"hello"_s); // linked string cannot contain '\0' at the moment
CHECK(spy.log() == AllocatorLog{});
}
SECTION("const char*") {
char str[16];
@ -25,8 +34,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
strcpy(str, "world");
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
REQUIRE(spy.log() == AllocatorLog{});
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("(const char*)0") {
@ -34,6 +45,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant.isNull());
REQUIRE(variant.as<const char*>() == nullptr);
REQUIRE(spy.log() == AllocatorLog{});
}
@ -105,16 +117,14 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
#endif
SECTION("std::string") {
std::string str;
str = "hello";
std::string str = "hello\0world"_s;
bool result = variant.set(str);
str.replace(0, 5, "world");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(variant == "hello\0world"_s); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofString("hello?world")),
});
}