raw_ostream_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream tests -----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/SmallString.h"
  9. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/Format.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. namespace {
  15. template<typename T> std::string printToString(const T &Value) {
  16. std::string res;
  17. llvm::raw_string_ostream(res) << Value;
  18. return res;
  19. }
  20. /// printToString - Print the given value to a stream which only has \arg
  21. /// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge
  22. /// cases in the buffer handling logic.
  23. template<typename T> std::string printToString(const T &Value,
  24. unsigned BytesLeftInBuffer) {
  25. // FIXME: This is relying on internal knowledge of how raw_ostream works to
  26. // get the buffer position right.
  27. SmallString<256> SVec;
  28. assert(BytesLeftInBuffer < 256 && "Invalid buffer count!");
  29. llvm::raw_svector_ostream OS(SVec);
  30. unsigned StartIndex = 256 - BytesLeftInBuffer;
  31. for (unsigned i = 0; i != StartIndex; ++i)
  32. OS << '?';
  33. OS << Value;
  34. return OS.str().substr(StartIndex);
  35. }
  36. template<typename T> std::string printToStringUnbuffered(const T &Value) {
  37. std::string res;
  38. llvm::raw_string_ostream OS(res);
  39. OS.SetUnbuffered();
  40. OS << Value;
  41. return res;
  42. }
  43. TEST(raw_ostreamTest, Types_Buffered) {
  44. // Char
  45. EXPECT_EQ("c", printToString('c'));
  46. // String
  47. EXPECT_EQ("hello", printToString("hello"));
  48. EXPECT_EQ("hello", printToString(std::string("hello")));
  49. // Int
  50. EXPECT_EQ("0", printToString(0));
  51. EXPECT_EQ("2425", printToString(2425));
  52. EXPECT_EQ("-2425", printToString(-2425));
  53. // Long long
  54. EXPECT_EQ("0", printToString(0LL));
  55. EXPECT_EQ("257257257235709", printToString(257257257235709LL));
  56. EXPECT_EQ("-257257257235709", printToString(-257257257235709LL));
  57. // Double
  58. EXPECT_EQ("1.100000e+00", printToString(1.1));
  59. // void*
  60. EXPECT_EQ("0x0", printToString((void*) nullptr));
  61. EXPECT_EQ("0xbeef", printToString((void*) 0xbeefLL));
  62. EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeefLL));
  63. // Min and max.
  64. EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX));
  65. EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN));
  66. }
  67. TEST(raw_ostreamTest, Types_Unbuffered) {
  68. // Char
  69. EXPECT_EQ("c", printToStringUnbuffered('c'));
  70. // String
  71. EXPECT_EQ("hello", printToStringUnbuffered("hello"));
  72. EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello")));
  73. // Int
  74. EXPECT_EQ("0", printToStringUnbuffered(0));
  75. EXPECT_EQ("2425", printToStringUnbuffered(2425));
  76. EXPECT_EQ("-2425", printToStringUnbuffered(-2425));
  77. // Long long
  78. EXPECT_EQ("0", printToStringUnbuffered(0LL));
  79. EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL));
  80. EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL));
  81. // Double
  82. EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1));
  83. // void*
  84. EXPECT_EQ("0x0", printToStringUnbuffered((void*) nullptr));
  85. EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeefLL));
  86. EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeefLL));
  87. // Min and max.
  88. EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX));
  89. EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN));
  90. }
  91. TEST(raw_ostreamTest, BufferEdge) {
  92. EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1));
  93. EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2));
  94. EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3));
  95. EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4));
  96. EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10));
  97. }
  98. TEST(raw_ostreamTest, TinyBuffer) {
  99. std::string Str;
  100. raw_string_ostream OS(Str);
  101. OS.SetBufferSize(1);
  102. OS << "hello";
  103. OS << 1;
  104. OS << 'w' << 'o' << 'r' << 'l' << 'd';
  105. EXPECT_EQ("hello1world", OS.str());
  106. }
  107. TEST(raw_ostreamTest, WriteEscaped) {
  108. std::string Str;
  109. Str = "";
  110. raw_string_ostream(Str).write_escaped("hi");
  111. EXPECT_EQ("hi", Str);
  112. Str = "";
  113. raw_string_ostream(Str).write_escaped("\\\t\n\"");
  114. EXPECT_EQ("\\\\\\t\\n\\\"", Str);
  115. Str = "";
  116. raw_string_ostream(Str).write_escaped("\1\10\200");
  117. EXPECT_EQ("\\001\\010\\200", Str);
  118. }
  119. TEST(raw_ostreamTest, Justify) {
  120. EXPECT_EQ("xyz ", printToString(left_justify("xyz", 6), 6));
  121. EXPECT_EQ("abc", printToString(left_justify("abc", 3), 3));
  122. EXPECT_EQ("big", printToString(left_justify("big", 1), 3));
  123. EXPECT_EQ(" xyz", printToString(right_justify("xyz", 6), 6));
  124. EXPECT_EQ("abc", printToString(right_justify("abc", 3), 3));
  125. EXPECT_EQ("big", printToString(right_justify("big", 1), 3));
  126. EXPECT_EQ(" on ", printToString(center_justify("on", 9), 9));
  127. EXPECT_EQ(" off ", printToString(center_justify("off", 10), 10));
  128. EXPECT_EQ("single ", printToString(center_justify("single", 7), 7));
  129. EXPECT_EQ("none", printToString(center_justify("none", 1), 4));
  130. EXPECT_EQ("none", printToString(center_justify("none", 1), 1));
  131. }
  132. TEST(raw_ostreamTest, FormatHex) {
  133. EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 6), 6));
  134. EXPECT_EQ("0x001234", printToString(format_hex(0x1234, 8), 8));
  135. EXPECT_EQ("0x00001234", printToString(format_hex(0x1234, 10), 10));
  136. EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 4), 6));
  137. EXPECT_EQ("0xff", printToString(format_hex(255, 4), 4));
  138. EXPECT_EQ("0xFF", printToString(format_hex(255, 4, true), 4));
  139. EXPECT_EQ("0x1", printToString(format_hex(1, 3), 3));
  140. EXPECT_EQ("0x12", printToString(format_hex(0x12, 3), 4));
  141. EXPECT_EQ("0x123", printToString(format_hex(0x123, 3), 5));
  142. EXPECT_EQ("FF", printToString(format_hex_no_prefix(0xFF, 2, true), 4));
  143. EXPECT_EQ("ABCD", printToString(format_hex_no_prefix(0xABCD, 2, true), 4));
  144. EXPECT_EQ("0xffffffffffffffff",
  145. printToString(format_hex(UINT64_MAX, 18), 18));
  146. EXPECT_EQ("0x8000000000000000",
  147. printToString(format_hex((INT64_MIN), 18), 18));
  148. }
  149. TEST(raw_ostreamTest, FormatDecimal) {
  150. EXPECT_EQ(" 0", printToString(format_decimal(0, 4), 4));
  151. EXPECT_EQ(" -1", printToString(format_decimal(-1, 4), 4));
  152. EXPECT_EQ(" -1", printToString(format_decimal(-1, 6), 6));
  153. EXPECT_EQ("1234567890", printToString(format_decimal(1234567890, 10), 10));
  154. EXPECT_EQ(" 9223372036854775807",
  155. printToString(format_decimal(INT64_MAX, 21), 21));
  156. EXPECT_EQ(" -9223372036854775808",
  157. printToString(format_decimal(INT64_MIN, 21), 21));
  158. }
  159. static std::string formatted_bytes_str(ArrayRef<uint8_t> Bytes,
  160. llvm::Optional<uint64_t> Offset = None,
  161. uint32_t NumPerLine = 16,
  162. uint8_t ByteGroupSize = 4) {
  163. std::string S;
  164. raw_string_ostream Str(S);
  165. Str << format_bytes(Bytes, Offset, NumPerLine, ByteGroupSize);
  166. Str.flush();
  167. return S;
  168. }
  169. static std::string format_bytes_with_ascii_str(ArrayRef<uint8_t> Bytes,
  170. Optional<uint64_t> Offset = None,
  171. uint32_t NumPerLine = 16,
  172. uint8_t ByteGroupSize = 4) {
  173. std::string S;
  174. raw_string_ostream Str(S);
  175. Str << format_bytes_with_ascii(Bytes, Offset, NumPerLine, ByteGroupSize);
  176. Str.flush();
  177. return S;
  178. }
  179. TEST(raw_ostreamTest, FormattedHexBytes) {
  180. std::vector<uint8_t> Buf = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  181. 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
  182. 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
  183. '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  184. ArrayRef<uint8_t> B(Buf);
  185. // Test invalid input.
  186. EXPECT_EQ("", formatted_bytes_str(ArrayRef<uint8_t>()));
  187. EXPECT_EQ("", format_bytes_with_ascii_str(ArrayRef<uint8_t>()));
  188. //----------------------------------------------------------------------
  189. // Test hex byte output with the default 4 byte groups
  190. //----------------------------------------------------------------------
  191. EXPECT_EQ("61", formatted_bytes_str(B.take_front()));
  192. EXPECT_EQ("61626364 65", formatted_bytes_str(B.take_front(5)));
  193. // Test that 16 bytes get written to a line correctly.
  194. EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70",
  195. formatted_bytes_str(B.take_front(16)));
  196. // Test raw bytes with default 16 bytes per line wrapping.
  197. EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71",
  198. formatted_bytes_str(B.take_front(17)));
  199. // Test raw bytes with 1 bytes per line wrapping.
  200. EXPECT_EQ("61\n62\n63\n64\n65\n66",
  201. formatted_bytes_str(B.take_front(6), None, 1));
  202. // Test raw bytes with 7 bytes per line wrapping.
  203. EXPECT_EQ("61626364 656667\n68696a6b 6c6d6e\n6f7071",
  204. formatted_bytes_str(B.take_front(17), None, 7));
  205. // Test raw bytes with 8 bytes per line wrapping.
  206. EXPECT_EQ("61626364 65666768\n696a6b6c 6d6e6f70\n71",
  207. formatted_bytes_str(B.take_front(17), None, 8));
  208. //----------------------------------------------------------------------
  209. // Test hex byte output with the 1 byte groups
  210. //----------------------------------------------------------------------
  211. EXPECT_EQ("61 62 63 64 65",
  212. formatted_bytes_str(B.take_front(5), None, 16, 1));
  213. // Test that 16 bytes get written to a line correctly.
  214. EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70",
  215. formatted_bytes_str(B.take_front(16), None, 16, 1));
  216. // Test raw bytes with default 16 bytes per line wrapping.
  217. EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70\n71",
  218. formatted_bytes_str(B.take_front(17), None, 16, 1));
  219. // Test raw bytes with 7 bytes per line wrapping.
  220. EXPECT_EQ("61 62 63 64 65 66 67\n68 69 6a 6b 6c 6d 6e\n6f 70 71",
  221. formatted_bytes_str(B.take_front(17), None, 7, 1));
  222. // Test raw bytes with 8 bytes per line wrapping.
  223. EXPECT_EQ("61 62 63 64 65 66 67 68\n69 6a 6b 6c 6d 6e 6f 70\n71",
  224. formatted_bytes_str(B.take_front(17), None, 8, 1));
  225. //----------------------------------------------------------------------
  226. // Test hex byte output with the 2 byte groups
  227. //----------------------------------------------------------------------
  228. EXPECT_EQ("6162 6364 65", formatted_bytes_str(B.take_front(5), None, 16, 2));
  229. // Test that 16 bytes get written to a line correctly.
  230. EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70",
  231. formatted_bytes_str(B.take_front(16), None, 16, 2));
  232. // Test raw bytes with default 16 bytes per line wrapping.
  233. EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70\n71",
  234. formatted_bytes_str(B.take_front(17), None, 16, 2));
  235. // Test raw bytes with 7 bytes per line wrapping.
  236. EXPECT_EQ("6162 6364 6566 67\n6869 6a6b 6c6d 6e\n6f70 71",
  237. formatted_bytes_str(B.take_front(17), None, 7, 2));
  238. // Test raw bytes with 8 bytes per line wrapping.
  239. EXPECT_EQ("6162 6364 6566 6768\n696a 6b6c 6d6e 6f70\n71",
  240. formatted_bytes_str(B.take_front(17), None, 8, 2));
  241. //----------------------------------------------------------------------
  242. // Test hex bytes with offset with the default 4 byte groups.
  243. //----------------------------------------------------------------------
  244. EXPECT_EQ("0000: 61", formatted_bytes_str(B.take_front(), 0x0));
  245. EXPECT_EQ("1000: 61", formatted_bytes_str(B.take_front(), 0x1000));
  246. EXPECT_EQ("1000: 61\n1001: 62",
  247. formatted_bytes_str(B.take_front(2), 0x1000, 1));
  248. //----------------------------------------------------------------------
  249. // Test hex bytes with ASCII with the default 4 byte groups.
  250. //----------------------------------------------------------------------
  251. EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70 |abcdefghijklmnop|",
  252. format_bytes_with_ascii_str(B.take_front(16)));
  253. EXPECT_EQ("61626364 65666768 |abcdefgh|\n"
  254. "696a6b6c 6d6e6f70 |ijklmnop|",
  255. format_bytes_with_ascii_str(B.take_front(16), None, 8));
  256. EXPECT_EQ("61626364 65666768 |abcdefgh|\n696a6b6c |ijkl|",
  257. format_bytes_with_ascii_str(B.take_front(12), None, 8));
  258. std::vector<uint8_t> Unprintable = {'a', '\x1e', 'b', '\x1f'};
  259. // Make sure the ASCII is still lined up correctly when fewer bytes than 16
  260. // bytes per line are available. The ASCII should still be aligned as if 16
  261. // bytes of hex might be displayed.
  262. EXPECT_EQ("611e621f |a.b.|",
  263. format_bytes_with_ascii_str(Unprintable));
  264. //----------------------------------------------------------------------
  265. // Test hex bytes with ASCII with offsets with the default 4 byte groups.
  266. //----------------------------------------------------------------------
  267. EXPECT_EQ("0000: 61626364 65666768 "
  268. "696a6b6c 6d6e6f70 |abcdefghijklmnop|",
  269. format_bytes_with_ascii_str(B.take_front(16), 0));
  270. EXPECT_EQ("0000: 61626364 65666768 |abcdefgh|\n"
  271. "0008: 696a6b6c 6d6e6f70 |ijklmnop|",
  272. format_bytes_with_ascii_str(B.take_front(16), 0, 8));
  273. EXPECT_EQ("0000: 61626364 656667 |abcdefg|\n"
  274. "0007: 68696a6b 6c |hijkl|",
  275. format_bytes_with_ascii_str(B.take_front(12), 0, 7));
  276. //----------------------------------------------------------------------
  277. // Test hex bytes with ASCII with offsets with the default 2 byte groups.
  278. //----------------------------------------------------------------------
  279. EXPECT_EQ("0000: 6162 6364 6566 6768 "
  280. "696a 6b6c 6d6e 6f70 |abcdefghijklmnop|",
  281. format_bytes_with_ascii_str(B.take_front(16), 0, 16, 2));
  282. EXPECT_EQ("0000: 6162 6364 6566 6768 |abcdefgh|\n"
  283. "0008: 696a 6b6c 6d6e 6f70 |ijklmnop|",
  284. format_bytes_with_ascii_str(B.take_front(16), 0, 8, 2));
  285. EXPECT_EQ("0000: 6162 6364 6566 67 |abcdefg|\n"
  286. "0007: 6869 6a6b 6c |hijkl|",
  287. format_bytes_with_ascii_str(B.take_front(12), 0, 7, 2));
  288. //----------------------------------------------------------------------
  289. // Test hex bytes with ASCII with offsets with the default 1 byte groups.
  290. //----------------------------------------------------------------------
  291. EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 "
  292. "69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|",
  293. format_bytes_with_ascii_str(B.take_front(16), 0, 16, 1));
  294. EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 |abcdefgh|\n"
  295. "0008: 69 6a 6b 6c 6d 6e 6f 70 |ijklmnop|",
  296. format_bytes_with_ascii_str(B.take_front(16), 0, 8, 1));
  297. EXPECT_EQ("0000: 61 62 63 64 65 66 67 |abcdefg|\n"
  298. "0007: 68 69 6a 6b 6c |hijkl|",
  299. format_bytes_with_ascii_str(B.take_front(12), 0, 7, 1));
  300. }
  301. TEST(raw_fd_ostreamTest, multiple_raw_fd_ostream_to_stdout) {
  302. std::error_code EC;
  303. { raw_fd_ostream("-", EC, sys::fs::OpenFlags::OF_None); }
  304. { raw_fd_ostream("-", EC, sys::fs::OpenFlags::OF_None); }
  305. }
  306. }