Test that DynamicJsonBuffer.blockCount() increases as expected

This commit is contained in:
Benoit Blanchon
2014-12-13 20:52:59 +01:00
parent 19cce08b2b
commit d19a34152f
2 changed files with 35 additions and 6 deletions

View File

@ -15,29 +15,47 @@ namespace ArduinoJson {
// more suitable for embedded systems. // more suitable for embedded systems.
class DynamicJsonBuffer : public JsonBuffer { class DynamicJsonBuffer : public JsonBuffer {
public: public:
explicit DynamicJsonBuffer() : _size(0) {} explicit DynamicJsonBuffer() : _next(NULL), _size(0) {}
size_t size() const { return _size; } size_t size() const { return _size; }
size_t blockCount() const { return 1; } size_t blockCount() const { return _next ? _next->blockCount() + 1 : 1; }
static const size_t BLOCK_CAPACITY = 32; static const size_t BLOCK_CAPACITY = 32;
protected: protected:
virtual void* alloc(size_t bytes) { virtual void* alloc(size_t bytes) {
if (bytes > BLOCK_CAPACITY) return NULL; if (canAllocInThisBlock(bytes))
return allocInThisBlock(bytes);
else if (canAllocInOtherBlocks(bytes))
return allocInOtherBlocks(bytes);
else
return NULL;
}
private:
bool canAllocInThisBlock(size_t bytes) const {
return _size + bytes <= BLOCK_CAPACITY;
}
void* allocInThisBlock(size_t bytes) {
void* p = _buffer + _size; void* p = _buffer + _size;
_size += bytes; _size += bytes;
return p; return p;
} }
bool canAllocInOtherBlocks(size_t bytes) const {
bool canStore(size_t bytes) {
// by design a DynamicJsonBuffer can't alloc a block bigger than // by design a DynamicJsonBuffer can't alloc a block bigger than
// BLOCK_CAPACITY // BLOCK_CAPACITY
return bytes < BLOCK_CAPACITY; return bytes < BLOCK_CAPACITY;
} }
void* allocInOtherBlocks(size_t bytes) {
if (!_next) _next = new DynamicJsonBuffer();
return _next->alloc(bytes);
}
size_t _size; size_t _size;
uint8_t _buffer[BLOCK_CAPACITY]; uint8_t _buffer[BLOCK_CAPACITY];
DynamicJsonBuffer* _next;
}; };
} }

View File

@ -24,13 +24,24 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, InitialBlockCountIsOne) {
ASSERT_EQ(1, buffer.blockCount()); ASSERT_EQ(1, buffer.blockCount());
} }
TEST_F(DynamicJsonBuffer_Basic_Tests, GrowsAfterAlloc) { TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
buffer.alloc(1); buffer.alloc(1);
ASSERT_EQ(1, buffer.size()); ASSERT_EQ(1, buffer.size());
buffer.alloc(1); buffer.alloc(1);
ASSERT_EQ(2, buffer.size()); ASSERT_EQ(2, buffer.size());
} }
TEST_F(DynamicJsonBuffer_Basic_Tests, BlockCountDoesntChangeWhenNotFull) {
buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
ASSERT_EQ(1, buffer.blockCount());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, BlockCountChangesWhenFull) {
buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
buffer.alloc(1);
ASSERT_EQ(2, buffer.blockCount());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, CanAllocLessThanBlockCapacity) { TEST_F(DynamicJsonBuffer_Basic_Tests, CanAllocLessThanBlockCapacity) {
void* p = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY); void* p = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
ASSERT_TRUE(p); ASSERT_TRUE(p);