Update tests

This commit is contained in:
Shawn Zhong 2023-04-29 07:45:36 -05:00
parent 91cf38c71c
commit ea4929da32
5 changed files with 52 additions and 44 deletions

View File

@ -504,7 +504,7 @@ FMT_BEGIN_EXPORT
template <typename CompiledFormat, typename... Args,
typename Char = typename CompiledFormat::char_type,
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
FMT_CONSTEXPR_LIB FMT_INLINE std::basic_string<Char> format(
FMT_CONSTEXPR_STR FMT_INLINE std::basic_string<Char> format(
const CompiledFormat& cf, const Args&... args) {
auto s = std::basic_string<Char>();
cf.format(std::back_inserter(s), args...);
@ -520,7 +520,7 @@ constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf,
template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
FMT_INLINE FMT_CONSTEXPR_LIB std::basic_string<typename S::char_type> format(
FMT_INLINE FMT_CONSTEXPR_STR std::basic_string<typename S::char_type> format(
const S&, Args&&... args) {
if constexpr (std::is_same<typename S::char_type, char>::value) {
constexpr auto str = basic_string_view<typename S::char_type>(S());

View File

@ -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 <typename Container>
FMT_CONSTEXPR_LIB inline auto get_container(
FMT_CONSTEXPR_STR inline auto get_container(
std::back_insert_iterator<Container> it) -> Container& {
using base = std::back_insert_iterator<Container>;
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<detail::buffer<char>> {
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 <typename Char, typename InputIt>
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;

View File

@ -4436,7 +4436,7 @@ auto join(Range&& range, string_view sep)
\endrst
*/
template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::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<char>(appender(buffer), value);
@ -4444,7 +4444,7 @@ FMT_NODISCARD FMT_CONSTEXPR_LIB inline auto to_string(const T& value)
}
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::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<T>() + 2;

View File

@ -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<len>(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<len>(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<double>::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<double>::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

View File

@ -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<len>(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<len>(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<len>(FMT_COMPILE(str), __VA_ARGS__));
EXPECT_EQ(expected, test_format<len>(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);