]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/test/ranges-test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / fmt / test / ranges-test.cc
1 // Formatting library for C++ - the core API
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 //
8 // Copyright (c) 2018 - present, Remotion (Igor Schulz)
9 // All Rights Reserved
10 // {fmt} support for ranges, containers and types tuple interface.
11
12 #include "fmt/ranges.h"
13
14 /// Check if 'if constexpr' is supported.
15 #if (__cplusplus > 201402L) || \
16 (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
17
18 #include "gtest.h"
19
20 #include <vector>
21 #include <array>
22 #include <map>
23 #include <string>
24
25 TEST(RangesTest, FormatVector) {
26 std::vector<int32_t> iv{1, 2, 3, 5, 7, 11};
27 auto ivf = fmt::format("{}", iv);
28 EXPECT_EQ("{1, 2, 3, 5, 7, 11}", ivf);
29 }
30
31 TEST(RangesTest, FormatVector2) {
32 std::vector<std::vector<int32_t>> ivv{{1, 2}, {3, 5}, {7, 11}};
33 auto ivf = fmt::format("{}", ivv);
34 EXPECT_EQ("{{1, 2}, {3, 5}, {7, 11}}", ivf);
35 }
36
37 TEST(RangesTest, FormatMap) {
38 std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
39 EXPECT_EQ("{(\"one\", 1), (\"two\", 2)}", fmt::format("{}", simap));
40 }
41
42 TEST(RangesTest, FormatPair) {
43 std::pair<int64_t, float> pa1{42, 3.14159265358979f};
44 EXPECT_EQ("(42, 3.14159)", fmt::format("{}", pa1));
45 }
46
47 TEST(RangesTest, FormatTuple) {
48 std::tuple<int64_t, float, std::string, char> tu1{42, 3.14159265358979f,
49 "this is tuple", 'i'};
50 EXPECT_EQ("(42, 3.14159, \"this is tuple\", 'i')", fmt::format("{}", tu1));
51 }
52
53 struct my_struct {
54 int32_t i;
55 std::string str; // can throw
56 template <std::size_t N>
57 decltype(auto) get() const noexcept {
58 if constexpr (N == 0)
59 return i;
60 else if constexpr (N == 1)
61 return fmt::string_view{str};
62 }
63 };
64
65 template <std::size_t N>
66 decltype(auto) get(const my_struct& s) noexcept {
67 return s.get<N>();
68 }
69
70 namespace std {
71
72 template <>
73 struct tuple_size<my_struct> : std::integral_constant<std::size_t, 2> {};
74
75 template <std::size_t N>
76 struct tuple_element<N, my_struct> {
77 using type = decltype(std::declval<my_struct>().get<N>());
78 };
79
80 } // namespace std
81
82 TEST(RangesTest, FormatStruct) {
83 my_struct mst{13, "my struct"};
84 EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst));
85 }
86
87 #endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >
88 // 201402L && _MSC_VER >= 1910)