diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a989039..193afe46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HEAD ---- * Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3) +* Added `StaticJsonBuffer::clear()` v5.10.1 ------- diff --git a/src/ArduinoJson/StaticJsonBuffer.hpp b/src/ArduinoJson/StaticJsonBuffer.hpp index 34b33696..0c31aad4 100644 --- a/src/ArduinoJson/StaticJsonBuffer.hpp +++ b/src/ArduinoJson/StaticJsonBuffer.hpp @@ -54,19 +54,29 @@ class StaticJsonBufferBase : public JsonBufferBase { StaticJsonBufferBase(char* buffer, size_t capa) : _buffer(buffer), _capacity(capa), _size(0) {} + // Gets the capacity of the buffer in bytes size_t capacity() const { return _capacity; } + + // Gets the current usage of the buffer in bytes size_t size() const { return _size; } + // Allocates the specified amount of bytes in the buffer virtual void* alloc(size_t bytes) { alignNextAlloc(); if (!canAlloc(bytes)) return NULL; return doAlloc(bytes); } + // Resets the size to zero. + // USE WITH CAUTION: this invalidates all previously allocated data + void clear() { + _size = 0; + } + String startString() { return String(this); } diff --git a/test/StaticJsonBuffer/CMakeLists.txt b/test/StaticJsonBuffer/CMakeLists.txt index d4425bb5..780678be 100644 --- a/test/StaticJsonBuffer/CMakeLists.txt +++ b/test/StaticJsonBuffer/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(StaticJsonBufferTests createObject.cpp parseArray.cpp parseObject.cpp + size.cpp startString.cpp ) diff --git a/test/StaticJsonBuffer/alloc.cpp b/test/StaticJsonBuffer/alloc.cpp index daea25a3..40948c8a 100644 --- a/test/StaticJsonBuffer/alloc.cpp +++ b/test/StaticJsonBuffer/alloc.cpp @@ -17,52 +17,39 @@ static bool isAligned(void *ptr) { TEST_CASE("StaticJsonBuffer::alloc()") { StaticJsonBuffer<64> buffer; - SECTION("CapacityMatchTemplateParameter") { - REQUIRE(64 == buffer.capacity()); + SECTION("Returns different addresses") { + void *p1 = buffer.alloc(1); + void *p2 = buffer.alloc(1); + REQUIRE(p1 != p2); } - SECTION("InitialSizeIsZero") { - 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") { + SECTION("Returns non-NULL when using full capacity") { void *p = buffer.alloc(64); REQUIRE(0 != p); } - SECTION("ReturnsNullWhenFull") { + SECTION("Returns NULL when full") { buffer.alloc(64); void *p = buffer.alloc(1); REQUIRE(0 == p); } - SECTION("ReturnsNullWhenTooSmall") { + SECTION("Returns NULL when buffer is too small") { void *p = buffer.alloc(65); REQUIRE(0 == p); } - SECTION("Alignment") { + 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); + } } diff --git a/test/StaticJsonBuffer/size.cpp b/test/StaticJsonBuffer/size.cpp new file mode 100644 index 00000000..152a4c64 --- /dev/null +++ b/test/StaticJsonBuffer/size.cpp @@ -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 +#include + +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()); + } +}