]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/fmt/test/ranges-test.cc
import ceph 14.2.5
[ceph.git] / ceph / src / seastar / fmt / test / ranges-test.cc
CommitLineData
11fdf7f2
TL
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
11fdf7f2
TL
12/// Check if 'if constexpr' is supported.
13#if (__cplusplus > 201402L) || \
14 (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
15
eafe8130 16#include "fmt/ranges.h"
11fdf7f2
TL
17#include "gtest.h"
18
19#include <vector>
20#include <array>
21#include <map>
22#include <string>
23
24TEST(RangesTest, FormatVector) {
25 std::vector<int32_t> iv{1, 2, 3, 5, 7, 11};
26 auto ivf = fmt::format("{}", iv);
27 EXPECT_EQ("{1, 2, 3, 5, 7, 11}", ivf);
28}
29
30TEST(RangesTest, FormatVector2) {
31 std::vector<std::vector<int32_t>> ivv{{1, 2}, {3, 5}, {7, 11}};
32 auto ivf = fmt::format("{}", ivv);
33 EXPECT_EQ("{{1, 2}, {3, 5}, {7, 11}}", ivf);
34}
35
36TEST(RangesTest, FormatMap) {
37 std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
38 EXPECT_EQ("{(\"one\", 1), (\"two\", 2)}", fmt::format("{}", simap));
39}
40
41TEST(RangesTest, FormatPair) {
42 std::pair<int64_t, float> pa1{42, 3.14159265358979f};
43 EXPECT_EQ("(42, 3.14159)", fmt::format("{}", pa1));
44}
45
46TEST(RangesTest, FormatTuple) {
47 std::tuple<int64_t, float, std::string, char> tu1{42, 3.14159265358979f,
48 "this is tuple", 'i'};
49 EXPECT_EQ("(42, 3.14159, \"this is tuple\", 'i')", fmt::format("{}", tu1));
50}
51
52struct my_struct {
53 int32_t i;
54 std::string str; // can throw
55 template <std::size_t N>
56 decltype(auto) get() const noexcept {
57 if constexpr (N == 0)
58 return i;
59 else if constexpr (N == 1)
60 return fmt::string_view{str};
61 }
62};
63
64template <std::size_t N>
65decltype(auto) get(const my_struct& s) noexcept {
66 return s.get<N>();
67}
68
69namespace std {
70
71template <>
72struct tuple_size<my_struct> : std::integral_constant<std::size_t, 2> {};
73
74template <std::size_t N>
75struct tuple_element<N, my_struct> {
76 using type = decltype(std::declval<my_struct>().get<N>());
77};
78
79} // namespace std
80
81TEST(RangesTest, FormatStruct) {
82 my_struct mst{13, "my struct"};
83 EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst));
84}
85
86#endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >
87 // 201402L && _MSC_VER >= 1910)