Added StaticJsonBuffer::clear()

This commit is contained in:
Benoit Blanchon
2017-06-17 14:50:21 +02:00
parent 508f936317
commit 789fa507b5
5 changed files with 72 additions and 28 deletions

View File

@ -5,6 +5,7 @@ HEAD
---- ----
* Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3) * Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3)
* Added `StaticJsonBuffer::clear()`
v5.10.1 v5.10.1
------- -------

View File

@ -54,19 +54,29 @@ class StaticJsonBufferBase : public JsonBufferBase<StaticJsonBufferBase> {
StaticJsonBufferBase(char* buffer, size_t capa) StaticJsonBufferBase(char* buffer, size_t capa)
: _buffer(buffer), _capacity(capa), _size(0) {} : _buffer(buffer), _capacity(capa), _size(0) {}
// Gets the capacity of the buffer in bytes
size_t capacity() const { size_t capacity() const {
return _capacity; return _capacity;
} }
// Gets the current usage of the buffer in bytes
size_t size() const { size_t size() const {
return _size; return _size;
} }
// Allocates the specified amount of bytes in the buffer
virtual void* alloc(size_t bytes) { virtual void* alloc(size_t bytes) {
alignNextAlloc(); alignNextAlloc();
if (!canAlloc(bytes)) return NULL; if (!canAlloc(bytes)) return NULL;
return doAlloc(bytes); return doAlloc(bytes);
} }
// Resets the size to zero.
// USE WITH CAUTION: this invalidates all previously allocated data
void clear() {
_size = 0;
}
String startString() { String startString() {
return String(this); return String(this);
} }

View File

@ -11,6 +11,7 @@ add_executable(StaticJsonBufferTests
createObject.cpp createObject.cpp
parseArray.cpp parseArray.cpp
parseObject.cpp parseObject.cpp
size.cpp
startString.cpp startString.cpp
) )

View File

@ -17,52 +17,39 @@ static bool isAligned(void *ptr) {
TEST_CASE("StaticJsonBuffer::alloc()") { TEST_CASE("StaticJsonBuffer::alloc()") {
StaticJsonBuffer<64> buffer; StaticJsonBuffer<64> buffer;
SECTION("CapacityMatchTemplateParameter") { SECTION("Returns different addresses") {
REQUIRE(64 == buffer.capacity()); void *p1 = buffer.alloc(1);
void *p2 = buffer.alloc(1);
REQUIRE(p1 != p2);
} }
SECTION("InitialSizeIsZero") { SECTION("Returns non-NULL when using full capacity") {
REQUIRE(0 == buffer.size());
}
SECTION("GrowsAfterAlloc") {
buffer.alloc(1);
REQUIRE(1U <= buffer.size());
buffer.alloc(1);
REQUIRE(2U <= buffer.size());
}
SECTION("DoesntGrowWhenFull") {
buffer.alloc(64);
buffer.alloc(1);
REQUIRE(64 == buffer.size());
}
SECTION("DoesntGrowWhenTooSmall") {
buffer.alloc(65);
REQUIRE(0 == buffer.size());
}
SECTION("ReturnsNonNull") {
void *p = buffer.alloc(64); void *p = buffer.alloc(64);
REQUIRE(0 != p); REQUIRE(0 != p);
} }
SECTION("ReturnsNullWhenFull") { SECTION("Returns NULL when full") {
buffer.alloc(64); buffer.alloc(64);
void *p = buffer.alloc(1); void *p = buffer.alloc(1);
REQUIRE(0 == p); REQUIRE(0 == p);
} }
SECTION("ReturnsNullWhenTooSmall") { SECTION("Returns NULL when buffer is too small") {
void *p = buffer.alloc(65); void *p = buffer.alloc(65);
REQUIRE(0 == p); REQUIRE(0 == p);
} }
SECTION("Alignment") { SECTION("Returns aligned pointers") {
for (size_t size = 1; size <= sizeof(void *); size++) { for (size_t size = 1; size <= sizeof(void *); size++) {
void *p = buffer.alloc(1); void *p = buffer.alloc(1);
REQUIRE(isAligned(p)); REQUIRE(isAligned(p));
} }
} }
SECTION("Returns same address after clear()") {
void *p1 = buffer.alloc(1);
buffer.clear();
void *p2 = buffer.alloc(1);
REQUIRE(p1 == p2);
}
} }

View File

@ -0,0 +1,45 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#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());
}
}