forked from bblanchon/ArduinoJson
Change string copy policy: only string literal are stored by pointer
This commit is contained in:
@ -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")),
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user