Skip to content

Commit 1236b7e

Browse files
committed
Add tests for extended-precision long double
Exercise the x87 80-bit / IEEE binary128 path with values that carry more precision and range than double, using snprintf's %L output as the oracle.
1 parent d84564b commit 1236b7e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

test/zmij-test.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,37 @@ TEST(long_double_test, write_fixed) {
617617
}
618618
}
619619

620+
// Exercise the actual extended-precision path (x87 80-bit / IEEE binary128) with
621+
// values carrying more precision and range than double. snprintf's %L output is
622+
// the oracle, so the test also holds where long double is just double.
623+
TEST(long_double_test, extended) {
624+
char buf[8192], ref[8192];
625+
long double values[] = {
626+
3.14159265358979323846264338327950288L, // pi beyond double precision
627+
1.0L + 0x1p-60L, // differs from 1.0 only when extended
628+
1.23456789012345678901234567890123L, // 33 significant digits
629+
std::numeric_limits<long double>::max(), // extreme range
630+
std::numeric_limits<long double>::denorm_min(), // smallest subnormal
631+
};
632+
for (long double value : values) {
633+
for (int precision : {-1, 0, 1, 20, 40}) {
634+
int p = precision < 0 ? 6 : precision;
635+
char* end = zmij::write_scientific(buf, sizeof(buf), value, precision);
636+
snprintf(ref, sizeof(ref), "%.*Le", p, value);
637+
EXPECT_EQ(std::string(buf, end), std::string(ref))
638+
<< "scientific value=" << value << " precision=" << precision;
639+
end = zmij::write_fixed(buf, sizeof(buf), value, precision);
640+
snprintf(ref, sizeof(ref), "%.*Lf", p, value);
641+
EXPECT_EQ(std::string(buf, end), std::string(ref))
642+
<< "fixed value=" << value << " precision=" << precision;
643+
end = zmij::write_general(buf, sizeof(buf), value, precision);
644+
snprintf(ref, sizeof(ref), "%.*Lg", p, value);
645+
EXPECT_EQ(std::string(buf, end), std::string(ref))
646+
<< "general value=" << value << " precision=" << precision;
647+
}
648+
}
649+
}
650+
620651
TEST(float_test, write_general) {
621652
EXPECT_EQ(to_general(1.5f, 6), "1.5");
622653
EXPECT_EQ(to_general(0.0001f, 6), "0.0001"); // exp10 == -4 -> fixed

0 commit comments

Comments
 (0)