Initial commit of Arduino libraries

This commit is contained in:
Sam
2025-05-23 10:47:41 +10:00
commit 5bfce5fc3e
2476 changed files with 1108481 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2025, Benoit BLANCHON
# MIT License
add_executable(MixedConfigurationTests
decode_unicode_0.cpp
decode_unicode_1.cpp
enable_alignment_0.cpp
enable_alignment_1.cpp
enable_comments_0.cpp
enable_comments_1.cpp
enable_infinity_0.cpp
enable_infinity_1.cpp
enable_nan_0.cpp
enable_nan_1.cpp
enable_progmem_1.cpp
issue1707.cpp
string_length_size_1.cpp
string_length_size_2.cpp
string_length_size_4.cpp
use_double_0.cpp
use_double_1.cpp
use_long_long_0.cpp
use_long_long_1.cpp
)
set_target_properties(MixedConfigurationTests PROPERTIES UNITY_BUILD OFF)
add_test(MixedConfiguration MixedConfigurationTests)
set_tests_properties(MixedConfiguration
PROPERTIES
LABELS "Catch"
)

View File

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

View File

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

View File

@@ -0,0 +1,41 @@
#define ARDUINOJSON_VERSION_NAMESPACE NoAlignment
#define ARDUINOJSON_ENABLE_ALIGNMENT 0
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_ENABLE_ALIGNMENT == 0") {
using namespace ArduinoJson::detail;
const size_t N = sizeof(void*);
SECTION("isAligned()") {
CHECK(isAligned(0) == true);
CHECK(isAligned(1) == true);
CHECK(isAligned(N) == true);
CHECK(isAligned(N + 1) == true);
CHECK(isAligned(2 * N) == true);
CHECK(isAligned(2 * N + 1) == true);
}
SECTION("addPadding()") {
CHECK(addPadding(0) == 0);
CHECK(addPadding(1) == 1);
CHECK(addPadding(N) == N);
CHECK(addPadding(N + 1) == N + 1);
}
SECTION("AddPadding<>") {
const size_t a = AddPadding<0>::value;
CHECK(a == 0);
const size_t b = AddPadding<1>::value;
CHECK(b == 1);
const size_t c = AddPadding<N>::value;
CHECK(c == N);
const size_t d = AddPadding<N + 1>::value;
CHECK(d == N + 1);
}
}

View File

@@ -0,0 +1,40 @@
#define ARDUINOJSON_ENABLE_ALIGNMENT 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_ENABLE_ALIGNMENT == 1") {
using namespace ArduinoJson::detail;
const size_t N = sizeof(void*);
SECTION("isAligned()") {
CHECK(isAligned(0) == true);
CHECK(isAligned(1) == false);
CHECK(isAligned(N) == true);
CHECK(isAligned(N + 1) == false);
CHECK(isAligned(2 * N) == true);
CHECK(isAligned(2 * N + 1) == false);
}
SECTION("addPadding()") {
CHECK(addPadding(0) == 0);
CHECK(addPadding(1) == N);
CHECK(addPadding(N) == N);
CHECK(addPadding(N + 1) == 2 * N);
}
SECTION("AddPadding<>") {
const size_t a = AddPadding<0>::value;
CHECK(a == 0);
const size_t b = AddPadding<1>::value;
CHECK(b == N);
const size_t c = AddPadding<N>::value;
CHECK(c == N);
const size_t d = AddPadding<N + 1>::value;
CHECK(d == 2 * N);
}
}

View File

@@ -0,0 +1,54 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#define ARDUINOJSON_ENABLE_COMMENTS 0
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Comments should produce InvalidInput") {
JsonDocument doc;
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,411 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#define ARDUINOJSON_ENABLE_COMMENTS 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Comments in arrays") {
JsonDocument doc;
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") {
JsonDocument doc;
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") {
JsonDocument doc;
SECTION("Just a trailing comment with no line break") {
DeserializationError err = deserializeJson(doc, "// comment");
REQUIRE(err == DeserializationError::IncompleteInput);
}
SECTION("Just a trailing comment with no a break") {
DeserializationError err = deserializeJson(doc, "// comment\n");
REQUIRE(err == DeserializationError::EmptyInput);
}
SECTION("Just a block comment") {
DeserializationError err = deserializeJson(doc, "/*comment*/");
REQUIRE(err == DeserializationError::EmptyInput);
}
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) {
JsonDocument doc;
DeserializationError 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()") {
JsonDocument doc;
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,39 @@
#define ARDUINOJSON_ENABLE_INFINITY 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include <limits>
namespace my {
using ArduinoJson::detail::isinf;
} // namespace my
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 1") {
JsonDocument doc;
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()") {
DeserializationError 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") {
JsonDocument doc;
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()") {
DeserializationError 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::detail::isnan;
} // namespace my
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 1") {
JsonDocument doc;
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()") {
DeserializationError err = deserializeJson(doc, "{\"X\":NaN}");
float x = doc["X"];
REQUIRE(err == DeserializationError::Ok);
REQUIRE(my::isnan(x));
}
}

View File

