aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorOrivej Desh <orivej@gmx.fr>2019-10-12 12:17:27 +0000
committerVictor Zverovich <victor.zverovich@gmail.com>2019-10-18 07:08:41 -0700
commit599e0aef45cf2dce0a60a7ae6feca4cebc0752c0 (patch)
tree8c85296945e3180ba6d971c67b8462328a1006f3 /test
parent91f7619cc9cfd38ca3b73494793570ab93f69e08 (diff)
downloadfmtlib-599e0aef45cf2dce0a60a7ae6feca4cebc0752c0.tar.gz
Support single precision floats in grisu formatting
Fixes #1336
Diffstat (limited to 'test')
-rw-r--r--test/format-impl-test.cc30
-rw-r--r--test/grisu-test.cc2
2 files changed, 32 insertions, 0 deletions
diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc
index c3f81904..852b91a1 100644
--- a/test/format-impl-test.cc
+++ b/test/format-impl-test.cc
@@ -221,6 +221,36 @@ TEST(FPTest, ComputeBoundaries) {
EXPECT_EQ(31, upper.e);
}
+TEST(FPTest, ComputeFloatBoundaries) {
+ struct {
+ double x, lower, upper;
+ } tests[] = {
+ // regular
+ {1.5f, 1.4999999403953552, 1.5000000596046448},
+ // boundary
+ {1.0f, 0.9999999701976776, 1.0000000596046448},
+ // min normal
+ {1.1754944e-38f, 1.1754942807573643e-38, 1.1754944208872107e-38},
+ // max subnormal
+ {1.1754942e-38f, 1.1754941406275179e-38, 1.1754942807573643e-38},
+ // min subnormal
+ {1e-45f, 7.006492321624085e-46, 2.1019476964872256e-45},
+ };
+ for (auto test : tests) {
+ auto v = fp(test.x);
+ fp vlower = normalize(fp(test.lower));
+ fp vupper = normalize(fp(test.upper));
+ vlower.f >>= vupper.e - vlower.e;
+ vlower.e = vupper.e;
+ fp lower, upper;
+ v.compute_float_boundaries(lower, upper);
+ EXPECT_EQ(vlower.f, lower.f);
+ EXPECT_EQ(vlower.e, lower.e);
+ EXPECT_EQ(vupper.f, upper.f);
+ EXPECT_EQ(vupper.e, upper.e);
+ }
+}
+
TEST(FPTest, Subtract) {
auto v = fp(123, 1) - fp(102, 1);
EXPECT_EQ(v.f, 21u);
diff --git a/test/grisu-test.cc b/test/grisu-test.cc
index 6b03b9c7..b66c6b7f 100644
--- a/test/grisu-test.cc
+++ b/test/grisu-test.cc
@@ -52,6 +52,8 @@ TEST(GrisuTest, Prettify) {
EXPECT_EQ("12340000000.0", fmt::format("{}", 1234e7));
EXPECT_EQ("12.34", fmt::format("{}", 1234e-2));
EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6));
+ EXPECT_EQ("0.1", fmt::format("{}", 0.1f));
+ EXPECT_EQ("0.10000000149011612", fmt::format("{}", double(0.1f)));
}
TEST(GrisuTest, ZeroPrecision) { EXPECT_EQ("1", fmt::format("{:.0}", 1.0)); }