initial commit

This commit is contained in:
proddy
2020-07-05 18:29:08 +02:00
parent 26b201ea2f
commit c5933e8c14
739 changed files with 86566 additions and 20952 deletions

View File

@@ -0,0 +1,28 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2020
# MIT License
# we need C++11 for 'long long'
set(CMAKE_CXX_STANDARD 11)
add_executable(MixedConfigurationTests
cpp11.cpp
decode_unicode_0.cpp
decode_unicode_1.cpp
enable_infinity_0.cpp
enable_infinity_1.cpp
enable_nan_0.cpp
enable_nan_1.cpp
use_double_0.cpp
use_double_1.cpp
use_long_long_0.cpp
use_long_long_1.cpp
enable_progmem_1.cpp
enable_comments_1.cpp
enable_comments_0.cpp
)
target_link_libraries(MixedConfigurationTests catch)
set_target_properties(MixedConfigurationTests PROPERTIES UNITY_BUILD OFF)
add_test(MixedConfiguration MixedConfigurationTests)

View File

@@ -0,0 +1,94 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#if __cplusplus >= 201103L
TEST_CASE("nullptr") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("JsonVariant == nullptr") {
REQUIRE((variant == nullptr));
REQUIRE_FALSE((variant != nullptr));
}
SECTION("JsonVariant != nullptr") {
variant.set(42);
REQUIRE_FALSE((variant == nullptr));
REQUIRE((variant != nullptr));
}
SECTION("JsonVariant.set(nullptr)") {
variant.set(42);
variant.set(nullptr);
REQUIRE(variant.isNull());
}
SECTION("JsonVariant.is<nullptr_t>()") {
variant.set(42);
REQUIRE(variant.is<std::nullptr_t>() == false);
variant.clear();
REQUIRE(variant.is<std::nullptr_t>() == true);
}
}
TEST_CASE("Issue #1120") {
StaticJsonDocument<500> doc;
constexpr char str[] =
"{\"contents\":[{\"module\":\"Packet\"},{\"module\":\"Analog\"}]}";
deserializeJson(doc, str);
SECTION("MemberProxy<std::string>::isNull()") {
SECTION("returns false") {
auto value = doc[std::string("contents")];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc[std::string("zontents")];
CHECK(value.isNull() == true);
}
}
SECTION("ElementProxy<MemberProxy<const char*> >::isNull()") {
SECTION("returns false") { // Issue #1120
auto value = doc["contents"][1];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][2];
CHECK(value.isNull() == true);
}
}
SECTION("MemberProxy<ElementProxy<MemberProxy>, const char*>::isNull()") {
SECTION("returns false") {
auto value = doc["contents"][1]["module"];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][1]["zodule"];
CHECK(value.isNull() == true);
}
}
SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
SECTION("returns false") {
auto value = doc["contents"][1][std::string("module")];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][1][std::string("zodule")];
CHECK(value.isNull() == true);
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
#define ARDUINOJSON_DECODE_UNICODE 0
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_DECODE_UNICODE == 0") {
DynamicJsonDocument doc(2048);
DeserializationError err = deserializeJson(doc, "\"\\uD834\\uDD1E\"");
REQUIRE(err == DeserializationError::NotSupported);
}

View File

@@ -0,0 +1,11 @@
#define ARDUINOJSON_DECODE_UNICODE 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_DECODE_UNICODE == 1") {
DynamicJsonDocument doc(2048);
DeserializationError err = deserializeJson(doc, "\"\\uD834\\uDD1E\"");
REQUIRE(err == DeserializationError::Ok);
}

View File

