From ea4929da32fd46b6e9e39a1d67e927885a1fbcb6 Mon Sep 17 00:00:00 2001 From: Shawn Zhong Date: Sat, 29 Apr 2023 07:45:36 -0500 Subject: [PATCH] Update tests --- include/fmt/compile.h | 4 +-- include/fmt/core.h | 16 ++++++------ include/fmt/format.h | 4 +-- test/compile-fp-test.cc | 54 ++++++++++++++++++++++++----------------- test/compile-test.cc | 18 ++++++-------- 5 files changed, 52 insertions(+), 44 deletions(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index d58eba0b..ed7b9a54 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -504,7 +504,7 @@ FMT_BEGIN_EXPORT template ::value)> -FMT_CONSTEXPR_LIB FMT_INLINE std::basic_string format( +FMT_CONSTEXPR_STR FMT_INLINE std::basic_string format( const CompiledFormat& cf, const Args&... args) { auto s = std::basic_string(); cf.format(std::back_inserter(s), args...); @@ -520,7 +520,7 @@ constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf, template ::value)> -FMT_INLINE FMT_CONSTEXPR_LIB std::basic_string format( +FMT_INLINE FMT_CONSTEXPR_STR std::basic_string format( const S&, Args&&... args) { if constexpr (std::is_same::value) { constexpr auto str = basic_string_view(S()); diff --git a/include/fmt/core.h b/include/fmt/core.h index e7a369a8..919c97ae 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -117,9 +117,11 @@ __cpp_lib_constexpr_string >= 201907L && \ defined(__cpp_lib_constexpr_iterator) && \ __cpp_lib_constexpr_iterator >= 201811L -# define FMT_CONSTEXPR_LIB constexpr +# define FMT_USE_CONSTEXPR_STR 1 +# define FMT_CONSTEXPR_STR constexpr #else -# define FMT_CONSTEXPR_LIB +# define FMT_USE_CONSTEXPR_STR 0 +# define FMT_CONSTEXPR_STR #endif // Check if constexpr std::char_traits<>::{compare,length} are supported. @@ -835,11 +837,11 @@ constexpr auto has_const_formatter() -> bool { // Extracts a reference to the container from back_insert_iterator. template -FMT_CONSTEXPR_LIB inline auto get_container( +FMT_CONSTEXPR_STR inline auto get_container( std::back_insert_iterator it) -> Container& { using base = std::back_insert_iterator; struct accessor : base { - FMT_CONSTEXPR_LIB accessor(base b) : base(b) {} + FMT_CONSTEXPR_STR accessor(base b) : base(b) {} using base::container; }; return *accessor(it).container; @@ -1529,8 +1531,8 @@ class appender : public std::back_insert_iterator> { appender(base it) noexcept : base(it) {} FMT_UNCHECKED_ITERATOR(appender); - FMT_CONSTEXPR_LIB auto operator++() noexcept -> appender& { return *this; } - FMT_CONSTEXPR_LIB auto operator++(int) noexcept -> appender { return *this; } + FMT_CONSTEXPR_STR auto operator++() noexcept -> appender& { return *this; } + FMT_CONSTEXPR_STR auto operator++(int) noexcept -> appender { return *this; } }; // A formatting argument. It is a trivially copyable/constructible type to @@ -1640,7 +1642,7 @@ FMT_CONSTEXPR FMT_INLINE auto visit_format_arg( namespace detail { template -FMT_CONSTEXPR_LIB auto copy_str(InputIt begin, InputIt end, appender out) +FMT_CONSTEXPR_STR auto copy_str(InputIt begin, InputIt end, appender out) -> appender { get_container(out).append(begin, end); return out; diff --git a/include/fmt/format.h b/include/fmt/format.h index 0d8701e3..d63b69dd 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -4436,7 +4436,7 @@ auto join(Range&& range, string_view sep) \endrst */ template ::value)> -FMT_NODISCARD FMT_CONSTEXPR_LIB inline auto to_string(const T& value) +FMT_NODISCARD FMT_CONSTEXPR_STR inline auto to_string(const T& value) -> std::string { auto buffer = memory_buffer(); detail::write(appender(buffer), value); @@ -4444,7 +4444,7 @@ FMT_NODISCARD FMT_CONSTEXPR_LIB inline auto to_string(const T& value) } template ::value)> -FMT_NODISCARD FMT_CONSTEXPR_LIB inline auto to_string(T value) -> std::string { +FMT_NODISCARD FMT_CONSTEXPR_STR inline auto to_string(T value) -> std::string { // The buffer should be large enough to store the number including the sign // or "false" for bool. constexpr int max_size = detail::digits10() + 2; diff --git a/test/compile-fp-test.cc b/test/compile-fp-test.cc index db0cd906..b35d8302 100644 --- a/test/compile-fp-test.cc +++ b/test/compile-fp-test.cc @@ -26,37 +26,47 @@ consteval auto test_format(auto format, const Args&... args) { return string; } +# if FMT_USE_CONSTEXPR_STR +# define TEST_FORMAT(expected, len, str, ...) \ + do { \ + 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__)) +# endif + TEST(compile_time_formatting_test, floating_point) { - EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{}"), 0.0f)); - EXPECT_EQ("392.500000", test_format<11>(FMT_COMPILE("{0:f}"), 392.5f)); + TEST_FORMAT("0", 2, "{}", 0.0f); + TEST_FORMAT("392.500000", 11, "{0:f}", 392.5f); - EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:}"), 0.0)); - EXPECT_EQ("0.000000", test_format<9>(FMT_COMPILE("{:f}"), 0.0)); - EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:g}"), 0.0)); - EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:}"), 392.65)); - EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:g}"), 392.65)); - EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:G}"), 392.65)); - EXPECT_EQ("4.9014e+06", test_format<11>(FMT_COMPILE("{:g}"), 4.9014e6)); - EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:f}"), -392.65)); - EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:F}"), -392.65)); + 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); - EXPECT_EQ("3.926500e+02", test_format<13>(FMT_COMPILE("{0:e}"), 392.65)); - EXPECT_EQ("3.926500E+02", test_format<13>(FMT_COMPILE("{0:E}"), 392.65)); - EXPECT_EQ("+0000392.6", test_format<11>(FMT_COMPILE("{0:+010.4g}"), 392.65)); - EXPECT_EQ("9223372036854775808.000000", - test_format<27>(FMT_COMPILE("{:f}"), 9223372036854775807.0)); + 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); constexpr double nan = std::numeric_limits::quiet_NaN(); - EXPECT_EQ("nan", test_format<4>(FMT_COMPILE("{}"), nan)); - EXPECT_EQ("+nan", test_format<5>(FMT_COMPILE("{:+}"), nan)); + TEST_FORMAT("nan", 4, "{}", nan); + TEST_FORMAT("+nan", 5, "{:+}", nan); if (std::signbit(-nan)) - EXPECT_EQ("-nan", test_format<5>(FMT_COMPILE("{}"), -nan)); + TEST_FORMAT("-nan", 5, "{}", -nan); else fmt::print("Warning: compiler doesn't handle negative NaN correctly"); constexpr double inf = std::numeric_limits::infinity(); - EXPECT_EQ("inf", test_format<4>(FMT_COMPILE("{}"), inf)); - EXPECT_EQ("+inf", test_format<5>(FMT_COMPILE("{:+}"), inf)); - EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf)); + TEST_FORMAT("inf", 4, "{}", inf); + TEST_FORMAT("+inf", 5, "{:+}", inf); + TEST_FORMAT("-inf", 5, "{}", -inf); } #endif diff --git a/test/compile-test.cc b/test/compile-test.cc index 60f4ca62..b2f41cab 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -323,22 +323,18 @@ consteval inline auto test_format(auto format, const Args&... args) { return string; } -# if defined(__cpp_lib_constexpr_string) && \ - __cpp_lib_constexpr_string >= 201907L && \ - defined(__cpp_lib_constexpr_iterator) && \ - __cpp_lib_constexpr_iterator >= 201811L -# define TEST_FORMAT(expected, len, str, ...) \ - EXPECT_EQ(expected, test_format(FMT_COMPILE(str), __VA_ARGS__)); \ - static_assert(fmt::format(FMT_COMPILE(str), __VA_ARGS__).size() == \ - len - 1); +# if FMT_USE_CONSTEXPR_STR +# define TEST_FORMAT(expected, len, str, ...) \ + do { \ + 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("true", 5, "{}", true); TEST_FORMAT("true", 5, "{}", true); TEST_FORMAT("false", 6, "{}", false); TEST_FORMAT("true ", 6, "{:5}", true);