From fbeee38e30e694519c8723c9467c62994b2a0518 Mon Sep 17 00:00:00 2001 From: Shawn Zhong Date: Sat, 29 Apr 2023 08:07:47 -0500 Subject: [PATCH] Update tests --- test/compile-fp-test.cc | 50 +++++++++++++------------ test/compile-test.cc | 82 +++++++++++++++++++++-------------------- 2 files changed, 68 insertions(+), 64 deletions(-) diff --git a/test/compile-fp-test.cc b/test/compile-fp-test.cc index b35d8302..55a66c31 100644 --- a/test/compile-fp-test.cc +++ b/test/compile-fp-test.cc @@ -27,46 +27,48 @@ consteval auto test_format(auto format, const Args&... args) { } # if FMT_USE_CONSTEXPR_STR -# define TEST_FORMAT(expected, len, str, ...) \ +# define TEST_FORMAT(expected, str, ...) \ do { \ - EXPECT_EQ(expected, test_format(FMT_COMPILE(str), __VA_ARGS__)); \ + EXPECT_EQ(expected, test_format(FMT_COMPILE(str), \ + __VA_ARGS__)); \ static_assert(fmt::format(FMT_COMPILE(str), __VA_ARGS__) == expected); \ } while (false) # else # define TEST_FORMAT(expected, len, str, ...) \ - EXPECT_EQ(expected, test_format(FMT_COMPILE(str), __VA_ARGS__)) + EXPECT_EQ(expected, \ + test_format(FMT_COMPILE(str), __VA_ARGS__)) # endif TEST(compile_time_formatting_test, floating_point) { - TEST_FORMAT("0", 2, "{}", 0.0f); - TEST_FORMAT("392.500000", 11, "{0:f}", 392.5f); + TEST_FORMAT("0", "{}", 0.0f); + TEST_FORMAT("392.500000", "{0:f}", 392.5f); - TEST_FORMAT("0", 2, "{:}", 0.0); - TEST_FORMAT("0.000000", 9, "{:f}", 0.0); - TEST_FORMAT("0", 2, "{:g}", 0.0); - TEST_FORMAT("392.65", 7, "{:}", 392.65); - TEST_FORMAT("392.65", 7, "{:g}", 392.65); - TEST_FORMAT("392.65", 7, "{:G}", 392.65); - TEST_FORMAT("4.9014e+06", 11, "{:g}", 4.9014e6); - TEST_FORMAT("-392.650000", 12, "{:f}", -392.65); - TEST_FORMAT("-392.650000", 12, "{:F}", -392.65); + TEST_FORMAT("0", "{:}", 0.0); + TEST_FORMAT("0.000000", "{:f}", 0.0); + TEST_FORMAT("0", "{:g}", 0.0); + TEST_FORMAT("392.65", "{:}", 392.65); + TEST_FORMAT("392.65", "{:g}", 392.65); + TEST_FORMAT("392.65", "{:G}", 392.65); + TEST_FORMAT("4.9014e+06", "{:g}", 4.9014e6); + TEST_FORMAT("-392.650000", "{:f}", -392.65); + TEST_FORMAT("-392.650000", "{:F}", -392.65); - TEST_FORMAT("3.926500e+02", 13, "{0:e}", 392.65); - TEST_FORMAT("3.926500E+02", 13, "{0:E}", 392.65); - TEST_FORMAT("+0000392.6", 11, "{0:+010.4g}", 392.65); - TEST_FORMAT("9223372036854775808.000000", 27, "{:f}", 9223372036854775807.0); + TEST_FORMAT("3.926500e+02", "{0:e}", 392.65); + TEST_FORMAT("3.926500E+02", "{0:E}", 392.65); + TEST_FORMAT("+0000392.6", "{0:+010.4g}", 392.65); + TEST_FORMAT("9223372036854775808.000000", "{:f}", 9223372036854775807.0); constexpr double nan = std::numeric_limits::quiet_NaN(); - TEST_FORMAT("nan", 4, "{}", nan); - TEST_FORMAT("+nan", 5, "{:+}", nan); + TEST_FORMAT("nan", "{}", nan); + TEST_FORMAT("+nan", "{:+}", nan); if (std::signbit(-nan)) - TEST_FORMAT("-nan", 5, "{}", -nan); + TEST_FORMAT("-nan", "{}", -nan); else fmt::print("Warning: compiler doesn't handle negative NaN correctly"); constexpr double inf = std::numeric_limits::infinity(); - TEST_FORMAT("inf", 4, "{}", inf); - TEST_FORMAT("+inf", 5, "{:+}", inf); - TEST_FORMAT("-inf", 5, "{}", -inf); + TEST_FORMAT("inf", "{}", inf); + TEST_FORMAT("+inf", "{:+}", inf); + TEST_FORMAT("-inf", "{}", -inf); } #endif diff --git a/test/compile-test.cc b/test/compile-test.cc index a117c4f6..fe3758a2 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -324,79 +324,81 @@ consteval auto test_format(auto format, const Args&... args) { } # if FMT_USE_CONSTEXPR_STR -# define TEST_FORMAT(expected, len, str, ...) \ +# define TEST_FORMAT(expected, str, ...) \ do { \ - EXPECT_EQ(expected, test_format(FMT_COMPILE(str), __VA_ARGS__)); \ + EXPECT_EQ(expected, test_format(FMT_COMPILE(str), \ + __VA_ARGS__)); \ static_assert(fmt::format(FMT_COMPILE(str), __VA_ARGS__) == expected); \ } while (false) # else # define TEST_FORMAT(expected, len, str, ...) \ - EXPECT_EQ(expected, test_format(FMT_COMPILE(str), __VA_ARGS__)) + EXPECT_EQ(expected, \ + test_format(FMT_COMPILE(str), __VA_ARGS__)) # endif TEST(compile_time_formatting_test, bool) { - TEST_FORMAT("true", 5, "{}", true); - TEST_FORMAT("false", 6, "{}", false); - TEST_FORMAT("true ", 6, "{:5}", true); - TEST_FORMAT("1", 2, "{:d}", true); + TEST_FORMAT("true", "{}", true); + TEST_FORMAT("false", "{}", false); + TEST_FORMAT("true ", "{:5}", true); + TEST_FORMAT("1", "{:d}", true); } TEST(compile_time_formatting_test, integer) { - TEST_FORMAT("42", 3, "{}", 42); - TEST_FORMAT("420", 4, "{}", 420); - TEST_FORMAT("42 42", 6, "{} {}", 42, 42); - TEST_FORMAT("42 42", 6, "{} {}", uint32_t{42}, uint64_t{42}); + TEST_FORMAT("42", "{}", 42); + TEST_FORMAT("420", "{}", 420); + TEST_FORMAT("42 42", "{} {}", 42, 42); + TEST_FORMAT("42 42", "{} {}", uint32_t{42}, uint64_t{42}); - TEST_FORMAT("+42", 4, "{:+}", 42); - TEST_FORMAT("42", 3, "{:-}", 42); - TEST_FORMAT(" 42", 4, "{: }", 42); + TEST_FORMAT("+42", "{:+}", 42); + TEST_FORMAT("42", "{:-}", 42); + TEST_FORMAT(" 42", "{: }", 42); - TEST_FORMAT("-0042", 6, "{:05}", -42); + TEST_FORMAT("-0042", "{:05}", -42); - TEST_FORMAT("101010", 7, "{:b}", 42); - TEST_FORMAT("0b101010", 9, "{:#b}", 42); - TEST_FORMAT("0B101010", 9, "{:#B}", 42); - TEST_FORMAT("042", 4, "{:#o}", 042); - TEST_FORMAT("0x4a", 5, "{:#x}", 0x4a); - TEST_FORMAT("0X4A", 5, "{:#X}", 0x4a); + TEST_FORMAT("101010", "{:b}", 42); + TEST_FORMAT("0b101010", "{:#b}", 42); + TEST_FORMAT("0B101010", "{:#B}", 42); + TEST_FORMAT("042", "{:#o}", 042); + TEST_FORMAT("0x4a", "{:#x}", 0x4a); + TEST_FORMAT("0X4A", "{:#X}", 0x4a); - TEST_FORMAT(" 42", 6, "{:5}", 42); - TEST_FORMAT(" 42", 6, "{:5}", 42ll); - TEST_FORMAT(" 42", 6, "{:5}", 42ull); + TEST_FORMAT(" 42", "{:5}", 42); + TEST_FORMAT(" 42", "{:5}", 42ll); + TEST_FORMAT(" 42", "{:5}", 42ull); - TEST_FORMAT("42 ", 5, "{:<4}", 42); - TEST_FORMAT(" 42", 5, "{:>4}", 42); - TEST_FORMAT(" 42 ", 5, "{:^4}", 42); - TEST_FORMAT("**-42", 6, "{:*>5}", -42); + TEST_FORMAT("42 ", "{:<4}", 42); + TEST_FORMAT(" 42", "{:>4}", 42); + TEST_FORMAT(" 42 ", "{:^4}", 42); + TEST_FORMAT("**-42", "{:*>5}", -42); } TEST(compile_time_formatting_test, char) { - TEST_FORMAT("c", 2, "{}", 'c'); + TEST_FORMAT("c", "{}", 'c'); - TEST_FORMAT("c ", 4, "{:3}", 'c'); - TEST_FORMAT("99", 3, "{:d}", 'c'); + TEST_FORMAT("c ", "{:3}", 'c'); + TEST_FORMAT("99", "{:d}", 'c'); } TEST(compile_time_formatting_test, string) { - TEST_FORMAT("42", 3, "{}", "42"); - TEST_FORMAT("The answer is 42", 17, "{} is {}", "The answer", "42"); + TEST_FORMAT("42", "{}", "42"); + TEST_FORMAT("The answer is 42", "{} is {}", "The answer", "42"); - TEST_FORMAT("abc**", 6, "{:*<5}", "abc"); - TEST_FORMAT("**🤡**", 9, "{:*^6}", "🤡"); + TEST_FORMAT("abc**", "{:*<5}", "abc"); + TEST_FORMAT("**🤡**", "{:*^6}", "🤡"); } TEST(compile_time_formatting_test, combination) { - TEST_FORMAT("420, true, answer", 18, "{}, {}, {}", 420, true, "answer"); + TEST_FORMAT("420, true, answer", "{}, {}, {}", 420, true, "answer"); - TEST_FORMAT(" -42", 5, "{:{}}", -42, 4); + TEST_FORMAT(" -42", "{:{}}", -42, 4); } TEST(compile_time_formatting_test, custom_type) { - TEST_FORMAT("foo", 4, "{}", test_formattable()); - TEST_FORMAT("bar", 4, "{:b}", test_formattable()); + TEST_FORMAT("foo", "{}", test_formattable()); + TEST_FORMAT("bar", "{:b}", test_formattable()); } TEST(compile_time_formatting_test, multibyte_fill) { - TEST_FORMAT("жж42", 7, "{:ж>4}", 42); + TEST_FORMAT("жж42", "{:ж>4}", 42); } #endif