ostream-test.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Formatting library for C++ - std::ostream support tests
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include "fmt/format.h"
  8. using fmt::runtime;
  9. struct test {};
  10. // Test that there is no issues with specializations when fmt/ostream.h is
  11. // included after fmt/format.h.
  12. namespace fmt {
  13. template <> struct formatter<test> : formatter<int> {
  14. auto format(const test&, format_context& ctx) -> decltype(ctx.out()) {
  15. return formatter<int>::format(42, ctx);
  16. }
  17. };
  18. } // namespace fmt
  19. #include <sstream>
  20. #include "fmt/compile.h"
  21. #include "fmt/ostream.h"
  22. #include "fmt/ranges.h"
  23. #include "gmock/gmock.h"
  24. #include "gtest-extra.h"
  25. #include "util.h"
  26. std::ostream& operator<<(std::ostream& os, const date& d) {
  27. os << d.year() << '-' << d.month() << '-' << d.day();
  28. return os;
  29. }
  30. std::wostream& operator<<(std::wostream& os, const date& d) {
  31. os << d.year() << L'-' << d.month() << L'-' << d.day();
  32. return os;
  33. }
  34. // Make sure that overloaded comma operators do no harm to is_streamable.
  35. struct type_with_comma_op {};
  36. template <typename T> void operator,(type_with_comma_op, const T&);
  37. template <typename T> type_with_comma_op operator<<(T&, const date&);
  38. enum streamable_enum {};
  39. std::ostream& operator<<(std::ostream& os, streamable_enum) {
  40. return os << "streamable_enum";
  41. }
  42. enum unstreamable_enum {};
  43. TEST(ostream_test, enum) {
  44. EXPECT_EQ("streamable_enum", fmt::format("{}", streamable_enum()));
  45. EXPECT_EQ("0", fmt::format("{}", unstreamable_enum()));
  46. }
  47. TEST(ostream_test, format) {
  48. EXPECT_EQ("a string", fmt::format("{0}", test_string("a string")));
  49. EXPECT_EQ("The date is 2012-12-9",
  50. fmt::format("The date is {0}", date(2012, 12, 9)));
  51. }
  52. TEST(ostream_test, format_specs) {
  53. using fmt::format_error;
  54. EXPECT_EQ("def ", fmt::format("{0:<5}", test_string("def")));
  55. EXPECT_EQ(" def", fmt::format("{0:>5}", test_string("def")));
  56. EXPECT_EQ(" def ", fmt::format("{0:^5}", test_string("def")));
  57. EXPECT_EQ("def**", fmt::format("{0:*<5}", test_string("def")));
  58. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), test_string()),
  59. format_error, "format specifier requires numeric argument");
  60. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), test_string()),
  61. format_error, "format specifier requires numeric argument");
  62. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), test_string()),
  63. format_error, "format specifier requires numeric argument");
  64. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), test_string()),
  65. format_error, "format specifier requires numeric argument");
  66. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), test_string()),
  67. format_error, "format specifier requires numeric argument");
  68. EXPECT_EQ("test ", fmt::format("{0:13}", test_string("test")));
  69. EXPECT_EQ("test ", fmt::format("{0:{1}}", test_string("test"), 13));
  70. EXPECT_EQ("te", fmt::format("{0:.2}", test_string("test")));
  71. EXPECT_EQ("te", fmt::format("{0:.{1}}", test_string("test"), 2));
  72. }
  73. struct empty_test {};
  74. std::ostream& operator<<(std::ostream& os, empty_test) { return os << ""; }
  75. TEST(ostream_test, empty_custom_output) {
  76. EXPECT_EQ("", fmt::format("{}", empty_test()));
  77. }
  78. TEST(ostream_test, print) {
  79. std::ostringstream os;
  80. fmt::print(os, "Don't {}!", "panic");
  81. EXPECT_EQ("Don't panic!", os.str());
  82. }
  83. TEST(ostream_test, write_to_ostream) {
  84. std::ostringstream os;
  85. fmt::memory_buffer buffer;
  86. const char* foo = "foo";
  87. buffer.append(foo, foo + std::strlen(foo));
  88. fmt::detail::write_buffer(os, buffer);
  89. EXPECT_EQ("foo", os.str());
  90. }
  91. TEST(ostream_test, write_to_ostream_max_size) {
  92. auto max_size = fmt::detail::max_value<size_t>();
  93. auto max_streamsize = fmt::detail::max_value<std::streamsize>();
  94. if (max_size <= fmt::detail::to_unsigned(max_streamsize)) return;
  95. struct test_buffer final : fmt::detail::buffer<char> {
  96. explicit test_buffer(size_t size)
  97. : fmt::detail::buffer<char>(nullptr, size, size) {}
  98. void grow(size_t) override {}
  99. } buffer(max_size);
  100. struct mock_streambuf : std::streambuf {
  101. MOCK_METHOD2(xsputn, std::streamsize(const void* s, std::streamsize n));
  102. std::streamsize xsputn(const char* s, std::streamsize n) override {
  103. const void* v = s;
  104. return xsputn(v, n);
  105. }
  106. } streambuf;
  107. struct test_ostream : std::ostream {
  108. explicit test_ostream(mock_streambuf& output_buffer)
  109. : std::ostream(&output_buffer) {}
  110. } os(streambuf);
  111. testing::InSequence sequence;
  112. const char* data = nullptr;
  113. using ustreamsize = std::make_unsigned<std::streamsize>::type;
  114. ustreamsize size = max_size;
  115. do {
  116. auto n = std::min(size, fmt::detail::to_unsigned(max_streamsize));
  117. EXPECT_CALL(streambuf, xsputn(data, static_cast<std::streamsize>(n)))
  118. .WillOnce(testing::Return(max_streamsize));
  119. data += n;
  120. size -= n;
  121. } while (size != 0);
  122. fmt::detail::write_buffer(os, buffer);
  123. }
  124. TEST(ostream_test, join) {
  125. int v[3] = {1, 2, 3};
  126. EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, v + 3, ", ")));
  127. }
  128. TEST(ostream_test, join_fallback_formatter) {
  129. auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
  130. EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", ")));
  131. }
  132. #if FMT_USE_CONSTEXPR
  133. TEST(ostream_test, constexpr_string) {
  134. EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42")));
  135. EXPECT_EQ("a string", format(FMT_STRING("{0}"), test_string("a string")));
  136. }
  137. #endif
  138. namespace fmt_test {
  139. struct abc {};
  140. template <typename Output> Output& operator<<(Output& out, abc) {
  141. return out << "abc";
  142. }
  143. } // namespace fmt_test
  144. template <typename T> struct test_template {};
  145. template <typename T>
  146. std::ostream& operator<<(std::ostream& os, test_template<T>) {
  147. return os << 1;
  148. }
  149. namespace fmt {
  150. template <typename T> struct formatter<test_template<T>> : formatter<int> {
  151. auto format(test_template<T>, format_context& ctx) -> decltype(ctx.out()) {
  152. return formatter<int>::format(2, ctx);
  153. }
  154. };
  155. } // namespace fmt
  156. TEST(ostream_test, template) {
  157. EXPECT_EQ("2", fmt::format("{}", test_template<int>()));
  158. }
  159. TEST(ostream_test, format_to_n) {
  160. char buffer[4];
  161. buffer[3] = 'x';
  162. auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::abc());
  163. EXPECT_EQ(3u, result.size);
  164. EXPECT_EQ(buffer + 3, result.out);
  165. EXPECT_EQ("abcx", fmt::string_view(buffer, 4));
  166. result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::abc());
  167. EXPECT_EQ(5u, result.size);
  168. EXPECT_EQ(buffer + 3, result.out);
  169. EXPECT_EQ("xabx", fmt::string_view(buffer, 4));
  170. }
  171. template <typename T> struct convertible {
  172. T value;
  173. explicit convertible(const T& val) : value(val) {}
  174. operator T() const { return value; }
  175. };
  176. TEST(ostream_test, disable_builtin_ostream_operators) {
  177. EXPECT_EQ("42", fmt::format("{:d}", convertible<unsigned short>(42)));
  178. EXPECT_EQ("foo", fmt::format("{}", convertible<const char*>("foo")));
  179. }
  180. struct explicitly_convertible_to_string_like {
  181. template <typename String,
  182. typename = typename std::enable_if<std::is_constructible<
  183. String, const char*, size_t>::value>::type>
  184. explicit operator String() const {
  185. return String("foo", 3u);
  186. }
  187. };
  188. std::ostream& operator<<(std::ostream& os,
  189. explicitly_convertible_to_string_like) {
  190. return os << "bar";
  191. }
  192. TEST(ostream_test, format_explicitly_convertible_to_string_like) {
  193. EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like()));
  194. }
  195. #ifdef FMT_USE_STRING_VIEW
  196. struct explicitly_convertible_to_std_string_view {
  197. explicit operator fmt::detail::std_string_view<char>() const {
  198. return {"foo", 3u};
  199. }
  200. };
  201. std::ostream& operator<<(std::ostream& os,
  202. explicitly_convertible_to_std_string_view) {
  203. return os << "bar";
  204. }
  205. TEST(ostream_test, format_explicitly_convertible_to_std_string_view) {
  206. EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like()));
  207. }
  208. #endif // FMT_USE_STRING_VIEW
  209. struct streamable_and_convertible_to_bool {
  210. operator bool() const { return true; }
  211. };
  212. std::ostream& operator<<(std::ostream& os, streamable_and_convertible_to_bool) {
  213. return os << "foo";
  214. }
  215. TEST(ostream_test, format_convertible_to_bool) {
  216. // operator<< is intentionally not used because of potential ODR violations.
  217. EXPECT_EQ(fmt::format("{}", streamable_and_convertible_to_bool()), "true");
  218. }
  219. struct copyfmt_test {};
  220. std::ostream& operator<<(std::ostream& os, copyfmt_test) {
  221. std::ios ios(nullptr);
  222. ios.copyfmt(os);
  223. return os << "foo";
  224. }
  225. TEST(ostream_test, copyfmt) {
  226. EXPECT_EQ("foo", fmt::format("{}", copyfmt_test()));
  227. }
  228. TEST(ostream_test, to_string) {
  229. EXPECT_EQ("abc", fmt::to_string(fmt_test::abc()));
  230. }
  231. TEST(ostream_test, range) {
  232. auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
  233. EXPECT_EQ("[foo, bar]", fmt::format("{}", strs));
  234. }
  235. struct abstract {
  236. virtual ~abstract() = default;
  237. virtual void f() = 0;
  238. friend std::ostream& operator<<(std::ostream& os, const abstract&) {
  239. return os;
  240. }
  241. };
  242. void format_abstract_compiles(const abstract& a) {
  243. fmt::format(FMT_COMPILE("{}"), a);
  244. }
  245. TEST(ostream_test, is_formattable) {
  246. EXPECT_TRUE(fmt::is_formattable<std::string>());
  247. EXPECT_TRUE(fmt::is_formattable<fmt::detail::std_string_view<char>>());
  248. }