1 // Formatting library for C++ - Grisu tests
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #include "fmt/format.h"
9 #include "gtest.h"
10 
11 static bool reported_skipped;
12 
13 #undef TEST
14 #define TEST(test_fixture, test_name)        \
15   void test_fixture##test_name();            \
16   GTEST_TEST(test_fixture, test_name) {      \
17     if (FMT_USE_GRISU) {                     \
18       test_fixture##test_name();             \
19     } else if (!reported_skipped) {          \
20       reported_skipped = true;               \
21       fmt::print("Skipping Grisu tests.\n"); \
22     }                                        \
23   }                                          \
24   void test_fixture##test_name()
25 
TEST(GrisuTest,NaN)26 TEST(GrisuTest, NaN) {
27   auto nan = std::numeric_limits<double>::quiet_NaN();
28   EXPECT_EQ("nan", fmt::format("{}", nan));
29   EXPECT_EQ("-nan", fmt::format("{}", -nan));
30 }
31 
TEST(GrisuTest,Inf)32 TEST(GrisuTest, Inf) {
33   auto inf = std::numeric_limits<double>::infinity();
34   EXPECT_EQ("inf", fmt::format("{}", inf));
35   EXPECT_EQ("-inf", fmt::format("{}", -inf));
36 }
37 
TEST(GrisuTest,Zero)38 TEST(GrisuTest, Zero) { EXPECT_EQ("0.0", fmt::format("{}", 0.0)); }
39 
TEST(GrisuTest,Round)40 TEST(GrisuTest, Round) {
41   EXPECT_EQ("1.9156918820264798e-56",
42             fmt::format("{}", 1.9156918820264798e-56));
43   EXPECT_EQ("0.0000", fmt::format("{:.4f}", 7.2809479766055470e-15));
44 }
45 
TEST(GrisuTest,Prettify)46 TEST(GrisuTest, Prettify) {
47   EXPECT_EQ("0.0001", fmt::format("{}", 1e-4));
48   EXPECT_EQ("1e-05", fmt::format("{}", 1e-5));
49   EXPECT_EQ("9.999e-05", fmt::format("{}", 9.999e-5));
50   EXPECT_EQ("10000000000.0", fmt::format("{}", 1e10));
51   EXPECT_EQ("100000000000.0", fmt::format("{}", 1e11));
52   EXPECT_EQ("12340000000.0", fmt::format("{}", 1234e7));
53   EXPECT_EQ("12.34", fmt::format("{}", 1234e-2));
54   EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6));
55   EXPECT_EQ("0.1", fmt::format("{}", 0.1f));
56   EXPECT_EQ("0.10000000149011612", fmt::format("{}", double(0.1f)));
57 }
58 
TEST(GrisuTest,ZeroPrecision)59 TEST(GrisuTest, ZeroPrecision) { EXPECT_EQ("1", fmt::format("{:.0}", 1.0)); }
60 
TEST(GrisuTest,Fallback)61 TEST(GrisuTest, Fallback) {
62   EXPECT_EQ("1e+23", fmt::format("{}", 1e23));
63   EXPECT_EQ("9e-265", fmt::format("{}", 9e-265));
64   EXPECT_EQ("5.423717798060526e+125",
65             fmt::format("{}", 5.423717798060526e+125));
66   EXPECT_EQ("1.372371880954233e-288",
67             fmt::format("{}", 1.372371880954233e-288));
68   EXPECT_EQ("55388492.622190244", fmt::format("{}", 55388492.622190244));
69   EXPECT_EQ("2.2506787569811123e-253",
70             fmt::format("{}", 2.2506787569811123e-253));
71   EXPECT_EQ("1103618912042992.8", fmt::format("{}", 1103618912042992.8));
72   // pow(2, -25) - assymetric boundaries:
73   EXPECT_EQ("2.9802322387695312e-08",
74             fmt::format("{}", 2.9802322387695312e-08));
75 }
76