Files
ArduinoJson/test/JsonVariant/printTo.cpp

110 lines
2.3 KiB
C++
Raw Normal View History

// 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/
// If you like this project, please add a star!
2014-11-11 17:26:51 +01:00
#include <ArduinoJson.h>
#include <catch.hpp>
#include <limits>
2014-11-11 17:26:51 +01: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
TEST_CASE("JsonVariant::printTo()") {
SECTION("Empty") {
check(JsonVariant(), "");
2014-11-11 17:26:51 +01:00
}
SECTION("Null") {
check(static_cast<char *>(0), "null");
}
2014-11-11 17:26:51 +01:00
SECTION("String") {
check("hello", "\"hello\"");
}
2014-11-11 17:26:51 +01:00
SECTION("DoubleZero") {
check(0.0, "0.00");
}
2014-11-11 17:26:51 +01:00
SECTION("DoubleDefaultDigits") {
check(3.14159265358979323846, "3.14");
}
2014-11-11 17:26:51 +01:00
SECTION("DoubleFourDigits") {
check(JsonVariant(3.14159265358979323846, 4), "3.1416");
}
2014-11-11 17:26:51 +01:00
SECTION("Infinity") {
check(std::numeric_limits<double>::infinity(), "Infinity");
}
SECTION("MinusInfinity") {
check(-std::numeric_limits<double>::infinity(), "-Infinity");
}
SECTION("SignalingNaN") {
check(std::numeric_limits<double>::signaling_NaN(), "NaN");
}
SECTION("QuietNaN") {
check(std::numeric_limits<double>::quiet_NaN(), "NaN");
}
SECTION("VeryBigPositiveDouble") {
check(JsonVariant(3.14159265358979323846e42, 4), "3.1416e42");
}
SECTION("VeryBigNegativeDouble") {
check(JsonVariant(-3.14159265358979323846e42, 4), "-3.1416e42");
}
SECTION("VerySmallPositiveDouble") {
check(JsonVariant(3.14159265358979323846e-42, 4), "3.1416e-42");
}
SECTION("VerySmallNegativeDouble") {
check(JsonVariant(-3.14159265358979323846e-42, 4), "-3.1416e-42");
}
SECTION("Integer") {
check(42, "42");
}
2014-11-11 17:26:51 +01:00
SECTION("NegativeLong") {
check(-42, "-42");
}
2014-11-11 17:26:51 +01:00
SECTION("UnsignedLong") {
check(4294967295UL, "4294967295");
}
SECTION("True") {
check(true, "true");
}
2014-11-11 17:26:51 +01:00
SECTION("OneFalse") {
check(false, "false");
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
SECTION("NegativeInt64") {
check(-9223372036854775807 - 1, "-9223372036854775808");
}
SECTION("PositiveInt64") {
check(9223372036854775807, "9223372036854775807");
}
SECTION("UInt64") {
check(18446744073709551615, "18446744073709551615");
}
#endif
}