@@ -0,0 +1,191 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#define ARDUINOJSON_ENABLE_PROGMEM 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Flash strings") {
JsonDocument doc;
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("parseNumber()") { // tables are in Flash
using ArduinoJson::detail::parseNumber;
CHECK(parseNumber<float>("1") == 1.f);
CHECK(parseNumber<float>("1.23") == 1.23f);
CHECK(parseNumber<float>("-1.23e34") == -1.23e34f);
}
TEST_CASE("strlen_P") {
CHECK(strlen_P(PSTR("")) == 0);
CHECK(strlen_P(PSTR("a")) == 1);
CHECK(strlen_P(PSTR("ac")) == 2);
}
TEST_CASE("strncmp_P") {
CHECK(strncmp_P("a", PSTR("b"), 0) == 0);
CHECK(strncmp_P("a", PSTR("b"), 1) == -1);
CHECK(strncmp_P("b", PSTR("a"), 1) == 1);
CHECK(strncmp_P("a", PSTR("a"), 0) == 0);
CHECK(strncmp_P("a", PSTR("b"), 2) == -1);
CHECK(strncmp_P("b", PSTR("a"), 2) == 1);
CHECK(strncmp_P("a", PSTR("a"), 2) == 0);
}
TEST_CASE("strcmp_P") {
CHECK(strcmp_P("a", PSTR("b")) == -1);
CHECK(strcmp_P("b", PSTR("a")) == 1);
CHECK(strcmp_P("a", PSTR("a")) == 0);
CHECK(strcmp_P("aa", PSTR("ab")) == -1);
CHECK(strcmp_P("ab", PSTR("aa")) == 1);
CHECK(strcmp_P("aa", PSTR("aa")) == 0);
}
TEST_CASE("memcpy_P") {
char dst[4];
CHECK(memcpy_P(dst, PSTR("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::detail;
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::detail;
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');
}
}
static void testStringification(DeserializationError error,
std::string expected) {
const __FlashStringHelper* s = error.f_str();
CHECK(reinterpret_cast<const char*>(convertFlashToPtr(s)) == expected);
}
#define TEST_STRINGIFICATION(symbol) \
testStringification(DeserializationError::symbol, #symbol)
TEST_CASE("DeserializationError::f_str()") {
TEST_STRINGIFICATION(Ok);
TEST_STRINGIFICATION(EmptyInput);
TEST_STRINGIFICATION(IncompleteInput);
TEST_STRINGIFICATION(InvalidInput);
TEST_STRINGIFICATION(NoMemory);
TEST_STRINGIFICATION(TooDeep);
}

View File

@@ -0,0 +1,17 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#define ARDUINO
#define memcpy_P(dest, src, n) memcpy((dest), (src), (n))
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Issue1707") {
JsonDocument doc;
DeserializationError err = deserializeJson(doc, F("{\"hello\":12}"));
REQUIRE(err == DeserializationError::Ok);
}

View File

@@ -0,0 +1,131 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
JsonDocument doc;
SECTION("set(std::string)") {
SECTION("returns true if len <= 255") {
auto result = doc.set(std::string(255, '?'));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if len >= 256") {
auto result = doc.set(std::string(256, '?'));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackBinary)") {
SECTION("returns true if size <= 253") {
auto str = std::string(253, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 254") {
auto str = std::string(254, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackExtension)") {
SECTION("returns true if size <= 252") {
auto str = std::string(252, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 253") {
auto str = std::string(253, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("deserializeJson()") {
SECTION("returns Ok if string length <= 255") {
auto input = "\"" + std::string(255, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 256") {
auto input = "\"" + std::string(256, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
SECTION("deserializeMsgPack()") {
SECTION("returns Ok if string length <= 255") {
auto input = "\xd9\xff" + std::string(255, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 256") {
auto input = "\xda\x01\x00"_s + std::string(256, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if binary size <= 253") {
auto input = "\xc4\xfd" + std::string(253, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if binary size >= 254") {
auto input = "\xc4\xfe" + std::string(254, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if extension size <= 252") {
auto input = "\xc7\xfc\x01" + std::string(252, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if binary size >= 253") {
auto input = "\xc7\xfd\x01" + std::string(253, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
}

View File

@@ -0,0 +1,140 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE 2
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
JsonDocument doc;
SECTION("set(std::string)") {
SECTION("returns true if len <= 65535") {
auto result = doc.set(std::string(65535, '?'));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if len >= 65536") {
auto result = doc.set(std::string(65536, '?'));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackBinary)") {
SECTION("returns true if size <= 65532") {
auto str = std::string(65532, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 65533") {
auto str = std::string(65533, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackExtension)") {
SECTION("returns true if size <= 65531") {
auto str = std::string(65531, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 65532") {
auto str = std::string(65532, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("deserializeJson()") {
SECTION("returns Ok if string length <= 65535") {
auto input = "\"" + std::string(65535, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 65536") {
auto input = "\"" + std::string(65536, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
SECTION("deserializeMsgPack()") {
SECTION("returns Ok if string length <= 65535") {
auto input = "\xda\xff\xff" + std::string(65535, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 65536") {
auto input = "\xdb\x00\x01\x00\x00"_s + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if binary size <= 65532") {
auto input = "\xc5\xff\xfc" + std::string(65532, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if binary size >= 65534") {
auto input = "\xc5\xff\xfd" + std::string(65534, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
// https://oss-fuzz.com/testcase?key=5354792971993088
SECTION("doesn't overflow if binary size == 0xFFFF") {
auto input = "\xc5\xff\xff"_s;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if extension size <= 65531") {
auto input = "\xc8\xff\xfb\x01" + std::string(65531, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if extension size >= 65532") {
auto input = "\xc8\xff\xfc\x01" + std::string(65532, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
}

View File

@@ -0,0 +1,146 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE 4
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
JsonDocument doc;
SECTION("set(std::string)") {
SECTION("returns true if string length >= 65536") {
auto result = doc.set(std::string(65536, '?'));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
}
SECTION("set(MsgPackBinary)") {
SECTION("returns true if size >= 65536") {
auto str = std::string(65536, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
}
SECTION("set(MsgPackExtension)") {
SECTION("returns true if size >= 65532") {
auto str = std::string(65532, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
}
SECTION("deserializeJson()") {
SECTION("returns Ok if string length >= 65536") {
auto input = "\"" + std::string(65536, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
}
SECTION("deserializeMsgPack()") {
SECTION("returns Ok if string size >= 65536") {
auto input = "\xda\xff\xff" + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns Ok if binary size >= 65536") {
auto input = "\xc5\xff\xff" + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns Ok if extension size >= 65532") {
auto input = "\xc8\xff\xfb\x01" + std::string(65532, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
// https://oss-fuzz.com/testcase?key=5354792971993088
SECTION("doesn't overflow if binary size == 0xFFFFFFFF") {
auto input = "\xc6\xff\xff\xff\xff"_s;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("doesn't overflow if string size == 0xFFFFFFFF") {
auto input = "\xdb\xff\xff\xff\xff???????????????????"_s;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err != DeserializationError::Ok);
}
}
SECTION("bin 32 deserialization") {
auto str = std::string(65536, '?');
auto input = "\xc6\x00\x01\x00\x00"_s + str;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 65536);
REQUIRE(binary.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
binary.size()) == str);
}
SECTION("bin 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackBinary(str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 5 + str.size());
REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
}
SECTION("ext 32 deserialization") {
auto str = std::string(65536, '?');
auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto value = doc.as<MsgPackExtension>();
REQUIRE(value.type() == 42);
REQUIRE(value.size() == 65536);
REQUIRE(value.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
value.size()) == str);
}
SECTION("ext 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackExtension(42, str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 6 + str.size());
REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
}
}

View File

@@ -0,0 +1,67 @@
#define ARDUINOJSON_USE_DOUBLE 0
#include <ArduinoJson.h>
#include <catch.hpp>
namespace my {
using ArduinoJson::detail::isinf;
} // namespace my
void checkFloat(const char* input, float expected) {
using ArduinoJson::detail::NumberType;
using ArduinoJson::detail::parseNumber;
CAPTURE(input);
auto result = parseNumber(input);
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() == Approx(expected));
}
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
SECTION("serializeJson()") {
JsonDocument doc;
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}");
}
SECTION("parseNumber()") {
using ArduinoJson::detail::NumberType;
using ArduinoJson::detail::parseNumber;
SECTION("Large positive number") {
auto result = parseNumber("1e300");
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() > 0);
REQUIRE(my::isinf(result.asFloat()));
}
SECTION("Large negative number") {
auto result = parseNumber("-1e300");
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() < 0);
REQUIRE(my::isinf(result.asFloat()));
}
SECTION("Too small to be represented") {
auto result = parseNumber("1e-300");
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() == 0);
}
SECTION("MantissaTooLongToFit") {
checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
}
}
}

View File

@@ -0,0 +1,17 @@
#define ARDUINOJSON_USE_DOUBLE 1
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 1") {
JsonDocument doc;
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,38 @@
#define ARDUINOJSON_USE_LONG_LONG 0
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 0") {
JsonDocument doc;
SECTION("smoke test") {
doc["A"] = 42;
doc["B"] = 84;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"A\":42,\"B\":84}");
}
SECTION("deserializeMsgPack()") {
SECTION("cf 00 00 00 00 ff ff ff ff") {
auto err =
deserializeMsgPack(doc, "\xcf\x00\x00\x00\x00\xff\xff\xff\xff"_s);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<uint32_t>() == 0xFFFFFFFF);
}
SECTION("cf 00 00 00 01 00 00 00 00") {
auto err =
deserializeMsgPack(doc, "\xcf\x00\x00\x00\x01\x00\x00\x00\x00"_s);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.isNull());
}
}
}

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") {
JsonDocument doc;
JsonObject root = doc.to<JsonObject>();
root["A"] = 123456789123456789;
root["B"] = 987654321987654321;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"A\":123456789123456789,\"B\":987654321987654321}");
}