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
|
|
|
|
|
2018-11-16 10:26:59 +01:00
|
|
|
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
2018-11-09 17:27:32 +01:00
|
|
|
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
using namespace ARDUINOJSON_NAMESPACE;
|
|
|
|
|
|
|
|
TEST_CASE("StringBuilder") {
|
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)));
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2018-12-07 09:16:58 +01:00
|
|
|
StringBuilder str(&pool);
|
2018-11-09 17:27:32 +01:00
|
|
|
str.append("hello");
|
|
|
|
|
2018-12-07 09:16:58 +01:00
|
|
|
REQUIRE(str.complete() == 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*));
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2018-12-07 09:16:58 +01:00
|
|
|
StringBuilder str(&pool);
|
2018-11-16 10:26:59 +01:00
|
|
|
str.append("hello world!");
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2018-12-07 09:16:58 +01:00
|
|
|
REQUIRE(str.complete() == 0);
|
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)));
|
2018-11-09 17:27:32 +01:00
|
|
|
|
2018-12-07 09:16:58 +01:00
|
|
|
StringBuilder str(&pool);
|
2018-11-09 17:27:32 +01:00
|
|
|
str.append('h');
|
|
|
|
str.complete();
|
|
|
|
|
2020-07-08 09:38:27 +02:00
|
|
|
REQUIRE(JSON_STRING_SIZE(1) == pool.size());
|
2018-11-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
}
|