2018-05-10 14:11:00 +00:00
|
|
|
// Formatting library for C++ - the core API
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018 - present, Remotion (Igor Schulz)
|
|
|
|
// All Rights Reserved
|
|
|
|
// {fmt} support for ranges, containers and types tuple interface.
|
|
|
|
|
2018-07-04 20:17:03 +00:00
|
|
|
/// Check if 'if constexpr' is supported.
|
|
|
|
#if (__cplusplus > 201402L) || \
|
|
|
|
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
# include "fmt/ranges.h"
|
|
|
|
# include "gtest.h"
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
# include <array>
|
|
|
|
# include <map>
|
|
|
|
# include <string>
|
|
|
|
# include <vector>
|
2018-05-10 14:11:00 +00:00
|
|
|
|
|
|
|
TEST(RangesTest, FormatVector) {
|
|
|
|
std::vector<int32_t> iv{1, 2, 3, 5, 7, 11};
|
|
|
|
auto ivf = fmt::format("{}", iv);
|
|
|
|
EXPECT_EQ("{1, 2, 3, 5, 7, 11}", ivf);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangesTest, FormatVector2) {
|
|
|
|
std::vector<std::vector<int32_t>> ivv{{1, 2}, {3, 5}, {7, 11}};
|
|
|
|
auto ivf = fmt::format("{}", ivv);
|
|
|
|
EXPECT_EQ("{{1, 2}, {3, 5}, {7, 11}}", ivf);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangesTest, FormatMap) {
|
|
|
|
std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
|
2018-06-07 20:32:47 +00:00
|
|
|
EXPECT_EQ("{(\"one\", 1), (\"two\", 2)}", fmt::format("{}", simap));
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangesTest, FormatPair) {
|
2019-03-06 15:59:23 +00:00
|
|
|
std::pair<int64_t, float> pa1{42, 1.5f};
|
|
|
|
EXPECT_EQ("(42, 1.5)", fmt::format("{}", pa1));
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangesTest, FormatTuple) {
|
2019-04-07 17:05:49 +00:00
|
|
|
std::tuple<int64_t, float, std::string, char> tu1{42, 1.5f, "this is tuple",
|
|
|
|
'i'};
|
2019-03-06 15:59:23 +00:00
|
|
|
EXPECT_EQ("(42, 1.5, \"this is tuple\", 'i')", fmt::format("{}", tu1));
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct my_struct {
|
|
|
|
int32_t i;
|
|
|
|
std::string str; // can throw
|
2019-01-13 02:27:38 +00:00
|
|
|
template <std::size_t N> decltype(auto) get() const noexcept {
|
2018-05-10 14:11:00 +00:00
|
|
|
if constexpr (N == 0)
|
|
|
|
return i;
|
|
|
|
else if constexpr (N == 1)
|
|
|
|
return fmt::string_view{str};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
template <std::size_t N> decltype(auto) get(const my_struct& s) noexcept {
|
2018-05-10 14:11:00 +00:00
|
|
|
return s.get<N>();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct tuple_size<my_struct> : std::integral_constant<std::size_t, 2> {};
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
template <std::size_t N> struct tuple_element<N, my_struct> {
|
2018-05-10 14:11:00 +00:00
|
|
|
using type = decltype(std::declval<my_struct>().get<N>());
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
TEST(RangesTest, FormatStruct) {
|
|
|
|
my_struct mst{13, "my struct"};
|
2018-06-07 20:32:47 +00:00
|
|
|
EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst));
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:59:23 +00:00
|
|
|
TEST(RangesTest, FormatTo) {
|
|
|
|
char buf[10];
|
|
|
|
auto end = fmt::format_to(buf, "{}", std::vector{1, 2, 3});
|
|
|
|
*end = '\0';
|
|
|
|
EXPECT_STREQ(buf, "{1, 2, 3}");
|
|
|
|
}
|
|
|
|
|
2019-08-21 18:22:59 +00:00
|
|
|
struct path_like {
|
|
|
|
const path_like* begin() const;
|
|
|
|
const path_like* end() const;
|
|
|
|
|
|
|
|
operator std::string() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(RangesTest, PathLike) {
|
|
|
|
EXPECT_FALSE((fmt::is_range<path_like, char>::value));
|
|
|
|
}
|
|
|
|
|
2018-05-10 14:11:00 +00:00
|
|
|
#endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >
|
|
|
|
// 201402L && _MSC_VER >= 1910)
|