// 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. #include "fmt/ranges.h" #include "gtest/gtest.h" #include #include #include #include TEST(RangesTest, FormatVector) { std::vector 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> 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 simap{{"one", 1}, {"two", 2}}; EXPECT_EQ("{(one, 1), (two, 2)}", fmt::format("{}", simap)); } TEST(RangesTest, FormatPair) { std::pair pa1{42, 3.14159265358979f}; EXPECT_EQ("(42, 3.14159)", fmt::format("{}", pa1)); } TEST(RangesTest, FormatTuple) { std::tuple tu1{42, 3.14159265358979f, "this is tuple"}; EXPECT_EQ("(42, 3.14159, this is tuple)", fmt::format("{}", tu1)); } /// check if 'if constexpr' is supported. #if (__cplusplus > 201402L) || \ (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) struct my_struct { int32_t i; std::string str; // can throw template decltype(auto) get() const noexcept { if constexpr (N == 0) return i; else if constexpr (N == 1) return fmt::string_view{str}; } }; template decltype(auto) get(const my_struct& s) noexcept { return s.get(); } namespace std { template <> struct tuple_size : std::integral_constant {}; template struct tuple_element { using type = decltype(std::declval().get()); }; } // namespace std TEST(RangesTest, FormatStruct) { my_struct mst{13, "my struct"}; EXPECT_EQ("(13, my struct)", fmt::format("{}", mst)); } #endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG > // 201402L && _MSC_VER >= 1910)