added library json

This commit is contained in:
hidaba
2023-05-14 11:02:22 +02:00
parent c60123ce97
commit ab8de582d8
421 changed files with 51080 additions and 6 deletions

View File

@ -0,0 +1,18 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2023, Benoit BLANCHON
# MIT License
add_executable(MemoryPoolTests
allocVariant.cpp
clear.cpp
saveString.cpp
size.cpp
StringCopier.cpp
)
add_test(MemoryPool MemoryPoolTests)
set_tests_properties(MemoryPool
PROPERTIES
LABELS "Catch"
)

View File

@ -0,0 +1,93 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/StringStorage/StringCopier.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
TEST_CASE("StringCopier") {
char buffer[4096];
SECTION("Works when buffer is big enough") {
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(5)));
StringCopier str(&pool);
str.startString();
str.append("hello");
REQUIRE(str.isValid() == true);
REQUIRE(str.str() == "hello");
REQUIRE(pool.overflowed() == false);
}
SECTION("Returns null when too small") {
MemoryPool pool(buffer, sizeof(void*));
StringCopier str(&pool);
str.startString();
str.append("hello world!");
REQUIRE(str.isValid() == false);
REQUIRE(pool.overflowed() == true);
}
SECTION("Increases size of memory pool") {
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
StringCopier str(&pool);
str.startString();
str.save();
REQUIRE(1 == pool.size());
REQUIRE(pool.overflowed() == false);
}
SECTION("Works when memory pool is 0 bytes") {
MemoryPool pool(buffer, 0);
StringCopier str(&pool);
str.startString();
REQUIRE(str.isValid() == false);
REQUIRE(pool.overflowed() == true);
}
}
static const char* addStringToPool(MemoryPool& pool, const char* s) {
StringCopier str(&pool);
str.startString();
str.append(s);
return str.save().c_str();
}
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);
}
}

View File

@ -0,0 +1,50 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
TEST_CASE("MemoryPool::allocVariant()") {
char buffer[4096];
SECTION("Returns different pointer") {
MemoryPool pool(buffer, sizeof(buffer));
VariantSlot* s1 = pool.allocVariant();
REQUIRE(s1 != 0);
VariantSlot* s2 = pool.allocVariant();
REQUIRE(s2 != 0);
REQUIRE(s1 != s2);
}
SECTION("Returns aligned pointers") {
MemoryPool pool(buffer, sizeof(buffer));
REQUIRE(isAligned(pool.allocVariant()));
REQUIRE(isAligned(pool.allocVariant()));
}
SECTION("Returns zero if capacity is 0") {
MemoryPool pool(buffer, 0);
REQUIRE(pool.allocVariant() == 0);
}
SECTION("Returns zero if buffer is null") {
MemoryPool pool(0, sizeof(buffer));
REQUIRE(pool.allocVariant() == 0);
}
SECTION("Returns zero if capacity is insufficient") {
MemoryPool pool(buffer, sizeof(VariantSlot));
pool.allocVariant();
REQUIRE(pool.allocVariant() == 0);
}
}

View File

@ -0,0 +1,32 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
static const size_t poolCapacity = 512;
TEST_CASE("MemoryPool::clear()") {
char buffer[poolCapacity];
MemoryPool pool(buffer, sizeof(buffer));
SECTION("Discards allocated variants") {
pool.allocVariant();
pool.clear();
REQUIRE(pool.size() == 0);
}
SECTION("Discards allocated strings") {
pool.saveString(adaptString(const_cast<char*>("123456789")));
REQUIRE(pool.size() == 10);
pool.clear();
REQUIRE(pool.size() == 0);
}
}

View File

@ -0,0 +1,106 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
static const char* saveString(MemoryPool& pool, const char* s) {
return pool.saveString(adaptString(const_cast<char*>(s)));
}
static const char* saveString(MemoryPool& pool, const char* s, size_t n) {
return pool.saveString(adaptString(s, n));
}
TEST_CASE("MemoryPool::saveString()") {
char buffer[32];
MemoryPool pool(buffer, 32);
SECTION("Duplicates different strings") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "world");
REQUIRE(a != b);
REQUIRE(pool.size() == 6 + 6);
}
SECTION("Deduplicates identical strings") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "hello");
REQUIRE(a == b);
REQUIRE(pool.size() == 6);
}
SECTION("Deduplicates identical strings that contain NUL") {
const char* a = saveString(pool, "hello\0world", 11);
const char* b = saveString(pool, "hello\0world", 11);
REQUIRE(a == b);
REQUIRE(pool.size() == 12);
}
SECTION("Reuse part of a string if it ends with NUL") {
const char* a = saveString(pool, "hello\0world", 11);
const char* b = saveString(pool, "hello");
REQUIRE(a == b);
REQUIRE(pool.size() == 12);
}
SECTION("Don't stop on first NUL") {
const char* a = saveString(pool, "hello");
const char* b = saveString(pool, "hello\0world", 11);
REQUIRE(a != b);
REQUIRE(pool.size() == 18);
}
SECTION("Returns NULL when full") {
REQUIRE(pool.capacity() == 32);
const void* p1 = saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
REQUIRE(p1 != 0);
REQUIRE(pool.size() == 32);
const void* p2 = saveString(pool, "b");
REQUIRE(p2 == 0);
}
SECTION("Returns NULL when pool is too small") {
const void* p = saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
REQUIRE(0 == p);
}
SECTION("Returns NULL when buffer is NULL") {
MemoryPool pool2(0, 32);
REQUIRE(0 == saveString(pool2, "a"));
}
SECTION("Returns NULL when capacity is 0") {
MemoryPool pool2(buffer, 0);
REQUIRE(0 == saveString(pool2, "a"));
}
SECTION("Returns same address after clear()") {
const void* a = saveString(pool, "hello");
pool.clear();
const void* b = saveString(pool, "world");
REQUIRE(a == b);
}
SECTION("Can use full capacity when fresh") {
const void* a = saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
REQUIRE(a != 0);
}
SECTION("Can use full capacity after clear") {
saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
pool.clear();
const void* a = saveString(pool, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
REQUIRE(a != 0);
}
}

View File

@ -0,0 +1,36 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
TEST_CASE("MemoryPool::capacity()") {
char buffer[4096];
const size_t capacity = 64;
MemoryPool pool(buffer, capacity);
REQUIRE(capacity == pool.capacity());
}
TEST_CASE("MemoryPool::size()") {
char buffer[4096];
MemoryPool pool(buffer, sizeof(buffer));
SECTION("Initial size is 0") {
REQUIRE(0 == pool.size());
}
SECTION("Doesn't grow when memory pool is full") {
const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
for (size_t i = 0; i < variantCount; i++)
pool.allocVariant();
size_t size = pool.size();
pool.allocVariant();
REQUIRE(size == pool.size());
}
}