Added support for CMake's unity builds

This commit is contained in:
Benoit Blanchon
2019-12-24 16:41:00 +01:00
parent 9723682d20
commit 00c391320c
20 changed files with 226 additions and 223 deletions

View File

@ -18,4 +18,6 @@ add_executable(JsonDeserializerTests
) )
target_link_libraries(JsonDeserializerTests catch) target_link_libraries(JsonDeserializerTests catch)
set_target_properties(JsonDeserializerTests PROPERTIES UNITY_BUILD OFF)
add_test(JsonDeserializer JsonDeserializerTests) add_test(JsonDeserializer JsonDeserializerTests)

View File

@ -5,7 +5,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #include <catch.hpp>
static void check(JsonArray array, std::string expected) { static void checkArray(JsonArray array, std::string expected) {
std::string actual; std::string actual;
size_t actualLen = serializeJsonPretty(array, actual); size_t actualLen = serializeJsonPretty(array, actual);
size_t measuredLen = measureJsonPretty(array); size_t measuredLen = measureJsonPretty(array);
@ -19,13 +19,13 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
JsonArray array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
SECTION("Empty") { SECTION("Empty") {
check(array, "[]"); checkArray(array, "[]");
} }
SECTION("OneElement") { SECTION("OneElement") {
array.add(1); array.add(1);
check(array, checkArray(array,
"[\r\n" "[\r\n"
" 1\r\n" " 1\r\n"
"]"); "]");
@ -35,7 +35,7 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
array.add(1); array.add(1);
array.add(2); array.add(2);
check(array, checkArray(array,
"[\r\n" "[\r\n"
" 1,\r\n" " 1,\r\n"
" 2\r\n" " 2\r\n"
@ -46,7 +46,7 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
array.createNestedArray(); array.createNestedArray();
array.createNestedArray(); array.createNestedArray();
check(array, checkArray(array,
"[\r\n" "[\r\n"
" [],\r\n" " [],\r\n"
" []\r\n" " []\r\n"
@ -61,7 +61,7 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
JsonObject nested2 = array.createNestedObject(); JsonObject nested2 = array.createNestedObject();
nested2["key"] = 3; nested2["key"] = 3;
check(array, checkArray(array,
"[\r\n" "[\r\n"
" [\r\n" " [\r\n"
" 1,\r\n" " 1,\r\n"

View File

@ -6,7 +6,7 @@
#include <catch.hpp> #include <catch.hpp>
#include <string> #include <string>
void check(const JsonObject obj, const std::string &expected) { static void checkObject(const JsonObject obj, const std::string &expected) {
char actual[256]; char actual[256];
size_t actualLen = serializeJson(obj, actual); size_t actualLen = serializeJson(obj, actual);
size_t measuredLen = measureJson(obj); size_t measuredLen = measureJson(obj);
@ -21,14 +21,14 @@ TEST_CASE("serializeJson(JsonObject)") {
JsonObject obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("EmptyObject") { SECTION("EmptyObject") {
check(obj, "{}"); checkObject(obj, "{}");
} }
SECTION("TwoStrings") { SECTION("TwoStrings") {
obj["key1"] = "value1"; obj["key1"] = "value1";
obj["key2"] = "value2"; obj["key2"] = "value2";
check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}"); checkObject(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
} }
SECTION("RemoveFirst") { SECTION("RemoveFirst") {
@ -36,7 +36,7 @@ TEST_CASE("serializeJson(JsonObject)") {
obj["key2"] = "value2"; obj["key2"] = "value2";
obj.remove("key1"); obj.remove("key1");
check(obj, "{\"key2\":\"value2\"}"); checkObject(obj, "{\"key2\":\"value2\"}");
} }
SECTION("RemoveLast") { SECTION("RemoveLast") {
@ -44,7 +44,7 @@ TEST_CASE("serializeJson(JsonObject)") {
obj["key2"] = "value2"; obj["key2"] = "value2";
obj.remove("key2"); obj.remove("key2");
check(obj, "{\"key1\":\"value1\"}"); checkObject(obj, "{\"key1\":\"value1\"}");
} }
SECTION("RemoveUnexistingKey") { SECTION("RemoveUnexistingKey") {
@ -52,44 +52,44 @@ TEST_CASE("serializeJson(JsonObject)") {
obj["key2"] = "value2"; obj["key2"] = "value2";
obj.remove("key3"); obj.remove("key3");
check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}"); checkObject(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
} }
SECTION("ReplaceExistingKey") { SECTION("ReplaceExistingKey") {
obj["key"] = "value1"; obj["key"] = "value1";
obj["key"] = "value2"; obj["key"] = "value2";
check(obj, "{\"key\":\"value2\"}"); checkObject(obj, "{\"key\":\"value2\"}");
} }
SECTION("TwoIntegers") { SECTION("TwoIntegers") {
obj["a"] = 1; obj["a"] = 1;
obj["b"] = 2; obj["b"] = 2;
check(obj, "{\"a\":1,\"b\":2}"); checkObject(obj, "{\"a\":1,\"b\":2}");
} }
SECTION("serialized(const char*)") { SECTION("serialized(const char*)") {
obj["a"] = serialized("[1,2]"); obj["a"] = serialized("[1,2]");
obj["b"] = serialized("[4,5]"); obj["b"] = serialized("[4,5]");
check(obj, "{\"a\":[1,2],\"b\":[4,5]}"); checkObject(obj, "{\"a\":[1,2],\"b\":[4,5]}");
} }
SECTION("Two doubles") { SECTION("Two doubles") {
obj["a"] = 12.34; obj["a"] = 12.34;
obj["b"] = 56.78; obj["b"] = 56.78;
check(obj, "{\"a\":12.34,\"b\":56.78}"); checkObject(obj, "{\"a\":12.34,\"b\":56.78}");
} }
SECTION("TwoNull") { SECTION("TwoNull") {
obj["a"] = static_cast<char *>(0); obj["a"] = static_cast<char *>(0);
obj["b"] = static_cast<char *>(0); obj["b"] = static_cast<char *>(0);
check(obj, "{\"a\":null,\"b\":null}"); checkObject(obj, "{\"a\":null,\"b\":null}");
} }
SECTION("TwoBooleans") { SECTION("TwoBooleans") {
obj["a"] = true; obj["a"] = true;
obj["b"] = false; obj["b"] = false;
check(obj, "{\"a\":true,\"b\":false}"); checkObject(obj, "{\"a\":true,\"b\":false}");
} }
SECTION("ThreeNestedArrays") { SECTION("ThreeNestedArrays") {
@ -100,7 +100,7 @@ TEST_CASE("serializeJson(JsonObject)") {
obj["b"] = b.to<JsonArray>(); obj["b"] = b.to<JsonArray>();
obj["c"] = c.to<JsonArray>(); obj["c"] = c.to<JsonArray>();
check(obj, "{\"a\":[],\"b\":[],\"c\":[]}"); checkObject(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
} }
SECTION("ThreeNestedObjects") { SECTION("ThreeNestedObjects") {
@ -111,6 +111,6 @@ TEST_CASE("serializeJson(JsonObject)") {
obj["b"] = b.to<JsonObject>(); obj["b"] = b.to<JsonObject>();
obj["c"] = c.to<JsonObject>(); obj["c"] = c.to<JsonObject>();
check(obj, "{\"a\":{},\"b\":{},\"c\":{}}"); checkObject(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
} }
} }

View File

@ -6,7 +6,8 @@
#include <catch.hpp> #include <catch.hpp>
#include <string> #include <string>
void check(const JsonObject obj, const std::string expected) { static void checkObjectPretty(const JsonObject obj,
const std::string expected) {
char json[256]; char json[256];
size_t actualLen = serializeJsonPretty(obj, json); size_t actualLen = serializeJsonPretty(obj, json);
@ -22,13 +23,13 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
JsonObject obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("EmptyObject") { SECTION("EmptyObject") {
check(obj, "{}"); checkObjectPretty(obj, "{}");
} }
SECTION("OneMember") { SECTION("OneMember") {
obj["key"] = "value"; obj["key"] = "value";
check(obj, checkObjectPretty(obj,
"{\r\n" "{\r\n"
" \"key\": \"value\"\r\n" " \"key\": \"value\"\r\n"
"}"); "}");
@ -38,7 +39,7 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
obj["key1"] = "value1"; obj["key1"] = "value1";
obj["key2"] = "value2"; obj["key2"] = "value2";
check(obj, checkObjectPretty(obj,
"{\r\n" "{\r\n"
" \"key1\": \"value1\",\r\n" " \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n" " \"key2\": \"value2\"\r\n"
@ -49,7 +50,7 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
obj.createNestedObject("key1"); obj.createNestedObject("key1");
obj.createNestedArray("key2"); obj.createNestedArray("key2");
check(obj, checkObjectPretty(obj,
"{\r\n" "{\r\n"
" \"key1\": {},\r\n" " \"key1\": {},\r\n"
" \"key2\": []\r\n" " \"key2\": []\r\n"
@ -63,7 +64,7 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
JsonArray nested2 = obj.createNestedArray("key2"); JsonArray nested2 = obj.createNestedArray("key2");
nested2.add(2); nested2.add(2);
check(obj, checkObjectPretty(obj,
"{\r\n" "{\r\n"
" \"key1\": {\r\n" " \"key1\": {\r\n"
" \"a\": 1\r\n" " \"a\": 1\r\n"

View File

@ -2,16 +2,6 @@
#include <catch.hpp> #include <catch.hpp>
#include <limits> #include <limits>
template <typename T>
void check(T value, const std::string &expected) {
DynamicJsonDocument doc(4096);
doc.to<JsonVariant>().set(value);
char buffer[256] = "";
size_t returnValue = serializeJson(doc, buffer, sizeof(buffer));
REQUIRE(expected == buffer);
REQUIRE(expected.size() == returnValue);
}
TEST_CASE("serializeJson(MemberProxy)") { TEST_CASE("serializeJson(MemberProxy)") {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
deserializeJson(doc, "{\"hello\":42}"); deserializeJson(doc, "{\"hello\":42}");

View File

@ -6,8 +6,6 @@
#include <stdint.h> #include <stdint.h>
#include <catch.hpp> #include <catch.hpp>
static const char* null = 0;
TEST_CASE("JsonVariant::add()") { TEST_CASE("JsonVariant::add()") {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
JsonVariant var = doc.to<JsonVariant>(); JsonVariant var = doc.to<JsonVariant>();

View File

@ -10,9 +10,9 @@ namespace my {
using ARDUINOJSON_NAMESPACE::isinf; using ARDUINOJSON_NAMESPACE::isinf;
} // namespace my } // namespace my
static const char* null = 0;
TEST_CASE("JsonVariant::as()") { TEST_CASE("JsonVariant::as()") {
static const char* null = 0;
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>(); JsonVariant variant = doc.to<JsonVariant>();

View File

@ -6,8 +6,6 @@
#include <stdint.h> #include <stdint.h>
#include <catch.hpp> #include <catch.hpp>
static const char* null = 0;
TEST_CASE("JsonVariant::clear()") { TEST_CASE("JsonVariant::clear()") {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
JsonVariant var = doc.to<JsonVariant>(); JsonVariant var = doc.to<JsonVariant>();

View File

@ -5,8 +5,6 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #include <catch.hpp>
static const char* null = 0;
template <typename T> template <typename T>
void checkEquals(T a, T b) { void checkEquals(T a, T b) {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
@ -70,6 +68,8 @@ void checkComparisons(T low, T mid, T high) {
} }
TEST_CASE("JsonVariant comparisons") { TEST_CASE("JsonVariant comparisons") {
static const char* null = 0;
SECTION("Double") { SECTION("Double") {
checkComparisons<double>(123.44, 123.45, 123.46); checkComparisons<double>(123.44, 123.45, 123.46);
} }

View File

@ -6,8 +6,6 @@
#include <stdint.h> #include <stdint.h>
#include <catch.hpp> #include <catch.hpp>
static const char* null = 0;
TEST_CASE("JsonVariant::createNestedObject()") { TEST_CASE("JsonVariant::createNestedObject()") {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>(); JsonVariant variant = doc.to<JsonVariant>();

View File

@ -8,9 +8,9 @@
using namespace ARDUINOJSON_NAMESPACE; using namespace ARDUINOJSON_NAMESPACE;
static char buffer[4096];
TEST_CASE("StringBuilder") { TEST_CASE("StringBuilder") {
char buffer[4096];
SECTION("Works when buffer is big enough") { SECTION("Works when buffer is big enough") {
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6))); MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));

View File

@ -7,9 +7,9 @@
using namespace ARDUINOJSON_NAMESPACE; using namespace ARDUINOJSON_NAMESPACE;
static char buffer[4096];
TEST_CASE("MemoryPool::allocVariant()") { TEST_CASE("MemoryPool::allocVariant()") {
char buffer[4096];
SECTION("Returns different pointer") { SECTION("Returns different pointer") {
MemoryPool pool(buffer, sizeof(buffer)); MemoryPool pool(buffer, sizeof(buffer));

View File

@ -7,15 +7,15 @@
using namespace ARDUINOJSON_NAMESPACE; using namespace ARDUINOJSON_NAMESPACE;
char buffer[4096];
TEST_CASE("MemoryPool::capacity()") { TEST_CASE("MemoryPool::capacity()") {
char buffer[4096];
const size_t capacity = 64; const size_t capacity = 64;
MemoryPool pool(buffer, capacity); MemoryPool pool(buffer, capacity);
REQUIRE(capacity == pool.capacity()); REQUIRE(capacity == pool.capacity());
} }
TEST_CASE("MemoryPool::size()") { TEST_CASE("MemoryPool::size()") {
char buffer[4096];
MemoryPool pool(buffer, sizeof(buffer)); MemoryPool pool(buffer, sizeof(buffer));
SECTION("Initial size is 0") { SECTION("Initial size is 0") {

View File

@ -14,6 +14,8 @@ add_executable(MiscTests
) )
target_link_libraries(MiscTests catch) target_link_libraries(MiscTests catch)
set_target_properties(MiscTests PROPERTIES UNITY_BUILD OFF)
add_test(Misc MiscTests) add_test(Misc MiscTests)

View File

@ -21,4 +21,6 @@ add_executable(MixedConfigurationTests
) )
target_link_libraries(MixedConfigurationTests catch) target_link_libraries(MixedConfigurationTests catch)
set_target_properties(MixedConfigurationTests PROPERTIES UNITY_BUILD OFF)
add_test(MixedConfiguration MixedConfigurationTests) add_test(MixedConfiguration MixedConfigurationTests)

View File

@ -6,7 +6,8 @@
#include <catch.hpp> #include <catch.hpp>
template <typename T> template <typename T>
void check(T value, const char* expected_data, size_t expected_len) { static void checkVariant(T value, const char* expected_data,
size_t expected_len) {
DynamicJsonDocument doc(4096); DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>(); JsonVariant variant = doc.to<JsonVariant>();
variant.set(value); variant.set(value);
@ -19,120 +20,122 @@ void check(T value, const char* expected_data, size_t expected_len) {
} }
template <typename T, size_t N> template <typename T, size_t N>
void check(T value, const char (&expected_data)[N]) { static void checkVariant(T value, const char (&expected_data)[N]) {
const size_t expected_len = N - 1; const size_t expected_len = N - 1;
check(value, expected_data, expected_len); checkVariant(value, expected_data, expected_len);
} }
template <typename T> template <typename T>
void check(T value, const std::string& expected) { static void checkVariant(T value, const std::string& expected) {
check(value, expected.data(), expected.length()); checkVariant(value, expected.data(), expected.length());
} }
TEST_CASE("serialize MsgPack value") { TEST_CASE("serialize MsgPack value") {
SECTION("undefined") { SECTION("undefined") {
check(JsonVariant(), "\xC0"); // we represent undefined as nil checkVariant(JsonVariant(), "\xC0"); // we represent undefined as nil
} }
SECTION("nil") { SECTION("nil") {
const char* nil = 0; // ArduinoJson uses a string for null const char* nil = 0; // ArduinoJson uses a string for null
check(nil, "\xC0"); checkVariant(nil, "\xC0");
} }
SECTION("bool") { SECTION("bool") {
check(false, "\xC2"); checkVariant(false, "\xC2");
check(true, "\xC3"); checkVariant(true, "\xC3");
} }
SECTION("positive fixint") { SECTION("positive fixint") {
check(0, "\x00"); checkVariant(0, "\x00");
check(127, "\x7F"); checkVariant(127, "\x7F");
} }
SECTION("uint 8") { SECTION("uint 8") {
check(128, "\xCC\x80"); checkVariant(128, "\xCC\x80");
check(255, "\xCC\xFF"); checkVariant(255, "\xCC\xFF");
} }
SECTION("uint 16") { SECTION("uint 16") {
check(256, "\xCD\x01\x00"); checkVariant(256, "\xCD\x01\x00");
check(0xFFFF, "\xCD\xFF\xFF"); checkVariant(0xFFFF, "\xCD\xFF\xFF");
} }
SECTION("uint 32") { SECTION("uint 32") {
check(0x00010000U, "\xCE\x00\x01\x00\x00"); checkVariant(0x00010000U, "\xCE\x00\x01\x00\x00");
check(0x12345678U, "\xCE\x12\x34\x56\x78"); checkVariant(0x12345678U, "\xCE\x12\x34\x56\x78");
check(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF"); checkVariant(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF");
} }
#if ARDUINOJSON_USE_LONG_LONG #if ARDUINOJSON_USE_LONG_LONG
SECTION("uint 64") { SECTION("uint 64") {
check(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00"); checkVariant(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00");
check(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0"); checkVariant(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
check(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); checkVariant(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
} }
#endif #endif
SECTION("negative fixint") { SECTION("negative fixint") {
check(-1, "\xFF"); checkVariant(-1, "\xFF");
check(-32, "\xE0"); checkVariant(-32, "\xE0");
} }
SECTION("int 8") { SECTION("int 8") {
check(-33, "\xD0\xDF"); checkVariant(-33, "\xD0\xDF");
check(-128, "\xD0\x80"); checkVariant(-128, "\xD0\x80");
} }
SECTION("int 16") { SECTION("int 16") {
check(-129, "\xD1\xFF\x7F"); checkVariant(-129, "\xD1\xFF\x7F");
check(-32768, "\xD1\x80\x00"); checkVariant(-32768, "\xD1\x80\x00");
} }
SECTION("int 32") { SECTION("int 32") {
check(-32769, "\xD2\xFF\xFF\x7F\xFF"); checkVariant(-32769, "\xD2\xFF\xFF\x7F\xFF");
check(-2147483647 - 1, "\xD2\x80\x00\x00\x00"); checkVariant(-2147483647 - 1, "\xD2\x80\x00\x00\x00");
} }
#if ARDUINOJSON_USE_LONG_LONG #if ARDUINOJSON_USE_LONG_LONG
SECTION("int 64") { SECTION("int 64") {
check(int64_t(0xFEDCBA9876543210), "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10"); checkVariant(int64_t(0xFEDCBA9876543210),
"\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10");
} }
#endif #endif
SECTION("float 32") { SECTION("float 32") {
check(1.25, "\xCA\x3F\xA0\x00\x00"); checkVariant(1.25, "\xCA\x3F\xA0\x00\x00");
} }
SECTION("float 64") { SECTION("float 64") {
check(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F"); checkVariant(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F");
} }
SECTION("fixstr") { SECTION("fixstr") {
check("", "\xA0"); checkVariant("", "\xA0");
check("hello world hello world hello !", checkVariant("hello world hello world hello !",
"\xBFhello world hello world hello !"); "\xBFhello world hello world hello !");
} }
SECTION("str 8") { SECTION("str 8") {
check("hello world hello world hello !!", checkVariant("hello world hello world hello !!",
"\xD9\x20hello world hello world hello !!"); "\xD9\x20hello world hello world hello !!");
} }
SECTION("str 16") { SECTION("str 16") {
std::string shortest(256, '?'); std::string shortest(256, '?');
check(shortest.c_str(), std::string("\xDA\x01\x00", 3) + shortest); checkVariant(shortest.c_str(), std::string("\xDA\x01\x00", 3) + shortest);
std::string longest(65535, '?'); std::string longest(65535, '?');
check(longest.c_str(), std::string("\xDA\xFF\xFF", 3) + longest); checkVariant(longest.c_str(), std::string("\xDA\xFF\xFF", 3) + longest);
} }
SECTION("str 32") { SECTION("str 32") {
std::string shortest(65536, '?'); std::string shortest(65536, '?');
check(shortest.c_str(), std::string("\xDB\x00\x01\x00\x00", 5) + shortest); checkVariant(shortest.c_str(),
std::string("\xDB\x00\x01\x00\x00", 5) + shortest);
} }
SECTION("serialized(const char*)") { SECTION("serialized(const char*)") {
check(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF"); checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
check(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00"); checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
} }
} }

View File

@ -9,4 +9,5 @@ add_executable(NumbersTests
) )
target_link_libraries(NumbersTests catch) target_link_libraries(NumbersTests catch)
add_test(Numbers NumbersTests) add_test(Numbers NumbersTests)

View File

@ -12,7 +12,7 @@
using namespace ARDUINOJSON_NAMESPACE; using namespace ARDUINOJSON_NAMESPACE;
template <typename T> template <typename T>
void check(const char* input, T expected) { void checkFloat(const char* input, T expected) {
CAPTURE(input); CAPTURE(input);
REQUIRE(parseFloat<T>(input) == Approx(expected)); REQUIRE(parseFloat<T>(input) == Approx(expected));
} }
@ -38,51 +38,54 @@ void checkInf(const char* input, bool negative) {
TEST_CASE("parseFloat<float>()") { TEST_CASE("parseFloat<float>()") {
SECTION("Float_Short_NoExponent") { SECTION("Float_Short_NoExponent") {
check<float>("3.14", 3.14f); checkFloat<float>("3.14", 3.14f);
check<float>("-3.14", -3.14f); checkFloat<float>("-3.14", -3.14f);
check<float>("+3.14", +3.14f); checkFloat<float>("+3.14", +3.14f);
} }
SECTION("Short_NoDot") { SECTION("Short_NoDot") {
check<float>("1E+38", 1E+38f); checkFloat<float>("1E+38", 1E+38f);
check<float>("-1E+38", -1E+38f); checkFloat<float>("-1E+38", -1E+38f);
check<float>("+1E-38", +1E-38f); checkFloat<float>("+1E-38", +1E-38f);
check<float>("+1e+38", +1e+38f); checkFloat<float>("+1e+38", +1e+38f);
check<float>("-1e-38", -1e-38f); checkFloat<float>("-1e-38", -1e-38f);
} }
SECTION("Max") { SECTION("Max") {
check<float>("340.2823e+36", 3.402823e+38f); checkFloat<float>("340.2823e+36", 3.402823e+38f);
check<float>("34.02823e+37", 3.402823e+38f); checkFloat<float>("34.02823e+37", 3.402823e+38f);
check<float>("3.402823e+38", 3.402823e+38f); checkFloat<float>("3.402823e+38", 3.402823e+38f);
check<float>("0.3402823e+39", 3.402823e+38f); checkFloat<float>("0.3402823e+39", 3.402823e+38f);
check<float>("0.03402823e+40", 3.402823e+38f); checkFloat<float>("0.03402823e+40", 3.402823e+38f);
check<float>("0.003402823e+41", 3.402823e+38f); checkFloat<float>("0.003402823e+41", 3.402823e+38f);
} }
SECTION("VeryLong") { SECTION("VeryLong") {
check<float>("0.00000000000000000000000000000001", 1e-32f); checkFloat<float>("0.00000000000000000000000000000001", 1e-32f);
check<float>("100000000000000000000000000000000.0", 1e+32f); checkFloat<float>("100000000000000000000000000000000.0", 1e+32f);
check<float>( checkFloat<float>(
"100000000000000000000000000000000.00000000000000000000000000000", "100000000000000000000000000000000.00000000000000000000000000000",
1e+32f); 1e+32f);
} }
SECTION("MantissaTooLongToFit") { SECTION("MantissaTooLongToFit") {
check<float>("0.340282346638528861111111111111", 0.34028234663852886f); checkFloat<float>("0.340282346638528861111111111111", 0.34028234663852886f);
check<float>("34028234663852886.11111111111111", 34028234663852886.0f); checkFloat<float>("34028234663852886.11111111111111", 34028234663852886.0f);
check<float>("34028234.66385288611111111111111", 34028234.663852886f); checkFloat<float>("34028234.66385288611111111111111", 34028234.663852886f);
check<float>("-0.340282346638528861111111111111", -0.34028234663852886f); checkFloat<float>("-0.340282346638528861111111111111",
check<float>("-34028234663852886.11111111111111", -34028234663852886.0f); -0.34028234663852886f);
check<float>("-34028234.66385288611111111111111", -34028234.663852886f); checkFloat<float>("-34028234663852886.11111111111111",
-34028234663852886.0f);
checkFloat<float>("-34028234.66385288611111111111111",
-34028234.663852886f);
} }
SECTION("ExponentTooBig") { SECTION("ExponentTooBig") {
checkInf<float>("1e39", false); checkInf<float>("1e39", false);
checkInf<float>("-1e39", true); checkInf<float>("-1e39", true);
checkInf<float>("1e255", false); checkInf<float>("1e255", false);
check<float>("1e-255", 0.0f); checkFloat<float>("1e-255", 0.0f);
} }
SECTION("NaN") { SECTION("NaN") {
@ -105,58 +108,61 @@ TEST_CASE("parseFloat<float>()") {
TEST_CASE("parseFloat<double>()") { TEST_CASE("parseFloat<double>()") {
SECTION("Short_NoExponent") { SECTION("Short_NoExponent") {
check<double>("3.14", 3.14); checkFloat<double>("3.14", 3.14);
check<double>("-3.14", -3.14); checkFloat<double>("-3.14", -3.14);
check<double>("+3.14", +3.14); checkFloat<double>("+3.14", +3.14);
} }
SECTION("Short_NoDot") { SECTION("Short_NoDot") {
check<double>("1E+308", 1E+308); checkFloat<double>("1E+308", 1E+308);
check<double>("-1E+308", -1E+308); checkFloat<double>("-1E+308", -1E+308);
check<double>("+1E-308", +1E-308); checkFloat<double>("+1E-308", +1E-308);
check<double>("+1e+308", +1e+308); checkFloat<double>("+1e+308", +1e+308);
check<double>("-1e-308", -1e-308); checkFloat<double>("-1e-308", -1e-308);
} }
SECTION("Max") { SECTION("Max") {
check<double>(".017976931348623147e+310", 1.7976931348623147e+308); checkFloat<double>(".017976931348623147e+310", 1.7976931348623147e+308);
check<double>(".17976931348623147e+309", 1.7976931348623147e+308); checkFloat<double>(".17976931348623147e+309", 1.7976931348623147e+308);
check<double>("1.7976931348623147e+308", 1.7976931348623147e+308); checkFloat<double>("1.7976931348623147e+308", 1.7976931348623147e+308);
check<double>("17.976931348623147e+307", 1.7976931348623147e+308); checkFloat<double>("17.976931348623147e+307", 1.7976931348623147e+308);
check<double>("179.76931348623147e+306", 1.7976931348623147e+308); checkFloat<double>("179.76931348623147e+306", 1.7976931348623147e+308);
} }
SECTION("Min") { SECTION("Min") {
check<double>(".022250738585072014e-306", 2.2250738585072014e-308); checkFloat<double>(".022250738585072014e-306", 2.2250738585072014e-308);
check<double>(".22250738585072014e-307", 2.2250738585072014e-308); checkFloat<double>(".22250738585072014e-307", 2.2250738585072014e-308);
check<double>("2.2250738585072014e-308", 2.2250738585072014e-308); checkFloat<double>("2.2250738585072014e-308", 2.2250738585072014e-308);
check<double>("22.250738585072014e-309", 2.2250738585072014e-308); checkFloat<double>("22.250738585072014e-309", 2.2250738585072014e-308);
check<double>("222.50738585072014e-310", 2.2250738585072014e-308); checkFloat<double>("222.50738585072014e-310", 2.2250738585072014e-308);
} }
SECTION("VeryLong") { SECTION("VeryLong") {
check<double>("0.00000000000000000000000000000001", 1e-32); checkFloat<double>("0.00000000000000000000000000000001", 1e-32);
check<double>("100000000000000000000000000000000.0", 1e+32); checkFloat<double>("100000000000000000000000000000000.0", 1e+32);
check<double>( checkFloat<double>(
"100000000000000000000000000000000.00000000000000000000000000000", "100000000000000000000000000000000.00000000000000000000000000000",
1e+32); 1e+32);
} }
SECTION("MantissaTooLongToFit") { SECTION("MantissaTooLongToFit") {
check<double>("0.179769313486231571111111111111", 0.17976931348623157); checkFloat<double>("0.179769313486231571111111111111", 0.17976931348623157);
check<double>("17976931348623157.11111111111111", 17976931348623157.0); checkFloat<double>("17976931348623157.11111111111111", 17976931348623157.0);
check<double>("1797693.134862315711111111111111", 1797693.1348623157); checkFloat<double>("1797693.134862315711111111111111", 1797693.1348623157);
check<double>("-0.179769313486231571111111111111", -0.17976931348623157); checkFloat<double>("-0.179769313486231571111111111111",
check<double>("-17976931348623157.11111111111111", -17976931348623157.0); -0.17976931348623157);
check<double>("-1797693.134862315711111111111111", -1797693.1348623157); checkFloat<double>("-17976931348623157.11111111111111",
-17976931348623157.0);
checkFloat<double>("-1797693.134862315711111111111111",
-1797693.1348623157);
} }
SECTION("ExponentTooBig") { SECTION("ExponentTooBig") {
checkInf<double>("1e309", false); checkInf<double>("1e309", false);
checkInf<double>("-1e309", true); checkInf<double>("-1e309", true);
checkInf<double>("1e65535", false); checkInf<double>("1e65535", false);
check<double>("1e-65535", 0.0); checkFloat<double>("1e-65535", 0.0);
} }
SECTION("NaN") { SECTION("NaN") {

View File

@ -9,59 +9,59 @@
using namespace ARDUINOJSON_NAMESPACE; using namespace ARDUINOJSON_NAMESPACE;
template <typename T> template <typename T>
void check(const char* input, T expected) { void checkInteger(const char* input, T expected) {
CAPTURE(input); CAPTURE(input);
T actual = parseInteger<T>(input); T actual = parseInteger<T>(input);
REQUIRE(expected == actual); REQUIRE(expected == actual);
} }
TEST_CASE("parseInteger<int8_t>()") { TEST_CASE("parseInteger<int8_t>()") {
check<int8_t>("-128", -128); checkInteger<int8_t>("-128", -128);
check<int8_t>("127", 127); checkInteger<int8_t>("127", 127);
check<int8_t>("+127", 127); checkInteger<int8_t>("+127", 127);
check<int8_t>("3.14", 3); checkInteger<int8_t>("3.14", 3);
check<int8_t>("x42", 0); checkInteger<int8_t>("x42", 0);
check<int8_t>("128", 0); // overflow checkInteger<int8_t>("128", 0); // overflow
check<int8_t>("-129", 0); // overflow checkInteger<int8_t>("-129", 0); // overflow
} }
TEST_CASE("parseInteger<int16_t>()") { TEST_CASE("parseInteger<int16_t>()") {
check<int16_t>("-32768", -32768); checkInteger<int16_t>("-32768", -32768);
check<int16_t>("32767", 32767); checkInteger<int16_t>("32767", 32767);
check<int16_t>("+32767", 32767); checkInteger<int16_t>("+32767", 32767);
check<int16_t>("3.14", 3); checkInteger<int16_t>("3.14", 3);
check<int16_t>("x42", 0); checkInteger<int16_t>("x42", 0);
check<int16_t>("-32769", 0); // overflow checkInteger<int16_t>("-32769", 0); // overflow
check<int16_t>("32768", 0); // overflow checkInteger<int16_t>("32768", 0); // overflow
} }
TEST_CASE("parseInteger<int32_t>()") { TEST_CASE("parseInteger<int32_t>()") {
check<int32_t>("-2147483648", (-2147483647 - 1)); checkInteger<int32_t>("-2147483648", (-2147483647 - 1));
check<int32_t>("2147483647", 2147483647); checkInteger<int32_t>("2147483647", 2147483647);
check<int32_t>("+2147483647", 2147483647); checkInteger<int32_t>("+2147483647", 2147483647);
check<int32_t>("3.14", 3); checkInteger<int32_t>("3.14", 3);
check<int32_t>("x42", 0); checkInteger<int32_t>("x42", 0);
check<int32_t>("-2147483649", 0); // overflow checkInteger<int32_t>("-2147483649", 0); // overflow
check<int32_t>("2147483648", 0); // overflow checkInteger<int32_t>("2147483648", 0); // overflow
} }
TEST_CASE("parseInteger<uint8_t>()") { TEST_CASE("parseInteger<uint8_t>()") {
check<uint8_t>("0", 0); checkInteger<uint8_t>("0", 0);
check<uint8_t>("255", 255); checkInteger<uint8_t>("255", 255);
check<uint8_t>("+255", 255); checkInteger<uint8_t>("+255", 255);
check<uint8_t>("3.14", 3); checkInteger<uint8_t>("3.14", 3);
check<uint8_t>("x42", 0); checkInteger<uint8_t>("x42", 0);
check<uint8_t>("-1", 0); checkInteger<uint8_t>("-1", 0);
check<uint8_t>("256", 0); checkInteger<uint8_t>("256", 0);
} }
TEST_CASE("parseInteger<uint16_t>()") { TEST_CASE("parseInteger<uint16_t>()") {
check<uint16_t>("0", 0); checkInteger<uint16_t>("0", 0);
check<uint16_t>("65535", 65535); checkInteger<uint16_t>("65535", 65535);
check<uint16_t>("+65535", 65535); checkInteger<uint16_t>("+65535", 65535);
check<uint16_t>("3.14", 3); checkInteger<uint16_t>("3.14", 3);
// check<uint16_t>(" 42", 0); // checkInteger<uint16_t>(" 42", 0);
check<uint16_t>("x42", 0); checkInteger<uint16_t>("x42", 0);
check<uint16_t>("-1", 0); checkInteger<uint16_t>("-1", 0);
check<uint16_t>("65536", 0); checkInteger<uint16_t>("65536", 0);
} }

View File

@ -8,4 +8,6 @@ add_executable(TextFormatterTests
) )
target_link_libraries(TextFormatterTests catch) target_link_libraries(TextFormatterTests catch)
set_target_properties(TextFormatterTests PROPERTIES UNITY_BUILD OFF)
add_test(TextFormatter TextFormatterTests) add_test(TextFormatter TextFormatterTests)