@@ -0,0 +1,54 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#define ARDUINOJSON_ENABLE_COMMENTS 0
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Comments should produce InvalidInput") {
DynamicJsonDocument doc(2048);
const char* testCases[] = {
"/*COMMENT*/ [\"hello\"]",
"[/*COMMENT*/ \"hello\"]",
"[\"hello\"/*COMMENT*/]",
"[\"hello\"/*COMMENT*/,\"world\"]",
"[\"hello\",/*COMMENT*/ \"world\"]",
"[/*/\n]",
"[/*COMMENT]",
"[/*COMMENT*]",
"//COMMENT\n\t[\"hello\"]",
"[//COMMENT\n\"hello\"]",
"[\"hello\"//COMMENT\r\n]",
"[\"hello\"//COMMENT\n,\"world\"]",
"[\"hello\",//COMMENT\n\"world\"]",
"[/COMMENT\n]",
"[//COMMENT",
"/*COMMENT*/ {\"hello\":\"world\"}",
"{/*COMMENT*/\"hello\":\"world\"}",
"{\"hello\"/*COMMENT*/:\"world\"}",
"{\"hello\":/*COMMENT*/\"world\"}",
"{\"hello\":\"world\"/*COMMENT*/}",
"//COMMENT\n {\"hello\":\"world\"}",
"{//COMMENT\n\"hello\":\"world\"}",
"{\"hello\"//COMMENT\n:\"world\"}",
"{\"hello\"://COMMENT\n\"world\"}",
"{\"hello\":\"world\"//COMMENT\n}",
"/{\"hello\":\"world\"}",
"{/\"hello\":\"world\"}",
"{\"hello\"/:\"world\"}",
"{\"hello\":/\"world\"}",
"{\"hello\":\"world\"/}",
"{\"hello\":\"world\"/,\"answer\":42}",
"{\"hello\":\"world\",/\"answer\":42}",
};
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
for (size_t i = 0; i < testCount; i++) {
const char* input = testCases[i];
CAPTURE(input);
REQUIRE(deserializeJson(doc, input) == DeserializationError::InvalidInput);
}
}

View File

