diff --git a/test/compile-test.cc b/test/compile-test.cc index 4038ac58..c762b581 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -7,9 +7,6 @@ #include #include -#if __cplusplus >= 202002L -# include -#endif // Check that fmt/compile.h compiles with windows.h included before it. #ifdef _WIN32 @@ -188,16 +185,16 @@ TEST(CompileTest, CompileFormatStringLiteral) { #endif #if __cplusplus >= 202002L -template struct test_string { +template struct test_string { template constexpr bool operator==(const T& rhs) const noexcept { - return (std::string_view(rhs).compare(buffer.data()) == 0); + return fmt::basic_string_view(rhs).compare(buffer.data()) == 0; } - std::array buffer{}; + std::array buffer{}; }; -template +template consteval auto test_format(auto format, const Args&... args) { - test_string string{}; + test_string string{}; fmt::format_to(string.buffer.data(), format, args...); return string; } @@ -205,6 +202,8 @@ consteval auto test_format(auto format, const Args&... args) { TEST(CompileTimeFormattingTest, Bool) { EXPECT_EQ("true", test_format<5>(FMT_COMPILE("{}"), true)); EXPECT_EQ("false", test_format<6>(FMT_COMPILE("{}"), false)); + EXPECT_EQ("true ", test_format<6>(FMT_COMPILE("{:5}"), true)); + EXPECT_EQ("1", test_format<2>(FMT_COMPILE("{:d}"), true)); } TEST(CompileTimeFormattingTest, Integer) { @@ -213,16 +212,52 @@ TEST(CompileTimeFormattingTest, Integer) { EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), 42, 42)); EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42})); + + EXPECT_EQ("+42", test_format<4>(FMT_COMPILE("{:+}"), 42)); + EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{:-}"), 42)); + EXPECT_EQ(" 42", test_format<4>(FMT_COMPILE("{: }"), 42)); + + EXPECT_EQ("101010", test_format<7>(FMT_COMPILE("{:b}"), 42)); + EXPECT_EQ("0b101010", test_format<9>(FMT_COMPILE("{:#b}"), 42)); + EXPECT_EQ("0B101010", test_format<9>(FMT_COMPILE("{:#B}"), 42)); + EXPECT_EQ("042", test_format<4>(FMT_COMPILE("{:#o}"), 042)); + EXPECT_EQ("0x4a", test_format<5>(FMT_COMPILE("{:#x}"), 0x4a)); + EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a)); + + EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42)); + EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll)); + EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull)); + + EXPECT_EQ("42 ", test_format<5>(FMT_COMPILE("{:<4}"), 42)); + EXPECT_EQ(" 42", test_format<5>(FMT_COMPILE("{:>4}"), 42)); + EXPECT_EQ(" 42 ", test_format<5>(FMT_COMPILE("{:^4}"), 42)); + EXPECT_EQ("**-42", test_format<6>(FMT_COMPILE("{:*>5}"), -42)); +} + +TEST(CompileTimeFormattingTest, Char) { + EXPECT_EQ("c", test_format<2>(FMT_COMPILE("{}"), 'c')); + + EXPECT_EQ("c ", test_format<4>(FMT_COMPILE("{:3}"), 'c')); + EXPECT_EQ("99", test_format<3>(FMT_COMPILE("{:d}"), 'c')); } TEST(CompileTimeFormattingTest, String) { EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), "42")); EXPECT_EQ("The answer is 42", test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42")); + + EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc")); + EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^5}"), "🤡")); } TEST(CompileTimeFormattingTest, Combination) { EXPECT_EQ("420, true, answer", test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer")); + + EXPECT_EQ(" -42", test_format<5>(FMT_COMPILE("{:{}}"), -42, 4)); +} + +TEST(CompileTimeFormattingTest, MultiByteFill) { + EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42)); } #endif