2023-09-04 16:09:01 +00:00
|
|
|
// Formatting library for C++ - ranges tests
|
2018-05-10 14:11:00 +00:00
|
|
|
//
|
2023-09-04 16:09:01 +00:00
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors
|
2018-05-10 14:11:00 +00:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
|
2019-09-28 16:13:32 +00:00
|
|
|
#include "fmt/ranges.h"
|
2020-03-14 14:41:08 +00:00
|
|
|
|
2023-12-23 17:11:50 +00:00
|
|
|
#include <list>
|
2021-01-30 15:42:58 +00:00
|
|
|
#include <map>
|
2023-12-23 17:11:50 +00:00
|
|
|
#include <numeric>
|
2023-02-10 17:45:27 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <stack>
|
2021-01-30 15:42:58 +00:00
|
|
|
#include <string>
|
2023-07-31 16:26:33 +00:00
|
|
|
#include <utility>
|
2021-01-30 15:42:58 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-12-23 17:11:50 +00:00
|
|
|
#if FMT_HAS_INCLUDE(<ranges>)
|
|
|
|
# include <ranges>
|
|
|
|
#endif
|
|
|
|
|
2024-01-13 17:44:59 +00:00
|
|
|
#include "fmt/format.h"
|
2021-04-28 22:59:43 +00:00
|
|
|
#include "gtest/gtest.h"
|
2019-09-28 16:13:32 +00:00
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601
|
|
|
|
# define FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
|
|
|
#endif
|
2018-07-04 20:17:03 +00:00
|
|
|
|
2022-05-29 22:01:43 +00:00
|
|
|
#if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1910
|
2021-01-30 15:42:58 +00:00
|
|
|
# define FMT_RANGES_TEST_ENABLE_JOIN
|
|
|
|
# define FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
|
|
|
#endif
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#ifdef FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_array) {
|
|
|
|
int arr[] = {1, 2, 3, 5, 7, 11};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", arr), "[1, 2, 3, 5, 7, 11]");
|
2020-10-24 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 04:04:21 +00:00
|
|
|
TEST(ranges_test, format_2d_array) {
|
2021-05-04 23:54:20 +00:00
|
|
|
int arr[][2] = {{1, 2}, {3, 5}, {7, 11}};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", arr), "[[1, 2], [3, 5], [7, 11]]");
|
2020-10-24 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_array_of_literals) {
|
|
|
|
const char* arr[] = {"1234", "abcd"};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", arr), "[\"1234\", \"abcd\"]");
|
2022-07-29 20:55:16 +00:00
|
|
|
EXPECT_EQ(fmt::format("{:n}", arr), "\"1234\", \"abcd\"");
|
|
|
|
EXPECT_EQ(fmt::format("{:n:}", arr), "1234, abcd");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
#endif // FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
|
|
|
|
|
2021-05-04 23:54:20 +00:00
|
|
|
TEST(ranges_test, format_vector) {
|
2021-05-05 03:53:56 +00:00
|
|
|
auto v = std::vector<int>{1, 2, 3, 5, 7, 11};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", v), "[1, 2, 3, 5, 7, 11]");
|
2022-01-08 17:48:26 +00:00
|
|
|
EXPECT_EQ(fmt::format("{::#x}", v), "[0x1, 0x2, 0x3, 0x5, 0x7, 0xb]");
|
2022-07-12 17:08:38 +00:00
|
|
|
EXPECT_EQ(fmt::format("{:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb");
|
2022-11-02 18:04:54 +00:00
|
|
|
|
|
|
|
auto vc = std::vector<char>{'a', 'b', 'c'};
|
2024-03-07 18:51:46 +00:00
|
|
|
auto vec = std::vector<char>{'a', '\n', '\t'};
|
2022-11-02 18:04:54 +00:00
|
|
|
auto vvc = std::vector<std::vector<char>>{vc, vc};
|
|
|
|
EXPECT_EQ(fmt::format("{}", vc), "['a', 'b', 'c']");
|
2024-03-07 18:51:46 +00:00
|
|
|
EXPECT_EQ(fmt::format("{:s}", vc), "\"abc\"");
|
|
|
|
EXPECT_EQ(fmt::format("{:?s}", vec), "\"a\\n\\t\"");
|
|
|
|
EXPECT_EQ(fmt::format("{:s}", vec), "\"a\n\t\"");
|
|
|
|
EXPECT_EQ(fmt::format("{::s}", vvc), "[\"abc\", \"abc\"]");
|
2022-11-02 18:04:54 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vvc), "[['a', 'b', 'c'], ['a', 'b', 'c']]");
|
|
|
|
EXPECT_EQ(fmt::format("{:n}", vvc), "['a', 'b', 'c'], ['a', 'b', 'c']");
|
|
|
|
EXPECT_EQ(fmt::format("{:n:n}", vvc), "'a', 'b', 'c', 'a', 'b', 'c'");
|
|
|
|
EXPECT_EQ(fmt::format("{:n:n:}", vvc), "a, b, c, a, b, c");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 15:04:28 +00:00
|
|
|
TEST(ranges_test, format_nested_vector) {
|
2021-05-05 03:53:56 +00:00
|
|
|
auto v = std::vector<std::vector<int>>{{1, 2}, {3, 5}, {7, 11}};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", v), "[[1, 2], [3, 5], [7, 11]]");
|
2022-01-08 17:48:26 +00:00
|
|
|
EXPECT_EQ(fmt::format("{:::#x}", v), "[[0x1, 0x2], [0x3, 0x5], [0x7, 0xb]]");
|
2022-07-12 17:08:38 +00:00
|
|
|
EXPECT_EQ(fmt::format("{:n:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 15:04:28 +00:00
|
|
|
TEST(ranges_test, to_string_vector) {
|
|
|
|
auto v = std::vector<std::string>{"a", "b", "c"};
|
|
|
|
EXPECT_EQ(fmt::to_string(v), "[\"a\", \"b\", \"c\"]");
|
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_map) {
|
|
|
|
auto m = std::map<std::string, int>{{"one", 1}, {"two", 2}};
|
2021-12-18 15:12:53 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", m), "{\"one\": 1, \"two\": 2}");
|
2022-07-29 20:55:16 +00:00
|
|
|
EXPECT_EQ(fmt::format("{:n}", m), "\"one\": 1, \"two\": 2");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 15:35:40 +00:00
|
|
|
TEST(ranges_test, format_set) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::set<std::string>{"one", "two"}),
|
|
|
|
"{\"one\", \"two\"}");
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:26:33 +00:00
|
|
|
// Models std::flat_set close enough to test if no ambiguous lookup of a
|
|
|
|
// formatter happens due to the flat_set type matching is_set and
|
2023-09-04 16:19:40 +00:00
|
|
|
// is_container_adaptor_like.
|
2023-07-31 16:26:33 +00:00
|
|
|
template <typename T> class flat_set {
|
|
|
|
public:
|
|
|
|
using key_type = T;
|
|
|
|
using container_type = std::vector<T>;
|
|
|
|
|
|
|
|
using iterator = typename std::vector<T>::iterator;
|
|
|
|
using const_iterator = typename std::vector<T>::const_iterator;
|
|
|
|
|
|
|
|
template <typename... Ts>
|
|
|
|
explicit flat_set(Ts&&... args) : c{std::forward<Ts>(args)...} {}
|
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto begin() -> iterator { return c.begin(); }
|
|
|
|
auto end() -> iterator { return c.end(); }
|
2023-07-31 16:26:33 +00:00
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto begin() const -> const_iterator { return c.begin(); }
|
|
|
|
auto end() const -> const_iterator { return c.end(); }
|
2023-07-31 16:26:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> c;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ranges_test, format_flat_set) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", flat_set<std::string>{"one", "two"}),
|
|
|
|
"{\"one\", \"two\"}");
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:36:39 +00:00
|
|
|
namespace adl {
|
|
|
|
struct box {
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2022-05-22 16:08:26 +00:00
|
|
|
auto begin(const box& b) -> const int* { return &b.value; }
|
|
|
|
auto end(const box& b) -> const int* { return &b.value + 1; }
|
2022-03-11 19:36:39 +00:00
|
|
|
} // namespace adl
|
|
|
|
|
|
|
|
TEST(ranges_test, format_adl_begin_end) {
|
|
|
|
auto b = adl::box{42};
|
|
|
|
EXPECT_EQ(fmt::format("{}", b), "[42]");
|
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_pair) {
|
|
|
|
auto p = std::pair<int, float>(42, 1.5f);
|
|
|
|
EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 15:25:58 +00:00
|
|
|
struct unformattable {};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_tuple) {
|
|
|
|
auto t =
|
|
|
|
std::tuple<int, float, std::string, char>(42, 1.5f, "this is tuple", 'i');
|
|
|
|
EXPECT_EQ(fmt::format("{}", t), "(42, 1.5, \"this is tuple\", 'i')");
|
2023-02-19 17:24:29 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", std::tuple<>()), "()");
|
2022-06-19 15:25:58 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE((fmt::is_formattable<std::tuple<>>::value));
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::tuple<unformattable>>::value));
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::tuple<unformattable, int>>::value));
|
|
|
|
EXPECT_FALSE((fmt::is_formattable<std::tuple<int, unformattable>>::value));
|
|
|
|
EXPECT_FALSE(
|
|
|
|
(fmt::is_formattable<std::tuple<unformattable, unformattable>>::value));
|
|
|
|
EXPECT_TRUE((fmt::is_formattable<std::tuple<int, float>>::value));
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 17:24:29 +00:00
|
|
|
struct not_default_formattable {};
|
|
|
|
struct bad_format {};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<not_default_formattable> {
|
|
|
|
auto parse(format_parse_context&) -> const char* { throw bad_format(); }
|
|
|
|
auto format(not_default_formattable, format_context& ctx)
|
|
|
|
-> format_context::iterator {
|
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
|
|
|
TEST(ranges_test, tuple_parse_calls_element_parse) {
|
|
|
|
auto f = fmt::formatter<std::tuple<not_default_formattable>>();
|
|
|
|
auto ctx = fmt::format_parse_context("");
|
|
|
|
EXPECT_THROW(f.parse(ctx), bad_format);
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#ifdef FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
2021-05-05 03:53:56 +00:00
|
|
|
struct tuple_like {
|
|
|
|
int i;
|
|
|
|
std::string str;
|
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
template <size_t N>
|
|
|
|
auto get() const noexcept -> fmt::enable_if_t<N == 0, int> {
|
2021-01-30 15:42:58 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
template <size_t N>
|
2023-09-04 16:19:40 +00:00
|
|
|
auto get() const noexcept -> fmt::enable_if_t<N == 1, fmt::string_view> {
|
2021-05-05 03:53:56 +00:00
|
|
|
return str;
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
template <size_t N>
|
2021-05-05 03:53:56 +00:00
|
|
|
auto get(const tuple_like& t) noexcept -> decltype(t.get<N>()) {
|
|
|
|
return t.get<N>();
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 04:04:21 +00:00
|
|
|
namespace std {
|
2021-05-05 03:53:56 +00:00
|
|
|
template <>
|
2021-05-05 04:04:21 +00:00
|
|
|
struct tuple_size<tuple_like> : std::integral_constant<size_t, 2> {};
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-05-05 04:04:21 +00:00
|
|
|
template <size_t N> struct tuple_element<N, tuple_like> {
|
2021-05-05 03:53:56 +00:00
|
|
|
using type = decltype(std::declval<tuple_like>().get<N>());
|
2018-05-10 14:11:00 +00:00
|
|
|
};
|
2021-05-05 04:04:21 +00:00
|
|
|
} // namespace std
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_struct) {
|
|
|
|
auto t = tuple_like{42, "foo"};
|
|
|
|
EXPECT_EQ(fmt::format("{}", t), "(42, \"foo\")");
|
2018-05-10 14:11:00 +00:00
|
|
|
}
|
2021-01-30 15:42:58 +00:00
|
|
|
#endif // FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
|
2018-05-10 14:11:00 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, format_to) {
|
2019-03-06 15:59:23 +00:00
|
|
|
char buf[10];
|
2021-01-30 15:42:58 +00:00
|
|
|
auto end = fmt::format_to(buf, "{}", std::vector<int>{1, 2, 3});
|
2019-03-06 15:59:23 +00:00
|
|
|
*end = '\0';
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_STREQ(buf, "[1, 2, 3]");
|
2019-03-06 15:59:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 16:03:00 +00:00
|
|
|
template <typename Char> struct path_like {
|
2023-09-04 16:19:40 +00:00
|
|
|
auto begin() const -> const path_like*;
|
|
|
|
auto end() const -> const path_like*;
|
2019-08-21 18:22:59 +00:00
|
|
|
|
2023-03-04 16:03:00 +00:00
|
|
|
operator std::basic_string<Char>() const;
|
2019-08-21 18:22:59 +00:00
|
|
|
};
|
|
|
|
|
2023-03-04 16:03:00 +00:00
|
|
|
TEST(ranges_test, disabled_range_formatting_of_path) {
|
|
|
|
// Range formatting of path is disabled because of infinite recursion
|
|
|
|
// (path element is itself a path).
|
|
|
|
EXPECT_EQ((fmt::range_format_kind<path_like<char>, char>::value),
|
|
|
|
fmt::range_format::disabled);
|
|
|
|
EXPECT_EQ((fmt::range_format_kind<path_like<wchar_t>, char>::value),
|
|
|
|
fmt::range_format::disabled);
|
2019-08-21 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 17:24:29 +00:00
|
|
|
// A range that provides non-const only begin()/end() to test fmt::join
|
|
|
|
// handles that.
|
2020-07-21 19:13:00 +00:00
|
|
|
//
|
2021-05-05 03:53:56 +00:00
|
|
|
// Some ranges (e.g. those produced by range-v3's views::filter()) can cache
|
2020-07-21 19:13:00 +00:00
|
|
|
// information during iteration so they only provide non-const begin()/end().
|
|
|
|
template <typename T> class non_const_only_range {
|
|
|
|
private:
|
|
|
|
std::vector<T> vec;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using const_iterator = typename ::std::vector<T>::const_iterator;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
explicit non_const_only_range(Args&&... args)
|
2021-05-05 03:53:56 +00:00
|
|
|
: vec(std::forward<Args>(args)...) {}
|
2020-07-21 19:13:00 +00:00
|
|
|
|
2023-12-23 17:11:50 +00:00
|
|
|
auto begin() -> const_iterator { return vec.begin(); }
|
2023-09-04 16:19:40 +00:00
|
|
|
auto end() -> const_iterator { return vec.end(); }
|
2020-07-21 19:13:00 +00:00
|
|
|
};
|
|
|
|
|
2020-10-24 09:25:29 +00:00
|
|
|
template <typename T> class noncopyable_range {
|
|
|
|
private:
|
|
|
|
std::vector<T> vec;
|
|
|
|
|
|
|
|
public:
|
2023-01-27 20:29:57 +00:00
|
|
|
using iterator = typename ::std::vector<T>::iterator;
|
2020-10-24 09:25:29 +00:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
explicit noncopyable_range(Args&&... args)
|
2021-05-05 03:53:56 +00:00
|
|
|
: vec(std::forward<Args>(args)...) {}
|
2020-10-24 09:25:29 +00:00
|
|
|
|
|
|
|
noncopyable_range(noncopyable_range const&) = delete;
|
|
|
|
noncopyable_range(noncopyable_range&) = delete;
|
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto begin() -> iterator { return vec.begin(); }
|
|
|
|
auto end() -> iterator { return vec.end(); }
|
2020-10-24 09:25:29 +00:00
|
|
|
};
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, range) {
|
2023-09-04 17:07:14 +00:00
|
|
|
auto&& w = noncopyable_range<int>(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", w), "[0, 0, 0]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", noncopyable_range<int>(3u, 0)), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto x = non_const_only_range<int>(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", x), "[0, 0, 0]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", non_const_only_range<int>(3u, 0)), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
auto y = std::vector<int>(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", y), "[0, 0, 0]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::vector<int>(3u, 0)), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
const auto z = std::vector<int>(3u, 0);
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", z), "[0, 0, 0]");
|
2020-10-24 09:25:29 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 00:21:21 +00:00
|
|
|
enum test_enum { foo, bar };
|
2022-07-03 15:05:47 +00:00
|
|
|
auto format_as(test_enum e) -> int { return e; }
|
2021-08-08 16:36:56 +00:00
|
|
|
|
|
|
|
TEST(ranges_test, enum_range) {
|
|
|
|
auto v = std::vector<test_enum>{test_enum::foo};
|
|
|
|
EXPECT_EQ(fmt::format("{}", v), "[0]");
|
|
|
|
}
|
|
|
|
|
2022-05-29 22:01:43 +00:00
|
|
|
#if !FMT_MSC_VERSION
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, unformattable_range) {
|
2020-09-20 14:53:13 +00:00
|
|
|
EXPECT_FALSE((fmt::has_formatter<std::vector<unformattable>,
|
|
|
|
fmt::format_context>::value));
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2024-01-02 00:21:21 +00:00
|
|
|
TEST(ranges_test, join) {
|
|
|
|
using fmt::join;
|
|
|
|
int v1[3] = {1, 2, 3};
|
|
|
|
auto v2 = std::vector<float>();
|
|
|
|
v2.push_back(1.2f);
|
|
|
|
v2.push_back(3.4f);
|
|
|
|
void* v3[2] = {&v1[0], &v1[1]};
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("({})", join(v1, v1 + 3, ", ")), "(1, 2, 3)");
|
|
|
|
EXPECT_EQ(fmt::format("({})", join(v1, v1 + 1, ", ")), "(1)");
|
|
|
|
EXPECT_EQ(fmt::format("({})", join(v1, v1, ", ")), "()");
|
|
|
|
EXPECT_EQ(fmt::format("({:03})", join(v1, v1 + 3, ", ")), "(001, 002, 003)");
|
|
|
|
EXPECT_EQ("(+01.20, +03.40)",
|
|
|
|
fmt::format("({:+06.2f})", join(v2.begin(), v2.end(), ", ")));
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{0:{1}}", join(v1, v1 + 3, ", "), 1), "1, 2, 3");
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}, {}", v3[0], v3[1]),
|
|
|
|
fmt::format("{}", join(v3, v3 + 2, ", ")));
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("({})", join(v1, ", ")), "(1, 2, 3)");
|
|
|
|
EXPECT_EQ(fmt::format("({:+06.2f})", join(v2, ", ")), "(+01.20, +03.40)");
|
|
|
|
|
|
|
|
auto v4 = std::vector<test_enum>{foo, bar, foo};
|
|
|
|
EXPECT_EQ(fmt::format("{}", join(v4, " ")), "0 1 0");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cpp_lib_byte
|
|
|
|
TEST(ranges_test, join_bytes) {
|
|
|
|
auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(v, ", ")), "1, 2, 3");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-30 15:42:58 +00:00
|
|
|
#ifdef FMT_RANGES_TEST_ENABLE_JOIN
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_tuple) {
|
|
|
|
// Value tuple args.
|
|
|
|
auto t1 = std::tuple<char, int, float>('a', 1, 2.0f);
|
|
|
|
EXPECT_EQ(fmt::format("({})", fmt::join(t1, ", ")), "(a, 1, 2)");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
// Testing lvalue tuple args.
|
2021-01-30 15:42:58 +00:00
|
|
|
int x = 4;
|
2021-05-05 03:53:56 +00:00
|
|
|
auto t2 = std::tuple<char, int&>('b', x);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(t2, " + ")), "b + 4");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
// Empty tuple.
|
|
|
|
auto t3 = std::tuple<>();
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(t3, "|")), "");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
// Single element tuple.
|
|
|
|
auto t4 = std::tuple<float>(4.0f);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(t4, "/")), "4");
|
2021-08-23 16:51:19 +00:00
|
|
|
|
2021-09-05 14:34:06 +00:00
|
|
|
# if FMT_TUPLE_JOIN_SPECIFIERS
|
2021-08-23 16:51:19 +00:00
|
|
|
// Specs applied to each element.
|
|
|
|
auto t5 = std::tuple<int, int, long>(-3, 100, 1);
|
|
|
|
EXPECT_EQ(fmt::format("{:+03}", fmt::join(t5, ", ")), "-03, +100, +01");
|
|
|
|
|
|
|
|
auto t6 = std::tuple<float, double, long double>(3, 3.14, 3.1415);
|
|
|
|
EXPECT_EQ(fmt::format("{:5.5f}", fmt::join(t6, ", ")),
|
|
|
|
"3.00000, 3.14000, 3.14150");
|
|
|
|
|
|
|
|
// Testing lvalue tuple args.
|
|
|
|
int y = -1;
|
|
|
|
auto t7 = std::tuple<int, int&, const int&>(3, y, y);
|
|
|
|
EXPECT_EQ(fmt::format("{:03}", fmt::join(t7, ", ")), "003, -01, -01");
|
2021-09-05 14:34:06 +00:00
|
|
|
# endif
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_initializer_list) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join({1, 2, 3}, ", ")), "1, 2, 3");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join({"fmt", "rocks", "!"}, " ")),
|
|
|
|
"fmt rocks !");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct zstring_sentinel {};
|
|
|
|
|
|
|
|
bool operator==(const char* p, zstring_sentinel) { return *p == '\0'; }
|
|
|
|
bool operator!=(const char* p, zstring_sentinel) { return *p != '\0'; }
|
|
|
|
|
|
|
|
struct zstring {
|
|
|
|
const char* p;
|
2023-09-04 16:19:40 +00:00
|
|
|
auto begin() const -> const char* { return p; }
|
|
|
|
auto end() const -> zstring_sentinel { return {}; }
|
2021-01-30 15:42:58 +00:00
|
|
|
};
|
|
|
|
|
2021-10-16 17:02:03 +00:00
|
|
|
# ifdef __cpp_lib_ranges
|
|
|
|
struct cpp20_only_range {
|
|
|
|
struct iterator {
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
using value_type = int;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using iterator_concept = std::input_iterator_tag;
|
|
|
|
|
|
|
|
iterator() = default;
|
|
|
|
iterator(int i) : val(i) {}
|
2023-09-04 16:19:40 +00:00
|
|
|
auto operator*() const -> int { return val; }
|
2023-12-23 17:11:50 +00:00
|
|
|
auto operator++() -> iterator& {
|
2021-10-16 17:02:03 +00:00
|
|
|
++val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void operator++(int) { ++*this; }
|
2023-09-04 16:19:40 +00:00
|
|
|
auto operator==(const iterator& rhs) const -> bool {
|
|
|
|
return val == rhs.val;
|
|
|
|
}
|
2021-10-16 17:02:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int lo;
|
|
|
|
int hi;
|
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto begin() const -> iterator { return iterator(lo); }
|
|
|
|
auto end() const -> iterator { return iterator(hi); }
|
2021-10-16 17:02:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(std::input_iterator<cpp20_only_range::iterator>);
|
|
|
|
# endif
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_sentinel) {
|
|
|
|
auto hello = zstring{"hello"};
|
2021-05-05 04:04:21 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", hello), "['h', 'e', 'l', 'l', 'o']");
|
2022-01-08 17:48:26 +00:00
|
|
|
EXPECT_EQ(fmt::format("{::}", hello), "[h, e, l, l, o]");
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(hello, "_")), "h_e_l_l_o");
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
TEST(ranges_test, join_range) {
|
2023-09-04 17:07:14 +00:00
|
|
|
auto&& w = noncopyable_range<int>(3u, 0);
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(w, ",")), "0,0,0");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(noncopyable_range<int>(3u, 0), ",")),
|
|
|
|
"0,0,0");
|
2021-01-30 15:42:58 +00:00
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto x = non_const_only_range<int>(3u, 0);
|
2021-05-05 03:53:56 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(x, ",")), "0,0,0");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(non_const_only_range<int>(3u, 0), ",")),
|
|
|
|
"0,0,0");
|
|
|
|
|
|
|
|
auto y = std::vector<int>(3u, 0);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(y, ",")), "0,0,0");
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(std::vector<int>(3u, 0), ",")),
|
|
|
|
"0,0,0");
|
|
|
|
|
|
|
|
const auto z = std::vector<int>(3u, 0);
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(z, ",")), "0,0,0");
|
2021-10-16 17:02:03 +00:00
|
|
|
|
|
|
|
# ifdef __cpp_lib_ranges
|
|
|
|
EXPECT_EQ(fmt::format("{}", cpp20_only_range{.lo = 0, .hi = 5}),
|
|
|
|
"[0, 1, 2, 3, 4]");
|
|
|
|
EXPECT_EQ(
|
|
|
|
fmt::format("{}", fmt::join(cpp20_only_range{.lo = 0, .hi = 5}, ",")),
|
|
|
|
"0,1,2,3,4");
|
|
|
|
# endif
|
2021-01-30 15:42:58 +00:00
|
|
|
}
|
2024-01-22 15:39:33 +00:00
|
|
|
|
|
|
|
namespace adl {
|
|
|
|
struct vec : std::vector<int> {
|
|
|
|
using std::vector<int>::vector; // inherit all constructors
|
|
|
|
};
|
|
|
|
|
|
|
|
// ADL-found begin() and end() skip the first and last element
|
|
|
|
auto begin(vec& v) -> typename vec::iterator { return v.begin() + 1; }
|
|
|
|
auto end(vec& v) -> typename vec::iterator { return v.end() - 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ranges_test, format_join_adl_begin_end) {
|
|
|
|
auto v = adl::vec{41, 42, 43, 44};
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::join(v, "/")), "42/43");
|
|
|
|
}
|
|
|
|
|
2021-05-05 03:53:56 +00:00
|
|
|
#endif // FMT_RANGES_TEST_ENABLE_JOIN
|
2021-08-19 21:12:26 +00:00
|
|
|
|
2023-12-23 18:02:42 +00:00
|
|
|
#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 202302L
|
|
|
|
TEST(ranges_test, nested_ranges) {
|
|
|
|
auto l = std::list{1, 2, 3};
|
|
|
|
auto r = std::views::iota(0, 3) | std::views::transform([&l](auto i) {
|
|
|
|
return std::views::take(std::ranges::subrange(l), i);
|
|
|
|
}) |
|
|
|
|
std::views::transform(std::views::reverse);
|
|
|
|
EXPECT_EQ(fmt::format("{}", r), "[[], [1], [2, 1]]");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-22 17:24:51 +00:00
|
|
|
TEST(ranges_test, is_printable) {
|
|
|
|
using fmt::detail::is_printable;
|
|
|
|
EXPECT_TRUE(is_printable(0x0323));
|
|
|
|
EXPECT_FALSE(is_printable(0x0378));
|
2021-08-22 22:14:13 +00:00
|
|
|
EXPECT_FALSE(is_printable(0x110000));
|
2021-08-22 17:24:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 17:39:58 +00:00
|
|
|
TEST(ranges_test, escape) {
|
2021-08-21 16:35:05 +00:00
|
|
|
using vec = std::vector<std::string>;
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\n\r\t\"\\"}), "[\"\\n\\r\\t\\\"\\\\\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\x07"}), "[\"\\x07\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]");
|
2021-08-22 23:33:39 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"n\xcc\x83"}), "[\"n\xcc\x83\"]");
|
2021-08-22 14:38:22 +00:00
|
|
|
|
|
|
|
if (fmt::detail::is_utf8()) {
|
2021-08-22 23:33:39 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xcd\xb8"}), "[\"\\u0378\"]");
|
|
|
|
// Unassigned Unicode code points.
|
2021-08-22 14:38:22 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]");
|
2022-08-24 14:13:27 +00:00
|
|
|
// Broken utf-8.
|
2021-08-22 23:33:39 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xf4\x8f\xbf\xc0"}),
|
|
|
|
"[\"\\xf4\\x8f\\xbf\\xc0\"]");
|
2022-08-24 14:13:27 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xf0\x28"}), "[\"\\xf0(\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"\xe1\x28"}), "[\"\\xe1(\"]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", vec{std::string("\xf0\x28\0\0anything", 12)}),
|
|
|
|
"[\"\\xf0(\\x00\\x00anything\"]");
|
|
|
|
|
|
|
|
// Correct utf-8.
|
2023-02-18 17:39:58 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", vec{"🦄"}), "[\"🦄\"]");
|
2021-08-22 14:38:22 +00:00
|
|
|
}
|
2023-02-18 17:39:58 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::vector<std::vector<char>>{{'x'}}),
|
|
|
|
"[['x']]");
|
2023-02-19 17:24:29 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", std::tuple<std::vector<char>>{{'x'}}), "(['x'])");
|
2021-08-21 16:35:05 +00:00
|
|
|
}
|
2021-12-27 17:38:06 +00:00
|
|
|
|
2022-03-05 00:21:00 +00:00
|
|
|
template <typename R> struct fmt_ref_view {
|
|
|
|
R* r;
|
|
|
|
|
|
|
|
auto begin() const -> decltype(r->begin()) { return r->begin(); }
|
|
|
|
auto end() const -> decltype(r->end()) { return r->end(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ranges_test, range_of_range_of_mixed_const) {
|
2023-09-04 16:19:40 +00:00
|
|
|
auto v = std::vector<std::vector<int>>{{1, 2, 3}, {4, 5}};
|
2022-03-05 00:21:00 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", v), "[[1, 2, 3], [4, 5]]");
|
|
|
|
|
2023-09-04 16:19:40 +00:00
|
|
|
auto r = fmt_ref_view<decltype(v)>{&v};
|
2022-03-05 00:21:00 +00:00
|
|
|
EXPECT_EQ(fmt::format("{}", r), "[[1, 2, 3], [4, 5]]");
|
|
|
|
}
|
2022-08-26 00:28:22 +00:00
|
|
|
|
|
|
|
TEST(ranges_test, vector_char) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::vector<char>{'a', 'b'}), "['a', 'b']");
|
|
|
|
}
|
2023-02-09 01:25:41 +00:00
|
|
|
|
|
|
|
TEST(ranges_test, container_adaptor) {
|
|
|
|
{
|
|
|
|
using fmt::detail::is_container_adaptor_like;
|
|
|
|
using T = std::nullptr_t;
|
|
|
|
static_assert(is_container_adaptor_like<std::stack<T>>::value, "");
|
|
|
|
static_assert(is_container_adaptor_like<std::queue<T>>::value, "");
|
|
|
|
static_assert(is_container_adaptor_like<std::priority_queue<T>>::value, "");
|
|
|
|
static_assert(!is_container_adaptor_like<std::vector<T>>::value, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-02-18 17:39:58 +00:00
|
|
|
auto s = std::stack<int>();
|
2023-02-09 01:25:41 +00:00
|
|
|
s.push(1);
|
|
|
|
s.push(2);
|
|
|
|
EXPECT_EQ(fmt::format("{}", s), "[1, 2]");
|
|
|
|
EXPECT_EQ(fmt::format("{}", const_cast<const decltype(s)&>(s)), "[1, 2]");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-02-18 17:39:58 +00:00
|
|
|
auto q = std::queue<int>();
|
2023-02-09 01:25:41 +00:00
|
|
|
q.push(1);
|
|
|
|
q.push(2);
|
|
|
|
EXPECT_EQ(fmt::format("{}", q), "[1, 2]");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-02-18 17:39:58 +00:00
|
|
|
auto q = std::priority_queue<int>();
|
2023-02-09 01:25:41 +00:00
|
|
|
q.push(3);
|
|
|
|
q.push(1);
|
|
|
|
q.push(2);
|
|
|
|
q.push(4);
|
|
|
|
EXPECT_EQ(fmt::format("{}", q), "[4, 3, 2, 1]");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-02-18 17:39:58 +00:00
|
|
|
auto s = std::stack<char, std::string>();
|
2023-02-09 01:25:41 +00:00
|
|
|
s.push('a');
|
|
|
|
s.push('b');
|
2023-02-10 17:45:27 +00:00
|
|
|
// See https://cplusplus.github.io/LWG/issue3881.
|
|
|
|
EXPECT_EQ(fmt::format("{}", s), "['a', 'b']");
|
2023-02-09 01:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct my_container_adaptor {
|
|
|
|
using value_type = int;
|
|
|
|
using container_type = std::vector<value_type>;
|
|
|
|
void push(const value_type& v) { c.push_back(v); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
container_type c;
|
|
|
|
};
|
|
|
|
|
2023-02-18 17:39:58 +00:00
|
|
|
auto m = my_container_adaptor();
|
2023-02-09 01:25:41 +00:00
|
|
|
m.push(1);
|
|
|
|
m.push(2);
|
|
|
|
EXPECT_EQ(fmt::format("{}", m), "[1, 2]");
|
|
|
|
}
|
|
|
|
}
|
2023-12-23 16:32:36 +00:00
|
|
|
|
|
|
|
struct tieable {
|
|
|
|
int a = 3;
|
|
|
|
double b = 0.42;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto format_as(const tieable& t) -> std::tuple<int, double> {
|
|
|
|
return std::tie(t.a, t.b);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ranges_test, format_as_tie) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", tieable()), "(3, 0.42)");
|
|
|
|
}
|
2024-01-10 19:17:28 +00:00
|
|
|
|
|
|
|
struct lvalue_qualified_begin_end {
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
|
2024-01-11 00:43:00 +00:00
|
|
|
auto begin() & -> const int* { return arr; }
|
|
|
|
auto end() & -> const int* { return arr + 5; }
|
2024-01-10 19:17:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ranges_test, lvalue_qualified_begin_end) {
|
|
|
|
EXPECT_EQ(fmt::format("{}", lvalue_qualified_begin_end{}), "[1, 2, 3, 4, 5]");
|
|
|
|
}
|