1 parent
f0546fab8c
commit
3b4c9d2d98
209 files changed
+25587
No files matched your search
@@ -0,0 +1,16 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(StaticJsonBufferTests
|
||||
alloc.cpp
|
||||
createArray.cpp
|
||||
createObject.cpp
|
||||
parseArray.cpp
|
||||
parseObject.cpp
|
||||
size.cpp
|
||||
startString.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(StaticJsonBufferTests catch)
|
||||
add_test(StaticJsonBuffer StaticJsonBufferTests)
|
||||
@@ -0,0 +1,52 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static bool isAligned(void *ptr) {
|
||||
const size_t mask = sizeof(void *) - 1;
|
||||
size_t addr = reinterpret_cast<size_t>(ptr);
|
||||
return (addr & mask) == 0;
|
||||
}
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::alloc()") {
|
||||
StaticJsonBuffer<64> buffer;
|
||||
|
||||
SECTION("Returns different addresses") {
|
||||
void *p1 = buffer.alloc(1);
|
||||
void *p2 = buffer.alloc(1);
|
||||
REQUIRE(p1 != p2);
|
||||
}
|
||||
|
||||
SECTION("Returns non-NULL when using full capacity") {
|
||||
void *p = buffer.alloc(64);
|
||||
REQUIRE(0 != p);
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when full") {
|
||||
buffer.alloc(64);
|
||||
void *p = buffer.alloc(1);
|
||||
REQUIRE(0 == p);
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when buffer is too small") {
|
||||
void *p = buffer.alloc(65);
|
||||
REQUIRE(0 == p);
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
for (size_t size = 1; size <= sizeof(void *); size++) {
|
||||
void *p = buffer.alloc(1);
|
||||
REQUIRE(isAligned(p));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Returns same address after clear()") {
|
||||
void *p1 = buffer.alloc(1);
|
||||
buffer.clear();
|
||||
void *p2 = buffer.alloc(1);
|
||||
REQUIRE(p1 == p2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::createArray()") {
|
||||
SECTION("GrowsWithArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE(JSON_ARRAY_SIZE(0) == json.size());
|
||||
|
||||
array.add("hello");
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) == json.size());
|
||||
|
||||
array.add("world");
|
||||
REQUIRE(JSON_ARRAY_SIZE(2) == json.size());
|
||||
}
|
||||
|
||||
SECTION("SucceedWhenBigEnough") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE(array.success());
|
||||
}
|
||||
|
||||
SECTION("FailsWhenTooSmall") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE_FALSE(array.success());
|
||||
}
|
||||
|
||||
SECTION("ArrayDoesntGrowWhenFull") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
REQUIRE(1 == array.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::createObject()") {
|
||||
SECTION("GrowsWithObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
|
||||
|
||||
obj["hello"];
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
|
||||
|
||||
obj["hello"] = 1;
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
|
||||
|
||||
obj["world"] = 2;
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
|
||||
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("SucceedWhenBigEnough") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
REQUIRE(object.success());
|
||||
}
|
||||
|
||||
SECTION("FailsWhenTooSmall") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
REQUIRE_FALSE(object.success());
|
||||
}
|
||||
|
||||
SECTION("ObjectDoesntGrowWhenFull") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
|
||||
REQUIRE(1 == obj.size());
|
||||
|
||||
char json[64];
|
||||
obj.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"hello\":1}") == json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::parseArray()") {
|
||||
SECTION("TooSmallBufferForEmptyArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
||||
char input[] = "[]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForEmptyArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
||||
char input[] = "[]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
|
||||
char input[] = "[1]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
|
||||
char input[] = "[1]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
|
||||
bufferTooSmall;
|
||||
char input[] = "[{}]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
|
||||
bufferOfRightSize;
|
||||
char input[] = "[{}]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
REQUIRE_FALSE(
|
||||
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
REQUIRE_FALSE(StaticJsonBuffer<100>()
|
||||
.parseArray(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
|
||||
SECTION("CopyStringNotSpaces") {
|
||||
StaticJsonBuffer<100> jsonBuffer;
|
||||
jsonBuffer.parseArray(" [ \"1234567\" ] ");
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == jsonBuffer.size());
|
||||
// note we use a string of 8 bytes to be sure that the StaticJsonBuffer
|
||||
// will not insert bytes to enforce alignement
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
TEST_CASE("StaticJsonBuffer::parseObject()") {
|
||||
SECTION("TooSmallBufferForEmptyObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||
char input[] = "{}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForEmptyObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||
char input[] = "{}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
|
||||
char input[] = "{\"a\":1}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
|
||||
char input[] = "{\"a\":1}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
|
||||
bufferTooSmall;
|
||||
char input[] = "{\"a\":[]}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
|
||||
bufferOfRightSize;
|
||||
char input[] = "{\"a\":[]}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
SECTION("CharPtrNull") {
|
||||
REQUIRE_FALSE(
|
||||
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
SECTION("ConstCharPtrNull") {
|
||||
REQUIRE_FALSE(StaticJsonBuffer<100>()
|
||||
.parseObject(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::size()") {
|
||||
StaticJsonBuffer<64> buffer;
|
||||
|
||||
SECTION("Capacity equals template parameter") {
|
||||
REQUIRE(64 == buffer.capacity());
|
||||
}
|
||||
|
||||
SECTION("Initial size is 0") {
|
||||
REQUIRE(0 == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("Increases after alloc()") {
|
||||
buffer.alloc(1);
|
||||
REQUIRE(1U <= buffer.size());
|
||||
buffer.alloc(1);
|
||||
REQUIRE(2U <= buffer.size());
|
||||
}
|
||||
|
||||
SECTION("Doesn't grow when buffer is full") {
|
||||
buffer.alloc(64);
|
||||
buffer.alloc(1);
|
||||
REQUIRE(64 == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("Does't grow when buffer is too small for alloc") {
|
||||
buffer.alloc(65);
|
||||
REQUIRE(0 == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("Goes back to zero after clear()") {
|
||||
buffer.alloc(1);
|
||||
buffer.clear();
|
||||
REQUIRE(0 == buffer.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::startString()") {
|
||||
SECTION("WorksWhenBufferIsBigEnough") {
|
||||
StaticJsonBuffer<6> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
REQUIRE(std::string("hello") == str.c_str());
|
||||
}
|
||||
|
||||
SECTION("ReturnsNullWhenTooSmall") {
|
||||
StaticJsonBuffer<5> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
REQUIRE(0 == str.c_str());
|
||||
}
|
||||
|
||||
SECTION("SizeIncreases") {
|
||||
StaticJsonBuffer<5> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
REQUIRE(0 == jsonBuffer.size());
|
||||
|
||||
str.append('h');
|
||||
REQUIRE(1 == jsonBuffer.size());
|
||||
|
||||
str.c_str();
|
||||
REQUIRE(2 == jsonBuffer.size());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user