diff --git a/test/module-test.cc b/test/module-test.cc index d8a120d0b566d..75e53138a75e1 100644 --- a/test/module-test.cc +++ b/test/module-test.cc @@ -9,6 +9,16 @@ // All Rights Reserved // {fmt} module. +#include +#include +#include +#include +#include +#include +#include +#include +#include + import fmt; // check for macros leaking from BMI @@ -19,11 +29,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); } @@ -34,8 +45,8 @@ bool oops_detail_namespace_is_visible; namespace fmt { bool namespace_detail_invisible() { #if defined(FMT_HIDE_MODULE_BUGS) && \ - defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 192930036 - // bug in msvc 16.10-pre4: + defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 192930129 + // bug in msvc up to 16.11-pre1: // the namespace is visible even when it is neither // implicitly nor explicitly exported return true; @@ -55,11 +66,495 @@ TEST(module_test, detail_namespace) { // macros must not be imported from a *named* module [cpp.import]/5.1 TEST(module_test, macros) { #if defined(FMT_HIDE_MODULE_BUGS) && \ - defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 192930036 -// bug in msvc 16.10-pre4: + defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 192930129 +// bug in msvc up to 16.11-pre1: // include-guard macros leak from BMI // and even worse: they cannot be #undef-ined macro_leaked = false; #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(42.0)); +} + +TEST(module_test, format) { + EXPECT_EQ("42", fmt::format("{:}", 42)); + EXPECT_EQ("-42", fmt::format("{0}", -42.0)); +} + +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)); + + fmt::memory_buffer mb; + fmt::format_to(mb, "{}", 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); + char buffer[4] = {0}; + fmt::format_to_n(buffer, 3, "{}", 12345); +} + +TEST(module_test, format_args) { + auto no_args = fmt::format_args(); + EXPECT_FALSE(no_args.get(1)); + + using ctx = fmt::format_context; + fmt::basic_format_args args = fmt::make_format_args(42); + EXPECT_TRUE(args.max_size() > 0); + auto arg0 = args.get(0); + EXPECT_TRUE(arg0); + decltype(arg0) arg_none; + EXPECT_FALSE(arg_none); + EXPECT_TRUE(arg0.type() != arg_none.type()); +} + +TEST(module_test, checked_format_args) { + fmt::basic_format_args args = + fmt::make_args_checked("{}", 42); + EXPECT_TRUE(args.get(0)); +} + +TEST(module_test, dynamic_format_args) { + using ctx = fmt::format_context; + fmt::dynamic_format_arg_store dyn_store; + dyn_store.push_back(fmt::arg("a42", 42)); + fmt::basic_format_args args = dyn_store; + EXPECT_FALSE(args.get(3)); + EXPECT_TRUE(args.get(fmt::string_view("a42"))); +} + +TEST(module_test, vformat) { + using ctx = fmt::format_context; + EXPECT_EQ("42", fmt::vformat("{}", fmt::make_format_args(42))); +} + +TEST(module_test, vformat_to) { + auto store = fmt::make_format_args(42); + std::string s; + fmt::vformat_to(std::back_inserter(s), "{}", store); + EXPECT_EQ("42", s); + + char buffer[4] = {0}; + fmt::vformat_to(buffer, "{}", store); + EXPECT_EQ("42", std::string_view(buffer)); +} + +TEST(module_test, vformat_to_n) { + auto store = fmt::make_format_args(12345); + std::string s; + auto result = fmt::vformat_to_n(std::back_inserter(s), 1, "{}", store); + char buffer[4] = {0}; + fmt::vformat_to_n(buffer, 3, "{}", store); +} + +TEST(module_test, print) { + EXPECT_WRITE(stdout, fmt::print("{}µ", 42), "42µ"); + EXPECT_WRITE(stderr, fmt::print(stderr, "{}µ", 4.2), "4.2µ"); +} + +TEST(module_test, vprint) { + using ctx = fmt::format_context; + EXPECT_WRITE(stdout, + fmt::vprint("{}", fmt::make_format_args(42)), "42"); + EXPECT_WRITE(stderr, + fmt::vprint(stderr, "{}", fmt::make_format_args(4.2)), "4.2"); +} + +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) { + auto store = fmt::make_format_args(4.2); + 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}", store)); + std::string s; + fmt::vformat_to(de, std::back_inserter(s), "{:L}", store); + 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()); +} + +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) { + auto store = fmt::make_format_args(42); + EXPECT_THROW(throw fmt::format_error("oops"), std::exception); + EXPECT_THROW(throw fmt::vsystem_error(0, "{}", store), + 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, ", "))); +} + +TEST(module_test, time) { + auto time_now = std::time(nullptr); + EXPECT_TRUE(fmt::localtime(time_now).tm_year > 120); + EXPECT_TRUE(fmt::gmtime(time_now).tm_year > 120); + auto chrono_now = std::chrono::system_clock::now(); + EXPECT_TRUE(fmt::localtime(chrono_now).tm_year > 120); + EXPECT_TRUE(fmt::gmtime(chrono_now).tm_year > 120); +} + +TEST(module_test, time_point) { + auto now = std::chrono::system_clock::now(); + std::string_view past("2021-05-20 10:30:15"); + EXPECT_TRUE(past < fmt::format("{:%Y-%m-%d %H:%M:%S}", now)); +} + +TEST(module_test, time_duration) { + EXPECT_EQ("42s", fmt::format("{}", std::chrono::seconds{42})); + using us = std::chrono::duration; + EXPECT_EQ("4.2µs", fmt::format("{:3.1}", us{4.234})); + const auto de = std::locale("de_DE"); +// localized float formatting is not yet implemented +// EXPECT_EQ("4,23µs", fmt::format(de, "{:3.2L}", us{4.234})); + EXPECT_EQ("4.23µs", fmt::format(de, "{:3.2L}", us{4.234})); +} + +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(module_test, printf) { + EXPECT_WRITE(stdout, fmt::printf("%f", 42.123456), "42.123456"); + EXPECT_WRITE(stdout, fmt::printf("%d", 42), "42"); +} + +TEST(module_test, fprintf) { + EXPECT_WRITE(stderr, fmt::fprintf(stderr, "%d", 42), "42"); + std::ostringstream os; + fmt::fprintf(os, "%s", "bla"); + EXPECT_EQ("bla", os.str()); +} + +TEST(module_test, sprintf) { + EXPECT_EQ("42", fmt::sprintf("%d", 42)); +} + +TEST(module_test, vprintf) { + EXPECT_WRITE(stdout, fmt::vprintf("%d", fmt::make_printf_args(42)), "42"); +} + +TEST(module_test, vfprintf) { + auto args = fmt::make_printf_args(42); + EXPECT_WRITE(stderr, fmt::vfprintf(stderr, "%d", args), "42"); + std::ostringstream os; + fmt::vfprintf(os, "%d", args); + EXPECT_EQ("42", os.str()); +} + +TEST(module_test, vsprintf) { + EXPECT_EQ("42", fmt::vsprintf("%d", fmt::make_printf_args(42))); +} + +TEST(module_test, color) { + auto fg_check = fg(fmt::rgb(255, 200, 30)); + auto bg_check = bg(fmt::color::dark_slate_gray) | fmt::emphasis::italic; + auto emphasis_check = fmt::emphasis::underline | fmt::emphasis::bold; + EXPECT_EQ("\x1B[30m42\x1B[0m", + fmt::format(fg(fmt::terminal_color::black), "{}", 42)); +} + +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); +}