Changed unit testing framework from Google Test to Catch

This commit is contained in:
Benoit Blanchon
2017-04-18 18:22:24 +02:00
parent f2ef338cb8
commit df541a2a22
266 changed files with 15955 additions and 146149 deletions

View File

@ -6,12 +6,13 @@
# If you like this project, please add a star!
add_executable(DynamicJsonBufferTests
array.cpp
basics.cpp
noMemory.cpp
object.cpp
string.cpp
alloc.cpp
createArray.cpp
no_memory.cpp
createObject.cpp
strdup.cpp
startString.cpp
)
target_link_libraries(DynamicJsonBufferTests gtest)
target_link_libraries(DynamicJsonBufferTests catch)
add_test(DynamicJsonBuffer DynamicJsonBufferTests)

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>
static bool isAligned(void* ptr) {
const size_t mask = sizeof(void*) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
TEST_CASE("DynamicJsonBuffer::alloc()") {
DynamicJsonBuffer buffer;
SECTION("InitialSizeIsZero") {
REQUIRE(0 == buffer.size());
}
SECTION("SizeIncreasesAfterAlloc") {
buffer.alloc(1);
REQUIRE(1U <= buffer.size());
buffer.alloc(1);
REQUIRE(2U <= buffer.size());
}
SECTION("ReturnDifferentPointer") {
void* p1 = buffer.alloc(1);
void* p2 = buffer.alloc(2);
REQUIRE(p1 != p2);
}
SECTION("Alignment") {
// make room for two but not three
buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
REQUIRE(isAligned(buffer.alloc(1))); // this on is aligned by design
REQUIRE(isAligned(buffer.alloc(1))); // this one fits in the first block
REQUIRE(isAligned(buffer.alloc(1))); // this one requires a new block
}
}

View File

@ -1,33 +0,0 @@
// 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 <gtest/gtest.h>
TEST(DynamicJsonBuffer_Array_Tests, GrowsWithArray) {
DynamicJsonBuffer jsonBuffer;
JsonArray &array = jsonBuffer.createArray();
ASSERT_EQ(JSON_ARRAY_SIZE(0), jsonBuffer.size());
array.add("hello");
ASSERT_EQ(JSON_ARRAY_SIZE(1), jsonBuffer.size());
array.add("world");
ASSERT_EQ(JSON_ARRAY_SIZE(2), jsonBuffer.size());
}
TEST(DynamicJsonBuffer_Array_Tests, CanAdd1000Values) {
DynamicJsonBuffer jsonBuffer;
JsonArray &array = jsonBuffer.createArray();
for (int i = 1; i <= 1000; i++) {
array.add("hello");
ASSERT_EQ(array.size(), i);
}
}

View File

@ -1,59 +0,0 @@
// 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 <gtest/gtest.h>
class DynamicJsonBuffer_Basic_Tests : public testing::Test {
protected:
DynamicJsonBuffer buffer;
};
TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) {
ASSERT_EQ(0, buffer.size());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
buffer.alloc(1);
ASSERT_LE(1U, buffer.size());
buffer.alloc(1);
ASSERT_LE(2U, buffer.size());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
void* p1 = buffer.alloc(1);
void* p2 = buffer.alloc(2);
ASSERT_NE(p1, p2);
}
static bool isAligned(void* ptr) {
const size_t mask = sizeof(void*) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
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) {
char original[] = "hello";
char* copy = buffer.strdup(original);
strcpy(original, "world");
ASSERT_STREQ("hello", copy);
}
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup_givenNull) {
const char* original = NULL;
char* copy = buffer.strdup(original);
ASSERT_EQ(NULL, copy);
}

View File

@ -0,0 +1,31 @@
// 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("DynamicJsonBuffer::createArray()") {
DynamicJsonBuffer jsonBuffer;
JsonArray &array = jsonBuffer.createArray();
SECTION("GrowsWithArray") {
REQUIRE(JSON_ARRAY_SIZE(0) == jsonBuffer.size());
array.add("hello");
REQUIRE(JSON_ARRAY_SIZE(1) == jsonBuffer.size());
array.add("world");
REQUIRE(JSON_ARRAY_SIZE(2) == jsonBuffer.size());
}
SECTION("CanAdd1000Values") {
for (size_t i = 1; i <= 1000; i++) {
array.add("hello");
REQUIRE(array.size() == i);
}
}
}

