]> git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/std-test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / fmt / test / std-test.cc
1 // Formatting library for C++ - tests of formatters for standard library types
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7
8 #include "fmt/std.h"
9 #include "fmt/ranges.h"
10
11 #include <string>
12 #include <vector>
13
14 #include "gtest/gtest.h"
15
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\"");
21
22 # ifdef _WIN32
23 // File.txt in Russian.
24 const wchar_t unicode_path[] = {0x424, 0x430, 0x439, 0x43b, 0x2e,
25 0x74, 0x78, 0x74, 0};
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',
29 't', '"', '\0'};
30 EXPECT_EQ(fmt::format("{}", std::filesystem::path(unicode_path)),
31 unicode_u8path);
32 # endif
33 #endif
34 }
35
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\"]");
43 #endif
44 }
45
46 TEST(std_test, thread_id) {
47 EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
48 }
49
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>;
54 V0 v0(42);
55 V0 v1(1.5f);
56 V0 v2("hello");
57 V0 v3('i');
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')");
62
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));
68 EXPECT_FALSE(
69 (fmt::is_formattable<std::variant<unformattable, unformattable>>::value));
70 EXPECT_TRUE((fmt::is_formattable<std::variant<int, float>>::value));
71
72 using V1 = std::variant<std::monostate, std::string, std::string>;
73 V1 v4{};
74 V1 v5{std::in_place_index<1>, "yes, this is variant"};
75
76 EXPECT_EQ(fmt::format("{}", v4), "variant(monostate)");
77 EXPECT_EQ(fmt::format("{}", v5), "variant(\"yes, this is variant\")");
78 #endif
79 }