blob: 5a62ad58e33bc754532c8f680645735ec93b3ead [file] [log] [blame]
Misha Brukman1757a812009-04-01 21:36:40 +00001//===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream tests -----===//
Daniel Dunbaraf419912009-03-17 16:14:59 +00002//
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
10#include "gtest/gtest.h"
Daniel Dunbar47a309c2009-08-23 20:31:39 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/Support/Format.h"
Daniel Dunbaraf419912009-03-17 16:14:59 +000013#include "llvm/Support/raw_ostream.h"
14
15using namespace llvm;
16
17namespace {
18
19template<typename T> std::string printToString(const T &Value) {
20 std::string res;
21 llvm::raw_string_ostream(res) << Value;
22 return res;
23}
24
Daniel Dunbar47a309c2009-08-23 20:31:39 +000025/// printToString - Print the given value to a stream which only has \arg
26/// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge
27/// cases in the buffer handling logic.
28template<typename T> std::string printToString(const T &Value,
29 unsigned BytesLeftInBuffer) {
30 // FIXME: This is relying on internal knowledge of how raw_ostream works to
31 // get the buffer position right.
32 SmallString<256> SVec;
33 assert(BytesLeftInBuffer < 256 && "Invalid buffer count!");
34 llvm::raw_svector_ostream OS(SVec);
35 unsigned StartIndex = 256 - BytesLeftInBuffer;
36 for (unsigned i = 0; i != StartIndex; ++i)
37 OS << '?';
38 OS << Value;
39 return OS.str().substr(StartIndex);
40}
41
Daniel Dunbaraf419912009-03-17 16:14:59 +000042template<typename T> std::string printToStringUnbuffered(const T &Value) {
43 std::string res;
44 llvm::raw_string_ostream OS(res);
45 OS.SetUnbuffered();
46 OS << Value;
47 return res;
48}
49
50TEST(raw_ostreamTest, Types_Buffered) {
51 // Char
52 EXPECT_EQ("c", printToString('c'));
53
54 // String
55 EXPECT_EQ("hello", printToString("hello"));
56 EXPECT_EQ("hello", printToString(std::string("hello")));
57
58 // Int
59 EXPECT_EQ("0", printToString(0));
60 EXPECT_EQ("2425", printToString(2425));
61 EXPECT_EQ("-2425", printToString(-2425));
62
63 // Long long
64 EXPECT_EQ("0", printToString(0LL));
65 EXPECT_EQ("257257257235709", printToString(257257257235709LL));
66 EXPECT_EQ("-257257257235709", printToString(-257257257235709LL));
67
68 // Double
69 EXPECT_EQ("1.100000e+00", printToString(1.1));
70
71 // void*
Craig Topper66f09ad2014-06-08 22:29:17 +000072 EXPECT_EQ("0x0", printToString((void*) nullptr));
Reid Kleckner19d532b2016-02-10 19:11:15 +000073 EXPECT_EQ("0xbeef", printToString((void*) 0xbeefLL));
74 EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeefLL));
Daniel Dunbarfaea9712009-08-19 19:58:19 +000075
76 // Min and max.
77 EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX));
78 EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN));
Daniel Dunbaraf419912009-03-17 16:14:59 +000079}
80
81TEST(raw_ostreamTest, Types_Unbuffered) {
82 // Char
83 EXPECT_EQ("c", printToStringUnbuffered('c'));
84
85 // String
86 EXPECT_EQ("hello", printToStringUnbuffered("hello"));
87 EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello")));
88
89 // Int
90 EXPECT_EQ("0", printToStringUnbuffered(0));
91 EXPECT_EQ("2425", printToStringUnbuffered(2425));
92 EXPECT_EQ("-2425", printToStringUnbuffered(-2425));
93
94 // Long long
95 EXPECT_EQ("0", printToStringUnbuffered(0LL));
96 EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL));
97 EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL));
98
99 // Double
100 EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1));
101
102 // void*
Craig Topper66f09ad2014-06-08 22:29:17 +0000103 EXPECT_EQ("0x0", printToStringUnbuffered((void*) nullptr));
Reid Kleckner19d532b2016-02-10 19:11:15 +0000104 EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeefLL));
105 EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeefLL));
Daniel Dunbarfaea9712009-08-19 19:58:19 +0000106
107 // Min and max.
108 EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX));
109 EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN));
Daniel Dunbaraf419912009-03-17 16:14:59 +0000110}
111
Daniel Dunbar47a309c2009-08-23 20:31:39 +0000112TEST(raw_ostreamTest, BufferEdge) {
113 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1));
114 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2));
115 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3));
116 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4));
117 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10));
118}
119
Daniel Dunbar316b4a02009-09-15 20:31:46 +0000120TEST(raw_ostreamTest, TinyBuffer) {
121 std::string Str;
122 raw_string_ostream OS(Str);
123 OS.SetBufferSize(1);
124 OS << "hello";
125 OS << 1;
126 OS << 'w' << 'o' << 'r' << 'l' << 'd';
127 EXPECT_EQ("hello1world", OS.str());
128}
129
Daniel Dunbar4108c432009-10-17 20:43:08 +0000130TEST(raw_ostreamTest, WriteEscaped) {
131 std::string Str;
132
133 Str = "";
134 raw_string_ostream(Str).write_escaped("hi");
135 EXPECT_EQ("hi", Str);
136
137 Str = "";
138 raw_string_ostream(Str).write_escaped("\\\t\n\"");
139 EXPECT_EQ("\\\\\\t\\n\\\"", Str);
140
141 Str = "";
142 raw_string_ostream(Str).write_escaped("\1\10\200");
143 EXPECT_EQ("\\001\\010\\200", Str);
144}
145
Nick Kledzike6480372014-09-25 20:30:58 +0000146TEST(raw_ostreamTest, Justify) {
147 EXPECT_EQ("xyz ", printToString(left_justify("xyz", 6), 6));
148 EXPECT_EQ("abc", printToString(left_justify("abc", 3), 3));
149 EXPECT_EQ("big", printToString(left_justify("big", 1), 3));
150 EXPECT_EQ(" xyz", printToString(right_justify("xyz", 6), 6));
151 EXPECT_EQ("abc", printToString(right_justify("abc", 3), 3));
152 EXPECT_EQ("big", printToString(right_justify("big", 1), 3));
153}
154
155TEST(raw_ostreamTest, FormatHex) {
156 EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 6), 6));
157 EXPECT_EQ("0x001234", printToString(format_hex(0x1234, 8), 8));
158 EXPECT_EQ("0x00001234", printToString(format_hex(0x1234, 10), 10));
159 EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 4), 6));
160 EXPECT_EQ("0xff", printToString(format_hex(255, 4), 4));
161 EXPECT_EQ("0xFF", printToString(format_hex(255, 4, true), 4));
162 EXPECT_EQ("0x1", printToString(format_hex(1, 3), 3));
163 EXPECT_EQ("0x12", printToString(format_hex(0x12, 3), 4));
164 EXPECT_EQ("0x123", printToString(format_hex(0x123, 3), 5));
Zachary Turner39571b32015-01-26 18:21:33 +0000165 EXPECT_EQ("FF", printToString(format_hex_no_prefix(0xFF, 2, true), 4));
166 EXPECT_EQ("ABCD", printToString(format_hex_no_prefix(0xABCD, 2, true), 4));
Nick Kledzike6480372014-09-25 20:30:58 +0000167 EXPECT_EQ("0xffffffffffffffff",
168 printToString(format_hex(UINT64_MAX, 18), 18));
169 EXPECT_EQ("0x8000000000000000",
170 printToString(format_hex((INT64_MIN), 18), 18));
171}
172
173TEST(raw_ostreamTest, FormatDecimal) {
174 EXPECT_EQ(" 0", printToString(format_decimal(0, 4), 4));
175 EXPECT_EQ(" -1", printToString(format_decimal(-1, 4), 4));
176 EXPECT_EQ(" -1", printToString(format_decimal(-1, 6), 6));
177 EXPECT_EQ("1234567890", printToString(format_decimal(1234567890, 10), 10));
178 EXPECT_EQ(" 9223372036854775807",
179 printToString(format_decimal(INT64_MAX, 21), 21));
180 EXPECT_EQ(" -9223372036854775808",
181 printToString(format_decimal(INT64_MIN, 21), 21));
182}
183
Greg Claytonbde0a162016-11-09 00:15:54 +0000184std::string
185format_hex_bytes(const void *P, size_t N,
186 llvm::Optional<uint64_t> Offset = llvm::Optional<uint64_t>(),
187 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) {
188 std::string S;
189 if (P && N) {
190 llvm::raw_string_ostream Str(S);
191 Str << llvm::format_hex_bytes(
192 llvm::ArrayRef<uint8_t>(static_cast<const uint8_t *>(P), N), Offset,
193 NumPerLine, ByteGroupSize);
194 Str.flush();
195 }
196 return S;
197}
Nick Kledzike6480372014-09-25 20:30:58 +0000198
Greg Claytonbde0a162016-11-09 00:15:54 +0000199std::string format_hex_bytes_with_ascii(
200 const void *P, size_t N,
201 llvm::Optional<uint64_t> Offset = llvm::Optional<uint64_t>(),
202 uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4) {
203 std::string S;
204 if (P && N) {
205 llvm::raw_string_ostream Str(S);
206 Str << llvm::format_hex_bytes_with_ascii(
207 llvm::ArrayRef<uint8_t>(static_cast<const uint8_t *>(P), N), Offset,
208 NumPerLine, ByteGroupSize);
209 Str.flush();
210 }
211 return S;
212}
213
214TEST(raw_ostreamTest, FormattedHexBytes) {
215 char b[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
216 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
217 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
218 // Test raw bytes
219 const llvm::Optional<uint64_t> InvalidOffset;
220 // Test invalid input.
221 EXPECT_EQ("", format_hex_bytes(nullptr, 0));
222 EXPECT_EQ("", format_hex_bytes(b, 0));
223 EXPECT_EQ("", format_hex_bytes(nullptr, 16));
224 //----------------------------------------------------------------------
225 // Test hex byte output with the default 4 byte groups
226 //----------------------------------------------------------------------
227 EXPECT_EQ("61", format_hex_bytes(b, 1));
228 EXPECT_EQ("61626364 65", format_hex_bytes(b, 5));
229 // Test that 16 bytes get written to a line correctly.
230 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70", format_hex_bytes(b, 16));
231 // Test raw bytes with default 16 bytes per line wrapping.
232 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71", format_hex_bytes(b, 17));
233 // Test raw bytes with 1 bytes per line wrapping.
234 EXPECT_EQ("61\n62\n63\n64\n65\n66", format_hex_bytes(b, 6, InvalidOffset, 1));
235 // Test raw bytes with 7 bytes per line wrapping.
236 EXPECT_EQ("61626364 656667\n68696a6b 6c6d6e\n6f7071",
237 format_hex_bytes(b, 17, InvalidOffset, 7));
238 // Test raw bytes with 8 bytes per line wrapping.
239 EXPECT_EQ("61626364 65666768\n696a6b6c 6d6e6f70\n71",
240 format_hex_bytes(b, 17, InvalidOffset, 8));
241 //----------------------------------------------------------------------
242 // Test hex byte output with the 1 byte groups
243 //----------------------------------------------------------------------
244 EXPECT_EQ("61 62 63 64 65", format_hex_bytes(b, 5, InvalidOffset, 16, 1));
245 // Test that 16 bytes get written to a line correctly.
246 EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70",
247 format_hex_bytes(b, 16, InvalidOffset, 16, 1));
248 // Test raw bytes with default 16 bytes per line wrapping.
249 EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70\n71",
250 format_hex_bytes(b, 17, InvalidOffset, 16, 1));
251 // Test raw bytes with 7 bytes per line wrapping.
252 EXPECT_EQ("61 62 63 64 65 66 67\n68 69 6a 6b 6c 6d 6e\n6f 70 71",
253 format_hex_bytes(b, 17, InvalidOffset, 7, 1));
254 // Test raw bytes with 8 bytes per line wrapping.
255 EXPECT_EQ("61 62 63 64 65 66 67 68\n69 6a 6b 6c 6d 6e 6f 70\n71",
256 format_hex_bytes(b, 17, InvalidOffset, 8, 1));
257
258 //----------------------------------------------------------------------
259 // Test hex byte output with the 2 byte groups
260 //----------------------------------------------------------------------
261 EXPECT_EQ("6162 6364 65", format_hex_bytes(b, 5, InvalidOffset, 16, 2));
262 // Test that 16 bytes get written to a line correctly.
263 EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70",
264 format_hex_bytes(b, 16, InvalidOffset, 16, 2));
265 // Test raw bytes with default 16 bytes per line wrapping.
266 EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70\n71",
267 format_hex_bytes(b, 17, InvalidOffset, 16, 2));
268 // Test raw bytes with 7 bytes per line wrapping.
269 EXPECT_EQ("6162 6364 6566 67\n6869 6a6b 6c6d 6e\n6f70 71",
270 format_hex_bytes(b, 17, InvalidOffset, 7, 2));
271 // Test raw bytes with 8 bytes per line wrapping.
272 EXPECT_EQ("6162 6364 6566 6768\n696a 6b6c 6d6e 6f70\n71",
273 format_hex_bytes(b, 17, InvalidOffset, 8, 2));
274
275 //----------------------------------------------------------------------
276 // Test hex bytes with offset with the default 4 byte groups.
277 //----------------------------------------------------------------------
278 EXPECT_EQ("0x0000000000000000: 61", format_hex_bytes(b, 1, 0x0));
279 EXPECT_EQ("0x0000000000001000: 61", format_hex_bytes(b, 1, 0x1000));
280 EXPECT_EQ("0x0000000000001000: 61\n0x0000000000001001: 62",
281 format_hex_bytes(b, 2, 0x1000, 1));
282 //----------------------------------------------------------------------
283 // Test hex bytes with ASCII with the default 4 byte groups.
284 //----------------------------------------------------------------------
285 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70 |abcdefghijklmnop|",
286 format_hex_bytes_with_ascii(b, 16));
287 EXPECT_EQ("61626364 65666768 |abcdefgh|\n"
288 "696a6b6c 6d6e6f70 |ijklmnop|",
289 format_hex_bytes_with_ascii(b, 16, InvalidOffset, 8));
290 EXPECT_EQ("61626364 65666768 |abcdefgh|\n696a6b6c |ijkl|",
291 format_hex_bytes_with_ascii(b, 12, InvalidOffset, 8));
292 char unprintable[] = {'a', '\x1e', 'b', '\x1f'};
293 // Make sure the ASCII is still lined up correctly when fewer bytes than 16
294 // bytes per line are available. The ASCII should still be aligned as if 16
295 // bytes of hex might be displayed.
296 EXPECT_EQ("611e621f |a.b.|",
297 format_hex_bytes_with_ascii(unprintable, 4));
298 //----------------------------------------------------------------------
299 // Test hex bytes with ASCII with offsets with the default 4 byte groups.
300 //----------------------------------------------------------------------
301 EXPECT_EQ("0x0000000000000000: 61626364 65666768 "
302 "696a6b6c 6d6e6f70 |abcdefghijklmnop|",
303 format_hex_bytes_with_ascii(b, 16, 0));
304 EXPECT_EQ("0x0000000000000000: 61626364 65666768 |abcdefgh|\n"
305 "0x0000000000000008: 696a6b6c 6d6e6f70 |ijklmnop|",
306 format_hex_bytes_with_ascii(b, 16, 0, 8));
307 EXPECT_EQ("0x0000000000000000: 61626364 656667 |abcdefg|\n"
308 "0x0000000000000007: 68696a6b 6c |hijkl|",
309 format_hex_bytes_with_ascii(b, 12, 0, 7));
310
311 //----------------------------------------------------------------------
312 // Test hex bytes with ASCII with offsets with the default 2 byte groups.
313 //----------------------------------------------------------------------
314 EXPECT_EQ("0x0000000000000000: 6162 6364 6566 6768 "
315 "696a 6b6c 6d6e 6f70 |abcdefghijklmnop|",
316 format_hex_bytes_with_ascii(b, 16, 0, 16, 2));
317 EXPECT_EQ("0x0000000000000000: 6162 6364 6566 6768 |abcdefgh|\n"
318 "0x0000000000000008: 696a 6b6c 6d6e 6f70 |ijklmnop|",
319 format_hex_bytes_with_ascii(b, 16, 0, 8, 2));
320 EXPECT_EQ("0x0000000000000000: 6162 6364 6566 67 |abcdefg|\n"
321 "0x0000000000000007: 6869 6a6b 6c |hijkl|",
322 format_hex_bytes_with_ascii(b, 12, 0, 7, 2));
323
324 //----------------------------------------------------------------------
325 // Test hex bytes with ASCII with offsets with the default 1 byte groups.
326 //----------------------------------------------------------------------
327 EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 "
328 "69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|",
329 format_hex_bytes_with_ascii(b, 16, 0, 16, 1));
330 EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 68 |abcdefgh|\n"
331 "0x0000000000000008: 69 6a 6b 6c 6d 6e 6f 70 |ijklmnop|",
332 format_hex_bytes_with_ascii(b, 16, 0, 8, 1));
333 EXPECT_EQ("0x0000000000000000: 61 62 63 64 65 66 67 |abcdefg|\n"
334 "0x0000000000000007: 68 69 6a 6b 6c |hijkl|",
335 format_hex_bytes_with_ascii(b, 12, 0, 7, 1));
336}
Daniel Dunbaraf419912009-03-17 16:14:59 +0000337}