From 2924fcf8f651ea376a566ec2dfb812517c87af13 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 11 May 2025 10:15:58 -0700 Subject: [PATCH] Cleanup base-test --- test/base-test.cc | 116 ++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/test/base-test.cc b/test/base-test.cc index bce5a1dc..5f292ef6 100644 --- a/test/base-test.cc +++ b/test/base-test.cc @@ -5,6 +5,7 @@ // // For the license information refer to format.h. +// Turn assertion failures into exceptions for testing. // clang-format off #include "test-assert.h" // clang-format on @@ -22,16 +23,14 @@ #include "gmock/gmock.h" -using fmt::detail::buffer; +#ifdef FMT_FORMAT_H_ +# error base-test includes format.h +#endif using testing::_; using testing::Invoke; using testing::Return; -#ifdef FMT_FORMAT_H_ -# error base-test includes format.h -#endif - auto copy(fmt::string_view s, fmt::appender out) -> fmt::appender { for (char c : s) *out++ = c; return out; @@ -96,53 +95,48 @@ TEST(string_view_test, compare) { } #if FMT_USE_CONSTEVAL -template struct fixed_string { - char data[N] = {}; - - constexpr fixed_string(const char (&m)[N]) { - for (size_t i = 0; i != N; ++i) data[i] = m[i]; - } -}; - TEST(string_view_test, from_constexpr_fixed_string) { - static constexpr auto fs = fixed_string<4>("foo"); + constexpr int size = 4; + + struct fixed_string { + char data[size] = {}; + + constexpr fixed_string(const char (&m)[size]) { + for (size_t i = 0; i != size; ++i) data[i] = m[i]; + } + }; + + static constexpr auto fs = fixed_string("foo"); static constexpr auto sv = fmt::string_view(fs.data); EXPECT_EQ(sv, "foo"); } #endif // FMT_USE_CONSTEVAL -#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 470 TEST(buffer_test, noncopyable) { - EXPECT_FALSE(std::is_copy_constructible>::value); -# if !FMT_MSC_VERSION - // std::is_copy_assignable is broken in MSVC2013. - EXPECT_FALSE(std::is_copy_assignable>::value); -# endif + EXPECT_FALSE(std::is_copy_constructible>::value); + EXPECT_FALSE(std::is_copy_assignable>::value); } TEST(buffer_test, nonmoveable) { - EXPECT_FALSE(std::is_move_constructible>::value); -# if !FMT_MSC_VERSION - // std::is_move_assignable is broken in MSVC2013. - EXPECT_FALSE(std::is_move_assignable>::value); -# endif + EXPECT_FALSE(std::is_move_constructible>::value); + EXPECT_FALSE(std::is_move_assignable>::value); } -#endif TEST(buffer_test, indestructible) { static_assert(!std::is_destructible>(), "buffer's destructor is protected"); } -template struct mock_buffer final : buffer { +template struct mock_buffer final : fmt::detail::buffer { MOCK_METHOD(size_t, do_grow, (size_t)); - static void grow(buffer& buf, size_t capacity) { + static void grow(fmt::detail::buffer& buf, size_t capacity) { auto& self = static_cast(buf); self.set(buf.data(), self.do_grow(capacity)); } - mock_buffer(T* data = nullptr, size_t buf_capacity = 0) : buffer(grow) { + mock_buffer(T* data = nullptr, size_t buf_capacity = 0) + : fmt::detail::buffer(grow) { this->set(data, buf_capacity); ON_CALL(*this, do_grow(_)).WillByDefault(Invoke([](size_t capacity) { return capacity; @@ -309,11 +303,6 @@ template struct formatter { }; FMT_END_NAMESPACE -TEST(arg_test, format_args) { - auto args = fmt::format_args(); - EXPECT_FALSE(args.get(1)); -} - // Use a unique result type to make sure that there are no undesirable // conversions. struct test_result {}; @@ -379,33 +368,9 @@ VISIT_TYPE(unsigned long, unsigned long long); CHECK_ARG(expected, value) \ } -template class numeric_arg_test : public testing::Test {}; - -#if FMT_BUILTIN_TYPES -using test_types = - testing::Types; -#else -using test_types = testing::Types; -#endif -TYPED_TEST_SUITE(numeric_arg_test, test_types); - -template ::value, int> = 0> -auto test_value() -> T { - return static_cast(42); -} - -template ::value, int> = 0> -auto test_value() -> T { - return static_cast(4.2); -} - -TYPED_TEST(numeric_arg_test, make_and_visit) { - CHECK_ARG_SIMPLE(test_value()); - CHECK_ARG_SIMPLE(std::numeric_limits::min()); - CHECK_ARG_SIMPLE(std::numeric_limits::max()); +TEST(arg_test, format_args) { + auto args = fmt::format_args(); + EXPECT_FALSE(args.get(1)); } TEST(arg_test, char_arg) { CHECK_ARG('a', 'a'); } @@ -467,6 +432,35 @@ TEST(arg_test, visit_invalid_arg) { fmt::basic_format_arg().visit(visitor); } +template class numeric_arg_test : public testing::Test {}; + +#if FMT_BUILTIN_TYPES +using test_types = + testing::Types; +#else +using test_types = testing::Types; +#endif +TYPED_TEST_SUITE(numeric_arg_test, test_types); + +template ::value, int> = 0> +auto test_value() -> T { + return static_cast(42); +} + +template ::value, int> = 0> +auto test_value() -> T { + return static_cast(4.2); +} + +TYPED_TEST(numeric_arg_test, make_and_visit) { + CHECK_ARG_SIMPLE(test_value()); + CHECK_ARG_SIMPLE(std::numeric_limits::min()); + CHECK_ARG_SIMPLE(std::numeric_limits::max()); +} + #if FMT_USE_CONSTEXPR enum class arg_id_result { none, index, name };