From 7a0da1c68a1ee3313603d7e5790a405580c250aa Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 22 Nov 2025 07:46:38 -0800 Subject: [PATCH] Merge compile-time FP tests into other compile-time tests --- test/CMakeLists.txt | 5 ---- test/compile-fp-test.cc | 62 ----------------------------------------- test/compile-test.cc | 34 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 67 deletions(-) delete mode 100644 test/compile-fp-test.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e1ea260d..d8b59389 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,11 +62,6 @@ if (NOT (MSVC AND BUILD_SHARED_LIBS)) endif () add_fmt_test(ostream-test) add_fmt_test(compile-test) -add_fmt_test(compile-fp-test) -if (MSVC) - # Without this option, MSVC returns 199711L for the __cplusplus macro. - target_compile_options(compile-fp-test PRIVATE /Zc:__cplusplus) -endif() add_fmt_test(printf-test) add_fmt_test(ranges-test ranges-odr-test.cc) add_fmt_test(no-builtin-types-test HEADER_ONLY) diff --git a/test/compile-fp-test.cc b/test/compile-fp-test.cc deleted file mode 100644 index e1522c72..00000000 --- a/test/compile-fp-test.cc +++ /dev/null @@ -1,62 +0,0 @@ -// Formatting library for C++ - formatting library tests -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#include "fmt/compile.h" -#include "gmock/gmock.h" - -#if FMT_USE_CONSTEVAL - -template struct test_string { - Char buffer[max_string_length] = {}; - - template constexpr bool operator==(const T& rhs) const noexcept { - return fmt::basic_string_view(rhs).compare(buffer) == 0; - } -}; - -template -consteval auto test_format(auto format, const Args&... args) { - test_string string{}; - fmt::format_to(string.buffer, format, args...); - return string; -} - -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)); - - 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)); - - 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)); - - 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)); - if (std::signbit(-nan)) - EXPECT_EQ("-nan", test_format<5>(FMT_COMPILE("{}"), -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)); -} - -#endif // FMT_USE_CONSTEVAL diff --git a/test/compile-test.cc b/test/compile-test.cc index 190435f2..5e381ada 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -417,6 +417,40 @@ TEST(compile_time_formatting_test, custom_type) { TEST(compile_time_formatting_test, multibyte_fill) { EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42)); } + +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)); + + 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)); + + 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)); + + 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)); + if (std::signbit(-nan)) + EXPECT_EQ("-nan", test_format<5>(FMT_COMPILE("{}"), -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)); +} #endif #if FMT_USE_CONSTEXPR_STRING