2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2025-02-24 15:18:26 +01:00
|
|
|
// Copyright © 2014-2025, Benoit BLANCHON
|
2018-11-09 17:27:32 +01:00
|
|
|
// MIT License
|
|
|
|
|
2023-05-10 10:12:55 +02:00
|
|
|
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
2018-11-09 17:27:32 +01:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2025-04-09 08:55:08 +02:00
|
|
|
using namespace ArduinoJson;
|
2023-02-14 10:04:48 +01:00
|
|
|
using namespace ArduinoJson::detail;
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2023-05-10 10:12:55 +02:00
|
|
|
TEST_CASE("StringBuilder") {
|
2023-07-25 14:46:25 +02:00
|
|
|
KillswitchAllocator killswitch;
|
|
|
|
SpyingAllocator spyingAllocator(&killswitch);
|
2023-07-17 18:15:13 +02:00
|
|
|
ResourceManager resources(&spyingAllocator);
|
2023-04-11 10:03:47 +02:00
|
|
|
|
|
|
|
SECTION("Empty string") {
|
2023-06-17 16:10:56 +02:00
|
|
|
StringBuilder str(&resources);
|
2025-02-28 09:59:46 +01:00
|
|
|
VariantData data;
|
2023-04-11 10:03:47 +02:00
|
|
|
|
|
|
|
str.startString();
|
2025-02-28 09:59:46 +01:00
|
|
|
str.save(&data);
|
2023-04-11 10:03:47 +02:00
|
|
|
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.overflowed() == false);
|
2025-04-09 08:55:08 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
});
|
|
|
|
REQUIRE(data.type() == VariantType::TinyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Tiny string") {
|
|
|
|
StringBuilder str(&resources);
|
|
|
|
|
|
|
|
str.startString();
|
|
|
|
str.append("url");
|
|
|
|
|
|
|
|
REQUIRE(str.isValid() == true);
|
|
|
|
REQUIRE(str.str() == "url");
|
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
});
|
|
|
|
|
|
|
|
VariantData data;
|
|
|
|
str.save(&data);
|
|
|
|
|
|
|
|
REQUIRE(resources.overflowed() == false);
|
|
|
|
REQUIRE(data.type() == VariantType::TinyString);
|
|
|
|
REQUIRE(data.asString() == "url");
|
2023-04-11 10:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Short string fits in first allocation") {
|
2023-06-17 16:10:56 +02:00
|
|
|
StringBuilder str(&resources);
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-09-05 10:54:46 +02:00
|
|
|
str.startString();
|
2018-11-09 17:27:32 +01:00
|
|
|
str.append("hello");
|
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
REQUIRE(str.isValid() == true);
|
2022-01-13 16:15:53 +01:00
|
|
|
REQUIRE(str.str() == "hello");
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.overflowed() == false);
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
});
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
SECTION("Long string needs reallocation") {
|
2023-06-17 16:10:56 +02:00
|
|
|
StringBuilder str(&resources);
|
2023-07-26 06:06:38 +02:00
|
|
|
const char* lorem =
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
|
|
|
"eiusmod tempor incididunt ut labore et dolore magna aliqua.";
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-09-05 10:54:46 +02:00
|
|
|
str.startString();
|
2023-07-26 06:06:38 +02:00
|
|
|
str.append(lorem);
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(str.isValid() == true);
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(str.str() == lorem);
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.overflowed() == false);
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
2023-07-26 06:06:38 +02:00
|
|
|
AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer(1)),
|
|
|
|
Reallocate(sizeofStringBuffer(1), sizeofStringBuffer(2)),
|
|
|
|
Reallocate(sizeofStringBuffer(2), sizeofStringBuffer(3)),
|
|
|
|
});
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
SECTION("Realloc fails") {
|
2023-06-17 16:10:56 +02:00
|
|
|
StringBuilder str(&resources);
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-09-05 10:54:46 +02:00
|
|
|
str.startString();
|
2023-07-25 14:46:25 +02:00
|
|
|
killswitch.on();
|
2023-04-11 10:03:47 +02:00
|
|
|
str.append(
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
|
|
|
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
|
|
|
|
|
|
|
|
REQUIRE(spyingAllocator.log() ==
|
2023-07-26 06:06:38 +02:00
|
|
|
AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
|
|
|
|
Deallocate(sizeofStringBuffer()),
|
|
|
|
});
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(str.isValid() == false);
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.overflowed() == true);
|
2021-11-20 20:29:09 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
SECTION("Initial allocation fails") {
|
2023-06-17 16:10:56 +02:00
|
|
|
StringBuilder str(&resources);
|
2021-11-20 20:29:09 +01:00
|
|
|
|
2023-07-25 14:46:25 +02:00
|
|
|
killswitch.on();
|
2021-11-20 20:29:09 +01:00
|
|
|
str.startString();
|
2023-04-11 10:03:47 +02:00
|
|
|
|
2021-11-20 20:29:09 +01:00
|
|
|
REQUIRE(str.isValid() == false);
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.overflowed() == true);
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
|
|
|
AllocateFail(sizeofStringBuffer()),
|
|
|
|
});
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 20:15:31 +02:00
|
|
|
|
2025-05-20 19:17:17 +02:00
|
|
|
static VariantData saveString(StringBuilder& builder, const char* s) {
|
2025-02-28 09:59:46 +01:00
|
|
|
VariantData data;
|
|
|
|
builder.startString();
|
|
|
|
builder.append(s);
|
|
|
|
builder.save(&data);
|
2025-05-20 19:17:17 +02:00
|
|
|
return data;
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 10:12:55 +02:00
|
|
|
TEST_CASE("StringBuilder::save() deduplicates strings") {
|
2025-02-28 09:59:46 +01:00
|
|
|
SpyingAllocator spy;
|
|
|
|
ResourceManager resources(&spy);
|
|
|
|
StringBuilder builder(&resources);
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
SECTION("Basic") {
|
2025-02-28 09:59:46 +01:00
|
|
|
auto s1 = saveString(builder, "hello");
|
|
|
|
auto s2 = saveString(builder, "world");
|
|
|
|
auto s3 = saveString(builder, "hello");
|
|
|
|
|
2025-05-20 19:17:17 +02:00
|
|
|
REQUIRE(s1.asString() == "hello");
|
|
|
|
REQUIRE(s2.asString() == "world");
|
|
|
|
REQUIRE(+s1.asString().c_str() == +s3.asString().c_str()); // same address
|
2025-02-28 09:59:46 +01:00
|
|
|
|
|
|
|
REQUIRE(spy.log() ==
|
|
|
|
AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Reallocate(sizeofStringBuffer(), sizeofString("world")),
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
});
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Requires terminator") {
|
2025-02-28 09:59:46 +01:00
|
|
|
auto s1 = saveString(builder, "hello world");
|
|
|
|
auto s2 = saveString(builder, "hello");
|
2020-07-21 20:15:31 +02:00
|
|
|
|
2025-05-20 19:17:17 +02:00
|
|
|
REQUIRE(s1.asString() == "hello world");
|
|
|
|
REQUIRE(s2.asString() == "hello");
|
|
|
|
REQUIRE(+s2.asString().c_str() !=
|
|
|
|
+s1.asString().c_str()); // different address
|
2025-02-28 09:59:46 +01:00
|
|
|
|
|
|
|
REQUIRE(spy.log() ==
|
|
|
|
AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
|
|
|
});
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Don't overrun") {
|
2025-02-28 09:59:46 +01:00
|
|
|
auto s1 = saveString(builder, "hello world");
|
2025-04-09 08:55:08 +02:00
|
|
|
auto s2 = saveString(builder, "worl");
|
2020-07-21 20:15:31 +02:00
|
|
|
|
2025-05-20 19:17:17 +02:00
|
|
|
REQUIRE(s1.asString() == "hello world");
|
|
|
|
REQUIRE(s2.asString() == "worl");
|
|
|
|
REQUIRE(s2.asString().c_str() !=
|
|
|
|
s1.asString().c_str()); // different address
|
2025-02-28 09:59:46 +01:00
|
|
|
|
|
|
|
REQUIRE(spy.log() ==
|
|
|
|
AllocatorLog{
|
|
|
|
Allocate(sizeofStringBuffer()),
|
|
|
|
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
|
|
|
|
Allocate(sizeofStringBuffer()),
|
2025-04-09 08:55:08 +02:00
|
|
|
Reallocate(sizeofStringBuffer(), sizeofString("worl")),
|
2025-02-28 09:59:46 +01:00
|
|
|
});
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
}
|