2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2023-02-16 11:45:01 +01:00
|
|
|
// Copyright © 2014-2023, 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"
|
|
|
|
|
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);
|
2023-04-11 10:03:47 +02:00
|
|
|
|
|
|
|
str.startString();
|
|
|
|
str.save();
|
|
|
|
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.size() == sizeofString(0));
|
|
|
|
REQUIRE(resources.overflowed() == false);
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31),
|
|
|
|
sizeofString(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)));
|
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);
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-09-05 10:54:46 +02:00
|
|
|
str.startString();
|
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.");
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(str.isValid() == true);
|
|
|
|
REQUIRE(str.str() ==
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
|
|
|
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.overflowed() == false);
|
2023-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31),
|
|
|
|
sizeofString(63))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(63),
|
|
|
|
sizeofString(127)));
|
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() ==
|
|
|
|
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::ReallocateFail(sizeofString(31),
|
|
|
|
sizeofString(63))
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(31)));
|
|
|
|
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-04-11 10:03:47 +02:00
|
|
|
REQUIRE(spyingAllocator.log() ==
|
|
|
|
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 20:15:31 +02:00
|
|
|
|
2023-06-17 16:10:56 +02:00
|
|
|
static StringNode* addStringToPool(ResourceManager& resources, const char* s) {
|
|
|
|
StringBuilder str(&resources);
|
2020-09-05 10:54:46 +02:00
|
|
|
str.startString();
|
2020-07-21 20:15:31 +02:00
|
|
|
str.append(s);
|
2023-05-02 18:29:19 +02:00
|
|
|
return str.save();
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 10:12:55 +02:00
|
|
|
TEST_CASE("StringBuilder::save() deduplicates strings") {
|
2023-07-17 18:15:13 +02:00
|
|
|
ResourceManager resources;
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
SECTION("Basic") {
|
2023-06-17 16:10:56 +02:00
|
|
|
auto s1 = addStringToPool(resources, "hello");
|
|
|
|
auto s2 = addStringToPool(resources, "world");
|
|
|
|
auto s3 = addStringToPool(resources, "hello");
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
REQUIRE(s1 == s3);
|
|
|
|
REQUIRE(s2 != s3);
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(s1->references == 2);
|
|
|
|
REQUIRE(s2->references == 1);
|
|
|
|
REQUIRE(s3->references == 2);
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.size() == 2 * sizeofString(5));
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Requires terminator") {
|
2023-06-17 16:10:56 +02:00
|
|
|
auto s1 = addStringToPool(resources, "hello world");
|
|
|
|
auto s2 = addStringToPool(resources, "hello");
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
REQUIRE(s2 != s1);
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(s1->references == 1);
|
|
|
|
REQUIRE(s2->references == 1);
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.size() == sizeofString(11) + sizeofString(5));
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Don't overrun") {
|
2023-06-17 16:10:56 +02:00
|
|
|
auto s1 = addStringToPool(resources, "hello world");
|
|
|
|
auto s2 = addStringToPool(resources, "wor");
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
REQUIRE(s2 != s1);
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(s1->references == 1);
|
|
|
|
REQUIRE(s2->references == 1);
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(resources.size() == sizeofString(11) + sizeofString(3));
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
}
|