diff --git a/tests/HostTests/modules/Libc.cpp b/tests/HostTests/modules/Libc.cpp index c5c1d55e61..8878fa96e6 100644 --- a/tests/HostTests/modules/Libc.cpp +++ b/tests/HostTests/modules/Libc.cpp @@ -1,4 +1,5 @@ #include +#include static int num_instances; @@ -21,6 +22,54 @@ A a1 __attribute__((init_priority(990))); // Simple check to determine if a pointer refers to ROM #define isRomPtr(ptr) (uint32_t(ptr) >= 0x40000000 && uint32_t(ptr) < 0x40100000) +/* + Format Exponent Mantissa (effective bits) + Float32 IEEE754 single-precision 8 23 (24) + Float64 IEEE754 double-precision 11 52 (53) +*/ +#define FLOAT32_TEST_MAP(XX) \ + XX(0x0100'0000L, "16777216.00000000", "16777216.0") \ + XX(-0x0100'0000L, "-16777216.00000000", "-16777216.0") \ + XX(0x0100'0000L / 1000.0, "16777.21679687", "16777.21679687") \ + XX(4294967040 * 10000.0, "42949668765696.00000000", "42949668765696.0") \ + XX(-4294967040, "-4294967040.00000000", "-4294967040.0") \ + XX(std::numeric_limits::infinity(), "Inf", "Inf") \ + XX(-std::numeric_limits::infinity(), "Inf", "Inf") \ + XX(3.1415926535897932384626433, "3.14159274", "3.14159274") \ + XX(32766.6612999999997555, "32766.66210937", "32766.66210937") \ + XX(1.6613, "1.66129994", "1.66129994") \ + XX(-32766.1234, "-32766.12304687", "-32766.12304687") \ + XX(0.0, "0.00000000", "0.0") \ + XX(-0.0, "0.00000000", "0.0") \ + XX(100.0, "100.00000000", "100.0") + +#define FLOAT64_TEST_MAP(XX) \ + XX(0x0020'0000'0000'0000LL, "9007199254740992.0000000000000000", "9007199254740992.0") \ + XX(-0x0020'0000'0000'0000LL, "-9007199254740992.0000000000000000", "-9007199254740992.0") \ + XX(4294967040 + 1, "4294967041.0000000000000000", "4294967041.0") \ + XX(-4294967040 - 1, "-4294967041.0000000000000000", "-4294967041.0") \ + XX(3.1415926535897932384626433, "3.1415926535897931", "3.1415926535897931") \ + XX(32766.6613, "32766.6612999999997555", "32766.6612999999997555") \ + XX(1.6613, "1.6612999999999999", "1.6612999999999999") \ + XX(-32766.1234, "-32766.1234000000004016", "-32766.1234000000004016") \ + XX(9223372036854775808.0, "9223372036854775808.0000000000000000", "9223372036854775808.0") \ + XX(-9223372036854775808.0, "-9223372036854775808.0000000000000000", "-9223372036854775808.0") \ + XX(3.4e18, "3400000000000000000.0000000000000000", "3400000000000000000.0") \ + XX((double)std::numeric_limits::max(), "9223372036854775808.0000000000000000", "9223372036854775808.0") \ + XX((double)std::numeric_limits::min(), "-9223372036854775808.0000000000000000", "-9223372036854775808.0") + +template struct FloatTest { + T num; + PGM_P ref; + PGM_P str1; + PGM_P str2; +}; + +#define XX(num, str1, str2) {num, STR(num), str1, str2}, +DEFINE_FSTR_ARRAY_LOCAL(float32_tests, FloatTest, FLOAT32_TEST_MAP(XX)) +DEFINE_FSTR_ARRAY_LOCAL(float64_tests, FloatTest, FLOAT64_TEST_MAP(XX)) +#undef XX + /* * Anything to do with main C/C++ libraries, startup code, etc. */ @@ -80,6 +129,22 @@ class LibcTest : public TestGroup m_snprintf(buffer, sizeof(buffer), "%llu", 123456789123456789ULL); REQUIRE_EQ(String(buffer), "123456789123456789"); } + + TEST_CASE("Floating-point printing") + { + checkFloats(float32_tests, 8); + checkFloats(float64_tests, 16); + } + } + + template void checkFloats(const FSTR::Array>& tests, int precision) + { + for(auto test : tests) { + Serial << test.ref << ": "; + CHECK_EQ(String(test.str1), String(test.num, precision)); + CHECK_EQ(String(test.str2), String(test.num, -precision)); + Serial << "OK" << endl; + } } };