2
0

ostream-test.cc 8.6 KB

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