From cbf3ad2b284a9e79ef5df564be6b16e8e746cb2b Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 27 Oct 2024 20:13:52 -0500 Subject: Setup float formatting. --- test/test_format.cpp | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) (limited to 'test') diff --git a/test/test_format.cpp b/test/test_format.cpp index 4a93d98..4dec6b1 100644 --- a/test/test_format.cpp +++ b/test/test_format.cpp @@ -504,6 +504,196 @@ TEST(FormatTest, OneBoolFormatTest) EXPECT_STREQ(sink.c_str(), expected.c_str()); } +TEST(FormatTest, OneFloatFormatUnspecifiedTest) +{ +/* + String sink{}; + + ASSERT_EQ(sink.clear(), FudStatus::Success); + auto expected = std::format("{:}", 42.0); + auto formatResult = format(sink, FormatCharMode::Unchecked, "{:}", 42.0); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + ASSERT_EQ(sink.clear(), FudStatus::Success); + expected = std::format("{:1.0}", 42.0); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.0}", 42.0); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + expected = std::format("{:1.0}", 42.0); + expected = std::format("u {:}", 10.0); + expected = std::format("u {:}", 100.0); + expected = std::format("u {:}", 1000.0); + expected = std::format("u {:}", 10000.0); + expected = std::format("u {:}", 100000.0); +*/ +} + +TEST(FormatTest, OneFloatFormatScientificTest) +{ + String sink{}; + + ASSERT_EQ(sink.clear(), FudStatus::Success); + auto expected = std::format("{:e}", 42.0); + auto formatResult = format(sink, FormatCharMode::Unchecked, "{:e}", 42.0); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.2e}", 0.99); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.2e}", 0.99); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.1e}", 0.99); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.1e}", 0.99); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.0e}", 0.99); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.0e}", 0.99); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.0e}", -0.99); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.0e}", -0.99); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.0e}", 42.0); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.0e}", 42.0); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.0e}", 4.20e-5); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.0e}", 4.20e-5); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("{:1.0e}", -4.20e-5); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "{:1.0e}", -4.20e-5); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:e}", 3.14); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:e}", 3.14); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("E {:E}", 3.14); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "E {:E}", 3.14); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:.0e}", 3.14); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", 3.14); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:+#010.0e}", 3.14); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:+#010.0e}", 3.14); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + double maxVal = std::numeric_limits::max(); + double subNormMin = std::numeric_limits::denorm_min(); + + expected = std::format("e {:.0e}", maxVal / 4); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", maxVal / 4); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:.0e}", maxVal / 2); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", maxVal / 2); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:.0e}", maxVal); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", maxVal); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:.0e}", std::numeric_limits::min()); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", std::numeric_limits::min()); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("e {:.0e}", subNormMin); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", subNormMin); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + double val = 3.0; + + expected = std::format("E#+ {:+#060.50E}", val); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "E#+ {:+#060.50E}", val); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + // Which is: "E#+ +0003.0000000000000000000000000000000000E+00" + // Which is: "E#+ +0003.00000000000000000000000000000000000000000000000000E+00" + +/* + expected = std::format("E#+ {:Z>+#060.30E}", val); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "E#+ {:Z>+#060.30E}", val); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("E#+ {:Z<+#060.30E}", val); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "E#+ {:Z<+#060.30E}", val); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); + + expected = std::format("E#+ {:Z^+#060.30E}", val); + ASSERT_EQ(sink.clear(), FudStatus::Success); + formatResult = format(sink, FormatCharMode::Unchecked, "E#+ {:Z^+#060.30E}", val); + EXPECT_TRUE(formatResult.isOkay()); + EXPECT_STREQ(sink.c_str(), expected.c_str()); +*/ + +/* + double val = 3.0; + expected = std::format("u {:}", val); + expected = std::format("f {:f}", val); + expected = std::format("F {:F}", val); + expected = std::format("a {:a}", val); + expected = std::format("A {:A}", val); + expected = std::format("g {:g}", val); + expected = std::format("G {:G}", val); + expected = std::format("u# {:#}", val); + expected = std::format("f# {:#f}", val); + expected = std::format("F# {:#F}", val); + expected = std::format("a# {:#a}", val); + expected = std::format("A# {:#A}", val); + expected = std::format("g# {:#g}", val); + expected = std::format("G# {:#G}", val); + expected = std::format("u#+ {:+#}", val); + expected = std::format("f#+ {:+#f}", val); + expected = std::format("F#+ {:+#F}", val); + expected = std::format("a#+ {:+#a}", val); + expected = std::format("A#+ {:+#A}", val); + expected = std::format("g#+ {:+#g}", val); + expected = std::format("G#+ {:+#010.3G}", val); +*/ +} + TEST(FormatTest, TwoArgFormatTest) { String sink{}; -- cgit v1.2.3