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
|
2020-07-21 20:15:31 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
|
|
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-03-20 12:28:34 +01:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2023-02-14 10:04:48 +01:00
|
|
|
using namespace ArduinoJson::detail;
|
2020-07-21 20:15:31 +02:00
|
|
|
|
2023-05-02 18:29:19 +02:00
|
|
|
static StringNode* saveString(MemoryPool& pool, const char* s) {
|
2023-04-07 14:43:16 +02:00
|
|
|
return pool.saveString(adaptString(s));
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 18:29:19 +02:00
|
|
|
static StringNode* saveString(MemoryPool& pool, const char* s, size_t n) {
|
2021-11-21 15:07:56 +01:00
|
|
|
return pool.saveString(adaptString(s, n));
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:15:31 +02:00
|
|
|
TEST_CASE("MemoryPool::saveString()") {
|
2023-03-20 12:28:34 +01:00
|
|
|
MemoryPool pool(32);
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
SECTION("Duplicates different strings") {
|
2023-05-02 18:29:19 +02:00
|
|
|
auto a = saveString(pool, "hello");
|
|
|
|
auto b = saveString(pool, "world");
|
|
|
|
REQUIRE(a->data != b->data);
|
|
|
|
REQUIRE(a->length == 5);
|
|
|
|
REQUIRE(b->length == 5);
|
|
|
|
REQUIRE(a->references == 1);
|
|
|
|
REQUIRE(b->references == 1);
|
2023-04-07 14:43:16 +02:00
|
|
|
REQUIRE(pool.size() == 2 * sizeofString(5));
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Deduplicates identical strings") {
|
2023-05-02 18:29:19 +02:00
|
|
|
auto a = saveString(pool, "hello");
|
|
|
|
auto b = saveString(pool, "hello");
|
2020-07-21 20:15:31 +02:00
|
|
|
REQUIRE(a == b);
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(a->length == 5);
|
|
|
|
REQUIRE(a->references == 2);
|
2023-04-07 14:43:16 +02:00
|
|
|
REQUIRE(pool.size() == sizeofString(5));
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:07:56 +01:00
|
|
|
SECTION("Deduplicates identical strings that contain NUL") {
|
2023-05-02 18:29:19 +02:00
|
|
|
auto a = saveString(pool, "hello\0world", 11);
|
|
|
|
auto b = saveString(pool, "hello\0world", 11);
|
2021-11-21 15:07:56 +01:00
|
|
|
REQUIRE(a == b);
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(a->length == 11);
|
|
|
|
REQUIRE(a->references == 2);
|
2023-04-07 14:43:16 +02:00
|
|
|
REQUIRE(pool.size() == sizeofString(11));
|
2021-11-21 15:07:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Don't stop on first NUL") {
|
2023-05-02 18:29:19 +02:00
|
|
|
auto a = saveString(pool, "hello");
|
|
|
|
auto b = saveString(pool, "hello\0world", 11);
|
2021-11-21 15:07:56 +01:00
|
|
|
REQUIRE(a != b);
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(a->length == 5);
|
|
|
|
REQUIRE(b->length == 11);
|
|
|
|
REQUIRE(a->references == 1);
|
|
|
|
REQUIRE(b->references == 1);
|
2023-04-07 14:43:16 +02:00
|
|
|
REQUIRE(pool.size() == sizeofString(5) + sizeofString(11));
|
2021-11-21 15:07:56 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 10:03:47 +02:00
|
|
|
SECTION("Returns NULL when allocation fails") {
|
2023-03-20 12:28:34 +01:00
|
|
|
MemoryPool pool2(32, FailingAllocator::instance());
|
2023-05-02 18:29:19 +02:00
|
|
|
REQUIRE(saveString(pool2, "a") == nullptr);
|
2020-07-21 20:15:31 +02:00
|
|
|
}
|
|
|
|
}
|