summaryrefslogtreecommitdiff
path: root/test/test_format.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-27 20:13:52 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-27 20:13:52 -0500
commitcbf3ad2b284a9e79ef5df564be6b16e8e746cb2b (patch)
treef3c3a1cf9c1286144d002611b209f63541b14426 /test/test_format.cpp
parentb8345246dcc2121bcb6d1515a9341789de20199f (diff)
Setup float formatting.
Diffstat (limited to 'test/test_format.cpp')
-rw-r--r--test/test_format.cpp190
1 files changed, 190 insertions, 0 deletions
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<double>::max();
+ double subNormMin = std::numeric_limits<double>::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<double>::min());
+ ASSERT_EQ(sink.clear(), FudStatus::Success);
+ formatResult = format(sink, FormatCharMode::Unchecked, "e {:.0e}", std::numeric_limits<double>::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{};