mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-25 16:27:33 +02:00
Changed unit testing framework from Google Test to Catch
This commit is contained in:
@ -5,109 +5,86 @@
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
#include <ArduinoJson/Serialization/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
class JsonWriter_WriteFloat_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(double input, uint8_t digits = 2) {
|
||||
StaticStringBuilder sb(buffer, sizeof(buffer));
|
||||
JsonWriter writer(sb);
|
||||
writer.writeFloat(input, digits);
|
||||
returnValue = writer.bytesWritten();
|
||||
void check(const std::string& expected, double input, uint8_t digits = 2) {
|
||||
char output[1024];
|
||||
StaticStringBuilder sb(output, sizeof(output));
|
||||
JsonWriter writer(sb);
|
||||
writer.writeFloat(input, digits);
|
||||
REQUIRE(output == expected);
|
||||
REQUIRE(writer.bytesWritten() == expected.size());
|
||||
}
|
||||
|
||||
TEST_CASE("JsonWriter::writeFloat()") {
|
||||
SECTION("NaN") {
|
||||
check("NaN", std::numeric_limits<double>::signaling_NaN());
|
||||
}
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), returnValue);
|
||||
SECTION("PositiveInfinity") {
|
||||
check("Infinity", std::numeric_limits<double>::infinity());
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
};
|
||||
SECTION("NegativeInfinity") {
|
||||
check("-Infinity", -std::numeric_limits<double>::infinity());
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, NaN) {
|
||||
whenInputIs(std::numeric_limits<double>::signaling_NaN());
|
||||
outputMustBe("NaN");
|
||||
}
|
||||
SECTION("Zero") {
|
||||
check("0.00", 0);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, PositiveInfinity) {
|
||||
whenInputIs(std::numeric_limits<double>::infinity());
|
||||
outputMustBe("Infinity");
|
||||
}
|
||||
SECTION("ZeroDigits_Rounding") {
|
||||
check("10", 9.5, 0);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, NegativeInfinity) {
|
||||
whenInputIs(-std::numeric_limits<double>::infinity());
|
||||
outputMustBe("-Infinity");
|
||||
}
|
||||
SECTION("ZeroDigits_NoRounding") {
|
||||
check("9", 9.4, 0);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, Zero) {
|
||||
whenInputIs(0);
|
||||
outputMustBe("0.00");
|
||||
}
|
||||
SECTION("OneDigit_Rounding") {
|
||||
check("10.0", 9.95, 1);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ZeroDigits_Rounding) {
|
||||
whenInputIs(9.5, 0);
|
||||
outputMustBe("10");
|
||||
}
|
||||
SECTION("OneDigit_NoRounding") {
|
||||
check("9.9", 9.94, 1);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ZeroDigits_NoRounding) {
|
||||
whenInputIs(9.4, 0);
|
||||
outputMustBe("9");
|
||||
}
|
||||
SECTION("TwoDigits_Rounding") {
|
||||
check("10.00", 9.995, 2);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, OneDigit_Rounding) {
|
||||
whenInputIs(9.95, 1);
|
||||
outputMustBe("10.0");
|
||||
}
|
||||
SECTION("TwoDigits_NoRounding") {
|
||||
check("9.99", 9.994, 2);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, OneDigit_NoRounding) {
|
||||
whenInputIs(9.94, 1);
|
||||
outputMustBe("9.9");
|
||||
}
|
||||
SECTION("ThreeDigits_Rounding") {
|
||||
check("10.000", 9.9995, 3);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, TwoDigits_Rounding) {
|
||||
whenInputIs(9.995, 2);
|
||||
outputMustBe("10.00");
|
||||
}
|
||||
SECTION("ThreeDigits_NoRounding") {
|
||||
check("9.999", 9.9994, 3);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, TwoDigits_NoRounding) {
|
||||
whenInputIs(9.994, 2);
|
||||
outputMustBe("9.99");
|
||||
}
|
||||
SECTION("FourDigits_Rounding") {
|
||||
check("10.0000", 9.99995, 4);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ThreeDigits_Rounding) {
|
||||
whenInputIs(9.9995, 3);
|
||||
outputMustBe("10.000");
|
||||
}
|
||||
SECTION("FourDigits_NoRounding") {
|
||||
check("9.9999", 9.99994, 4);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ThreeDigits_NoRounding) {
|
||||
whenInputIs(9.9994, 3);
|
||||
outputMustBe("9.999");
|
||||
}
|
||||
SECTION("FiveDigits_Rounding") {
|
||||
check("10.00000", 9.999995, 5);
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FourDigits_Rounding) {
|
||||
whenInputIs(9.99995, 4);
|
||||
outputMustBe("10.0000");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FourDigits_NoRounding) {
|
||||
whenInputIs(9.99994, 4);
|
||||
outputMustBe("9.9999");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FiveDigits_Rounding) {
|
||||
whenInputIs(9.999995, 5);
|
||||
outputMustBe("10.00000");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FiveDigits_NoRounding) {
|
||||
whenInputIs(9.999994, 5);
|
||||
outputMustBe("9.99999");
|
||||
SECTION("FiveDigits_NoRounding") {
|
||||
check("9.99999", 9.999994, 5);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user