]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/test/ranges-test.cc
1 // Formatting library for C++ - the core API
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
8 // Copyright (c) 2018 - present, Remotion (Igor Schulz)
10 // {fmt} support for ranges, containers and types tuple interface.
12 #include "fmt/ranges.h"
14 /// Check if 'if constexpr' is supported.
15 #if (__cplusplus > 201402L) || \
16 (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
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
);
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
);
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
));
42 TEST(RangesTest
, FormatPair
) {
43 std::pair
<int64_t, float> pa1
{42, 3.14159265358979f
};
44 EXPECT_EQ("(42, 3.14159)", fmt::format("{}", pa1
));
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
));
55 std::string str
; // can throw
56 template <std::size_t N
>
57 decltype(auto) get() const noexcept
{
60 else if constexpr (N
== 1)
61 return fmt::string_view
{str
};
65 template <std::size_t N
>
66 decltype(auto) get(const my_struct
& s
) noexcept
{
73 struct tuple_size
<my_struct
> : std::integral_constant
<std::size_t, 2> {};
75 template <std::size_t N
>
76 struct tuple_element
<N
, my_struct
> {
77 using type
= decltype(std::declval
<my_struct
>().get
<N
>());
82 TEST(RangesTest
, FormatStruct
) {
83 my_struct mst
{13, "my struct"};
84 EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst
));
87 #endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >
88 // 201402L && _MSC_VER >= 1910)