8000 Test that DynamicJsonBuffer.size() is the sum of all blocks · littlestrange/ArduinoJson@13e907c · GitHub
[go: up one dir, main page]

Skip to content

Commit 13e907c

Browse files
committed
Test that DynamicJsonBuffer.size() is the sum of all blocks
1 parent d19a341 commit 13e907c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

include/ArduinoJson/DynamicJsonBuffer.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ namespace ArduinoJson {
1515
// more suitable for embedded systems.
1616
class DynamicJsonBuffer : public JsonBuffer {
1717
public:
18-
explicit DynamicJsonBuffer() : _next(NULL), _size(0) {}
18+
DynamicJsonBuffer() : _next(NULL), _size(0) {}
1919

20-
size_t size() const { return _size; }
20+
~DynamicJsonBuffer() { delete _next; }
2121

22-
size_t blockCount() const { return _next ? _next->blockCount() + 1 : 1; }
22+
size_t size() const { return _size + (_next ? _next->size() : 0); }
23+
24+
size_t blockCount() const { return 1 + (_next ? _next->blockCount() : 0); }
2325

2426
static const size_t BLOCK_CAPACITY = 32;
2527

@@ -43,10 +45,11 @@ class DynamicJsonBuffer : public JsonBuffer {
4345
_size += bytes;
4446
return p;
4547
}
48+
4649
bool canAllocInOtherBlocks(size_t bytes) const {
4750
// by design a DynamicJsonBuffer can't alloc a block bigger than
4851
// BLOCK_CAPACITY
49-
return bytes < BLOCK_CAPACITY;
52+
return bytes <= BLOCK_CAPACITY;
5053
}
5154

5255
void* allocInOtherBlocks(size_t bytes) {

test/DynamicJsonBuffer_Basic_Tests.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
2929
ASSERT_EQ(1, buffer.size());
3030
buffer.alloc(1);
3131
ASSERT_EQ(2, buffer.size());
32+
buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
33+
ASSERT_EQ(2 + DynamicJsonBuffer::BLOCK_CAPACITY, buffer.size());
3234
}
3335

3436
TEST_F(DynamicJsonBuffer_Basic_Tests, BlockCountDoesntChangeWhenNotFull) {
@@ -43,8 +45,10 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, BlockCountChangesWhenFull) {
4345
}
4446

4547
TEST_F(DynamicJsonBuffer_Basic_Tests, CanAllocLessThanBlockCapacity) {
46-
void* p = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
47-
ASSERT_TRUE(p);
48+
void* p1 = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
49+
ASSERT_TRUE(p1);
50+
void* p2 = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
51+
ASSERT_TRUE(p2);
4852
}
4953

5054
TEST_F(DynamicJsonBuffer_Basic_Tests, CantAllocMoreThanBlockCapacity) {

0 commit comments

Comments
 (0)
0