raw_ostream_test.cpp 15 KB

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