]>
git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/std-test.cc
1 // Formatting library for C++ - tests of formatters for standard library types
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
9 #include "fmt/ranges.h"
14 #include "gtest/gtest.h"
16 TEST(std_test
, path
) {
17 #ifdef __cpp_lib_filesystem
18 EXPECT_EQ(fmt::format("{:8}", std::filesystem::path("foo")), "\"foo\" ");
19 EXPECT_EQ(fmt::format("{}", std::filesystem::path("foo\"bar.txt")),
20 "\"foo\\\"bar.txt\"");
23 // File.txt in Russian.
24 const wchar_t unicode_path
[] = {0x424, 0x430, 0x439, 0x43b, 0x2e,
26 const char unicode_u8path
[] = {'"', char(0xd0), char(0xa4), char(0xd0),
27 char(0xb0), char(0xd0), char(0xb9), char(0xd0),
28 char(0xbb), '.', 't', 'x',
30 EXPECT_EQ(fmt::format("{}", std::filesystem::path(unicode_path
)),
36 TEST(ranges_std_test
, format_vector_path
) {
37 // Test ambiguity problem described in #2954.
38 #ifdef __cpp_lib_filesystem
39 auto p
= std::filesystem::path("foo/bar.txt");
40 auto c
= std::vector
<std::string
>{"abc", "def"};
41 EXPECT_EQ(fmt::format("path={}, range={}", p
, c
),
42 "path=\"foo/bar.txt\", range=[\"abc\", \"def\"]");
46 TEST(std_test
, thread_id
) {
47 EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
50 TEST(std_test
, variant
) {
51 #ifdef __cpp_lib_variant
52 EXPECT_EQ(fmt::format("{}", std::monostate
{}), "monostate");
53 using V0
= std::variant
<int, float, std::string
, char>;
58 EXPECT_EQ(fmt::format("{}", v0
), "variant(42)");
59 EXPECT_EQ(fmt::format("{}", v1
), "variant(1.5)");
60 EXPECT_EQ(fmt::format("{}", v2
), "variant(\"hello\")");
61 EXPECT_EQ(fmt::format("{}", v3
), "variant('i')");
63 struct unformattable
{};
64 EXPECT_FALSE((fmt::is_formattable
<unformattable
>::value
));
65 EXPECT_FALSE((fmt::is_formattable
<std::variant
<unformattable
>>::value
));
66 EXPECT_FALSE((fmt::is_formattable
<std::variant
<unformattable
, int>>::value
));
67 EXPECT_FALSE((fmt::is_formattable
<std::variant
<int, unformattable
>>::value
));
69 (fmt::is_formattable
<std::variant
<unformattable
, unformattable
>>::value
));
70 EXPECT_TRUE((fmt::is_formattable
<std::variant
<int, float>>::value
));
72 using V1
= std::variant
<std::monostate
, std::string
, std::string
>;
74 V1 v5
{std::in_place_index
<1>, "yes, this is variant"};
76 EXPECT_EQ(fmt::format("{}", v4
), "variant(monostate)");
77 EXPECT_EQ(fmt::format("{}", v5
), "variant(\"yes, this is variant\")");