ranges-test.cc 10 KB

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