Skip to content

Commit

Permalink
Add fp printing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Sep 5, 2024
1 parent 3ef912f commit 03b2c73
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/HostTests/modules/Libc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <HostTests.h>
#include <FlashString/Array.hpp>

static int num_instances;

Expand All @@ -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<float>::infinity(), "Inf", "Inf") \
XX(-std::numeric_limits<float>::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<int64_t>::max(), "9223372036854775808.0000000000000000", "9223372036854775808.0") \
XX((double)std::numeric_limits<int64_t>::min(), "-9223372036854775808.0000000000000000", "-9223372036854775808.0")

template <typename T> 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<float>, FLOAT32_TEST_MAP(XX))
DEFINE_FSTR_ARRAY_LOCAL(float64_tests, FloatTest<double>, FLOAT64_TEST_MAP(XX))
#undef XX

/*
* Anything to do with main C/C++ libraries, startup code, etc.
*/
Expand Down Expand Up @@ -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 <typename T> void checkFloats(const FSTR::Array<FloatTest<T>>& 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;
}
}
};

Expand Down

0 comments on commit 03b2c73

Please sign in to comment.