added library json

This commit is contained in:
hidaba
2023-05-14 11:02:22 +02:00
parent c60123ce97
commit ab8de582d8
421 changed files with 51080 additions and 6 deletions

View File

@ -0,0 +1,18 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2023, Benoit BLANCHON
# MIT License
add_executable(TextFormatterTests
writeFloat.cpp
writeInteger.cpp
writeString.cpp
)
set_target_properties(TextFormatterTests PROPERTIES UNITY_BUILD OFF)
add_test(TextFormatter TextFormatterTests)
set_tests_properties(TextFormatter
PROPERTIES
LABELS "Catch"
)

View File

@ -0,0 +1,119 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <catch.hpp>
#include <limits>
#include <string>
#define ARDUINOJSON_ENABLE_NAN 1
#define ARDUINOJSON_ENABLE_INFINITY 1
#include <ArduinoJson/Json/TextFormatter.hpp>
#include <ArduinoJson/Serialization/Writer.hpp>
using namespace ArduinoJson::detail;
template <typename TFloat>
void check(TFloat input, const std::string& expected) {
std::string output;
Writer<std::string> sb(output);
TextFormatter<Writer<std::string>> writer(sb);
writer.writeFloat(input);
REQUIRE(writer.bytesWritten() == output.size());
CHECK(expected == output);
}
TEST_CASE("TextFormatter::writeFloat(double)") {
SECTION("Pi") {
check<double>(3.14159265359, "3.141592654");
}
SECTION("Signaling NaN") {
double nan = std::numeric_limits<double>::signaling_NaN();
check<double>(nan, "NaN");
}
SECTION("Quiet NaN") {
double nan = std::numeric_limits<double>::quiet_NaN();
check<double>(nan, "NaN");
}
SECTION("Infinity") {
double inf = std::numeric_limits<double>::infinity();
check<double>(inf, "Infinity");
check<double>(-inf, "-Infinity");
}
SECTION("Zero") {
check<double>(0.0, "0");
check<double>(-0.0, "0");
}
SECTION("Espilon") {
check<double>(2.2250738585072014E-308, "2.225073859e-308");
check<double>(-2.2250738585072014E-308, "-2.225073859e-308");
}
SECTION("Max double") {
check<double>(1.7976931348623157E+308, "1.797693135e308");
check<double>(-1.7976931348623157E+308, "-1.797693135e308");
}
SECTION("Big exponent") {
// this test increases coverage of normalize()
check<double>(1e255, "1e255");
check<double>(1e-255, "1e-255");
}
SECTION("Exponentation when <= 1e-5") {
check<double>(1e-4, "0.0001");
check<double>(1e-5, "1e-5");
check<double>(-1e-4, "-0.0001");
check<double>(-1e-5, "-1e-5");
}
SECTION("Exponentation when >= 1e7") {
check<double>(9999999.999, "9999999.999");
check<double>(10000000.0, "1e7");
check<double>(-9999999.999, "-9999999.999");
check<double>(-10000000.0, "-1e7");
}
SECTION("Rounding when too many decimals") {
check<double>(0.000099999999999, "0.0001");
check<double>(0.0000099999999999, "1e-5");
check<double>(0.9999999996, "1");
}
SECTION("9 decimal places") {
check<double>(0.100000001, "0.100000001");
check<double>(0.999999999, "0.999999999");
check<double>(9.000000001, "9.000000001");
check<double>(9.999999999, "9.999999999");
}
SECTION("10 decimal places") {
check<double>(0.1000000001, "0.1");
check<double>(0.9999999999, "1");
check<double>(9.0000000001, "9");
check<double>(9.9999999999, "10");
}
}
TEST_CASE("TextFormatter::writeFloat(float)") {
SECTION("Pi") {
check<float>(3.14159265359f, "3.141593");
}
SECTION("999.9") { // issue #543
check<float>(999.9f, "999.9");
}
SECTION("24.3") { // # issue #588
check<float>(24.3f, "24.3");
}
}

View File

@ -0,0 +1,55 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <catch.hpp>
#include <limits>
#include <string>
#include <ArduinoJson/Json/TextFormatter.hpp>
#include <ArduinoJson/Serialization/Writer.hpp>
using namespace ArduinoJson::detail;
template <typename T>
void checkWriteInteger(T value, std::string expected) {
char output[64] = {0};
StaticStringWriter sb(output, sizeof(output));
TextFormatter<StaticStringWriter> writer(sb);
writer.writeInteger<T>(value);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
}
TEST_CASE("int8_t") {
checkWriteInteger<int8_t>(0, "0");
checkWriteInteger<int8_t>(-128, "-128");
checkWriteInteger<int8_t>(127, "127");
}
TEST_CASE("uint8_t") {
checkWriteInteger<uint8_t>(0, "0");
checkWriteInteger<uint8_t>(255, "255");
}
TEST_CASE("int16_t") {
checkWriteInteger<int16_t>(0, "0");
checkWriteInteger<int16_t>(-32768, "-32768");
checkWriteInteger<int16_t>(32767, "32767");
}
TEST_CASE("uint16_t") {
checkWriteInteger<uint16_t>(0, "0");
checkWriteInteger<uint16_t>(65535, "65535");
}
TEST_CASE("int32_t") {
checkWriteInteger<int32_t>(0, "0");
checkWriteInteger<int32_t>(-2147483647 - 1, "-2147483648");
checkWriteInteger<int32_t>(2147483647, "2147483647");
}
TEST_CASE("uint32_t") {
checkWriteInteger<uint32_t>(0, "0");
checkWriteInteger<uint32_t>(4294967295U, "4294967295");
}

View File

@ -0,0 +1,57 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <catch.hpp>
#include <ArduinoJson/Json/TextFormatter.hpp>
#include <ArduinoJson/Serialization/Writers/StaticStringWriter.hpp>
using namespace ArduinoJson::detail;
void check(const char* input, std::string expected) {
char output[64] = {0};
StaticStringWriter sb(output, sizeof(output));
TextFormatter<StaticStringWriter> writer(sb);
writer.writeString(input);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
}
TEST_CASE("TextFormatter::writeString()") {
SECTION("EmptyString") {
check("", "\"\"");
}
SECTION("QuotationMark") {
check("\"", "\"\\\"\"");
}
SECTION("ReverseSolidus") {
check("\\", "\"\\\\\"");
}
SECTION("Solidus") {
check("/", "\"/\""); // but the JSON format allows \/
}
SECTION("Backspace") {
check("\b", "\"\\b\"");
}
SECTION("Formfeed") {
check("\f", "\"\\f\"");
}
SECTION("Newline") {
check("\n", "\"\\n\"");
}
SECTION("CarriageReturn") {
check("\r", "\"\\r\"");
}
SECTION("HorizontalTab") {
check("\t", "\"\\t\"");
}
}