2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2014-11-11 17:26:51 +01:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
2017-03-25 22:05:06 +01:00
|
|
|
// https://bblanchon.github.io/ArduinoJson/
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2014-11-11 17:26:51 +01:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2016-04-28 18:54:14 +02:00
|
|
|
#include <limits>
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
void check(JsonVariant variant, const std::string &expected) {
|
|
|
|
char buffer[256] = "";
|
|
|
|
size_t returnValue = variant.printTo(buffer, sizeof(buffer));
|
|
|
|
REQUIRE(expected == buffer);
|
|
|
|
REQUIRE(expected.size() == returnValue);
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonVariant::printTo()") {
|
|
|
|
SECTION("Empty") {
|
|
|
|
check(JsonVariant(), "");
|
2014-11-11 17:26:51 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Null") {
|
|
|
|
check(static_cast<char *>(0), "null");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("String") {
|
|
|
|
check("hello", "\"hello\"");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("DoubleZero") {
|
|
|
|
check(0.0, "0.00");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("DoubleDefaultDigits") {
|
|
|
|
check(3.14159265358979323846, "3.14");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("DoubleFourDigits") {
|
|
|
|
check(JsonVariant(3.14159265358979323846, 4), "3.1416");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Infinity") {
|
|
|
|
check(std::numeric_limits<double>::infinity(), "Infinity");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("MinusInfinity") {
|
|
|
|
check(-std::numeric_limits<double>::infinity(), "-Infinity");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("SignalingNaN") {
|
|
|
|
check(std::numeric_limits<double>::signaling_NaN(), "NaN");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("QuietNaN") {
|
|
|
|
check(std::numeric_limits<double>::quiet_NaN(), "NaN");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("VeryBigPositiveDouble") {
|
|
|
|
check(JsonVariant(3.14159265358979323846e42, 4), "3.1416e42");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("VeryBigNegativeDouble") {
|
|
|
|
check(JsonVariant(-3.14159265358979323846e42, 4), "-3.1416e42");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("VerySmallPositiveDouble") {
|
|
|
|
check(JsonVariant(3.14159265358979323846e-42, 4), "3.1416e-42");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("VerySmallNegativeDouble") {
|
|
|
|
check(JsonVariant(-3.14159265358979323846e-42, 4), "-3.1416e-42");
|
|
|
|
}
|
2016-04-28 18:54:14 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("Integer") {
|
|
|
|
check(42, "42");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("NegativeLong") {
|
|
|
|
check(-42, "-42");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("UnsignedLong") {
|
|
|
|
check(4294967295UL, "4294967295");
|
|
|
|
}
|
2016-04-28 08:42:59 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("True") {
|
|
|
|
check(true, "true");
|
|
|
|
}
|
2014-11-11 17:26:51 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("OneFalse") {
|
|
|
|
check(false, "false");
|
|
|
|
}
|
2016-02-14 16:18:13 +01:00
|
|
|
|
|
|
|
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("NegativeInt64") {
|
|
|
|
check(-9223372036854775807 - 1, "-9223372036854775808");
|
|
|
|
}
|
2016-02-14 16:18:13 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("PositiveInt64") {
|
|
|
|
check(9223372036854775807, "9223372036854775807");
|
|
|
|
}
|
2016-04-28 08:42:59 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("UInt64") {
|
2017-05-02 21:32:19 +02:00
|
|
|
check(18446744073709551615U, "18446744073709551615");
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2016-02-14 16:18:13 +01:00
|
|
|
#endif
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|