View File

@ -6,20 +6,20 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
TEST(DynamicJsonBuffer_Object_Tests, GrowsWithObject) {
TEST_CASE("DynamicJsonBuffer::createObject()") {
DynamicJsonBuffer json;
JsonObject &obj = json.createObject();
ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size());
REQUIRE(JSON_OBJECT_SIZE(0) == json.size());
obj["hello"] = 1;
ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
REQUIRE(JSON_OBJECT_SIZE(1) == json.size());
obj["world"] = 2;
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
REQUIRE(JSON_OBJECT_SIZE(2) == json.size());
obj["world"] = 3; // <- same key, should not grow
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
REQUIRE(JSON_OBJECT_SIZE(2) == json.size());
}

View File

@ -1,52 +0,0 @@
// 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 <gtest/gtest.h>
class NoMemoryAllocator {
public:
void* allocate(size_t) {
return NULL;
}
void deallocate(void*) {}
};
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
protected:
DynamicJsonBufferBase<NoMemoryAllocator> _jsonBuffer;
};
TEST_F(DynamicJsonBuffer_NoMemory_Tests, FixCodeCoverage) {
// call this function to fix code coverage
NoMemoryAllocator().deallocate(NULL);
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, CreateArray) {
ASSERT_FALSE(_jsonBuffer.createArray().success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, CreateObject) {
ASSERT_FALSE(_jsonBuffer.createObject().success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, ParseArray) {
char json[] = "[]";
ASSERT_FALSE(_jsonBuffer.parseArray(json).success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, ParseObject) {
char json[] = "{}";
ASSERT_FALSE(_jsonBuffer.parseObject(json).success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, String) {
DynamicJsonBufferBase<NoMemoryAllocator>::String str =
_jsonBuffer.startString();
str.append('!');
ASSERT_EQ(NULL, str.c_str());
}

View File

@ -0,0 +1,50 @@
// 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>
struct NoMemoryAllocator {
void* allocate(size_t) {
return NULL;
}
void deallocate(void*) {}
};
TEST_CASE("DynamicJsonBuffer no memory") {
DynamicJsonBufferBase<NoMemoryAllocator> _jsonBuffer;
SECTION("FixCodeCoverage") {
// call this function to fix code coverage
NoMemoryAllocator().deallocate(NULL);
}
SECTION("createArray()") {
REQUIRE_FALSE(_jsonBuffer.createArray().success());
}
SECTION("createObject()") {
REQUIRE_FALSE(_jsonBuffer.createObject().success());
}
SECTION("parseArray()") {
char json[] = "[]";
REQUIRE_FALSE(_jsonBuffer.parseArray(json).success());
}
SECTION("parseObject()") {
char json[] = "{}";
REQUIRE_FALSE(_jsonBuffer.parseObject(json).success());
}
SECTION("startString()") {
DynamicJsonBufferBase<NoMemoryAllocator>::String str =
_jsonBuffer.startString();
str.append('!');
REQUIRE(0 == str.c_str());
}
}

View File

@ -0,0 +1,50 @@
// 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("DynamicJsonBuffer::startString()") {
SECTION("WorksWhenBufferIsBigEnough") {
DynamicJsonBuffer jsonBuffer(6);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
}
SECTION("GrowsWhenBufferIsTooSmall") {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
}
SECTION("SizeIncreases") {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
REQUIRE(0 == jsonBuffer.size());
str.append('h');
REQUIRE(1 == jsonBuffer.size());
str.c_str();
REQUIRE(2 == jsonBuffer.size());
}
}

View File

@ -0,0 +1,26 @@
// 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("DynamicJsonBuffer::strdup()") {
DynamicJsonBuffer buffer;
SECTION("Should return a copy") {
char original[] = "hello";
char* copy = buffer.strdup(original);
strcpy(original, "world");
REQUIRE(std::string("hello") == copy);
}
SECTION("Given NULL, return NULL") {
const char* original = NULL;
char* copy = buffer.strdup(original);
REQUIRE(0 == copy);
}
}

View File

@ -1,48 +0,0 @@
// 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 <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());
}