2018-11-09 17:27:32 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2020-01-09 15:48:38 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2020
|
2018-11-09 17:27:32 +01:00
|
|
|
// MIT License
|
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
#include <ArduinoJson/StringStorage/StringCopier.hpp>
|
2018-11-09 17:27:32 +01:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
using namespace ARDUINOJSON_NAMESPACE;
|
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
TEST_CASE("StringCopier") {
|
2019-12-24 16:41:00 +01:00
|
|
|
char buffer[4096];
|
|
|
|
|
2018-11-16 10:26:59 +01:00
|
|
|
SECTION("Works when buffer is big enough") {
|
2018-12-07 09:16:58 +01:00
|
|
|
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
2020-07-11 17:51:39 +02:00
|
|
|
StringCopier str;
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
str.startString(&pool);
|
2018-11-09 17:27:32 +01:00
|
|
|
str.append("hello");
|
2020-07-11 17:51:39 +02:00
|
|
|
str.append('\0');
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
REQUIRE(str.isValid() == true);
|
|
|
|
REQUIRE(str.c_str() == std::string("hello"));
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 10:26:59 +01:00
|
|
|
SECTION("Returns null when too small") {
|
2018-12-07 09:16:58 +01:00
|
|
|
MemoryPool pool(buffer, sizeof(void*));
|
2020-07-11 17:51:39 +02:00
|
|
|
StringCopier str;
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
str.startString(&pool);
|
2018-11-16 10:26:59 +01:00
|
|
|
str.append("hello world!");
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
REQUIRE(str.isValid() == false);
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Increases size of memory pool") {
|
2018-12-07 09:16:58 +01:00
|
|
|
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
2020-07-11 17:51:39 +02:00
|
|
|
StringCopier str;
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
str.startString(&pool);
|
2018-11-09 17:27:32 +01:00
|
|
|
str.append('h');
|
2020-07-21 20:15:31 +02:00
|
|
|
str.save(&pool);
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2020-07-11 17:51:39 +02:00
|
|
|
REQUIRE(1 == pool.size());
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 20:15:31 +02:00
|
|
|
|
|
|
|
static const char* addStringToPool(MemoryPool* pool, const char* s) {
|
|
|
|
StringCopier str;
|
|
|
|
str.startString(pool);
|
|
|
|
str.append(s);
|
|
|
|
str.append('\0');
|
|
|
|
return str.save(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("StringCopier::save() deduplicates strings") {
|
|
|
|
char buffer[4096];
|
|
|
|
MemoryPool pool(buffer, 4096);
|
|
|
|
|
|
|
|
SECTION("Basic") {
|
|
|
|
const char* s1 = addStringToPool(&pool, "hello");
|
|
|
|
const char* s2 = addStringToPool(&pool, "world");
|
|
|
|
const char* s3 = addStringToPool(&pool, "hello");
|
|
|
|
|
|
|
|
REQUIRE(s1 == s3);
|
|
|
|
REQUIRE(s2 != s3);
|
|
|
|
REQUIRE(pool.size() == 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Requires terminator") {
|
|
|
|
const char* s1 = addStringToPool(&pool, "hello world");
|
|
|
|
const char* s2 = addStringToPool(&pool, "hello");
|
|
|
|
|
|
|
|
REQUIRE(s2 != s1);
|
|
|
|
REQUIRE(pool.size() == 12 + 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Don't overrun") {
|
|
|
|
const char* s1 = addStringToPool(&pool, "hello world");
|
|
|
|
const char* s2 = addStringToPool(&pool, "wor");
|
|
|
|
|
|
|
|
REQUIRE(s2 != s1);
|
|
|
|
REQUIRE(pool.size() == 12 + 4);
|
|
|
|
}
|
|
|
|
}
|