/* * libfud * Copyright 2024 Dominick Allen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "fud_format.hpp" #include "fud_string_view.hpp" #include "gtest/gtest.h" #include namespace fud { TEST(FormatTest, BasePositionalTest) { size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8" {1:}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::ArgumentInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(length, 3); formatSpecResult = FormatSpec::parse(StringView{u8""}, length); ASSERT_EQ(formatSpecResult.takeErrorOr(FudStatus::NotImplemented), FudStatus::ArgumentInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1:}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 4); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default); EXPECT_EQ(formatSpec.fill.fill, ' '); EXPECT_EQ(formatSpec.formatSign, FormatSign::Default); EXPECT_FALSE(formatSpec.takesWidth); EXPECT_FALSE(formatSpec.takesPrecision); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); EXPECT_FALSE(formatSpec.hasLocale); EXPECT_EQ(formatSpec.formatType, FormatType::Unspecified); } TEST(FormatTest, AlignTest) { size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8"{1:<}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(length, 5); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, ' '); EXPECT_FALSE(formatSpec.takesWidth); EXPECT_FALSE(formatSpec.takesPrecision); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); EXPECT_FALSE(formatSpec.hasLocale); EXPECT_EQ(formatSpec.formatType, FormatType::Unspecified); formatSpecResult = FormatSpec::parse(StringView{u8"{1:<<}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, '<'); formatSpecResult = FormatSpec::parse(StringView{u8"{:<<}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 5); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, '<'); formatSpecResult = FormatSpec::parse(StringView{u8"{1:_<}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 6); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, '_'); } TEST(FormatTest, SpecialTest) { size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8"{1:_< }"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, '_'); EXPECT_EQ(formatSpec.formatSign, FormatSign::Space); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1:+}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); EXPECT_EQ(length, 5); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.formatSign, FormatSign::Plus); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1:-}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 5); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.formatSign, FormatSign::Minus); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1:_<#}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, '_'); EXPECT_EQ(formatSpec.formatSign, FormatSign::Default); EXPECT_TRUE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1:_<0}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Left); EXPECT_EQ(formatSpec.fill.fill, '_'); EXPECT_EQ(formatSpec.formatSign, FormatSign::Default); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_TRUE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1: #}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 6); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default); EXPECT_EQ(formatSpec.fill.fill, ' '); EXPECT_EQ(formatSpec.formatSign, FormatSign::Space); EXPECT_TRUE(formatSpec.alternateForm); EXPECT_FALSE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1:# }"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1:##}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1: 0}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 6); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default); EXPECT_EQ(formatSpec.fill.fill, ' '); EXPECT_EQ(formatSpec.formatSign, FormatSign::Space); EXPECT_FALSE(formatSpec.alternateForm); EXPECT_TRUE(formatSpec.leadingZero); formatSpecResult = FormatSpec::parse(StringView{u8"{1:0 }"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1:0#}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1:#00}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{1: #0}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, 1); EXPECT_EQ(formatSpec.fill.align(), FormatAlign::Value::Default); EXPECT_EQ(formatSpec.fill.fill, ' '); EXPECT_EQ(formatSpec.formatSign, FormatSign::Space); EXPECT_TRUE(formatSpec.alternateForm); EXPECT_TRUE(formatSpec.leadingZero); } TEST(FormatTest, WidthTest) { size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8"{1:1}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 5); EXPECT_EQ(formatSpec.position, 1); EXPECT_FALSE(formatSpec.takesWidth); EXPECT_EQ(formatSpec.width, 1); formatSpecResult = FormatSpec::parse(StringView{u8"{1:543}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, 1); EXPECT_FALSE(formatSpec.takesWidth); EXPECT_EQ(formatSpec.width, 543); formatSpecResult = FormatSpec::parse(StringView{u8"{:543}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 6); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_FALSE(formatSpec.takesWidth); EXPECT_EQ(formatSpec.width, 543); // leading zero formatSpecResult = FormatSpec::parse(StringView{u8"{1:00}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid); // #x100000000 4294967296 formatSpecResult = FormatSpec::parse(StringView{u8"{1:4294967296}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError); formatSpecResult = FormatSpec::parse(StringView{u8"{1:{1}}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, 1); EXPECT_TRUE(formatSpec.takesWidth); EXPECT_EQ(formatSpec.width, 1); // #x10000 65536 formatSpecResult = FormatSpec::parse(StringView{u8"{1:{65536}}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError); formatSpecResult = FormatSpec::parse(StringView{u8"{:{1}}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{:{}}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 5); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_TRUE(formatSpec.takesWidth); EXPECT_EQ(formatSpec.width, FormatSpec::widthUnspecified); } TEST(FormatTest, PrecisionTest) { size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8"{1:.1}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 6); EXPECT_EQ(formatSpec.position, 1); EXPECT_FALSE(formatSpec.takesPrecision); EXPECT_EQ(formatSpec.precision, 1); formatSpecResult = FormatSpec::parse(StringView{u8"{1:.543}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 8); EXPECT_EQ(formatSpec.position, 1); EXPECT_FALSE(formatSpec.takesPrecision); EXPECT_EQ(formatSpec.precision, 543); formatSpecResult = FormatSpec::parse(StringView{u8"{:.543}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 7); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_FALSE(formatSpec.takesPrecision); EXPECT_EQ(formatSpec.precision, 543); // leading zero formatSpecResult = FormatSpec::parse(StringView{u8"{1:00}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid); // #x100000000 4294967296 formatSpecResult = FormatSpec::parse(StringView{u8"{1:.4294967296}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError); formatSpecResult = FormatSpec::parse(StringView{u8"{1:.{1}}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 8); EXPECT_EQ(formatSpec.position, 1); EXPECT_TRUE(formatSpec.takesPrecision); EXPECT_EQ(formatSpec.precision, 1); // #x10000 65536 formatSpecResult = FormatSpec::parse(StringView{u8"{1:.{65536}}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::RangeError); formatSpecResult = FormatSpec::parse(StringView{u8"{:.{1}}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.getError(), FudStatus::FormatInvalid); formatSpecResult = FormatSpec::parse(StringView{u8"{:.{}}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 6); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_TRUE(formatSpec.takesPrecision); EXPECT_EQ(formatSpec.precision, FormatSpec::widthUnspecified); } TEST(FormatTest, LocaleTest) { size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8"{1:L}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 5); EXPECT_EQ(formatSpec.position, 1); EXPECT_TRUE(formatSpec.hasLocale); formatSpecResult = FormatSpec::parse(StringView{u8"{:L}"}, length); ASSERT_TRUE(formatSpecResult.isOkay()); formatSpec = formatSpecResult.takeOkay(); EXPECT_EQ(length, 4); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_TRUE(formatSpec.hasLocale); } TEST(FormatTest, FormatTest) { auto lengthArray{Array::constFill(0U)}; const Array formatTypeResults{ FormatSpec::parse(StringView{u8"{:s}"}, lengthArray[0x00]), FormatSpec::parse(StringView{u8"{:?}"}, lengthArray[0x01]), FormatSpec::parse(StringView{u8"{:b}"}, lengthArray[0x02]), FormatSpec::parse(StringView{u8"{:B}"}, lengthArray[0x03]), FormatSpec::parse(StringView{u8"{:c}"}, lengthArray[0x04]), FormatSpec::parse(StringView{u8"{:d}"}, lengthArray[0x05]), FormatSpec::parse(StringView{u8"{:o}"}, lengthArray[0x06]), FormatSpec::parse(StringView{u8"{:x}"}, lengthArray[0x07]), FormatSpec::parse(StringView{u8"{:X}"}, lengthArray[0x08]), FormatSpec::parse(StringView{u8"{:a}"}, lengthArray[0x09]), FormatSpec::parse(StringView{u8"{:A}"}, lengthArray[0x0A]), FormatSpec::parse(StringView{u8"{:e}"}, lengthArray[0x0B]), FormatSpec::parse(StringView{u8"{:E}"}, lengthArray[0x0C]), FormatSpec::parse(StringView{u8"{:f}"}, lengthArray[0x0D]), FormatSpec::parse(StringView{u8"{:F}"}, lengthArray[0x0E]), FormatSpec::parse(StringView{u8"{:g}"}, lengthArray[0x0F]), FormatSpec::parse(StringView{u8"{:G}"}, lengthArray[0x10])}; const Array formatTypes{ FormatType::String, FormatType::Escaped, FormatType::BinaryLower, FormatType::BinaryUpper, FormatType::Character, FormatType::Decimal, FormatType::Octal, FormatType::HexLower, FormatType::HexUpper, FormatType::FloatHexLower, FormatType::FloatHexUpper, FormatType::ScientificLower, FormatType::ScientificUpper, FormatType::FixedLower, FormatType::FixedUpper, FormatType::GeneralLower, FormatType::GeneralUpper, }; for (size_t index = 0; index < lengthArray.size(); ++index) { const auto& formatSpecResult = formatTypeResults[index]; const auto& length = lengthArray[index]; ASSERT_TRUE(formatSpecResult.isOkay()); auto formatSpec = formatSpecResult.getOkay(); EXPECT_EQ(length, 4); EXPECT_EQ(formatSpec.position, FormatSpec::positionUnspecified); EXPECT_EQ(formatSpec.formatType, formatTypes[index]); } size_t length = 0; auto formatSpecResult = FormatSpec::parse(StringView{u8"{:N}"}, length); ASSERT_TRUE(formatSpecResult.isError()); EXPECT_EQ(formatSpecResult.takeError(), FudStatus::FormatInvalid); } TEST(FormatTest, NoArgFormatTest) { String sink{}; auto expected = std::format("Test"); auto formatResult = format(sink, FormatCharMode::Unchecked, u8"Test"); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format(""); formatResult = format(sink, FormatCharMode::Unchecked, u8""); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); } TEST(FormatTest, OneNumberFormatTest) { String sink{}; auto formatResult = format(sink, FormatCharMode::Unchecked, u8"Test {:}", 42U); EXPECT_TRUE(formatResult.isOkay()); ASSERT_EQ(sink.clear(), FudStatus::Success); auto expected = std::format("{:6}", 42U); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:6}", 42U); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:*<6}", std::numeric_limits::max()); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:*<6}", std::numeric_limits::max()); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:*<6}", std::numeric_limits::min()); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:*<6}", std::numeric_limits::min()); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:}", std::numeric_limits::min()); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:}", std::numeric_limits::min()); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:#X}", std::numeric_limits::min()); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:#X}", std::numeric_limits::min()); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:*<6}", 42U); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:*<6}", 42U); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:*>6}", 42U); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:*>6}", 42U); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:*^6}", 42U); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:*^6}", 42U); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:*<6}", 'x'); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:*<6}", 'x'); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("{:6d}", 'x'); formatResult = format(sink, FormatCharMode::Unchecked, u8"{:6d}", 'x'); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); } TEST(FormatTest, OneBoolFormatTest) { String sink{}; ASSERT_EQ(sink.clear(), FudStatus::Success); auto expected = std::format("{:6}", true); auto formatResult = format(sink, FormatCharMode::Unchecked, u8"{:6}", true); EXPECT_TRUE(formatResult.isOkay()); 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, u8"{:}", 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, u8"{: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, u8"{: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, u8"{: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, u8"{: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, u8"{: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, u8"{: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, u8"{: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, u8"{: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, u8"{: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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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, u8"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{}; auto expected = std::format("Test {:} {:}", 1U, false); auto formatResult = format(sink, FormatCharMode::Unchecked, u8"Test {:} {:}", 1U, false); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); ASSERT_EQ(sink.clear(), FudStatus::Success); expected = std::format("Test {:} {:}", 1U, "Hello"); formatResult = format(sink, FormatCharMode::Unchecked, u8"Test {:} {:}", 1U, u8"Hello"); EXPECT_TRUE(formatResult.isOkay()); EXPECT_STREQ(sink.c_str(), expected.c_str()); } } // namespace fud