diff --git a/test/module-test.cc b/test/module-test.cc index d8a120d0b566..1ad1f1ce181e 100644 --- a/test/module-test.cc +++ b/test/module-test.cc @@ -9,6 +9,14 @@ // All Rights Reserved // {fmt} module. +#include +#include +#include +#include +#include +#include +#include + import fmt; // check for macros leaking from BMI @@ -19,11 +27,12 @@ static bool macro_leaked = false; #endif -#include "gtest/gtest.h" +#include "gtest-extra.h" // an implicitly exported namespace must be visible [module.interface]/2.2 TEST(module_test, namespace) { using namespace fmt; + using namespace fmt::literals; ASSERT_TRUE(true); } @@ -63,3 +72,482 @@ TEST(module_test, macros) { #endif EXPECT_FALSE(macro_leaked); } + +// The following is less about functional testing (that's done elsewhere) +// but rather visibility of all client-facing overloads, reachability of +// non-exported entities, name lookup and overload resolution within +// template instantitions. +// Excercise all exported entities of the API at least once. +// Instantiate as many code paths as possible. + +TEST(module_test, to_string) { + EXPECT_EQ("42", fmt::to_string(42)); + EXPECT_EQ("42", fmt::to_string(42u)); + EXPECT_EQ("42", fmt::to_string(short{42})); + EXPECT_EQ("42", fmt::to_string(unsigned short{42u})); + EXPECT_EQ("42", fmt::to_string(42L)); + EXPECT_EQ("42", fmt::to_string(42uL)); + EXPECT_EQ("42", fmt::to_string(42LL)); + EXPECT_EQ("42", fmt::to_string(42uLL)); + EXPECT_EQ("42", fmt::to_string(42.0)); + EXPECT_EQ("42", fmt::to_string(42.0f)); + EXPECT_EQ("42", fmt::to_string(42.0L)); + EXPECT_EQ("42", fmt::to_string("42")); + EXPECT_EQ("a", fmt::to_string('a')); + EXPECT_EQ("97", fmt::to_string(unsigned char{'a'})); + EXPECT_EQ("97", fmt::to_string(signed char{'a'})); + EXPECT_EQ("true", fmt::to_string(true)); + EXPECT_EQ("0x2a", fmt::to_string((void *)42)); +} + +TEST(module_test, format) { + EXPECT_EQ("42", fmt::format("{}", 42)); + EXPECT_EQ("42", fmt::format("{:}", 42u)); + EXPECT_EQ("+42", fmt::format("{:+d}", short{42})); + EXPECT_EQ("42", fmt::format("{:d}", unsigned short{42u})); + EXPECT_EQ("-42", fmt::format("{0}", -42L)); + EXPECT_EQ("2a", fmt::format("{0:x}", 42uL)); + EXPECT_EQ("***42", fmt::format("{:*>5}", 42LL)); + EXPECT_EQ("0X2A", fmt::format("{:#X}", 42uLL)); + EXPECT_EQ("===42.000000===", fmt::format("{:=^15f}", 42.0)); + EXPECT_EQ("00000042.1", fmt::format("{:<010.5G}", 42.1f)); + EXPECT_EQ(" +4.21235e+11", fmt::format("{:+{}.{}e}", 42.1234567e10L, 13, 5)); + EXPECT_EQ("-0x1.5000000000000p+5", fmt::format("{: a}", -42.0)); + constexpr auto nan = std::numeric_limits::quiet_NaN(); + EXPECT_EQ("-nan-", fmt::format("{:-^5}", nan)); + constexpr auto inf = std::numeric_limits::infinity(); + EXPECT_EQ("-INF-", fmt::format("{:-^5F}", inf)); + EXPECT_EQ("-INF ", fmt::format("{: 5G}", -inf)); + EXPECT_EQ("42", fmt::format("{:s}", "42")); + EXPECT_EQ("a", fmt::format("{:c}", 'a')); + EXPECT_EQ("a", fmt::format("{:c}", unsigned char{'a'})); + EXPECT_EQ("a", fmt::format("{:c}", signed char{'a'})); + EXPECT_EQ("false", fmt::format("{}", false)); + EXPECT_EQ("0B1", fmt::format("{:#B}", true)); + EXPECT_EQ("0x2a", fmt::format("{:p}", (void *)42)); + EXPECT_EQ("1234", fmt::format("{:L}", 1234)); +} + +TEST(module_test, format_to) { + std::string s; + fmt::format_to(std::back_inserter(s), "{}", 42); + EXPECT_EQ("42", s); + + char buffer[4] = {0}; + fmt::format_to(buffer, "{}", 42); + EXPECT_EQ("42", std::string_view(buffer)); +} + +TEST(module_test, formatted_size) { + EXPECT_EQ(2u, fmt::formatted_size("{}", 42)); +} + +TEST(module_test, format_to_n) { + { + std::string s; + auto result = fmt::format_to_n(std::back_inserter(s), 1, "{}", 42); + EXPECT_EQ(2u, result.size); + EXPECT_EQ("4", s); + } + { + char buffer[4] = {0}; + auto result = fmt::format_to_n(buffer, 3, "{}", 12345); + EXPECT_EQ(5u, result.size); + EXPECT_EQ(buffer + 3, result.out); + EXPECT_EQ("123", std::string_view(buffer)); + } +} + +template +auto create_args(Args&&... args) { + return fmt::make_format_args(args...); +} + +TEST(module_test, format_args) { + auto args = fmt::format_args(); + EXPECT_FALSE(args.get(1)); + + fmt::basic_format_args store = create_args(42); + EXPECT_TRUE(store.max_size() > 0); + auto arg0 = store.get(0); + EXPECT_TRUE(arg0); + EXPECT_FALSE(store.get(1)); + decltype(arg0) arg_none; + EXPECT_FALSE(static_cast(arg_none)); + EXPECT_TRUE(arg0.type() != arg_none.type()); +} + +template +auto create_checked_args(const S& format, Args&&... args) { + return fmt::make_args_checked(format, args...); +} + +TEST(module_test, checked_format_args) { + using context = fmt::format_context; + fmt::basic_format_args store = create_checked_args("{}", 42); + EXPECT_TRUE(static_cast(store.get(0))); + EXPECT_FALSE(static_cast(store.get(1))); +} + +template +auto create_dynamic_args(Args&&... args) { + fmt::dynamic_format_arg_store store; + (store.push_back(args), ...); + return store; +} + + +TEST(module_test, dynamic_format_args) { + auto dyn_store = create_dynamic_args(42); + dyn_store.push_back(fmt::arg("a42", 42)); + using context = fmt::format_context; + fmt::basic_format_args store = dyn_store; + EXPECT_TRUE(store.get(0)); + EXPECT_TRUE(store.get(1)); + EXPECT_TRUE(store.get(2)); + EXPECT_FALSE(store.get(3)); + EXPECT_TRUE(store.get(fmt::string_view("a42"))); +} + +TEST(module_test, vformat) { + EXPECT_EQ("42", fmt::vformat("{}", create_args(42))); +} + +TEST(module_test, vformat_to) { + std::string s; + fmt::vformat_to(std::back_inserter(s), "{}", create_checked_args("{}", 42)); + EXPECT_EQ("42", s); +} + +TEST(module_test, named_args) { + EXPECT_EQ("42", fmt::format("{answer}", fmt::arg("answer", 42))); +} + +TEST(module_test, literals) { + using namespace fmt::literals; + EXPECT_EQ("42", fmt::format("{answer}", "answer"_a = 42)); + EXPECT_EQ("42", "{}"_format(42)); +} + +TEST(module_test, locale) { + const auto de = std::locale("de_DE"); + EXPECT_EQ("4,2", fmt::format(de, "{:L}", 4.2)); + EXPECT_EQ("4,2", fmt::vformat(de, "{:L}", create_dynamic_args(4.2))); + std::string s; + fmt::vformat_to(de, std::back_inserter(s), "{:L}", create_args(4.2)); + EXPECT_EQ("4,2", s); + + const auto previous = std::locale::global(std::locale("ru_RU")); + EXPECT_EQ("4,2", fmt::format("{:L}", 4.2)); + std::locale::global(previous); +} + +TEST(module_test, string_view) { + fmt::string_view nsv("fmt"); + EXPECT_EQ("fmt", nsv); + EXPECT_TRUE(fmt::string_view("fmt") == nsv); + + fmt::wstring_view wsv(L"fmt"); + EXPECT_EQ(L"fmt", wsv); + EXPECT_TRUE(fmt::wstring_view(L"fmt") == wsv); +} + +TEST(module_test, memory_buffer) { + fmt::basic_memory_buffer buffer; + fmt::format_to(buffer, "{}", "42"); + EXPECT_EQ("42", to_string(buffer)); + fmt::memory_buffer nbuffer(std::move(buffer)); + EXPECT_EQ("42", to_string(nbuffer)); + buffer = std::move(nbuffer); + EXPECT_EQ("42", to_string(buffer)); + nbuffer.clear(); + EXPECT_EQ(0u, to_string(nbuffer).size()); + + fmt::wmemory_buffer wbuffer; + EXPECT_EQ(0u, to_string(wbuffer).size()); +} + +TEST(module_test, is_char) { + EXPECT_TRUE(fmt::is_char()); + EXPECT_TRUE(fmt::is_char()); + EXPECT_TRUE(fmt::is_char()); + EXPECT_TRUE(fmt::is_char()); + EXPECT_TRUE(fmt::is_char()); + EXPECT_FALSE(fmt::is_char()); + EXPECT_FALSE(fmt::is_char()); + EXPECT_FALSE(fmt::is_char()); +} + +TEST(module_test, ptr) { + auto p = (int*)42; + EXPECT_EQ("0x2a", fmt::to_string(fmt::ptr(p))); + std::unique_ptr up(p); + EXPECT_EQ("0x2a", fmt::to_string(fmt::ptr(up))); + up.release(); + auto sp = std::make_shared(0); + p = sp.get(); + EXPECT_EQ(fmt::to_string(fmt::ptr(p)), fmt::to_string(fmt::ptr(sp))); +} + +TEST(module_test, errors) { + EXPECT_THROW(throw fmt::format_error("oops"), std::exception); + EXPECT_THROW(throw fmt::vsystem_error(0, "{}", create_args(42)), + std::system_error); + EXPECT_THROW(throw fmt::system_error(0, "{}", 42), + std::system_error); + + fmt::memory_buffer buffer; + fmt::format_system_error(buffer, 0, "oops"); + EXPECT_TRUE(to_string(buffer).size() > 0); + EXPECT_WRITE(stderr, fmt::report_system_error(0, "oops"), + to_string(buffer)); +} + +TEST(module_test, format_int) { + fmt::format_int sanswer(42); + EXPECT_EQ("42", fmt::string_view(sanswer.data(), sanswer.size())); + fmt::format_int uanswer(42u); + EXPECT_EQ("42", fmt::string_view(uanswer.data(), uanswer.size())); +} + +struct test_formatter : fmt::formatter { + bool check() { return true; } +}; + +struct test_dynamic_formatter : fmt::dynamic_formatter<> { + bool check() { return true; } +}; + +TEST(module_test, formatter) { + EXPECT_TRUE(test_formatter{}.check()); + EXPECT_TRUE(test_dynamic_formatter{}.check()); +} + +TEST(module_test, join) { + int arr[3] = {1, 2, 3}; + std::vector vec{1.0, 2.0, 3.0}; + EXPECT_EQ("1, 2, 3", to_string(fmt::join(arr + 0, arr + 3, ", "))); + EXPECT_EQ("1, 2, 3", to_string(fmt::join(arr, ", "))); + EXPECT_EQ("1, 2, 3", to_string(fmt::join(vec.begin(), vec.end(), ", "))); + EXPECT_EQ("1, 2, 3", to_string(fmt::join(vec, ", "))); +} + +namespace test_ns { +template class test_string { + private: + std::basic_string s_; + + public: + explicit test_string(const Char* s) : s_(s) {} + const Char* data() const { return s_.data(); } + size_t length() const { return s_.size(); } +}; + +template +fmt::basic_string_view to_string_view(const test_string& s) { + return {s.data(), s.length()}; +} +} // namespace test_ns + +TEST(module_test, to_string_view) { + using fmt::to_string_view; + fmt::string_view nsv{to_string_view("42")}; + EXPECT_EQ("42", nsv); + fmt::wstring_view wsv{to_string_view(L"42")}; + EXPECT_EQ(L"42", wsv); + test_ns::test_string nts("42"); + fmt::string_view ntv(to_string_view(nts)); + EXPECT_EQ("42", ntv); + test_ns::test_string wts(L"42"); + fmt::wstring_view wtv(to_string_view(wts)); + EXPECT_EQ(L"42", wtv); +} + +struct custom_context { + using char_type = char; + using parse_context_type = fmt::format_parse_context; + + bool called = false; + + template struct formatter_type { + auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + const char* format(const T&, custom_context& ctx) { + ctx.called = true; + return nullptr; + } + }; + + void advance_to(const char*) {} +}; + +struct test_struct {}; + +namespace fmt { +template struct formatter { + auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + auto format(test_struct, format_context& ctx) -> decltype(ctx.out()) { + auto test = string_view("test"); + return std::copy_n(test.data(), test.size(), ctx.out()); + } +}; +} + +TEST(arg_test, custom_context) { + auto ctx = custom_context(); + auto parse_ctx = fmt::format_parse_context(""); + fmt::basic_format_arg custom_arg; + EXPECT_TRUE(!custom_arg); + + auto test = test_struct(); + auto store = fmt::dynamic_format_arg_store(); + store.push_back(test); +} + +struct enabled_formatter {}; +struct disabled_formatter {}; +struct disabled_formatter_convertible { + operator int() const { return 42; } +}; + +namespace fmt { + template <> struct formatter { + auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + auto format(enabled_formatter, format_context& ctx) -> decltype(ctx.out()) { + return ctx.out(); + } +}; +} + +TEST(core_test, has_formatter) { + using fmt::has_formatter; + using context = fmt::format_context; + static_assert(has_formatter::value, ""); + static_assert(!has_formatter::value, ""); + static_assert(!has_formatter::value, + ""); +} + +TEST(core_test, is_formattable) { + static_assert(fmt::is_formattable::value, ""); + static_assert(!fmt::is_formattable::value, ""); + static_assert(fmt::is_formattable::value, ""); +} + +struct convertible_to_int { + operator int() const { return 42; } +}; + +struct convertible_to_c_string { + operator const char*() const { return "foo"; } +}; + +namespace fmt { +template <> struct formatter { + auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + auto format(convertible_to_int, format_context& ctx) -> decltype(ctx.out()) { + return std::copy_n("foo", 3, ctx.out()); + } +}; + +template <> struct formatter { + constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + auto format(convertible_to_c_string, format_context& ctx) + -> decltype(ctx.out()) { + return std::copy_n("bar", 3, ctx.out()); + } +}; +} + +TEST(core_test, formatter_overrides_implicit_conversion) { + EXPECT_EQ(fmt::format("{}", convertible_to_int()), "foo"); + EXPECT_EQ(fmt::format("{}", convertible_to_c_string()), "bar"); +} + +// Test that check is not found by ADL. +template void check(T); +TEST(core_test, adl_check) { + EXPECT_EQ(fmt::format("{}", test_struct()), "test"); +} + +struct implicitly_convertible_to_string { + operator std::string() const { return "foo"; } +}; + +struct implicitly_convertible_to_string_view { + operator fmt::string_view() const { return "foo"; } +}; + +TEST(core_test, format_implicitly_convertible_to_string_view) { + EXPECT_EQ("foo", fmt::format("{}", implicitly_convertible_to_string_view())); +} + +struct explicitly_convertible_to_string_view { + explicit operator fmt::string_view() const { return "foo"; } +}; + +TEST(core_test, format_explicitly_convertible_to_string_view) { + EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_view())); +} + +struct explicitly_convertible_to_std_string_view { + explicit operator std::string_view() const { return "foo"; } +}; + +TEST(core_test, format_explicitly_convertible_to_std_string_view) { + EXPECT_EQ("foo", + fmt::format("{}", explicitly_convertible_to_std_string_view())); +} + +struct convertible_to_long_long { + operator long long() const { return 1LL << 32; } +}; + +TEST(format_test, format_convertible_to_long_long) { + EXPECT_EQ("100000000", fmt::format("{:x}", convertible_to_long_long())); +} + +struct disabled_rvalue_conversion { + operator const char*() const& { return "foo"; } + operator const char*() & { return "foo"; } + operator const char*() const&& = delete; + operator const char*() && = delete; +}; + +TEST(core_test, disabled_rvalue_conversion) { + EXPECT_EQ("foo", fmt::format("{}", disabled_rvalue_conversion())); +} + +namespace adl_test { +template void make_format_args(const T&...) = delete; + +struct string : std::string {}; +} // namespace adl_test + +template constexpr T const_check(T value) { return value; } + +// Test that formatting functions compile when make_format_args is found by ADL. +TEST(core_test, adl) { + // Only check compilation and don't run the code to avoid polluting the output + // and since the output is tested elsewhere. + if (::const_check(true)) return; + auto s = adl_test::string(); + char buf[10]; + fmt::format("{}", s); + fmt::format_to(buf, "{}", s); + fmt::format_to_n(buf, 10, "{}", s); + fmt::formatted_size("{}", s); + fmt::print("{}", s); + fmt::print(stdout, "{}", s); +}