ranges-test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #include "fmt/ranges.h"
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include "gtest/gtest.h"
  16. #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601
  17. # define FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
  18. #endif
  19. #if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1910
  20. # define FMT_RANGES_TEST_ENABLE_JOIN
  21. # define FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
  22. #endif
  23. #ifdef FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
  24. TEST(ranges_test, format_array) {
  25. int arr[] = {1, 2, 3, 5, 7, 11};
  26. EXPECT_EQ(fmt::format("{}", arr), "[1, 2, 3, 5, 7, 11]");
  27. }
  28. TEST(ranges_test, format_2d_array) {
  29. int arr[][2] = {{1, 2}, {3, 5}, {7, 11}};
  30. EXPECT_EQ(fmt::format("{}", arr), "[[1, 2], [3, 5], [7, 11]]");
  31. }
  32. TEST(ranges_test, format_array_of_literals) {
  33. const char* arr[] = {"1234", "abcd"};
  34. EXPECT_EQ(fmt::format("{}", arr), "[\"1234\", \"abcd\"]");
  35. EXPECT_EQ(fmt::format("{:n}", arr), "\"1234\", \"abcd\"");
  36. EXPECT_EQ(fmt::format("{:n:}", arr), "1234, abcd");
  37. }
  38. #endif // FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
  39. TEST(ranges_test, format_vector) {
  40. auto v = std::vector<int>{1, 2, 3, 5, 7, 11};
  41. EXPECT_EQ(fmt::format("{}", v), "[1, 2, 3, 5, 7, 11]");
  42. EXPECT_EQ(fmt::format("{::#x}", v), "[0x1, 0x2, 0x3, 0x5, 0x7, 0xb]");
  43. EXPECT_EQ(fmt::format("{:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb");
  44. }
  45. TEST(ranges_test, format_vector2) {
  46. auto v = std::vector<std::vector<int>>{{1, 2}, {3, 5}, {7, 11}};
  47. EXPECT_EQ(fmt::format("{}", v), "[[1, 2], [3, 5], [7, 11]]");
  48. EXPECT_EQ(fmt::format("{:::#x}", v), "[[0x1, 0x2], [0x3, 0x5], [0x7, 0xb]]");
  49. EXPECT_EQ(fmt::format("{:n:n:#x}", v), "0x1, 0x2, 0x3, 0x5, 0x7, 0xb");
  50. }
  51. TEST(ranges_test, format_map) {
  52. auto m = std::map<std::string, int>{{"one", 1}, {"two", 2}};
  53. EXPECT_EQ(fmt::format("{}", m), "{\"one\": 1, \"two\": 2}");
  54. EXPECT_EQ(fmt::format("{:n}", m), "\"one\": 1, \"two\": 2");
  55. }
  56. TEST(ranges_test, format_set) {
  57. EXPECT_EQ(fmt::format("{}", std::set<std::string>{"one", "two"}),
  58. "{\"one\", \"two\"}");
  59. }
  60. namespace adl {
  61. struct box {
  62. int value;
  63. };
  64. auto begin(const box& b) -> const int* { return &b.value; }
  65. auto end(const box& b) -> const int* { return &b.value + 1; }
  66. } // namespace adl
  67. TEST(ranges_test, format_adl_begin_end) {
  68. auto b = adl::box{42};
  69. EXPECT_EQ(fmt::format("{}", b), "[42]");
  70. }
  71. TEST(ranges_test, format_pair) {
  72. auto p = std::pair<int, float>(42, 1.5f);
  73. EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)");
  74. }
  75. struct unformattable {};
  76. TEST(ranges_test, format_tuple) {
  77. auto t =
  78. std::tuple<int, float, std::string, char>(42, 1.5f, "this is tuple", 'i');
  79. EXPECT_EQ(fmt::format("{}", t), "(42, 1.5, \"this is tuple\", 'i')");
  80. EXPECT_EQ(fmt::format("{}", std::tuple<>()), "()");
  81. EXPECT_TRUE((fmt::is_formattable<std::tuple<>>::value));
  82. EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
  83. EXPECT_FALSE((fmt::is_formattable<std::tuple<unformattable>>::value));
  84. EXPECT_FALSE((fmt::is_formattable<std::tuple<unformattable, int>>::value));
  85. EXPECT_FALSE((fmt::is_formattable<std::tuple<int, unformattable>>::value));
  86. EXPECT_FALSE(
  87. (fmt::is_formattable<std::tuple<unformattable, unformattable>>::value));
  88. EXPECT_TRUE((fmt::is_formattable<std::tuple<int, float>>::value));
  89. }
  90. #ifdef FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
  91. struct tuple_like {
  92. int i;
  93. std::string str;
  94. template <size_t N> fmt::enable_if_t<N == 0, int> get() const noexcept {
  95. return i;
  96. }
  97. template <size_t N>
  98. fmt::enable_if_t<N == 1, fmt::string_view> get() const noexcept {
  99. return str;
  100. }
  101. };
  102. template <size_t N>
  103. auto get(const tuple_like& t) noexcept -> decltype(t.get<N>()) {
  104. return t.get<N>();
  105. }
  106. namespace std {
  107. template <>
  108. struct tuple_size<tuple_like> : std::integral_constant<size_t, 2> {};
  109. template <size_t N> struct tuple_element<N, tuple_like> {
  110. using type = decltype(std::declval<tuple_like>().get<N>());
  111. };
  112. } // namespace std
  113. TEST(ranges_test, format_struct) {
  114. auto t = tuple_like{42, "foo"};
  115. EXPECT_EQ(fmt::format("{}", t), "(42, \"foo\")");
  116. }
  117. #endif // FMT_RANGES_TEST_ENABLE_FORMAT_STRUCT
  118. TEST(ranges_test, format_to) {
  119. char buf[10];
  120. auto end = fmt::format_to(buf, "{}", std::vector<int>{1, 2, 3});
  121. *end = '\0';
  122. EXPECT_STREQ(buf, "[1, 2, 3]");
  123. }
  124. struct path_like {
  125. const path_like* begin() const;
  126. const path_like* end() const;
  127. operator std::string() const;
  128. };
  129. TEST(ranges_test, path_like) {
  130. EXPECT_FALSE((fmt::is_range<path_like, char>::value));
  131. }
  132. #ifdef FMT_USE_STRING_VIEW
  133. struct string_like {
  134. const char* begin();
  135. const char* end();
  136. operator fmt::string_view() const { return "foo"; }
  137. operator std::string_view() const { return "foo"; }
  138. };
  139. TEST(ranges_test, format_string_like) {
  140. EXPECT_EQ(fmt::format("{}", string_like()), "foo");
  141. }
  142. #endif // FMT_USE_STRING_VIEW
  143. // A range that provides non-const only begin()/end() to test fmt::join handles
  144. // that.
  145. //
  146. // Some ranges (e.g. those produced by range-v3's views::filter()) can cache
  147. // information during iteration so they only provide non-const begin()/end().
  148. template <typename T> class non_const_only_range {
  149. private:
  150. std::vector<T> vec;
  151. public:
  152. using const_iterator = typename ::std::vector<T>::const_iterator;
  153. template <typename... Args>
  154. explicit non_const_only_range(Args&&... args)
  155. : vec(std::forward<Args>(args)...) {}
  156. const_iterator begin() { return vec.begin(); }
  157. const_iterator end() { return vec.end(); }
  158. };
  159. template <typename T> class noncopyable_range {
  160. private:
  161. std::vector<T> vec;
  162. public:
  163. using const_iterator = typename ::std::vector<T>::const_iterator;
  164. template <typename... Args>
  165. explicit noncopyable_range(Args&&... args)
  166. : vec(std::forward<Args>(args)...) {}
  167. noncopyable_range(noncopyable_range const&) = delete;
  168. noncopyable_range(noncopyable_range&) = delete;
  169. const_iterator begin() const { return vec.begin(); }
  170. const_iterator end() const { return vec.end(); }
  171. };
  172. TEST(ranges_test, range) {
  173. noncopyable_range<int> w(3u, 0);
  174. EXPECT_EQ(fmt::format("{}", w), "[0, 0, 0]");
  175. EXPECT_EQ(fmt::format("{}", noncopyable_range<int>(3u, 0)), "[0, 0, 0]");
  176. non_const_only_range<int> x(3u, 0);
  177. EXPECT_EQ(fmt::format("{}", x), "[0, 0, 0]");
  178. EXPECT_EQ(fmt::format("{}", non_const_only_range<int>(3u, 0)), "[0, 0, 0]");
  179. auto y = std::vector<int>(3u, 0);
  180. EXPECT_EQ(fmt::format("{}", y), "[0, 0, 0]");
  181. EXPECT_EQ(fmt::format("{}", std::vector<int>(3u, 0)), "[0, 0, 0]");
  182. const auto z = std::vector<int>(3u, 0);
  183. EXPECT_EQ(fmt::format("{}", z), "[0, 0, 0]");
  184. }
  185. enum test_enum { foo };
  186. auto format_as(test_enum e) -> int { return e; }
  187. TEST(ranges_test, enum_range) {
  188. auto v = std::vector<test_enum>{test_enum::foo};
  189. EXPECT_EQ(fmt::format("{}", v), "[0]");
  190. }
  191. #if !FMT_MSC_VERSION
  192. TEST(ranges_test, unformattable_range) {
  193. EXPECT_FALSE((fmt::has_formatter<std::vector<unformattable>,
  194. fmt::format_context>::value));
  195. }
  196. #endif
  197. #ifdef FMT_RANGES_TEST_ENABLE_JOIN
  198. TEST(ranges_test, join_tuple) {
  199. // Value tuple args.
  200. auto t1 = std::tuple<char, int, float>('a', 1, 2.0f);
  201. EXPECT_EQ(fmt::format("({})", fmt::join(t1, ", ")), "(a, 1, 2)");
  202. // Testing lvalue tuple args.
  203. int x = 4;
  204. auto t2 = std::tuple<char, int&>('b', x);
  205. EXPECT_EQ(fmt::format("{}", fmt::join(t2, " + ")), "b + 4");
  206. // Empty tuple.
  207. auto t3 = std::tuple<>();
  208. EXPECT_EQ(fmt::format("{}", fmt::join(t3, "|")), "");
  209. // Single element tuple.
  210. auto t4 = std::tuple<float>(4.0f);
  211. EXPECT_EQ(fmt::format("{}", fmt::join(t4, "/")), "4");
  212. # if FMT_TUPLE_JOIN_SPECIFIERS
  213. // Specs applied to each element.
  214. auto t5 = std::tuple<int, int, long>(-3, 100, 1);
  215. EXPECT_EQ(fmt::format("{:+03}", fmt::join(t5, ", ")), "-03, +100, +01");
  216. auto t6 = std::tuple<float, double, long double>(3, 3.14, 3.1415);
  217. EXPECT_EQ(fmt::format("{:5.5f}", fmt::join(t6, ", ")),
  218. "3.00000, 3.14000, 3.14150");
  219. // Testing lvalue tuple args.
  220. int y = -1;
  221. auto t7 = std::tuple<int, int&, const int&>(3, y, y);
  222. EXPECT_EQ(fmt::format("{:03}", fmt::join(t7, ", ")), "003, -01, -01");
  223. # endif
  224. }
  225. TEST(ranges_test, join_initializer_list) {
  226. EXPECT_EQ(fmt::format("{}", fmt::join({1, 2, 3}, ", ")), "1, 2, 3");
  227. EXPECT_EQ(fmt::format("{}", fmt::join({"fmt", "rocks", "!"}, " ")),
  228. "fmt rocks !");
  229. }
  230. struct zstring_sentinel {};
  231. bool operator==(const char* p, zstring_sentinel) { return *p == '\0'; }
  232. bool operator!=(const char* p, zstring_sentinel) { return *p != '\0'; }
  233. struct zstring {
  234. const char* p;
  235. const char* begin() const { return p; }
  236. zstring_sentinel end() const { return {}; }
  237. };
  238. # ifdef __cpp_lib_ranges
  239. struct cpp20_only_range {
  240. struct iterator {
  241. int val = 0;
  242. using value_type = int;
  243. using difference_type = std::ptrdiff_t;
  244. using iterator_concept = std::input_iterator_tag;
  245. iterator() = default;
  246. iterator(int i) : val(i) {}
  247. int operator*() const { return val; }
  248. iterator& operator++() {
  249. ++val;
  250. return *this;
  251. }
  252. void operator++(int) { ++*this; }
  253. bool operator==(const iterator& rhs) const { return val == rhs.val; }
  254. };
  255. int lo;
  256. int hi;
  257. iterator begin() const { return iterator(lo); }
  258. iterator end() const { return iterator(hi); }
  259. };
  260. static_assert(std::input_iterator<cpp20_only_range::iterator>);
  261. # endif
  262. TEST(ranges_test, join_sentinel) {
  263. auto hello = zstring{"hello"};
  264. EXPECT_EQ(fmt::format("{}", hello), "['h', 'e', 'l', 'l', 'o']");
  265. EXPECT_EQ(fmt::format("{::}", hello), "[h, e, l, l, o]");
  266. EXPECT_EQ(fmt::format("{}", fmt::join(hello, "_")), "h_e_l_l_o");
  267. }
  268. TEST(ranges_test, join_range) {
  269. noncopyable_range<int> w(3u, 0);
  270. EXPECT_EQ(fmt::format("{}", fmt::join(w, ",")), "0,0,0");
  271. EXPECT_EQ(fmt::format("{}", fmt::join(noncopyable_range<int>(3u, 0), ",")),
  272. "0,0,0");
  273. non_const_only_range<int> x(3u, 0);
  274. EXPECT_EQ(fmt::format("{}", fmt::join(x, ",")), "0,0,0");
  275. EXPECT_EQ(fmt::format("{}", fmt::join(non_const_only_range<int>(3u, 0), ",")),
  276. "0,0,0");
  277. auto y = std::vector<int>(3u, 0);
  278. EXPECT_EQ(fmt::format("{}", fmt::join(y, ",")), "0,0,0");
  279. EXPECT_EQ(fmt::format("{}", fmt::join(std::vector<int>(3u, 0), ",")),
  280. "0,0,0");
  281. const auto z = std::vector<int>(3u, 0);
  282. EXPECT_EQ(fmt::format("{}", fmt::join(z, ",")), "0,0,0");
  283. # ifdef __cpp_lib_ranges
  284. EXPECT_EQ(fmt::format("{}", cpp20_only_range{.lo = 0, .hi = 5}),
  285. "[0, 1, 2, 3, 4]");
  286. EXPECT_EQ(
  287. fmt::format("{}", fmt::join(cpp20_only_range{.lo = 0, .hi = 5}, ",")),
  288. "0,1,2,3,4");
  289. # endif
  290. }
  291. #endif // FMT_RANGES_TEST_ENABLE_JOIN
  292. TEST(ranges_test, is_printable) {
  293. using fmt::detail::is_printable;
  294. EXPECT_TRUE(is_printable(0x0323));
  295. EXPECT_FALSE(is_printable(0x0378));
  296. EXPECT_FALSE(is_printable(0x110000));
  297. }
  298. TEST(ranges_test, escape_string) {
  299. using vec = std::vector<std::string>;
  300. EXPECT_EQ(fmt::format("{}", vec{"\n\r\t\"\\"}), "[\"\\n\\r\\t\\\"\\\\\"]");
  301. EXPECT_EQ(fmt::format("{}", vec{"\x07"}), "[\"\\x07\"]");
  302. EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]");
  303. EXPECT_EQ(fmt::format("{}", vec{"n\xcc\x83"}), "[\"n\xcc\x83\"]");
  304. if (fmt::detail::is_utf8()) {
  305. EXPECT_EQ(fmt::format("{}", vec{"\xcd\xb8"}), "[\"\\u0378\"]");
  306. // Unassigned Unicode code points.
  307. EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]");
  308. // Broken utf-8.
  309. EXPECT_EQ(fmt::format("{}", vec{"\xf4\x8f\xbf\xc0"}),
  310. "[\"\\xf4\\x8f\\xbf\\xc0\"]");
  311. EXPECT_EQ(fmt::format("{}", vec{"\xf0\x28"}), "[\"\\xf0(\"]");
  312. EXPECT_EQ(fmt::format("{}", vec{"\xe1\x28"}), "[\"\\xe1(\"]");
  313. EXPECT_EQ(fmt::format("{}", vec{std::string("\xf0\x28\0\0anything", 12)}),
  314. "[\"\\xf0(\\x00\\x00anything\"]");
  315. // Correct utf-8.
  316. EXPECT_EQ(fmt::format("{}", vec{"понедельник"}), "[\"понедельник\"]");
  317. }
  318. }
  319. #ifdef FMT_USE_STRING_VIEW
  320. struct convertible_to_string_view {
  321. operator std::string_view() const { return "foo"; }
  322. };
  323. TEST(ranges_test, escape_convertible_to_string_view) {
  324. EXPECT_EQ(fmt::format("{}", std::vector<convertible_to_string_view>(1)),
  325. "[\"foo\"]");
  326. }
  327. #endif // FMT_USE_STRING_VIEW
  328. template <typename R> struct fmt_ref_view {
  329. R* r;
  330. auto begin() const -> decltype(r->begin()) { return r->begin(); }
  331. auto end() const -> decltype(r->end()) { return r->end(); }
  332. };
  333. TEST(ranges_test, range_of_range_of_mixed_const) {
  334. std::vector<std::vector<int>> v = {{1, 2, 3}, {4, 5}};
  335. EXPECT_EQ(fmt::format("{}", v), "[[1, 2, 3], [4, 5]]");
  336. fmt_ref_view<decltype(v)> r{&v};
  337. EXPECT_EQ(fmt::format("{}", r), "[[1, 2, 3], [4, 5]]");
  338. }
  339. TEST(ranges_test, vector_char) {
  340. EXPECT_EQ(fmt::format("{}", std::vector<char>{'a', 'b'}), "['a', 'b']");
  341. }