Reduced memory consumption by not duplicating spaces and comments

This commit is contained in:
Benoit Blanchon
2016-12-29 17:26:16 +01:00
parent 8032a4b564
commit 3f96e070ce
20 changed files with 596 additions and 287 deletions

View File

@ -30,13 +30,19 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
ASSERT_NE(p1, p2);
}
TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
size_t mask = sizeof(void*) - 1;
static bool isAligned(void* ptr) {
const size_t mask = sizeof(void*) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
for (size_t size = 1; size <= sizeof(void*); size++) {
size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
ASSERT_EQ(0, addr & mask);
}
TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
// make room for tow but not three
buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this on is aligned by design
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one fits in the first block
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one requires a new block
}
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup) {

View File

@ -5,18 +5,20 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h>
#include <gtest/gtest.h>
class NoMemoryAllocator {
public:
void* allocate(size_t) { return NULL; }
void* allocate(size_t) {
return NULL;
}
void deallocate(void*) {}
};
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
protected:
Internals::BlockJsonBuffer<NoMemoryAllocator> _jsonBuffer;
DynamicJsonBufferBase<NoMemoryAllocator> _jsonBuffer;
};
TEST_F(DynamicJsonBuffer_NoMemory_Tests, FixCodeCoverage) {

View File

@ -0,0 +1,48 @@
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
TEST(DynamicJsonBuffer_String_Tests, WorksWhenBufferIsBigEnough) {
DynamicJsonBuffer jsonBuffer(6);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_STREQ("hello", str.c_str());
}
TEST(DynamicJsonBuffer_String_Tests, GrowsWhenBufferIsTooSmall) {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_STREQ("hello", str.c_str());
}
TEST(DynamicJsonBuffer_String_Tests, SizeIncreases) {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
ASSERT_EQ(0, jsonBuffer.size());
str.append('h');
ASSERT_EQ(1, jsonBuffer.size());
str.c_str();
ASSERT_EQ(2, jsonBuffer.size());
}

View File

@ -5,12 +5,12 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h>
#include <gtest/gtest.h>
class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
protected:
void with(JsonBuffer& jsonBuffer) {
void with(StaticJsonBufferBase& jsonBuffer) {
_jsonBuffer = &jsonBuffer;
}
@ -27,7 +27,7 @@ class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
}
private:
JsonBuffer* _jsonBuffer;
StaticJsonBufferBase* _jsonBuffer;
char _jsonString[256];
};
@ -86,3 +86,11 @@ TEST_F(StaticJsonBuffer_ParseArray_Tests, ConstCharPtrNull) {
.parseArray(static_cast<const char*>(0))
.success());
}
TEST_F(StaticJsonBuffer_ParseArray_Tests, CopyStringNotSpaces) {
StaticJsonBuffer<100> jsonBuffer;
jsonBuffer.parseArray(" [ \"1234567\" ] ");
ASSERT_EQ(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
}

View File

@ -10,7 +10,7 @@
class StaticJsonBuffer_ParseObject_Tests : public testing::Test {
protected:
void with(JsonBuffer& jsonBuffer) {
void with(StaticJsonBufferBase& jsonBuffer) {
_jsonBuffer = &jsonBuffer;
}
@ -27,7 +27,7 @@ class StaticJsonBuffer_ParseObject_Tests : public testing::Test {
}
private:
JsonBuffer* _jsonBuffer;
StaticJsonBufferBase* _jsonBuffer;
char _jsonString[256];
};

View File

@ -0,0 +1,48 @@
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
TEST(StaticJsonBuffer_String_Tests, WorksWhenBufferIsBigEnough) {
StaticJsonBuffer<6> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_STREQ("hello", str.c_str());
}
TEST(StaticJsonBuffer_String_Tests, ReturnsNullWhenTooSmall) {
StaticJsonBuffer<5> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_EQ(NULL, str.c_str());
}
TEST(StaticJsonBuffer_String_Tests, SizeIncreases) {
StaticJsonBuffer<5> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
ASSERT_EQ(0, jsonBuffer.size());
str.append('h');
ASSERT_EQ(1, jsonBuffer.size());
str.c_str();
ASSERT_EQ(2, jsonBuffer.size());
}