diff --git a/test/format-test.cc b/test/format-test.cc index a9dd41a2..0f7fb008 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -452,18 +452,18 @@ TEST(format_test, exception_from_lib) { } TEST(format_test, escape) { - EXPECT_EQ("{", fmt::format("{{")); - EXPECT_EQ("before {", fmt::format("before {{")); - EXPECT_EQ("{ after", fmt::format("{{ after")); - EXPECT_EQ("before { after", fmt::format("before {{ after")); + EXPECT_EQ(fmt::format("{{"), "{"); + EXPECT_EQ(fmt::format("before {{"), "before {"); + EXPECT_EQ(fmt::format("{{ after"), "{ after"); + EXPECT_EQ(fmt::format("before {{ after"), "before { after"); - EXPECT_EQ("}", fmt::format("}}")); - EXPECT_EQ("before }", fmt::format("before }}")); - EXPECT_EQ("} after", fmt::format("}} after")); - EXPECT_EQ("before } after", fmt::format("before }} after")); + EXPECT_EQ(fmt::format("}}"), "}"); + EXPECT_EQ(fmt::format("before }}"), "before }"); + EXPECT_EQ(fmt::format("}} after"), "} after"); + EXPECT_EQ(fmt::format("before }} after"), "before } after"); - EXPECT_EQ("{}", fmt::format("{{}}")); - EXPECT_EQ("{42}", fmt::format("{{{0}}}", 42)); + EXPECT_EQ(fmt::format("{{}}"), "{}"); + EXPECT_EQ(fmt::format("{{{0}}}", 42), "{42}"); } TEST(format_test, unmatched_braces) { @@ -475,16 +475,16 @@ TEST(format_test, unmatched_braces) { "invalid format string"); } -TEST(format_test, no_args) { EXPECT_EQ("test", fmt::format("test")); } +TEST(format_test, no_args) { EXPECT_EQ(fmt::format("test"), "test"); } TEST(format_test, args_in_different_positions) { - EXPECT_EQ("42", fmt::format("{0}", 42)); - EXPECT_EQ("before 42", fmt::format("before {0}", 42)); - EXPECT_EQ("42 after", fmt::format("{0} after", 42)); - EXPECT_EQ("before 42 after", fmt::format("before {0} after", 42)); - EXPECT_EQ("answer = 42", fmt::format("{0} = {1}", "answer", 42)); - EXPECT_EQ("42 is the answer", fmt::format("{1} is the {0}", "answer", 42)); - EXPECT_EQ("abracadabra", fmt::format("{0}{1}{0}", "abra", "cad")); + EXPECT_EQ(fmt::format("{0}", 42), "42"); + EXPECT_EQ(fmt::format("before {0}", 42), "before 42"); + EXPECT_EQ(fmt::format("{0} after", 42), "42 after"); + EXPECT_EQ(fmt::format("before {0} after", 42), "before 42 after"); + EXPECT_EQ(fmt::format("{0} = {1}", "answer", 42), "answer = 42"); + EXPECT_EQ(fmt::format("{1} is the {0}", "answer", 42), "42 is the answer"); + EXPECT_EQ(fmt::format("{0}{1}{0}", "abra", "cad"), "abracadabra"); } TEST(format_test, arg_errors) { @@ -514,14 +514,14 @@ TEST(format_test, arg_errors) { template struct test_format { template - static std::string format(fmt::string_view fmt, const T&... args) { + static auto format(fmt::string_view fmt, const T&... args) -> std::string { return test_format::format(fmt, N - 1, args...); } }; template <> struct test_format<0> { template - static std::string format(fmt::string_view fmt, const T&... args) { + static auto format(fmt::string_view fmt, const T&... args) -> std::string { return fmt::format(runtime(fmt), args...); } }; @@ -541,10 +541,10 @@ TEST(format_test, many_args) { TEST(format_test, named_arg) { EXPECT_EQ("1/a/A", fmt::format("{_1}/{a_}/{A_}", fmt::arg("a_", 'a'), fmt::arg("A_", "A"), fmt::arg("_1", 1))); - EXPECT_EQ(" -42", fmt::format("{0:{width}}", -42, fmt::arg("width", 4))); + EXPECT_EQ(fmt::format("{0:{width}}", -42, fmt::arg("width", 4)), " -42"); EXPECT_EQ("st", fmt::format("{0:.{precision}}", "str", fmt::arg("precision", 2))); - EXPECT_EQ("1 2", fmt::format("{} {two}", 1, fmt::arg("two", 2))); + EXPECT_EQ(fmt::format("{} {two}", 1, fmt::arg("two", 2)), "1 2"); EXPECT_EQ("42", fmt::format("{c}", fmt::arg("a", 0), fmt::arg("b", 0), fmt::arg("c", 42), fmt::arg("d", 0), fmt::arg("e", 0), @@ -559,12 +559,12 @@ TEST(format_test, named_arg) { } TEST(format_test, auto_arg_index) { - EXPECT_EQ("abc", fmt::format("{}{}{}", 'a', 'b', 'c')); + EXPECT_EQ(fmt::format("{}{}{}", 'a', 'b', 'c'), "abc"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0}{}"), 'a', 'b'), format_error, "cannot switch from manual to automatic argument indexing"); EXPECT_THROW_MSG((void)fmt::format(runtime("{}{0}"), 'a', 'b'), format_error, "cannot switch from automatic to manual argument indexing"); - EXPECT_EQ("1.2", fmt::format("{:.{}}", 1.2345, 2)); + EXPECT_EQ(fmt::format("{:.{}}", 1.2345, 2), "1.2"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0}:.{}"), 1.2345, 2), format_error, "cannot switch from manual to automatic argument indexing"); @@ -575,57 +575,57 @@ TEST(format_test, auto_arg_index) { "argument not found"); } -TEST(format_test, empty_specs) { EXPECT_EQ("42", fmt::format("{0:}", 42)); } +TEST(format_test, empty_specs) { EXPECT_EQ(fmt::format("{0:}", 42), "42"); } TEST(format_test, left_align) { - EXPECT_EQ("42 ", fmt::format("{0:<4}", 42)); - EXPECT_EQ("42 ", fmt::format("{0:<4o}", 042)); - EXPECT_EQ("42 ", fmt::format("{0:<4x}", 0x42)); - EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42)); - EXPECT_EQ("42 ", fmt::format("{0:<5}", 42u)); - EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42l)); - EXPECT_EQ("42 ", fmt::format("{0:<5}", 42ul)); - EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42ll)); - EXPECT_EQ("42 ", fmt::format("{0:<5}", 42ull)); - EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42.0)); - EXPECT_EQ("-42 ", fmt::format("{0:<5}", -42.0l)); - EXPECT_EQ("c ", fmt::format("{0:<5}", 'c')); - EXPECT_EQ("abc ", fmt::format("{0:<5}", "abc")); - EXPECT_EQ("0xface ", fmt::format("{0:<8}", reinterpret_cast(0xface))); + EXPECT_EQ(fmt::format("{0:<4}", 42), "42 "); + EXPECT_EQ(fmt::format("{0:<4o}", 042), "42 "); + EXPECT_EQ(fmt::format("{0:<4x}", 0x42), "42 "); + EXPECT_EQ(fmt::format("{0:<5}", -42), "-42 "); + EXPECT_EQ(fmt::format("{0:<5}", 42u), "42 "); + EXPECT_EQ(fmt::format("{0:<5}", -42l), "-42 "); + EXPECT_EQ(fmt::format("{0:<5}", 42ul), "42 "); + EXPECT_EQ(fmt::format("{0:<5}", -42ll), "-42 "); + EXPECT_EQ(fmt::format("{0:<5}", 42ull), "42 "); + EXPECT_EQ(fmt::format("{0:<5}", -42.0), "-42 "); + EXPECT_EQ(fmt::format("{0:<5}", -42.0l), "-42 "); + EXPECT_EQ(fmt::format("{0:<5}", 'c'), "c "); + EXPECT_EQ(fmt::format("{0:<5}", "abc"), "abc "); + EXPECT_EQ(fmt::format("{0:<8}", reinterpret_cast(0xface)), "0xface "); } TEST(format_test, right_align) { - EXPECT_EQ(" 42", fmt::format("{0:>4}", 42)); - EXPECT_EQ(" 42", fmt::format("{0:>4o}", 042)); - EXPECT_EQ(" 42", fmt::format("{0:>4x}", 0x42)); - EXPECT_EQ(" -42", fmt::format("{0:>5}", -42)); - EXPECT_EQ(" 42", fmt::format("{0:>5}", 42u)); - EXPECT_EQ(" -42", fmt::format("{0:>5}", -42l)); - EXPECT_EQ(" 42", fmt::format("{0:>5}", 42ul)); - EXPECT_EQ(" -42", fmt::format("{0:>5}", -42ll)); - EXPECT_EQ(" 42", fmt::format("{0:>5}", 42ull)); - EXPECT_EQ(" -42", fmt::format("{0:>5}", -42.0)); - EXPECT_EQ(" -42", fmt::format("{0:>5}", -42.0l)); - EXPECT_EQ(" c", fmt::format("{0:>5}", 'c')); - EXPECT_EQ(" abc", fmt::format("{0:>5}", "abc")); - EXPECT_EQ(" 0xface", fmt::format("{0:>8}", reinterpret_cast(0xface))); + EXPECT_EQ(fmt::format("{0:>4}", 42), " 42"); + EXPECT_EQ(fmt::format("{0:>4o}", 042), " 42"); + EXPECT_EQ(fmt::format("{0:>4x}", 0x42), " 42"); + EXPECT_EQ(fmt::format("{0:>5}", -42), " -42"); + EXPECT_EQ(fmt::format("{0:>5}", 42u), " 42"); + EXPECT_EQ(fmt::format("{0:>5}", -42l), " -42"); + EXPECT_EQ(fmt::format("{0:>5}", 42ul), " 42"); + EXPECT_EQ(fmt::format("{0:>5}", -42ll), " -42"); + EXPECT_EQ(fmt::format("{0:>5}", 42ull), " 42"); + EXPECT_EQ(fmt::format("{0:>5}", -42.0), " -42"); + EXPECT_EQ(fmt::format("{0:>5}", -42.0l), " -42"); + EXPECT_EQ(fmt::format("{0:>5}", 'c'), " c"); + EXPECT_EQ(fmt::format("{0:>5}", "abc"), " abc"); + EXPECT_EQ(fmt::format("{0:>8}", reinterpret_cast(0xface)), " 0xface"); } TEST(format_test, center_align) { - EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42)); - EXPECT_EQ(" 42 ", fmt::format("{0:^5o}", 042)); - EXPECT_EQ(" 42 ", fmt::format("{0:^5x}", 0x42)); - EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42)); - EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42u)); - EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42l)); - EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42ul)); - EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42ll)); - EXPECT_EQ(" 42 ", fmt::format("{0:^5}", 42ull)); - EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42.0)); - EXPECT_EQ(" -42 ", fmt::format("{0:^5}", -42.0l)); - EXPECT_EQ(" c ", fmt::format("{0:^5}", 'c')); - EXPECT_EQ(" abc ", fmt::format("{0:^6}", "abc")); - EXPECT_EQ(" 0xface ", fmt::format("{0:^8}", reinterpret_cast(0xface))); + EXPECT_EQ(fmt::format("{0:^5}", 42), " 42 "); + EXPECT_EQ(fmt::format("{0:^5o}", 042), " 42 "); + EXPECT_EQ(fmt::format("{0:^5x}", 0x42), " 42 "); + EXPECT_EQ(fmt::format("{0:^5}", -42), " -42 "); + EXPECT_EQ(fmt::format("{0:^5}", 42u), " 42 "); + EXPECT_EQ(fmt::format("{0:^5}", -42l), " -42 "); + EXPECT_EQ(fmt::format("{0:^5}", 42ul), " 42 "); + EXPECT_EQ(fmt::format("{0:^5}", -42ll), " -42 "); + EXPECT_EQ(fmt::format("{0:^5}", 42ull), " 42 "); + EXPECT_EQ(fmt::format("{0:^5}", -42.0), " -42 "); + EXPECT_EQ(fmt::format("{0:^5}", -42.0l), " -42 "); + EXPECT_EQ(fmt::format("{0:^5}", 'c'), " c "); + EXPECT_EQ(fmt::format("{0:^6}", "abc"), " abc "); + EXPECT_EQ(fmt::format("{0:^8}", reinterpret_cast(0xface)), " 0xface "); } TEST(format_test, fill) { @@ -633,44 +633,44 @@ TEST(format_test, fill) { "invalid fill character '{'"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{<5}}"), 'c'), format_error, "invalid fill character '{'"); - EXPECT_EQ("**42", fmt::format("{0:*>4}", 42)); - EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42)); - EXPECT_EQ("***42", fmt::format("{0:*>5}", 42u)); - EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42l)); - EXPECT_EQ("***42", fmt::format("{0:*>5}", 42ul)); - EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42ll)); - EXPECT_EQ("***42", fmt::format("{0:*>5}", 42ull)); - EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42.0)); - EXPECT_EQ("**-42", fmt::format("{0:*>5}", -42.0l)); - EXPECT_EQ("c****", fmt::format("{0:*<5}", 'c')); - EXPECT_EQ("abc**", fmt::format("{0:*<5}", "abc")); + EXPECT_EQ(fmt::format("{0:*>4}", 42), "**42"); + EXPECT_EQ(fmt::format("{0:*>5}", -42), "**-42"); + EXPECT_EQ(fmt::format("{0:*>5}", 42u), "***42"); + EXPECT_EQ(fmt::format("{0:*>5}", -42l), "**-42"); + EXPECT_EQ(fmt::format("{0:*>5}", 42ul), "***42"); + EXPECT_EQ(fmt::format("{0:*>5}", -42ll), "**-42"); + EXPECT_EQ(fmt::format("{0:*>5}", 42ull), "***42"); + EXPECT_EQ(fmt::format("{0:*>5}", -42.0), "**-42"); + EXPECT_EQ(fmt::format("{0:*>5}", -42.0l), "**-42"); + EXPECT_EQ(fmt::format("{0:*<5}", 'c'), "c****"); + EXPECT_EQ(fmt::format("{0:*<5}", "abc"), "abc**"); EXPECT_EQ("**0xface", fmt::format("{0:*>8}", reinterpret_cast(0xface))); - EXPECT_EQ("foo=", fmt::format("{:}=", "foo")); + EXPECT_EQ(fmt::format("{:}=", "foo"), "foo="); EXPECT_EQ(std::string("\0\0\0*", 4), fmt::format(string_view("{:\0>4}", 6), '*')); - EXPECT_EQ("жж42", fmt::format("{0:ж>4}", 42)); + EXPECT_EQ(fmt::format("{0:ж>4}", 42), "жж42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{:\x80\x80\x80\x80\x80>}"), 0), format_error, "invalid format specifier"); } TEST(format_test, plus_sign) { - EXPECT_EQ("+42", fmt::format("{0:+}", 42)); - EXPECT_EQ("-42", fmt::format("{0:+}", -42)); - EXPECT_EQ("+42", fmt::format("{0:+}", 42)); + EXPECT_EQ(fmt::format("{0:+}", 42), "+42"); + EXPECT_EQ(fmt::format("{0:+}", -42), "-42"); + EXPECT_EQ(fmt::format("{0:+}", 42), "+42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42u), format_error, "invalid format specifier"); - EXPECT_EQ("+42", fmt::format("{0:+}", 42l)); + EXPECT_EQ(fmt::format("{0:+}", 42l), "+42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42ul), format_error, "invalid format specifier"); - EXPECT_EQ("+42", fmt::format("{0:+}", 42ll)); + EXPECT_EQ(fmt::format("{0:+}", 42ll), "+42"); #if FMT_USE_INT128 - EXPECT_EQ("+42", fmt::format("{0:+}", __int128_t(42))); + EXPECT_EQ(fmt::format("{0:+}", __int128_t(42)), "+42"); #endif EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 42ull), format_error, "invalid format specifier"); - EXPECT_EQ("+42", fmt::format("{0:+}", 42.0)); - EXPECT_EQ("+42", fmt::format("{0:+}", 42.0l)); + EXPECT_EQ(fmt::format("{0:+}", 42.0), "+42"); + EXPECT_EQ(fmt::format("{0:+}", 42.0l), "+42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), 'c'), format_error, "invalid format specifier"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), "abc"), format_error, @@ -681,19 +681,19 @@ TEST(format_test, plus_sign) { } TEST(format_test, minus_sign) { - EXPECT_EQ("42", fmt::format("{0:-}", 42)); - EXPECT_EQ("-42", fmt::format("{0:-}", -42)); - EXPECT_EQ("42", fmt::format("{0:-}", 42)); + EXPECT_EQ(fmt::format("{0:-}", 42), "42"); + EXPECT_EQ(fmt::format("{0:-}", -42), "-42"); + EXPECT_EQ(fmt::format("{0:-}", 42), "42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42u), format_error, "invalid format specifier"); - EXPECT_EQ("42", fmt::format("{0:-}", 42l)); + EXPECT_EQ(fmt::format("{0:-}", 42l), "42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42ul), format_error, "invalid format specifier"); - EXPECT_EQ("42", fmt::format("{0:-}", 42ll)); + EXPECT_EQ(fmt::format("{0:-}", 42ll), "42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 42ull), format_error, "invalid format specifier"); - EXPECT_EQ("42", fmt::format("{0:-}", 42.0)); - EXPECT_EQ("42", fmt::format("{0:-}", 42.0l)); + EXPECT_EQ(fmt::format("{0:-}", 42.0), "42"); + EXPECT_EQ(fmt::format("{0:-}", 42.0l), "42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), 'c'), format_error, "invalid format specifier"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), "abc"), format_error, @@ -704,19 +704,19 @@ TEST(format_test, minus_sign) { } TEST(format_test, space_sign) { - EXPECT_EQ(" 42", fmt::format("{0: }", 42)); - EXPECT_EQ("-42", fmt::format("{0: }", -42)); - EXPECT_EQ(" 42", fmt::format("{0: }", 42)); + EXPECT_EQ(fmt::format("{0: }", 42), " 42"); + EXPECT_EQ(fmt::format("{0: }", -42), "-42"); + EXPECT_EQ(fmt::format("{0: }", 42), " 42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42u), format_error, "invalid format specifier"); - EXPECT_EQ(" 42", fmt::format("{0: }", 42l)); + EXPECT_EQ(fmt::format("{0: }", 42l), " 42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42ul), format_error, "invalid format specifier"); - EXPECT_EQ(" 42", fmt::format("{0: }", 42ll)); + EXPECT_EQ(fmt::format("{0: }", 42ll), " 42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 42ull), format_error, "invalid format specifier"); - EXPECT_EQ(" 42", fmt::format("{0: }", 42.0)); - EXPECT_EQ(" 42", fmt::format("{0: }", 42.0l)); + EXPECT_EQ(fmt::format("{0: }", 42.0), " 42"); + EXPECT_EQ(fmt::format("{0: }", 42.0l), " 42"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), 'c'), format_error, "invalid format specifier"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), "abc"), format_error, @@ -727,45 +727,45 @@ TEST(format_test, space_sign) { } TEST(format_test, hash_flag) { - EXPECT_EQ("42", fmt::format("{0:#}", 42)); - EXPECT_EQ("-42", fmt::format("{0:#}", -42)); - EXPECT_EQ("0b101010", fmt::format("{0:#b}", 42)); - EXPECT_EQ("0B101010", fmt::format("{0:#B}", 42)); - EXPECT_EQ("-0b101010", fmt::format("{0:#b}", -42)); - EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42)); - EXPECT_EQ("0X42", fmt::format("{0:#X}", 0x42)); - EXPECT_EQ("-0x42", fmt::format("{0:#x}", -0x42)); - EXPECT_EQ("0", fmt::format("{0:#o}", 0)); - EXPECT_EQ("042", fmt::format("{0:#o}", 042)); - EXPECT_EQ("-042", fmt::format("{0:#o}", -042)); - EXPECT_EQ("42", fmt::format("{0:#}", 42u)); - EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42u)); - EXPECT_EQ("042", fmt::format("{0:#o}", 042u)); + EXPECT_EQ(fmt::format("{0:#}", 42), "42"); + EXPECT_EQ(fmt::format("{0:#}", -42), "-42"); + EXPECT_EQ(fmt::format("{0:#b}", 42), "0b101010"); + EXPECT_EQ(fmt::format("{0:#B}", 42), "0B101010"); + EXPECT_EQ(fmt::format("{0:#b}", -42), "-0b101010"); + EXPECT_EQ(fmt::format("{0:#x}", 0x42), "0x42"); + EXPECT_EQ(fmt::format("{0:#X}", 0x42), "0X42"); + EXPECT_EQ(fmt::format("{0:#x}", -0x42), "-0x42"); + EXPECT_EQ(fmt::format("{0:#o}", 0), "0"); + EXPECT_EQ(fmt::format("{0:#o}", 042), "042"); + EXPECT_EQ(fmt::format("{0:#o}", -042), "-042"); + EXPECT_EQ(fmt::format("{0:#}", 42u), "42"); + EXPECT_EQ(fmt::format("{0:#x}", 0x42u), "0x42"); + EXPECT_EQ(fmt::format("{0:#o}", 042u), "042"); - EXPECT_EQ("-42", fmt::format("{0:#}", -42l)); - EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42l)); - EXPECT_EQ("-0x42", fmt::format("{0:#x}", -0x42l)); - EXPECT_EQ("042", fmt::format("{0:#o}", 042l)); - EXPECT_EQ("-042", fmt::format("{0:#o}", -042l)); - EXPECT_EQ("42", fmt::format("{0:#}", 42ul)); - EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42ul)); - EXPECT_EQ("042", fmt::format("{0:#o}", 042ul)); + EXPECT_EQ(fmt::format("{0:#}", -42l), "-42"); + EXPECT_EQ(fmt::format("{0:#x}", 0x42l), "0x42"); + EXPECT_EQ(fmt::format("{0:#x}", -0x42l), "-0x42"); + EXPECT_EQ(fmt::format("{0:#o}", 042l), "042"); + EXPECT_EQ(fmt::format("{0:#o}", -042l), "-042"); + EXPECT_EQ(fmt::format("{0:#}", 42ul), "42"); + EXPECT_EQ(fmt::format("{0:#x}", 0x42ul), "0x42"); + EXPECT_EQ(fmt::format("{0:#o}", 042ul), "042"); - EXPECT_EQ("-42", fmt::format("{0:#}", -42ll)); - EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42ll)); - EXPECT_EQ("-0x42", fmt::format("{0:#x}", -0x42ll)); - EXPECT_EQ("042", fmt::format("{0:#o}", 042ll)); - EXPECT_EQ("-042", fmt::format("{0:#o}", -042ll)); - EXPECT_EQ("42", fmt::format("{0:#}", 42ull)); - EXPECT_EQ("0x42", fmt::format("{0:#x}", 0x42ull)); - EXPECT_EQ("042", fmt::format("{0:#o}", 042ull)); + EXPECT_EQ(fmt::format("{0:#}", -42ll), "-42"); + EXPECT_EQ(fmt::format("{0:#x}", 0x42ll), "0x42"); + EXPECT_EQ(fmt::format("{0:#x}", -0x42ll), "-0x42"); + EXPECT_EQ(fmt::format("{0:#o}", 042ll), "042"); + EXPECT_EQ(fmt::format("{0:#o}", -042ll), "-042"); + EXPECT_EQ(fmt::format("{0:#}", 42ull), "42"); + EXPECT_EQ(fmt::format("{0:#x}", 0x42ull), "0x42"); + EXPECT_EQ(fmt::format("{0:#o}", 042ull), "042"); - EXPECT_EQ("-42.", fmt::format("{0:#}", -42.0)); - EXPECT_EQ("-42.", fmt::format("{0:#}", -42.0l)); - EXPECT_EQ("4.e+01", fmt::format("{:#.0e}", 42.0)); - EXPECT_EQ("0.", fmt::format("{:#.0f}", 0.01)); - EXPECT_EQ("0.50", fmt::format("{:#.2g}", 0.5)); - EXPECT_EQ("0.", fmt::format("{:#.0f}", 0.5)); + EXPECT_EQ(fmt::format("{0:#}", -42.0), "-42."); + EXPECT_EQ(fmt::format("{0:#}", -42.0l), "-42."); + EXPECT_EQ(fmt::format("{:#.0e}", 42.0), "4.e+01"); + EXPECT_EQ(fmt::format("{:#.0f}", 0.01), "0."); + EXPECT_EQ(fmt::format("{:#.2g}", 0.5), "0.50"); + EXPECT_EQ(fmt::format("{:#.0f}", 0.5), "0."); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#"), 'c'), format_error, "missing '}' in format string"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), 'c'), format_error, @@ -778,15 +778,15 @@ TEST(format_test, hash_flag) { } TEST(format_test, zero_flag) { - EXPECT_EQ("42", fmt::format("{0:0}", 42)); - EXPECT_EQ("-0042", fmt::format("{0:05}", -42)); - EXPECT_EQ("00042", fmt::format("{0:05}", 42u)); - EXPECT_EQ("-0042", fmt::format("{0:05}", -42l)); - EXPECT_EQ("00042", fmt::format("{0:05}", 42ul)); - EXPECT_EQ("-0042", fmt::format("{0:05}", -42ll)); - EXPECT_EQ("00042", fmt::format("{0:05}", 42ull)); - EXPECT_EQ("-000042", fmt::format("{0:07}", -42.0)); - EXPECT_EQ("-000042", fmt::format("{0:07}", -42.0l)); + EXPECT_EQ(fmt::format("{0:0}", 42), "42"); + EXPECT_EQ(fmt::format("{0:05}", -42), "-0042"); + EXPECT_EQ(fmt::format("{0:05}", 42u), "00042"); + EXPECT_EQ(fmt::format("{0:05}", -42l), "-0042"); + EXPECT_EQ(fmt::format("{0:05}", 42ul), "00042"); + EXPECT_EQ(fmt::format("{0:05}", -42ll), "-0042"); + EXPECT_EQ(fmt::format("{0:05}", 42ull), "00042"); + EXPECT_EQ(fmt::format("{0:07}", -42.0), "-000042"); + EXPECT_EQ(fmt::format("{0:07}", -42.0l), "-000042"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:0"), 'c'), format_error, "missing '}' in format string"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), 'c'), format_error, @@ -801,44 +801,33 @@ TEST(format_test, zero_flag) { TEST(format_test, zero_flag_and_align) { // If the 0 character and an align option both appear, the 0 character is // ignored. - EXPECT_EQ("42 ", fmt::format("{0:<05}", 42)); - EXPECT_EQ("-42 ", fmt::format("{0:<05}", -42)); - EXPECT_EQ(" 42 ", fmt::format("{0:^05}", 42)); - EXPECT_EQ(" -42 ", fmt::format("{0:^05}", -42)); - EXPECT_EQ(" 42", fmt::format("{0:>05}", 42)); - EXPECT_EQ(" -42", fmt::format("{0:>05}", -42)); + EXPECT_EQ(fmt::format("{:<05}", 42), "42 "); + EXPECT_EQ(fmt::format("{:<05}", -42), "-42 "); + EXPECT_EQ(fmt::format("{:^05}", 42), " 42 "); + EXPECT_EQ(fmt::format("{:^05}", -42), " -42 "); + EXPECT_EQ(fmt::format("{:>05}", 42), " 42"); + EXPECT_EQ(fmt::format("{:>05}", -42), " -42"); } TEST(format_test, width) { - char format_str[buffer_size]; - safe_sprintf(format_str, "{0:%u", UINT_MAX); - increment(format_str + 3); - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "number is too big"); - size_t size = std::strlen(format_str); - format_str[size] = '}'; - format_str[size + 1] = 0; - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "number is too big"); + auto int_maxer = std::to_string(INT_MAX + 1u); + EXPECT_THROW_MSG((void)fmt::format(runtime("{:" + int_maxer), 0), + format_error, "number is too big"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{:" + int_maxer + "}"), 0), + format_error, "number is too big"); - safe_sprintf(format_str, "{0:%u", INT_MAX + 1u); - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "number is too big"); - safe_sprintf(format_str, "{0:%u}", INT_MAX + 1u); - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "number is too big"); - EXPECT_EQ(" -42", fmt::format("{0:4}", -42)); - EXPECT_EQ(" 42", fmt::format("{0:5}", 42u)); - EXPECT_EQ(" -42", fmt::format("{0:6}", -42l)); - EXPECT_EQ(" 42", fmt::format("{0:7}", 42ul)); - EXPECT_EQ(" -42", fmt::format("{0:6}", -42ll)); - EXPECT_EQ(" 42", fmt::format("{0:7}", 42ull)); - EXPECT_EQ(" -1.23", fmt::format("{0:8}", -1.23)); - EXPECT_EQ(" -1.23", fmt::format("{0:9}", -1.23l)); - EXPECT_EQ(" 0xcafe", - fmt::format("{0:10}", reinterpret_cast(0xcafe))); - EXPECT_EQ("x ", fmt::format("{0:11}", 'x')); - EXPECT_EQ("str ", fmt::format("{0:12}", "str")); + EXPECT_EQ(fmt::format("{:4}", -42), " -42"); + EXPECT_EQ(fmt::format("{:5}", 42u), " 42"); + EXPECT_EQ(fmt::format("{:6}", -42l), " -42"); + EXPECT_EQ(fmt::format("{:7}", 42ul), " 42"); + EXPECT_EQ(fmt::format("{:6}", -42ll), " -42"); + EXPECT_EQ(fmt::format("{:7}", 42ull), " 42"); + EXPECT_EQ(fmt::format("{:8}", -1.23), " -1.23"); + EXPECT_EQ(fmt::format("{:9}", -1.23l), " -1.23"); + EXPECT_EQ(fmt::format("{:10}", reinterpret_cast(0xcafe)), + " 0xcafe"); + EXPECT_EQ(fmt::format("{:11}", 'x'), "x "); + EXPECT_EQ(fmt::format("{:12}", "str"), "str "); EXPECT_EQ(fmt::format("{:*^6}", "🤡"), "**🤡**"); EXPECT_EQ(fmt::format("{:*^8}", "你好"), "**你好**"); EXPECT_EQ(fmt::format("{:#6}", 42.0), " 42."); @@ -847,20 +836,13 @@ TEST(format_test, width) { } TEST(format_test, runtime_width) { - char format_str[buffer_size]; - safe_sprintf(format_str, "{0:{%u", UINT_MAX); - increment(format_str + 4); - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "invalid format string"); - size_t size = std::strlen(format_str); - format_str[size] = '}'; - format_str[size + 1] = 0; - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "argument not found"); - format_str[size + 1] = '}'; - format_str[size + 2] = 0; - EXPECT_THROW_MSG((void)fmt::format(runtime(format_str), 0), format_error, - "argument not found"); + auto int_maxer = std::to_string(INT_MAX + 1u); + EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{" + int_maxer), 0), + format_error, "invalid format string"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{" + int_maxer + "}"), 0), + format_error, "argument not found"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{" + int_maxer + "}}"), 0), + format_error, "argument not found"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{"), 0), format_error, "invalid format string"); @@ -893,18 +875,18 @@ TEST(format_test, runtime_width) { EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, 0.0), format_error, "width is not integer"); - EXPECT_EQ(" -42", fmt::format("{0:{1}}", -42, 4)); - EXPECT_EQ(" 42", fmt::format("{0:{1}}", 42u, 5)); - EXPECT_EQ(" -42", fmt::format("{0:{1}}", -42l, 6)); - EXPECT_EQ(" 42", fmt::format("{0:{1}}", 42ul, 7)); - EXPECT_EQ(" -42", fmt::format("{0:{1}}", -42ll, 6)); - EXPECT_EQ(" 42", fmt::format("{0:{1}}", 42ull, 7)); - EXPECT_EQ(" -1.23", fmt::format("{0:{1}}", -1.23, 8)); - EXPECT_EQ(" -1.23", fmt::format("{0:{1}}", -1.23l, 9)); + EXPECT_EQ(fmt::format("{0:{1}}", -42, 4), " -42"); + EXPECT_EQ(fmt::format("{0:{1}}", 42u, 5), " 42"); + EXPECT_EQ(fmt::format("{0:{1}}", -42l, 6), " -42"); + EXPECT_EQ(fmt::format("{0:{1}}", 42ul, 7), " 42"); + EXPECT_EQ(fmt::format("{0:{1}}", -42ll, 6), " -42"); + EXPECT_EQ(fmt::format("{0:{1}}", 42ull, 7), " 42"); + EXPECT_EQ(fmt::format("{0:{1}}", -1.23, 8), " -1.23"); + EXPECT_EQ(fmt::format("{0:{1}}", -1.23l, 9), " -1.23"); EXPECT_EQ(" 0xcafe", fmt::format("{0:{1}}", reinterpret_cast(0xcafe), 10)); - EXPECT_EQ("x ", fmt::format("{0:{1}}", 'x', 11)); - EXPECT_EQ("str ", fmt::format("{0:{1}}", "str", 12)); + EXPECT_EQ(fmt::format("{0:{1}}", 'x', 11), "x "); + EXPECT_EQ(fmt::format("{0:{1}}", "str", 12), "str "); EXPECT_EQ(fmt::format("{:{}}", 42, short(4)), " 42"); } @@ -960,12 +942,12 @@ TEST(format_test, precision) { "invalid format specifier"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:3.0}"), 'x'), format_error, "invalid format specifier"); - EXPECT_EQ("1.2", fmt::format("{0:.2}", 1.2345)); - EXPECT_EQ("1.2", fmt::format("{0:.2}", 1.2345l)); - EXPECT_EQ("1.2e+56", fmt::format("{:.2}", 1.234e56)); - EXPECT_EQ("1.1", fmt::format("{0:.3}", 1.1)); - EXPECT_EQ("1e+00", fmt::format("{:.0e}", 1.0L)); - EXPECT_EQ(" 0.0e+00", fmt::format("{:9.1e}", 0.0)); + EXPECT_EQ(fmt::format("{0:.2}", 1.2345), "1.2"); + EXPECT_EQ(fmt::format("{0:.2}", 1.2345l), "1.2"); + EXPECT_EQ(fmt::format("{:.2}", 1.234e56), "1.2e+56"); + EXPECT_EQ(fmt::format("{0:.3}", 1.1), "1.1"); + EXPECT_EQ(fmt::format("{:.0e}", 1.0L), "1e+00"); + EXPECT_EQ(fmt::format("{:9.1e}", 0.0), " 0.0e+00"); EXPECT_EQ( fmt::format("{:.494}", 4.9406564584124654E-324), "4.9406564584124654417656879286822137236505980261432476442558568250067550" @@ -1029,12 +1011,12 @@ TEST(format_test, precision) { "3.6452e-4951"); } - EXPECT_EQ("123.", fmt::format("{:#.0f}", 123.0)); - EXPECT_EQ("1.23", fmt::format("{:.02f}", 1.234)); - EXPECT_EQ("0.001", fmt::format("{:.1g}", 0.001)); - EXPECT_EQ("1019666400", fmt::format("{}", 1019666432.0f)); - EXPECT_EQ("1e+01", fmt::format("{:.0e}", 9.5)); - EXPECT_EQ("1.0e-34", fmt::format("{:.1e}", 1e-34)); + EXPECT_EQ(fmt::format("{:#.0f}", 123.0), "123."); + EXPECT_EQ(fmt::format("{:.02f}", 1.234), "1.23"); + EXPECT_EQ(fmt::format("{:.1g}", 0.001), "0.001"); + EXPECT_EQ(fmt::format("{}", 1019666432.0f), "1019666400"); + EXPECT_EQ(fmt::format("{:.0e}", 9.5), "1e+01"); + EXPECT_EQ(fmt::format("{:.1e}", 1e-34), "1.0e-34"); EXPECT_THROW_MSG( (void)fmt::format(runtime("{0:.2}"), reinterpret_cast(0xcafe)), @@ -1049,9 +1031,9 @@ TEST(format_test, precision) { (void)fmt::format("{:.2147483646f}", -2.2121295195081227E+304), format_error, "number is too big"); - EXPECT_EQ("st", fmt::format("{0:.2}", "str")); - EXPECT_EQ("вожык", fmt::format("{0:.5}", "вожыкі")); - EXPECT_EQ("123456", fmt::format("{0:.6}", "123456\xad")); + EXPECT_EQ(fmt::format("{0:.2}", "str"), "st"); + EXPECT_EQ(fmt::format("{0:.5}", "вожыкі"), "вожык"); + EXPECT_EQ(fmt::format("{0:.6}", "123456\xad"), "123456"); } TEST(xchar_test, utf8_precision) { @@ -1135,8 +1117,8 @@ TEST(format_test, runtime_precision) { format_error, "invalid format specifier"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:3.{1}}"), 'x', 0), format_error, "invalid format specifier"); - EXPECT_EQ("1.2", fmt::format("{0:.{1}}", 1.2345, 2)); - EXPECT_EQ("1.2", fmt::format("{1:.{0}}", 2, 1.2345l)); + EXPECT_EQ(fmt::format("{0:.{1}}", 1.2345, 2), "1.2"); + EXPECT_EQ(fmt::format("{1:.{0}}", 2, 1.2345l), "1.2"); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), reinterpret_cast(0xcafe), 2), @@ -1145,26 +1127,26 @@ TEST(format_test, runtime_precision) { reinterpret_cast(0xcafe), 2), format_error, "invalid format specifier"); - EXPECT_EQ("st", fmt::format("{0:.{1}}", "str", 2)); + EXPECT_EQ(fmt::format("{0:.{1}}", "str", 2), "st"); } TEST(format_test, format_bool) { - EXPECT_EQ("true", fmt::format("{}", true)); - EXPECT_EQ("false", fmt::format("{}", false)); - EXPECT_EQ("1", fmt::format("{:d}", true)); - EXPECT_EQ("true ", fmt::format("{:5}", true)); - EXPECT_EQ("true", fmt::format("{:s}", true)); - EXPECT_EQ("false", fmt::format("{:s}", false)); - EXPECT_EQ("false ", fmt::format("{:6s}", false)); + EXPECT_EQ(fmt::format("{}", true), "true"); + EXPECT_EQ(fmt::format("{}", false), "false"); + EXPECT_EQ(fmt::format("{:d}", true), "1"); + EXPECT_EQ(fmt::format("{:5}", true), "true "); + EXPECT_EQ(fmt::format("{:s}", true), "true"); + EXPECT_EQ(fmt::format("{:s}", false), "false"); + EXPECT_EQ(fmt::format("{:6s}", false), "false "); EXPECT_THROW_MSG((void)fmt::format(runtime("{:c}"), false), format_error, "invalid format specifier"); } TEST(format_test, format_short) { short s = 42; - EXPECT_EQ("42", fmt::format("{0:d}", s)); + EXPECT_EQ(fmt::format("{0:d}", s), "42"); unsigned short us = 42; - EXPECT_EQ("42", fmt::format("{0:d}", us)); + EXPECT_EQ(fmt::format("{0:d}", us), "42"); } template @@ -1186,16 +1168,16 @@ TEST(format_test, format_int) { EXPECT_THROW_MSG((void)fmt::format(runtime("{0:v"), 42), format_error, "invalid format specifier"); check_unknown_types(42, "bBdoxXnLc", "integer"); - EXPECT_EQ("x", fmt::format("{:c}", static_cast('x'))); + EXPECT_EQ(fmt::format("{:c}", static_cast('x')), "x"); } TEST(format_test, format_bin) { - EXPECT_EQ("0", fmt::format("{0:b}", 0)); - EXPECT_EQ("101010", fmt::format("{0:b}", 42)); - EXPECT_EQ("101010", fmt::format("{0:b}", 42u)); - EXPECT_EQ("-101010", fmt::format("{0:b}", -42)); - EXPECT_EQ("11000000111001", fmt::format("{0:b}", 12345)); - EXPECT_EQ("10010001101000101011001111000", fmt::format("{0:b}", 0x12345678)); + EXPECT_EQ(fmt::format("{0:b}", 0), "0"); + EXPECT_EQ(fmt::format("{0:b}", 42), "101010"); + EXPECT_EQ(fmt::format("{0:b}", 42u), "101010"); + EXPECT_EQ(fmt::format("{0:b}", -42), "-101010"); + EXPECT_EQ(fmt::format("{0:b}", 12345), "11000000111001"); + EXPECT_EQ(fmt::format("{0:b}", 0x12345678), "10010001101000101011001111000"); EXPECT_EQ("10010000101010111100110111101111", fmt::format("{0:b}", 0x90ABCDEF)); EXPECT_EQ("11111111111111111111111111111111", @@ -1211,17 +1193,17 @@ constexpr auto uint128_max = ~static_cast<__uint128_t>(0); #endif TEST(format_test, format_dec) { - EXPECT_EQ("0", fmt::format("{0}", 0)); - EXPECT_EQ("42", fmt::format("{0}", 42)); - EXPECT_EQ("42>", fmt::format("{:}>", 42)); - EXPECT_EQ("42", fmt::format("{0:d}", 42)); - EXPECT_EQ("42", fmt::format("{0}", 42u)); - EXPECT_EQ("-42", fmt::format("{0}", -42)); - EXPECT_EQ("12345", fmt::format("{0}", 12345)); - EXPECT_EQ("67890", fmt::format("{0}", 67890)); + EXPECT_EQ(fmt::format("{0}", 0), "0"); + EXPECT_EQ(fmt::format("{0}", 42), "42"); + EXPECT_EQ(fmt::format("{:}>", 42), "42>"); + EXPECT_EQ(fmt::format("{0:d}", 42), "42"); + EXPECT_EQ(fmt::format("{0}", 42u), "42"); + EXPECT_EQ(fmt::format("{0}", -42), "-42"); + EXPECT_EQ(fmt::format("{0}", 12345), "12345"); + EXPECT_EQ(fmt::format("{0}", 67890), "67890"); #if FMT_USE_INT128 - EXPECT_EQ("0", fmt::format("{0}", static_cast<__int128_t>(0))); - EXPECT_EQ("0", fmt::format("{0}", static_cast<__uint128_t>(0))); + EXPECT_EQ(fmt::format("{0}", static_cast<__int128_t>(0)), "0"); + EXPECT_EQ(fmt::format("{0}", static_cast<__uint128_t>(0)), "0"); EXPECT_EQ("9223372036854775808", fmt::format("{0}", static_cast<__int128_t>(INT64_MAX) + 1)); EXPECT_EQ("-9223372036854775809", @@ -1252,17 +1234,17 @@ TEST(format_test, format_dec) { } TEST(format_test, format_hex) { - EXPECT_EQ("0", fmt::format("{0:x}", 0)); - EXPECT_EQ("42", fmt::format("{0:x}", 0x42)); - EXPECT_EQ("42", fmt::format("{0:x}", 0x42u)); - EXPECT_EQ("-42", fmt::format("{0:x}", -0x42)); - EXPECT_EQ("12345678", fmt::format("{0:x}", 0x12345678)); - EXPECT_EQ("90abcdef", fmt::format("{0:x}", 0x90abcdef)); - EXPECT_EQ("12345678", fmt::format("{0:X}", 0x12345678)); - EXPECT_EQ("90ABCDEF", fmt::format("{0:X}", 0x90ABCDEF)); + EXPECT_EQ(fmt::format("{0:x}", 0), "0"); + EXPECT_EQ(fmt::format("{0:x}", 0x42), "42"); + EXPECT_EQ(fmt::format("{0:x}", 0x42u), "42"); + EXPECT_EQ(fmt::format("{0:x}", -0x42), "-42"); + EXPECT_EQ(fmt::format("{0:x}", 0x12345678), "12345678"); + EXPECT_EQ(fmt::format("{0:x}", 0x90abcdef), "90abcdef"); + EXPECT_EQ(fmt::format("{0:X}", 0x12345678), "12345678"); + EXPECT_EQ(fmt::format("{0:X}", 0x90ABCDEF), "90ABCDEF"); #if FMT_USE_INT128 - EXPECT_EQ("0", fmt::format("{0:x}", static_cast<__int128_t>(0))); - EXPECT_EQ("0", fmt::format("{0:x}", static_cast<__uint128_t>(0))); + EXPECT_EQ(fmt::format("{0:x}", static_cast<__int128_t>(0)), "0"); + EXPECT_EQ(fmt::format("{0:x}", static_cast<__uint128_t>(0)), "0"); EXPECT_EQ("8000000000000000", fmt::format("{0:x}", static_cast<__int128_t>(INT64_MAX) + 1)); EXPECT_EQ("-8000000000000001", @@ -1293,14 +1275,14 @@ TEST(format_test, format_hex) { } TEST(format_test, format_oct) { - EXPECT_EQ("0", fmt::format("{0:o}", 0)); - EXPECT_EQ("42", fmt::format("{0:o}", 042)); - EXPECT_EQ("42", fmt::format("{0:o}", 042u)); - EXPECT_EQ("-42", fmt::format("{0:o}", -042)); - EXPECT_EQ("12345670", fmt::format("{0:o}", 012345670)); + EXPECT_EQ(fmt::format("{0:o}", 0), "0"); + EXPECT_EQ(fmt::format("{0:o}", 042), "42"); + EXPECT_EQ(fmt::format("{0:o}", 042u), "42"); + EXPECT_EQ(fmt::format("{0:o}", -042), "-42"); + EXPECT_EQ(fmt::format("{0:o}", 012345670), "12345670"); #if FMT_USE_INT128 - EXPECT_EQ("0", fmt::format("{0:o}", static_cast<__int128_t>(0))); - EXPECT_EQ("0", fmt::format("{0:o}", static_cast<__uint128_t>(0))); + EXPECT_EQ(fmt::format("{0:o}", static_cast<__int128_t>(0)), "0"); + EXPECT_EQ(fmt::format("{0:o}", static_cast<__uint128_t>(0)), "0"); EXPECT_EQ("1000000000000000000000", fmt::format("{0:o}", static_cast<__int128_t>(INT64_MAX) + 1)); EXPECT_EQ("-1000000000000000000001", @@ -1331,12 +1313,12 @@ TEST(format_test, format_oct) { } TEST(format_test, format_int_locale) { - EXPECT_EQ("1234", fmt::format("{:L}", 1234)); + EXPECT_EQ(fmt::format("{:L}", 1234), "1234"); } TEST(format_test, format_float) { - EXPECT_EQ("0", fmt::format("{}", 0.0f)); - EXPECT_EQ("392.500000", fmt::format("{0:f}", 392.5f)); + EXPECT_EQ(fmt::format("{}", 0.0f), "0"); + EXPECT_EQ(fmt::format("{0:f}", 392.5f), "392.500000"); } TEST(format_test, format_double) { @@ -1405,92 +1387,92 @@ TEST(format_test, format_double) { } TEST(format_test, precision_rounding) { - EXPECT_EQ("0", fmt::format("{:.0f}", 0.0)); - EXPECT_EQ("0", fmt::format("{:.0f}", 0.01)); - EXPECT_EQ("0", fmt::format("{:.0f}", 0.1)); - EXPECT_EQ("0.000", fmt::format("{:.3f}", 0.00049)); - EXPECT_EQ("0.001", fmt::format("{:.3f}", 0.0005)); - EXPECT_EQ("0.001", fmt::format("{:.3f}", 0.00149)); - EXPECT_EQ("0.002", fmt::format("{:.3f}", 0.0015)); - EXPECT_EQ("1.000", fmt::format("{:.3f}", 0.9999)); - EXPECT_EQ("0.00123", fmt::format("{:.3}", 0.00123)); - EXPECT_EQ("0.1", fmt::format("{:.16g}", 0.1)); - EXPECT_EQ("1", fmt::format("{:.0}", 1.0)); + EXPECT_EQ(fmt::format("{:.0f}", 0.0), "0"); + EXPECT_EQ(fmt::format("{:.0f}", 0.01), "0"); + EXPECT_EQ(fmt::format("{:.0f}", 0.1), "0"); + EXPECT_EQ(fmt::format("{:.3f}", 0.00049), "0.000"); + EXPECT_EQ(fmt::format("{:.3f}", 0.0005), "0.001"); + EXPECT_EQ(fmt::format("{:.3f}", 0.00149), "0.001"); + EXPECT_EQ(fmt::format("{:.3f}", 0.0015), "0.002"); + EXPECT_EQ(fmt::format("{:.3f}", 0.9999), "1.000"); + EXPECT_EQ(fmt::format("{:.3}", 0.00123), "0.00123"); + EXPECT_EQ(fmt::format("{:.16g}", 0.1), "0.1"); + EXPECT_EQ(fmt::format("{:.0}", 1.0), "1"); EXPECT_EQ("225.51575035152063720", fmt::format("{:.17f}", 225.51575035152064)); - EXPECT_EQ("-761519619559038.2", fmt::format("{:.1f}", -761519619559038.2)); + EXPECT_EQ(fmt::format("{:.1f}", -761519619559038.2), "-761519619559038.2"); EXPECT_EQ("1.9156918820264798e-56", fmt::format("{}", 1.9156918820264798e-56)); - EXPECT_EQ("0.0000", fmt::format("{:.4f}", 7.2809479766055470e-15)); + EXPECT_EQ(fmt::format("{:.4f}", 7.2809479766055470e-15), "0.0000"); } TEST(format_test, prettify_float) { - EXPECT_EQ("0.0001", fmt::format("{}", 1e-4)); - EXPECT_EQ("1e-05", fmt::format("{}", 1e-5)); - EXPECT_EQ("1000000000000000", fmt::format("{}", 1e15)); - EXPECT_EQ("1e+16", fmt::format("{}", 1e16)); - EXPECT_EQ("9.999e-05", fmt::format("{}", 9.999e-5)); - EXPECT_EQ("10000000000", fmt::format("{}", 1e10)); - EXPECT_EQ("100000000000", fmt::format("{}", 1e11)); - EXPECT_EQ("12340000000", fmt::format("{}", 1234e7)); - EXPECT_EQ("12.34", fmt::format("{}", 1234e-2)); - EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6)); - EXPECT_EQ("0.1", fmt::format("{}", 0.1f)); - EXPECT_EQ("1.3563156e-19", fmt::format("{}", 1.35631564e-19f)); + EXPECT_EQ(fmt::format("{}", 1e-4), "0.0001"); + EXPECT_EQ(fmt::format("{}", 1e-5), "1e-05"); + EXPECT_EQ(fmt::format("{}", 1e15), "1000000000000000"); + EXPECT_EQ(fmt::format("{}", 1e16), "1e+16"); + EXPECT_EQ(fmt::format("{}", 9.999e-5), "9.999e-05"); + EXPECT_EQ(fmt::format("{}", 1e10), "10000000000"); + EXPECT_EQ(fmt::format("{}", 1e11), "100000000000"); + EXPECT_EQ(fmt::format("{}", 1234e7), "12340000000"); + EXPECT_EQ(fmt::format("{}", 1234e-2), "12.34"); + EXPECT_EQ(fmt::format("{}", 1234e-6), "0.001234"); + EXPECT_EQ(fmt::format("{}", 0.1f), "0.1"); + EXPECT_EQ(fmt::format("{}", 1.35631564e-19f), "1.3563156e-19"); } TEST(format_test, format_nan) { double nan = std::numeric_limits::quiet_NaN(); - EXPECT_EQ("nan", fmt::format("{}", nan)); - EXPECT_EQ("+nan", fmt::format("{:+}", nan)); - EXPECT_EQ(" +nan", fmt::format("{:+06}", nan)); - EXPECT_EQ("+nan ", fmt::format("{:<+06}", nan)); - EXPECT_EQ(" +nan ", fmt::format("{:^+06}", nan)); - EXPECT_EQ(" +nan", fmt::format("{:>+06}", nan)); + EXPECT_EQ(fmt::format("{}", nan), "nan"); + EXPECT_EQ(fmt::format("{:+}", nan), "+nan"); + EXPECT_EQ(fmt::format("{:+06}", nan), " +nan"); + EXPECT_EQ(fmt::format("{:<+06}", nan), "+nan "); + EXPECT_EQ(fmt::format("{:^+06}", nan), " +nan "); + EXPECT_EQ(fmt::format("{:>+06}", nan), " +nan"); if (std::signbit(-nan)) { - EXPECT_EQ("-nan", fmt::format("{}", -nan)); - EXPECT_EQ(" -nan", fmt::format("{:+06}", -nan)); + EXPECT_EQ(fmt::format("{}", -nan), "-nan"); + EXPECT_EQ(fmt::format("{:+06}", -nan), " -nan"); } else { fmt::print("Warning: compiler doesn't handle negative NaN correctly"); } - EXPECT_EQ(" nan", fmt::format("{: }", nan)); - EXPECT_EQ("NAN", fmt::format("{:F}", nan)); - EXPECT_EQ("nan ", fmt::format("{:<7}", nan)); - EXPECT_EQ(" nan ", fmt::format("{:^7}", nan)); - EXPECT_EQ(" nan", fmt::format("{:>7}", nan)); + EXPECT_EQ(fmt::format("{: }", nan), " nan"); + EXPECT_EQ(fmt::format("{:F}", nan), "NAN"); + EXPECT_EQ(fmt::format("{:<7}", nan), "nan "); + EXPECT_EQ(fmt::format("{:^7}", nan), " nan "); + EXPECT_EQ(fmt::format("{:>7}", nan), " nan"); } TEST(format_test, format_infinity) { double inf = std::numeric_limits::infinity(); - EXPECT_EQ("inf", fmt::format("{}", inf)); - EXPECT_EQ("+inf", fmt::format("{:+}", inf)); - EXPECT_EQ("-inf", fmt::format("{}", -inf)); - EXPECT_EQ(" +inf", fmt::format("{:+06}", inf)); - EXPECT_EQ(" -inf", fmt::format("{:+06}", -inf)); - EXPECT_EQ("+inf ", fmt::format("{:<+06}", inf)); - EXPECT_EQ(" +inf ", fmt::format("{:^+06}", inf)); - EXPECT_EQ(" +inf", fmt::format("{:>+06}", inf)); - EXPECT_EQ(" inf", fmt::format("{: }", inf)); - EXPECT_EQ("INF", fmt::format("{:F}", inf)); - EXPECT_EQ("inf ", fmt::format("{:<7}", inf)); - EXPECT_EQ(" inf ", fmt::format("{:^7}", inf)); - EXPECT_EQ(" inf", fmt::format("{:>7}", inf)); + EXPECT_EQ(fmt::format("{}", inf), "inf"); + EXPECT_EQ(fmt::format("{:+}", inf), "+inf"); + EXPECT_EQ(fmt::format("{}", -inf), "-inf"); + EXPECT_EQ(fmt::format("{:+06}", inf), " +inf"); + EXPECT_EQ(fmt::format("{:+06}", -inf), " -inf"); + EXPECT_EQ(fmt::format("{:<+06}", inf), "+inf "); + EXPECT_EQ(fmt::format("{:^+06}", inf), " +inf "); + EXPECT_EQ(fmt::format("{:>+06}", inf), " +inf"); + EXPECT_EQ(fmt::format("{: }", inf), " inf"); + EXPECT_EQ(fmt::format("{:F}", inf), "INF"); + EXPECT_EQ(fmt::format("{:<7}", inf), "inf "); + EXPECT_EQ(fmt::format("{:^7}", inf), " inf "); + EXPECT_EQ(fmt::format("{:>7}", inf), " inf"); } TEST(format_test, format_long_double) { - EXPECT_EQ("0", fmt::format("{0:}", 0.0l)); - EXPECT_EQ("0.000000", fmt::format("{0:f}", 0.0l)); - EXPECT_EQ("0.0", fmt::format("{:.1f}", 0.000000001l)); - EXPECT_EQ("0.10", fmt::format("{:.2f}", 0.099l)); - EXPECT_EQ("392.65", fmt::format("{0:}", 392.65l)); - EXPECT_EQ("392.65", fmt::format("{0:g}", 392.65l)); - EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l)); - EXPECT_EQ("392.650000", fmt::format("{0:f}", 392.65l)); - EXPECT_EQ("392.650000", fmt::format("{0:F}", 392.65l)); + EXPECT_EQ(fmt::format("{0:}", 0.0l), "0"); + EXPECT_EQ(fmt::format("{0:f}", 0.0l), "0.000000"); + EXPECT_EQ(fmt::format("{:.1f}", 0.000000001l), "0.0"); + EXPECT_EQ(fmt::format("{:.2f}", 0.099l), "0.10"); + EXPECT_EQ(fmt::format("{0:}", 392.65l), "392.65"); + EXPECT_EQ(fmt::format("{0:g}", 392.65l), "392.65"); + EXPECT_EQ(fmt::format("{0:G}", 392.65l), "392.65"); + EXPECT_EQ(fmt::format("{0:f}", 392.65l), "392.650000"); + EXPECT_EQ(fmt::format("{0:F}", 392.65l), "392.650000"); char buffer[buffer_size]; safe_sprintf(buffer, "%Le", 392.65l); EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65l)); - EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.64l)); + EXPECT_EQ(fmt::format("{0:+010.4g}", 392.64l), "+0000392.6"); auto ld = 3.31l; if (fmt::detail::is_double_double::value) { @@ -1504,8 +1486,8 @@ TEST(format_test, format_long_double) { TEST(format_test, format_char) { const char types[] = "cbBdoxX"; check_unknown_types('a', types, "char"); - EXPECT_EQ("a", fmt::format("{0}", 'a')); - EXPECT_EQ("z", fmt::format("{0:c}", 'z')); + EXPECT_EQ(fmt::format("{0}", 'a'), "a"); + EXPECT_EQ(fmt::format("{0:c}", 'z'), "z"); int n = 'x'; for (const char* type = types + 1; *type; ++type) { std::string format_str = fmt::format("{{:{}}}", *type); @@ -1515,27 +1497,27 @@ TEST(format_test, format_char) { } EXPECT_EQ(fmt::format("{:02X}", n), fmt::format("{:02X}", 'x')); - EXPECT_EQ("\n", fmt::format("{}", '\n')); - EXPECT_EQ("'\\n'", fmt::format("{:?}", '\n')); - EXPECT_EQ("ff", fmt::format("{:x}", '\xff')); + EXPECT_EQ(fmt::format("{}", '\n'), "\n"); + EXPECT_EQ(fmt::format("{:?}", '\n'), "'\\n'"); + EXPECT_EQ(fmt::format("{:x}", '\xff'), "ff"); } TEST(format_test, format_volatile_char) { volatile char c = 'x'; - EXPECT_EQ("x", fmt::format("{}", c)); + EXPECT_EQ(fmt::format("{}", c), "x"); } TEST(format_test, format_unsigned_char) { - EXPECT_EQ("42", fmt::format("{}", static_cast(42))); - EXPECT_EQ("42", fmt::format("{}", static_cast(42))); + EXPECT_EQ(fmt::format("{}", static_cast(42)), "42"); + EXPECT_EQ(fmt::format("{}", static_cast(42)), "42"); } TEST(format_test, format_cstring) { check_unknown_types("test", "sp", "string"); - EXPECT_EQ("test", fmt::format("{0}", "test")); - EXPECT_EQ("test", fmt::format("{0:s}", "test")); + EXPECT_EQ(fmt::format("{0}", "test"), "test"); + EXPECT_EQ(fmt::format("{0:s}", "test"), "test"); char nonconst[] = "nonconst"; - EXPECT_EQ("nonconst", fmt::format("{0}", nonconst)); + EXPECT_EQ(fmt::format("{0}", nonconst), "nonconst"); auto nullstr = static_cast(nullptr); EXPECT_THROW_MSG((void)fmt::format("{}", nullstr), format_error, "string pointer is null"); @@ -1547,9 +1529,9 @@ void function_pointer_test(int, double, std::string) {} TEST(format_test, format_pointer) { check_unknown_types(reinterpret_cast(0x1234), "p", "pointer"); - EXPECT_EQ("0x0", fmt::format("{0}", static_cast(nullptr))); - EXPECT_EQ("0x1234", fmt::format("{0}", reinterpret_cast(0x1234))); - EXPECT_EQ("0x1234", fmt::format("{0:p}", reinterpret_cast(0x1234))); + EXPECT_EQ(fmt::format("{0}", static_cast(nullptr)), "0x0"); + EXPECT_EQ(fmt::format("{0}", reinterpret_cast(0x1234)), "0x1234"); + EXPECT_EQ(fmt::format("{0:p}", reinterpret_cast(0x1234)), "0x1234"); // On CHERI (or other fat-pointer) systems, the size of a pointer is greater // than the size an integer that can hold a virtual address. There is no // portable address-as-an-integer type (yet) in C++, so we use `size_t` as @@ -1573,7 +1555,7 @@ TEST(format_test, format_pointer) { EXPECT_EQ(fmt::format("{}", fmt::detail::bit_cast( &function_pointer_test)), fmt::format("{}", fmt::ptr(function_pointer_test))); - EXPECT_EQ("0x0", fmt::format("{}", nullptr)); + EXPECT_EQ(fmt::format("{}", nullptr), "0x0"); } TEST(format_test, write_uintptr_fallback) { @@ -1611,9 +1593,9 @@ TEST(format_test, format_string) { } TEST(format_test, format_string_view) { - EXPECT_EQ("test", fmt::format("{}", string_view("test"))); - EXPECT_EQ("\"t\\nst\"", fmt::format("{:?}", string_view("t\nst"))); - EXPECT_EQ("", fmt::format("{}", string_view())); + EXPECT_EQ(fmt::format("{}", string_view("test")), "test"); + EXPECT_EQ(fmt::format("{:?}", string_view("t\nst")), "\"t\\nst\""); + EXPECT_EQ(fmt::format("{}", string_view()), ""); } #ifdef FMT_USE_STRING_VIEW @@ -1628,8 +1610,8 @@ template <> struct formatter : formatter { FMT_END_NAMESPACE TEST(format_test, format_std_string_view) { - EXPECT_EQ("test", fmt::format("{}", std::string_view("test"))); - EXPECT_EQ("foo", fmt::format("{}", string_viewable())); + EXPECT_EQ(fmt::format("{}", std::string_view("test")), "test"); + EXPECT_EQ(fmt::format("{}", string_viewable()), "foo"); } struct explicitly_convertible_to_std_string_view { @@ -1680,8 +1662,8 @@ FMT_END_NAMESPACE TEST(format_test, format_custom) { EXPECT_THROW_MSG((void)fmt::format(runtime("{:s}"), date(2012, 12, 9)), format_error, "unknown format specifier"); - EXPECT_EQ("42", fmt::format("{0}", Answer())); - EXPECT_EQ("0042", fmt::format("{:04}", Answer())); + EXPECT_EQ(fmt::format("{0}", Answer()), "42"); + EXPECT_EQ(fmt::format("{:04}", Answer()), "0042"); } TEST(format_test, format_to_custom) { @@ -1701,7 +1683,7 @@ TEST(format_test, format_examples) { std::string message = fmt::format("The answer is {}", 42); EXPECT_EQ("The answer is 42", message); - EXPECT_EQ("42", fmt::format("{}", 42)); + EXPECT_EQ(fmt::format("{}", 42), "42"); memory_buffer out; fmt::format_to(std::back_inserter(out), "The answer is {}.", 42); @@ -1723,17 +1705,17 @@ TEST(format_test, format_examples) { EXPECT_EQ("First, thou shalt count to three", fmt::format("First, thou shalt count to {0}", "three")); - EXPECT_EQ("Bring me a shrubbery", fmt::format("Bring me a {}", "shrubbery")); - EXPECT_EQ("From 1 to 3", fmt::format("From {} to {}", 1, 3)); + EXPECT_EQ(fmt::format("Bring me a {}", "shrubbery"), "Bring me a shrubbery"); + EXPECT_EQ(fmt::format("From {} to {}", 1, 3), "From 1 to 3"); char buffer[buffer_size]; safe_sprintf(buffer, "%03.2f", -1.2); EXPECT_EQ(buffer, fmt::format("{:03.2f}", -1.2)); - EXPECT_EQ("a, b, c", fmt::format("{0}, {1}, {2}", 'a', 'b', 'c')); - EXPECT_EQ("a, b, c", fmt::format("{}, {}, {}", 'a', 'b', 'c')); - EXPECT_EQ("c, b, a", fmt::format("{2}, {1}, {0}", 'a', 'b', 'c')); - EXPECT_EQ("abracadabra", fmt::format("{0}{1}{0}", "abra", "cad")); + EXPECT_EQ(fmt::format("{0}, {1}, {2}", 'a', 'b', 'c'), "a, b, c"); + EXPECT_EQ(fmt::format("{}, {}, {}", 'a', 'b', 'c'), "a, b, c"); + EXPECT_EQ(fmt::format("{2}, {1}, {0}", 'a', 'b', 'c'), "c, b, a"); + EXPECT_EQ(fmt::format("{0}{1}{0}", "abra", "cad"), "abracadabra"); EXPECT_EQ("left aligned ", fmt::format("{:<30}", "left aligned")); @@ -1744,16 +1726,16 @@ TEST(format_test, format_examples) { EXPECT_EQ("***********centered***********", fmt::format("{:*^30}", "centered")); - EXPECT_EQ("+3.140000; -3.140000", fmt::format("{:+f}; {:+f}", 3.14, -3.14)); - EXPECT_EQ(" 3.140000; -3.140000", fmt::format("{: f}; {: f}", 3.14, -3.14)); - EXPECT_EQ("3.140000; -3.140000", fmt::format("{:-f}; {:-f}", 3.14, -3.14)); + EXPECT_EQ(fmt::format("{:+f}; {:+f}", 3.14, -3.14), "+3.140000; -3.140000"); + EXPECT_EQ(fmt::format("{: f}; {: f}", 3.14, -3.14), " 3.140000; -3.140000"); + EXPECT_EQ(fmt::format("{:-f}; {:-f}", 3.14, -3.14), "3.140000; -3.140000"); EXPECT_EQ("int: 42; hex: 2a; oct: 52", fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}", 42)); EXPECT_EQ("int: 42; hex: 0x2a; oct: 052", fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}", 42)); - EXPECT_EQ("The answer is 42", fmt::format("The answer is {}", 42)); + EXPECT_EQ(fmt::format("The answer is {}", 42), "The answer is 42"); EXPECT_THROW_MSG( (void)fmt::format(runtime("The answer is {:d}"), "forty-two"), format_error, "invalid format specifier"); @@ -1772,7 +1754,7 @@ TEST(format_test, print) { } TEST(format_test, variadic) { - EXPECT_EQ("abc1", fmt::format("{}c{}", "ab", 1)); + EXPECT_EQ(fmt::format("{}c{}", "ab", 1), "abc1"); } TEST(format_test, bytes) { @@ -1817,23 +1799,23 @@ TEST(format_test, join) { v2.push_back(3.4f); void* v3[2] = {&v1[0], &v1[1]}; - EXPECT_EQ("(1, 2, 3)", fmt::format("({})", join(v1, v1 + 3, ", "))); - EXPECT_EQ("(1)", fmt::format("({})", join(v1, v1 + 1, ", "))); - EXPECT_EQ("()", fmt::format("({})", join(v1, v1, ", "))); - EXPECT_EQ("(001, 002, 003)", fmt::format("({:03})", join(v1, v1 + 3, ", "))); + EXPECT_EQ(fmt::format("({})", join(v1, v1 + 3, ", ")), "(1, 2, 3)"); + EXPECT_EQ(fmt::format("({})", join(v1, v1 + 1, ", ")), "(1)"); + EXPECT_EQ(fmt::format("({})", join(v1, v1, ", ")), "()"); + EXPECT_EQ(fmt::format("({:03})", join(v1, v1 + 3, ", ")), "(001, 002, 003)"); EXPECT_EQ("(+01.20, +03.40)", fmt::format("({:+06.2f})", join(v2.begin(), v2.end(), ", "))); - EXPECT_EQ("1, 2, 3", fmt::format("{0:{1}}", join(v1, v1 + 3, ", "), 1)); + EXPECT_EQ(fmt::format("{0:{1}}", join(v1, v1 + 3, ", "), 1), "1, 2, 3"); EXPECT_EQ(fmt::format("{}, {}", v3[0], v3[1]), fmt::format("{}", join(v3, v3 + 2, ", "))); - EXPECT_EQ("(1, 2, 3)", fmt::format("({})", join(v1, ", "))); - EXPECT_EQ("(+01.20, +03.40)", fmt::format("({:+06.2f})", join(v2, ", "))); + EXPECT_EQ(fmt::format("({})", join(v1, ", ")), "(1, 2, 3)"); + EXPECT_EQ(fmt::format("({:+06.2f})", join(v2, ", ")), "(+01.20, +03.40)"); auto v4 = std::vector{foo, bar, foo}; - EXPECT_EQ("0 1 0", fmt::format("{}", join(v4, " "))); + EXPECT_EQ(fmt::format("{}", join(v4, " ")), "0 1 0"); } #ifdef __cpp_lib_byte @@ -1880,45 +1862,45 @@ static constexpr const char static_with_null[3] = {'{', '}', '\0'}; static constexpr const char static_no_null[2] = {'{', '}'}; TEST(format_test, compile_time_string) { - EXPECT_EQ("foo", fmt::format(FMT_STRING("foo"))); - EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42)); + EXPECT_EQ(fmt::format(FMT_STRING("foo")), "foo"); + EXPECT_EQ(fmt::format(FMT_STRING("{}"), 42), "42"); #if FMT_USE_NONTYPE_TEMPLATE_ARGS using namespace fmt::literals; EXPECT_EQ("foobar", fmt::format(FMT_STRING("{foo}{bar}"), "bar"_a = "bar", "foo"_a = "foo")); - EXPECT_EQ("", fmt::format(FMT_STRING(""))); - EXPECT_EQ("", fmt::format(FMT_STRING(""), "arg"_a = 42)); - EXPECT_EQ("42", fmt::format(FMT_STRING("{answer}"), "answer"_a = Answer())); - EXPECT_EQ("1 2", fmt::format(FMT_STRING("{} {two}"), 1, "two"_a = 2)); + EXPECT_EQ(fmt::format(FMT_STRING("")), ""); + EXPECT_EQ(fmt::format(FMT_STRING(""), "arg"_a = 42), ""); + EXPECT_EQ(fmt::format(FMT_STRING("{answer}"), "answer"_a = Answer()), "42"); + EXPECT_EQ(fmt::format(FMT_STRING("{} {two}"), 1, "two"_a = 2), "1 2"); #endif (void)static_with_null; (void)static_no_null; #ifndef _MSC_VER - EXPECT_EQ("42", fmt::format(FMT_STRING(static_with_null), 42)); - EXPECT_EQ("42", fmt::format(FMT_STRING(static_no_null), 42)); + EXPECT_EQ(fmt::format(FMT_STRING(static_with_null), 42), "42"); + EXPECT_EQ(fmt::format(FMT_STRING(static_no_null), 42), "42"); #endif (void)with_null; (void)no_null; #if FMT_CPLUSPLUS >= 201703L - EXPECT_EQ("42", fmt::format(FMT_STRING(with_null), 42)); - EXPECT_EQ("42", fmt::format(FMT_STRING(no_null), 42)); + EXPECT_EQ(fmt::format(FMT_STRING(with_null), 42), "42"); + EXPECT_EQ(fmt::format(FMT_STRING(no_null), 42), "42"); #endif #if defined(FMT_USE_STRING_VIEW) && FMT_CPLUSPLUS >= 201703L - EXPECT_EQ("42", fmt::format(FMT_STRING(std::string_view("{}")), 42)); + EXPECT_EQ(fmt::format(FMT_STRING(std::string_view("{}")), 42), "42"); #endif } TEST(format_test, custom_format_compile_time_string) { - EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), Answer())); + EXPECT_EQ(fmt::format(FMT_STRING("{}"), Answer()), "42"); auto answer = Answer(); - EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), answer)); + EXPECT_EQ(fmt::format(FMT_STRING("{}"), answer), "42"); char buf[10] = {}; fmt::format_to(buf, FMT_STRING("{}"), answer); const Answer const_answer = Answer(); - EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), const_answer)); + EXPECT_EQ(fmt::format(FMT_STRING("{}"), const_answer), "42"); } #if FMT_USE_USER_DEFINED_LITERALS @@ -1931,11 +1913,11 @@ TEST(format_test, named_arg_udl) { fmt::arg("second", "cad"), fmt::arg("third", 99)), udl_a); - EXPECT_EQ("42", fmt::format("{answer}", "answer"_a = Answer())); + EXPECT_EQ(fmt::format("{answer}", "answer"_a = Answer()), "42"); } #endif // FMT_USE_USER_DEFINED_LITERALS -TEST(format_test, enum) { EXPECT_EQ("0", fmt::format("{}", foo)); } +TEST(format_test, enum) { EXPECT_EQ(fmt::format("{}", foo), "0"); } TEST(format_test, formatter_not_specialized) { static_assert(!fmt::has_formatter, @@ -1948,12 +1930,12 @@ enum big_enum : unsigned long long { big_enum_value = 5000000000ULL }; auto format_as(big_enum e) -> unsigned long long { return e; } TEST(format_test, strong_enum) { - EXPECT_EQ("5000000000", fmt::format("{}", big_enum_value)); + EXPECT_EQ(fmt::format("{}", big_enum_value), "5000000000"); } #endif TEST(format_test, non_null_terminated_format_string) { - EXPECT_EQ("42", fmt::format(string_view("{}foo", 2), 42)); + EXPECT_EQ(fmt::format(string_view("{}foo", 2), 42), "42"); } namespace adl_test { @@ -2086,16 +2068,16 @@ struct test_output_iterator { using pointer = void; using reference = void; - test_output_iterator& operator++() { + auto operator++() -> test_output_iterator& { ++data; return *this; } - test_output_iterator operator++(int) { + auto operator++(int) -> test_output_iterator { auto tmp = *this; ++data; return tmp; } - char& operator*() { return *data; } + auto operator*() -> char& { return *data; } }; TEST(format_test, format_to_n_output_iterator) { @@ -2110,13 +2092,13 @@ TEST(format_test, vformat_to) { auto args = fmt::make_format_args(n); auto s = std::string(); fmt::vformat_to(std::back_inserter(s), "{}", args); - EXPECT_EQ("42", s); + EXPECT_EQ(s, "42"); s.clear(); fmt::vformat_to(std::back_inserter(s), FMT_STRING("{}"), args); - EXPECT_EQ("42", s); + EXPECT_EQ(s, "42"); } -TEST(format_test, char_traits_is_not_ambiguous) { +TEST(format_test, char_traits_not_ambiguous) { // Test that we don't inject detail names into the std namespace. using namespace std; auto c = char_traits::char_type(); @@ -2187,7 +2169,7 @@ TEST(format_test, format_as_to_string) { EXPECT_EQ(fmt::to_string(test::struct_as_int()), "42"); } -template bool check_enabled_formatter() { +template auto check_enabled_formatter() -> bool { static_assert(std::is_default_constructible>::value, ""); return true; @@ -2213,21 +2195,20 @@ TEST(format_test, test_formatters_enabled) { TEST(format_int_test, data) { fmt::format_int format_int(42); - EXPECT_EQ("42", std::string(format_int.data(), format_int.size())); + EXPECT_EQ(std::string(format_int.data(), format_int.size()), "42"); } TEST(format_int_test, format_int) { - EXPECT_EQ("42", fmt::format_int(42).str()); - EXPECT_EQ(2u, fmt::format_int(42).size()); - EXPECT_EQ("-42", fmt::format_int(-42).str()); - EXPECT_EQ(3u, fmt::format_int(-42).size()); - EXPECT_EQ("42", fmt::format_int(42ul).str()); - EXPECT_EQ("-42", fmt::format_int(-42l).str()); - EXPECT_EQ("42", fmt::format_int(42ull).str()); - EXPECT_EQ("-42", fmt::format_int(-42ll).str()); - std::ostringstream os; - os << max_value(); - EXPECT_EQ(os.str(), fmt::format_int(max_value()).str()); + EXPECT_EQ(fmt::format_int(42).str(), "42"); + EXPECT_EQ(fmt::format_int(42).size(), 2u); + EXPECT_EQ(fmt::format_int(-42).str(), "-42"); + EXPECT_EQ(fmt::format_int(-42).size(), 3u); + EXPECT_EQ(fmt::format_int(42ul).str(), "42"); + EXPECT_EQ(fmt::format_int(-42l).str(), "-42"); + EXPECT_EQ(fmt::format_int(42ull).str(), "42"); + EXPECT_EQ(fmt::format_int(-42ll).str(), "-42");\ + EXPECT_EQ(fmt::format_int(max_value()).str(), + std::to_string(max_value())); } #ifndef FMT_STATIC_THOUSANDS_SEPARATOR @@ -2289,10 +2270,10 @@ TEST(format_test, format_named_arg_with_locale) { TEST(format_test, format_locale) { auto loc = std::locale({}, new fmt::format_facet(",")); - EXPECT_EQ("7,5bc,d15", fmt::format(loc, "{:Lx}", 123456789)); - EXPECT_EQ("-0b111,010,110,111,100,110,100,010,101", - fmt::format(loc, "{:#Lb}", -123456789)); - EXPECT_EQ(" 30,071", fmt::format(loc, "{:10Lo}", 12345)); + EXPECT_EQ(fmt::format(loc, "{:Lx}", 123456789), "7,5bc,d15"); + EXPECT_EQ(fmt::format(loc, "{:#Lb}", -123456789), + "-0b111,010,110,111,100,110,100,010,101"); + EXPECT_EQ(fmt::format(loc, "{:10Lo}", 12345), " 30,071"); } #endif // FMT_STATIC_THOUSANDS_SEPARATOR