@@ -0,0 +1,405 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#define ARDUINOJSON_ENABLE_COMMENTS 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Comments in arrays") {
DynamicJsonDocument doc(2048);
SECTION("Block comments") {
SECTION("Before opening bracket") {
DeserializationError err =
deserializeJson(doc, "/*COMMENT*/ [\"hello\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("After opening bracket") {
DeserializationError err =
deserializeJson(doc, "[/*COMMENT*/ \"hello\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("Before closing bracket") {
DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("After closing bracket") {
DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("Before comma") {
DeserializationError err =
deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
}
SECTION("After comma") {
DeserializationError err =
deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
}
SECTION("/*/") {
DeserializationError err = deserializeJson(doc, "[/*/\n]");
REQUIRE(err == DeserializationError::IncompleteInput);
}
SECTION("Unfinished comment") {
DeserializationError err = deserializeJson(doc, "[/*COMMENT]");
REQUIRE(err == DeserializationError::IncompleteInput);
}
SECTION("Final slash missing") {
DeserializationError err = deserializeJson(doc, "[/*COMMENT*]");
REQUIRE(err == DeserializationError::IncompleteInput);
}
}
SECTION("Trailing comments") {
SECTION("Before opening bracket") {
DeserializationError err =
deserializeJson(doc, "//COMMENT\n\t[\"hello\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("After opening bracket") {
DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("Before closing bracket") {
DeserializationError err =
deserializeJson(doc, "[\"hello\"//COMMENT\r\n]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("After closing bracket") {
DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
}
SECTION("Before comma") {
DeserializationError err =
deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
}
SECTION("After comma") {
DeserializationError err =
deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
}
SECTION("Invalid comment") {
DeserializationError err = deserializeJson(doc, "[/COMMENT\n]");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("End document with comment") {
DeserializationError err = deserializeJson(doc, "[//COMMENT");
REQUIRE(err == DeserializationError::IncompleteInput);
}
}
}
TEST_CASE("Comments in objects") {
DynamicJsonDocument doc(2048);
SECTION("Block comments") {
SECTION("Before opening brace") {
DeserializationError err =
deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("After opening brace") {
DeserializationError err =
deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before colon") {
DeserializationError err =
deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("After colon") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before closing brace") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("After closing brace") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before comma") {
DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
REQUIRE(obj["answer"] == 42);
}
SECTION("After comma") {
DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
REQUIRE(obj["answer"] == 42);
}
}
SECTION("Trailing comments") {
SECTION("Before opening brace") {
DeserializationError err =
deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("After opening brace") {
DeserializationError err =
deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before colon") {
DeserializationError err =
deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("After colon") {
DeserializationError err =
deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before closing brace") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("After closing brace") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before comma") {
DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
REQUIRE(obj["answer"] == 42);
}
SECTION("After comma") {
DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
REQUIRE(obj["answer"] == 42);
}
}
SECTION("Dangling slash") {
SECTION("Before opening brace") {
DeserializationError err = deserializeJson(doc, "/{\"hello\":\"world\"}");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("After opening brace") {
DeserializationError err = deserializeJson(doc, "{/\"hello\":\"world\"}");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("Before colon") {
DeserializationError err = deserializeJson(doc, "{\"hello\"/:\"world\"}");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("After colon") {
DeserializationError err = deserializeJson(doc, "{\"hello\":/\"world\"}");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("Before closing brace") {
DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/}");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("After closing brace") {
DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world");
}
SECTION("Before comma") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"/,\"answer\":42}");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("After comma") {
DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\",/\"answer\":42}");
REQUIRE(err == DeserializationError::InvalidInput);
}
}
}
TEST_CASE("Comments alone") {
DynamicJsonDocument doc(2048);
SECTION("Just a trailing comment") {
DeserializationError err = deserializeJson(doc, "// comment");
REQUIRE(err == DeserializationError::IncompleteInput);
}
SECTION("Just a block comment") {
DeserializationError err = deserializeJson(doc, "/*comment*/");
REQUIRE(err == DeserializationError::IncompleteInput);
}
SECTION("Just a slash") {
DeserializationError err = deserializeJson(doc, "/");
REQUIRE(err == DeserializationError::InvalidInput);
}
SECTION("Premature terminator") {
DeserializationError err = deserializeJson(doc, "/* comment");
REQUIRE(err == DeserializationError::IncompleteInput);
}
SECTION("Premature end on sized input") {
DeserializationError err = deserializeJson(doc, "/* comment */", 10);
REQUIRE(err == DeserializationError::IncompleteInput);
}
}

View File

@@ -0,0 +1,35 @@
#define ARDUINOJSON_ENABLE_INFINITY 0
#include <ArduinoJson.h>
#include <catch.hpp>
#include <limits>
static void assertParseFails(const char* json) {
DynamicJsonDocument doc(4096);
auto err = deserializeJson(doc, json);
REQUIRE(err == DeserializationError::InvalidInput);
}
static void assertJsonEquals(const JsonDocument& doc,
std::string expectedJson) {
std::string actualJson;
serializeJson(doc, actualJson);
REQUIRE(actualJson == expectedJson);
}
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 0") {
SECTION("serializeJson()") {
DynamicJsonDocument doc(4096);
doc.add(std::numeric_limits<double>::infinity());
doc.add(-std::numeric_limits<double>::infinity());
assertJsonEquals(doc, "[null,null]");
}
SECTION("deserializeJson()") {
assertParseFails("{\"X\":Infinity}");
assertParseFails("{\"X\":-Infinity}");
assertParseFails("{\"X\":+Infinity}");
}
}

View File

@@ -0,0 +1,38 @@
#define ARDUINOJSON_ENABLE_INFINITY 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include <limits>
namespace my {
using ARDUINOJSON_NAMESPACE::isinf;
} // namespace my
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 1") {
DynamicJsonDocument doc(4096);
SECTION("serializeJson()") {
doc.add(std::numeric_limits<double>::infinity());
doc.add(-std::numeric_limits<double>::infinity());
std::string json;
serializeJson(doc, json);
REQUIRE(json == "[Infinity,-Infinity]");
}
SECTION("deserializeJson()") {
auto err = deserializeJson(doc, "[Infinity,-Infinity,+Infinity]");
float a = doc[0];
float b = doc[1];
float c = doc[2];
REQUIRE(err == DeserializationError::Ok);
REQUIRE(my::isinf(a));
REQUIRE(a > 0);
REQUIRE(my::isinf(b));
REQUIRE(b < 0);
REQUIRE(my::isinf(c));
REQUIRE(c > 0);
}
}

View File

@@ -0,0 +1,25 @@
#define ARDUINOJSON_ENABLE_NAN 0
#include <ArduinoJson.h>
#include <catch.hpp>
#include <limits>
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 0") {
DynamicJsonDocument doc(4096);
JsonObject root = doc.to<JsonObject>();
SECTION("serializeJson()") {
root["X"] = std::numeric_limits<double>::signaling_NaN();
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"X\":null}");
}
SECTION("deserializeJson()") {
auto err = deserializeJson(doc, "{\"X\":NaN}");
REQUIRE(err == DeserializationError::InvalidInput);
}
}

View File

@@ -0,0 +1,31 @@
#define ARDUINOJSON_ENABLE_NAN 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include <limits>
namespace my {
using ARDUINOJSON_NAMESPACE::isnan;
} // namespace my
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 1") {
DynamicJsonDocument doc(4096);
JsonObject root = doc.to<JsonObject>();
SECTION("serializeJson()") {
root["X"] = std::numeric_limits<double>::signaling_NaN();
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"X\":NaN}");
}
SECTION("deserializeJson()") {
auto err = deserializeJson(doc, "{\"X\":NaN}");
float x = doc["X"];
REQUIRE(err == DeserializationError::Ok);
REQUIRE(my::isnan(x));
}
}

View File

@@ -0,0 +1,167 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#include "progmem_emulation.hpp"
#define ARDUINOJSON_ENABLE_PROGMEM 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Flash strings") {
DynamicJsonDocument doc(2048);
SECTION("deserializeJson()") {
DeserializationError err = deserializeJson(doc, F("{'hello':'world'}"));
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc["hello"] == "world");
}
SECTION("JsonDocument::operator[]") {
doc[F("hello")] = F("world");
REQUIRE(doc["hello"] == "world");
}
SECTION("JsonDocument::add()") {
doc.add(F("world"));
REQUIRE(doc[0] == "world");
}
SECTION("JsonVariant::set()") {
JsonVariant var = doc.to<JsonVariant>();
var.set(F("world"));
REQUIRE(var == "world");
}
SECTION("MemberProxy::operator==") {
doc["hello"] = "world";
REQUIRE(doc["hello"] == F("world"));
}
SECTION("ElementProxy::operator==") {
doc.add("world");
REQUIRE(doc[0] == F("world"));
}
}
TEST_CASE("strlen_P") {
CHECK(strlen_P(FC("")) == 0);
CHECK(strlen_P(FC("a")) == 1);
CHECK(strlen_P(FC("ac")) == 2);
}
TEST_CASE("strncmp_P") {
CHECK(strncmp_P("a", FC("b"), 0) == 0);
CHECK(strncmp_P("a", FC("b"), 1) == -1);
CHECK(strncmp_P("b", FC("a"), 1) == 1);
CHECK(strncmp_P("a", FC("a"), 0) == 0);
CHECK(strncmp_P("a", FC("b"), 2) == -1);
CHECK(strncmp_P("b", FC("a"), 2) == 1);
CHECK(strncmp_P("a", FC("a"), 2) == 0);
}
TEST_CASE("strcmp_P") {
CHECK(strcmp_P("a", FC("b")) == -1);
CHECK(strcmp_P("b", FC("a")) == 1);
CHECK(strcmp_P("a", FC("a")) == 0);
CHECK(strcmp_P("aa", FC("ab")) == -1);
CHECK(strcmp_P("ab", FC("aa")) == 1);
CHECK(strcmp_P("aa", FC("aa")) == 0);
}
TEST_CASE("memcpy_P") {
char dst[4];
CHECK(memcpy_P(dst, FC("ABC"), 4) == dst);
CHECK(dst[0] == 'A');
CHECK(dst[1] == 'B');
CHECK(dst[2] == 'C');
CHECK(dst[3] == 0);
}
TEST_CASE("BoundedReader<const __FlashStringHelper*>") {
using namespace ARDUINOJSON_NAMESPACE;
SECTION("read") {
BoundedReader<const __FlashStringHelper*> reader(F("\x01\xFF"), 2);
REQUIRE(reader.read() == 0x01);
REQUIRE(reader.read() == 0xFF);
REQUIRE(reader.read() == -1);
REQUIRE(reader.read() == -1);
}
SECTION("readBytes() all at once") {
BoundedReader<const __FlashStringHelper*> reader(F("ABCD"), 3);
char buffer[8] = "abcd";
REQUIRE(reader.readBytes(buffer, 4) == 3);
REQUIRE(buffer[0] == 'A');
REQUIRE(buffer[1] == 'B');
REQUIRE(buffer[2] == 'C');
REQUIRE(buffer[3] == 'd');
}
SECTION("readBytes() in two parts") {
BoundedReader<const __FlashStringHelper*> reader(F("ABCDEF"), 6);
char buffer[8] = "abcdefg";
REQUIRE(reader.readBytes(buffer, 4) == 4);
REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
REQUIRE(buffer[0] == 'A');
REQUIRE(buffer[1] == 'B');
REQUIRE(buffer[2] == 'C');
REQUIRE(buffer[3] == 'D');
REQUIRE(buffer[4] == 'E');
REQUIRE(buffer[5] == 'F');
REQUIRE(buffer[6] == 'g');
}
}
TEST_CASE("Reader<const __FlashStringHelper*>") {
using namespace ARDUINOJSON_NAMESPACE;
SECTION("read()") {
Reader<const __FlashStringHelper*> reader(F("\x01\xFF\x00\x12"));
REQUIRE(reader.read() == 0x01);
REQUIRE(reader.read() == 0xFF);
REQUIRE(reader.read() == 0);
REQUIRE(reader.read() == 0x12);
}
SECTION("readBytes() all at once") {
Reader<const __FlashStringHelper*> reader(F("ABCD"));
char buffer[8] = "abcd";
REQUIRE(reader.readBytes(buffer, 3) == 3);
REQUIRE(buffer[0] == 'A');
REQUIRE(buffer[1] == 'B');
REQUIRE(buffer[2] == 'C');
REQUIRE(buffer[3] == 'd');
}
SECTION("readBytes() in two parts") {
Reader<const __FlashStringHelper*> reader(F("ABCDEF"));
char buffer[8] = "abcdefg";
REQUIRE(reader.readBytes(buffer, 4) == 4);
REQUIRE(reader.readBytes(buffer + 4, 2) == 2);
REQUIRE(buffer[0] == 'A');
REQUIRE(buffer[1] == 'B');
REQUIRE(buffer[2] == 'C');
REQUIRE(buffer[3] == 'D');
REQUIRE(buffer[4] == 'E');
REQUIRE(buffer[5] == 'F');
REQUIRE(buffer[6] == 'g');
}
}

View File

@@ -0,0 +1,17 @@
#define ARDUINOJSON_USE_DOUBLE 0
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
DynamicJsonDocument doc(4096);
JsonObject root = doc.to<JsonObject>();
root["pi"] = 3.14;
root["e"] = 2.72;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
}

View File

@@ -0,0 +1,17 @@
#define ARDUINOJSON_USE_DOUBLE 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 1") {
DynamicJsonDocument doc(4096);
JsonObject root = doc.to<JsonObject>();
root["pi"] = 3.14;
root["e"] = 2.72;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
}

View File

@@ -0,0 +1,30 @@
#define ARDUINOJSON_USE_LONG_LONG 0
#include <ArduinoJson.h>
#include <catch.hpp>
template <size_t size_of_long>
std::string get_expected_json();
template <>
std::string get_expected_json<4>() {
return "{\"A\":2899336981,\"B\":2129924785}";
}
template <>
std::string get_expected_json<8>() {
return "{\"A\":123456789123456789,\"B\":987654321987654321}";
}
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 0") {
DynamicJsonDocument doc(4096);
JsonObject root = doc.to<JsonObject>();
root["A"] = 123456789123456789;
root["B"] = 987654321987654321;
std::string json;
serializeJson(doc, json);
REQUIRE(json == get_expected_json<sizeof(long)>());
}

View File

@@ -0,0 +1,17 @@
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 1") {
DynamicJsonDocument doc(4096);
JsonObject root = doc.to<JsonObject>();
root["A"] = 123456789123456789;
root["B"] = 987654321987654321;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"A\":123456789123456789,\"B\":987654321987654321}");
}