[SimplifyCFG] Replace unreachable switch lookup table holes with poison (#94990)
[llvm-project.git] / lldb / unittests / Utility / StreamTest.cpp
blob959097dc52f987157db0b9293b7b3ea31aa2d1a4
1 //===-- StreamTest.cpp ----------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/StreamString.h"
10 #include "gtest/gtest.h"
12 using namespace lldb_private;
14 namespace {
15 struct StreamTest : ::testing::Test {
16 // Note: Stream is an abstract class, so we use StreamString to test it. To
17 // make it easier to change this later, only methods in this class explicitly
18 // refer to the StringStream class.
19 StreamString s;
20 // We return here a std::string because that way gtest can print better
21 // assertion messages.
22 std::string TakeValue() {
23 std::string result = s.GetString().str();
24 s.Clear();
25 return result;
30 namespace {
31 // A StreamTest where we expect the Stream output to be binary.
32 struct BinaryStreamTest : StreamTest {
33 void SetUp() override {
34 s.GetFlags().Set(Stream::eBinary);
39 TEST_F(StreamTest, AddressPrefix) {
40 DumpAddress(s.AsRawOstream(), 0x1, 1, "foo");
41 EXPECT_EQ("foo0x01", TakeValue());
44 TEST_F(StreamTest, AddressEmptyPrefix) {
45 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr);
46 EXPECT_EQ("0x01", TakeValue());
47 DumpAddress(s.AsRawOstream(), 0x1, 1, "");
48 EXPECT_EQ("0x01", TakeValue());
51 TEST_F(StreamTest, AddressSuffix) {
52 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, "foo");
53 EXPECT_EQ("0x01foo", TakeValue());
56 TEST_F(StreamTest, AddressNoSuffix) {
57 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, nullptr);
58 EXPECT_EQ("0x01", TakeValue());
59 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, "");
60 EXPECT_EQ("0x01", TakeValue());
63 TEST_F(StreamTest, AddressPrefixAndSuffix) {
64 DumpAddress(s.AsRawOstream(), 0x1, 1, "foo", "bar");
65 EXPECT_EQ("foo0x01bar", TakeValue());
68 TEST_F(StreamTest, AddressSize) {
69 DumpAddress(s.AsRawOstream(), 0x0, 0);
70 EXPECT_EQ("0x0", TakeValue());
71 DumpAddress(s.AsRawOstream(), 0x1, 0);
72 EXPECT_EQ("0x1", TakeValue());
74 DumpAddress(s.AsRawOstream(), 0x1, 1);
75 EXPECT_EQ("0x01", TakeValue());
76 DumpAddress(s.AsRawOstream(), 0xf1, 1);
77 EXPECT_EQ("0xf1", TakeValue());
78 DumpAddress(s.AsRawOstream(), 0xff, 1);
79 EXPECT_EQ("0xff", TakeValue());
80 DumpAddress(s.AsRawOstream(), 0x100, 1);
81 EXPECT_EQ("0x100", TakeValue());
83 DumpAddress(s.AsRawOstream(), 0xf00, 4);
84 EXPECT_EQ("0x00000f00", TakeValue());
85 DumpAddress(s.AsRawOstream(), 0x100, 8);
86 EXPECT_EQ("0x0000000000000100", TakeValue());
89 TEST_F(StreamTest, AddressRange) {
90 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 2);
91 EXPECT_EQ("[0x0100-0x0101)", TakeValue());
94 TEST_F(StreamTest, AddressRangeEmptyRange) {
95 DumpAddressRange(s.AsRawOstream(), 0x100, 0x100, 2);
96 EXPECT_EQ("[0x0100-0x0100)", TakeValue());
97 DumpAddressRange(s.AsRawOstream(), 0x0, 0x0, 2);
98 EXPECT_EQ("[0x0000-0x0000)", TakeValue());
101 TEST_F(StreamTest, AddressRangeInvalidRange) {
102 DumpAddressRange(s.AsRawOstream(), 0x100, 0x0FF, 2);
103 EXPECT_EQ("[0x0100-0x00ff)", TakeValue());
104 DumpAddressRange(s.AsRawOstream(), 0x100, 0x0, 2);
105 EXPECT_EQ("[0x0100-0x0000)", TakeValue());
108 TEST_F(StreamTest, AddressRangeSize) {
109 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 0);
110 EXPECT_EQ("[0x100-0x101)", TakeValue());
111 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 2);
112 EXPECT_EQ("[0x0100-0x0101)", TakeValue());
113 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 4);
114 EXPECT_EQ("[0x00000100-0x00000101)", TakeValue());
116 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 4);
117 EXPECT_EQ("[0x00000100-0x00000101)", TakeValue());
118 DumpAddressRange(s.AsRawOstream(), 0x1, 0x101, 4);
119 EXPECT_EQ("[0x00000001-0x00000101)", TakeValue());
120 DumpAddressRange(s.AsRawOstream(), 0x101, 0x1, 4);
121 EXPECT_EQ("[0x00000101-0x00000001)", TakeValue());
123 DumpAddressRange(s.AsRawOstream(), 0x1, 0x101, 1);
124 EXPECT_EQ("[0x01-0x101)", TakeValue());
127 TEST_F(StreamTest, ChangingByteOrder) {
128 s.SetByteOrder(lldb::eByteOrderPDP);
129 EXPECT_EQ(lldb::eByteOrderPDP, s.GetByteOrder());
132 TEST_F(StreamTest, SetIndentLevel) {
133 s.Indent("a");
134 EXPECT_EQ("a", TakeValue());
136 s.SetIndentLevel(3);
137 s.Indent("a");
138 EXPECT_EQ(" a", TakeValue());
140 s.SetIndentLevel(2);
141 s.Indent("a");
142 EXPECT_EQ(" a", TakeValue());
144 s.SetIndentLevel(0);
145 s.Indent("a");
146 EXPECT_EQ("a", TakeValue());
149 TEST_F(StreamTest, Indent) {
150 s.SetIndentLevel(2);
151 const char *nullptr_cstring = nullptr;
152 s.Indent(nullptr_cstring);
153 EXPECT_EQ(" ", TakeValue());
155 s.Indent("");
156 EXPECT_EQ(" ", TakeValue());
158 s.Indent(" ");
159 EXPECT_EQ(" ", TakeValue());
161 s.Indent(" aa");
162 EXPECT_EQ(" aa", TakeValue());
165 TEST_F(StreamTest, PutChar) {
166 s.PutChar('a');
167 EXPECT_EQ(1U, s.GetWrittenBytes());
168 EXPECT_EQ("a", TakeValue());
170 s.PutChar('1');
171 EXPECT_EQ(1U, s.GetWrittenBytes());
172 EXPECT_EQ("1", TakeValue());
175 TEST_F(StreamTest, PutCharWhitespace) {
176 s.PutChar(' ');
177 EXPECT_EQ(1U, s.GetWrittenBytes());
178 EXPECT_EQ(" ", TakeValue());
180 s.PutChar('\n');
181 EXPECT_EQ(1U, s.GetWrittenBytes());
182 EXPECT_EQ("\n", TakeValue());
184 s.PutChar('\r');
185 EXPECT_EQ(1U, s.GetWrittenBytes());
186 EXPECT_EQ("\r", TakeValue());
188 s.PutChar('\t');
189 EXPECT_EQ(1U, s.GetWrittenBytes());
190 EXPECT_EQ("\t", TakeValue());
193 TEST_F(StreamTest, PutCString) {
194 s.PutCString("");
195 EXPECT_EQ(0U, s.GetWrittenBytes());
196 EXPECT_EQ("", TakeValue());
198 s.PutCString("foobar");
199 EXPECT_EQ(6U, s.GetWrittenBytes());
200 EXPECT_EQ("foobar", TakeValue());
202 s.PutCString(" ");
203 EXPECT_EQ(1U, s.GetWrittenBytes());
204 EXPECT_EQ(" ", TakeValue());
207 TEST_F(StreamTest, PutCStringWithStringRef) {
208 s.PutCString(llvm::StringRef(""));
209 EXPECT_EQ(0U, s.GetWrittenBytes());
210 EXPECT_EQ("", TakeValue());
212 s.PutCString(llvm::StringRef("foobar"));
213 EXPECT_EQ(6U, s.GetWrittenBytes());
214 EXPECT_EQ("foobar", TakeValue());
216 s.PutCString(llvm::StringRef(" "));
217 EXPECT_EQ(1U, s.GetWrittenBytes());
218 EXPECT_EQ(" ", TakeValue());
221 TEST_F(StreamTest, QuotedCString) {
222 s.QuotedCString("foo");
223 EXPECT_EQ(5U, s.GetWrittenBytes());
224 EXPECT_EQ(R"("foo")", TakeValue());
226 s.QuotedCString("ba r");
227 EXPECT_EQ(6U, s.GetWrittenBytes());
228 EXPECT_EQ(R"("ba r")", TakeValue());
230 s.QuotedCString(" ");
231 EXPECT_EQ(3U, s.GetWrittenBytes());
232 EXPECT_EQ(R"(" ")", TakeValue());
235 TEST_F(StreamTest, PutCharNull) {
236 s.PutChar('\0');
237 EXPECT_EQ(1U, s.GetWrittenBytes());
238 EXPECT_EQ(std::string("\0", 1), TakeValue());
240 s.PutChar('a');
241 EXPECT_EQ(1U, s.GetWrittenBytes());
242 EXPECT_EQ(std::string("a", 1), TakeValue());
245 TEST_F(StreamTest, PutStringAsRawHex8) {
246 s.PutStringAsRawHex8("");
247 EXPECT_EQ(0U, s.GetWrittenBytes());
248 EXPECT_EQ("", TakeValue());
250 s.PutStringAsRawHex8("foobar");
251 EXPECT_EQ(12U, s.GetWrittenBytes());
252 EXPECT_EQ("666f6f626172", TakeValue());
254 s.PutStringAsRawHex8(" ");
255 EXPECT_EQ(2U, s.GetWrittenBytes());
256 EXPECT_EQ("20", TakeValue());
259 TEST_F(StreamTest, PutHex8) {
260 s.PutHex8((uint8_t)55);
261 EXPECT_EQ(2U, s.GetWrittenBytes());
262 EXPECT_EQ("37", TakeValue());
264 s.PutHex8(std::numeric_limits<uint8_t>::max());
265 EXPECT_EQ(2U, s.GetWrittenBytes());
266 EXPECT_EQ("ff", TakeValue());
268 s.PutHex8((uint8_t)0);
269 EXPECT_EQ(2U, s.GetWrittenBytes());
270 EXPECT_EQ("00", TakeValue());
273 TEST_F(StreamTest, PutNHex8) {
274 s.PutNHex8(0, (uint8_t)55);
275 EXPECT_EQ(0U, s.GetWrittenBytes());
276 EXPECT_EQ("", TakeValue());
278 s.PutNHex8(1, (uint8_t)55);
279 EXPECT_EQ(2U, s.GetWrittenBytes());
280 EXPECT_EQ("37", TakeValue());
282 s.PutNHex8(2, (uint8_t)55);
283 EXPECT_EQ(4U, s.GetWrittenBytes());
284 EXPECT_EQ("3737", TakeValue());
286 s.PutNHex8(1, (uint8_t)56);
287 EXPECT_EQ(2U, s.GetWrittenBytes());
288 EXPECT_EQ("38", TakeValue());
291 TEST_F(StreamTest, PutHex16ByteOrderLittle) {
292 s.PutHex16(0x1234U, lldb::eByteOrderLittle);
293 EXPECT_EQ(4U, s.GetWrittenBytes());
294 EXPECT_EQ("3412", TakeValue());
296 s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderLittle);
297 EXPECT_EQ(4U, s.GetWrittenBytes());
298 EXPECT_EQ("ffff", TakeValue());
300 s.PutHex16(0U, lldb::eByteOrderLittle);
301 EXPECT_EQ(4U, s.GetWrittenBytes());
302 EXPECT_EQ("0000", TakeValue());
305 TEST_F(StreamTest, PutHex16ByteOrderBig) {
306 s.PutHex16(0x1234U, lldb::eByteOrderBig);
307 EXPECT_EQ(4U, s.GetWrittenBytes());
308 EXPECT_EQ("1234", TakeValue());
310 s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderBig);
311 EXPECT_EQ(4U, s.GetWrittenBytes());
312 EXPECT_EQ("ffff", TakeValue());
314 s.PutHex16(0U, lldb::eByteOrderBig);
315 EXPECT_EQ(4U, s.GetWrittenBytes());
316 EXPECT_EQ("0000", TakeValue());
319 TEST_F(StreamTest, PutHex32ByteOrderLittle) {
320 s.PutHex32(0x12345678U, lldb::eByteOrderLittle);
321 EXPECT_EQ(8U, s.GetWrittenBytes());
322 EXPECT_EQ("78563412", TakeValue());
324 s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderLittle);
325 EXPECT_EQ(8U, s.GetWrittenBytes());
326 EXPECT_EQ("ffffffff", TakeValue());
328 s.PutHex32(0U, lldb::eByteOrderLittle);
329 EXPECT_EQ(8U, s.GetWrittenBytes());
330 EXPECT_EQ("00000000", TakeValue());
333 TEST_F(StreamTest, PutHex32ByteOrderBig) {
334 s.PutHex32(0x12345678U, lldb::eByteOrderBig);
335 EXPECT_EQ(8U, s.GetWrittenBytes());
336 EXPECT_EQ("12345678", TakeValue());
338 s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderBig);
339 EXPECT_EQ(8U, s.GetWrittenBytes());
340 EXPECT_EQ("ffffffff", TakeValue());
342 s.PutHex32(0U, lldb::eByteOrderBig);
343 EXPECT_EQ(8U, s.GetWrittenBytes());
344 EXPECT_EQ("00000000", TakeValue());
347 TEST_F(StreamTest, PutHex64ByteOrderLittle) {
348 s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderLittle);
349 EXPECT_EQ(16U, s.GetWrittenBytes());
350 EXPECT_EQ("efcdab9078563412", TakeValue());
352 s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderLittle);
353 EXPECT_EQ(16U, s.GetWrittenBytes());
354 EXPECT_EQ("ffffffffffffffff", TakeValue());
356 s.PutHex64(0U, lldb::eByteOrderLittle);
357 EXPECT_EQ(16U, s.GetWrittenBytes());
358 EXPECT_EQ("0000000000000000", TakeValue());
361 TEST_F(StreamTest, PutHex64ByteOrderBig) {
362 s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderBig);
363 EXPECT_EQ(16U, s.GetWrittenBytes());
364 EXPECT_EQ("1234567890abcdef", TakeValue());
366 s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderBig);
367 EXPECT_EQ(16U, s.GetWrittenBytes());
368 EXPECT_EQ("ffffffffffffffff", TakeValue());
370 s.PutHex64(0U, lldb::eByteOrderBig);
371 EXPECT_EQ(16U, s.GetWrittenBytes());
372 EXPECT_EQ("0000000000000000", TakeValue());
375 TEST_F(StreamTest, PutMaxHex64ByteOrderBig) {
376 std::size_t bytes;
377 bytes = s.PutMaxHex64(0x12U, 1, lldb::eByteOrderBig);
378 EXPECT_EQ(2U, bytes);
379 bytes = s.PutMaxHex64(0x1234U, 2, lldb::eByteOrderBig);
380 EXPECT_EQ(4U, bytes);
381 bytes = s.PutMaxHex64(0x12345678U, 4, lldb::eByteOrderBig);
382 EXPECT_EQ(8U, bytes);
383 bytes = s.PutMaxHex64(0x1234567890ABCDEFU, 8, lldb::eByteOrderBig);
384 EXPECT_EQ(16U, bytes);
385 EXPECT_EQ(30U, s.GetWrittenBytes());
386 EXPECT_EQ("121234123456781234567890abcdef", TakeValue());
389 TEST_F(StreamTest, PutMaxHex64ByteOrderLittle) {
390 std::size_t bytes;
391 bytes = s.PutMaxHex64(0x12U, 1, lldb::eByteOrderLittle);
392 EXPECT_EQ(2U, bytes);
393 bytes = s.PutMaxHex64(0x1234U, 2, lldb::eByteOrderLittle);
394 EXPECT_EQ(4U, bytes);
395 bytes = s.PutMaxHex64(0x12345678U, 4, lldb::eByteOrderLittle);
396 EXPECT_EQ(8U, bytes);
397 bytes = s.PutMaxHex64(0x1234567890ABCDEFU, 8, lldb::eByteOrderLittle);
398 EXPECT_EQ(16U, bytes);
399 EXPECT_EQ(30U, s.GetWrittenBytes());
400 EXPECT_EQ("12341278563412efcdab9078563412", TakeValue());
403 // Shift operator tests.
405 TEST_F(StreamTest, ShiftOperatorChars) {
406 s << 'a' << 'b';
407 EXPECT_EQ(2U, s.GetWrittenBytes());
408 EXPECT_EQ("ab", TakeValue());
411 TEST_F(StreamTest, ShiftOperatorStrings) {
412 s << "cstring\n";
413 EXPECT_EQ(8U, s.GetWrittenBytes());
414 s << llvm::StringRef("llvm::StringRef\n");
415 EXPECT_EQ(24U, s.GetWrittenBytes());
416 EXPECT_EQ("cstring\nllvm::StringRef\n", TakeValue());
419 TEST_F(StreamTest, ShiftOperatorPtr) {
420 // This test is a bit tricky because pretty much everything related to
421 // pointer printing seems to lead to UB or IB. So let's make the most basic
422 // test that just checks that we print *something*. This way we at least know
423 // that pointer printing doesn't do really bad things (e.g. crashing, reading
424 // OOB/uninitialized memory which the sanitizers would spot).
426 // Shift our own pointer to the output.
427 int i = 3;
428 int *ptr = &i;
429 s << ptr;
431 EXPECT_NE(0U, s.GetWrittenBytes());
432 EXPECT_TRUE(!TakeValue().empty());
435 TEST_F(StreamTest, PutPtr) {
436 // See the ShiftOperatorPtr test for the rationale.
437 int i = 3;
438 int *ptr = &i;
439 s.PutPointer(ptr);
441 EXPECT_NE(0U, s.GetWrittenBytes());
442 EXPECT_TRUE(!TakeValue().empty());
445 // Alias to make it more clear that 'invalid' means for the Stream interface
446 // that it should use the host byte order.
447 const static auto hostByteOrder = lldb::eByteOrderInvalid;
449 // PutRawBytes/PutBytesAsRawHex tests.
451 TEST_F(StreamTest, PutBytesAsRawHex8ToBigEndian) {
452 uint32_t value = 0x12345678;
453 s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
454 hostByteOrder, lldb::eByteOrderBig);
455 EXPECT_EQ(8U, s.GetWrittenBytes());
456 EXPECT_EQ("78563412", TakeValue());
459 TEST_F(StreamTest, PutRawBytesToBigEndian) {
460 uint32_t value = 0x12345678;
461 s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
462 hostByteOrder, lldb::eByteOrderBig);
463 EXPECT_EQ(4U, s.GetWrittenBytes());
464 EXPECT_EQ("\x78\x56\x34\x12", TakeValue());
467 TEST_F(StreamTest, PutBytesAsRawHex8ToLittleEndian) {
468 uint32_t value = 0x12345678;
469 s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
470 hostByteOrder, lldb::eByteOrderLittle);
471 EXPECT_EQ(8U, s.GetWrittenBytes());
472 EXPECT_EQ("12345678", TakeValue());
475 TEST_F(StreamTest, PutRawBytesToLittleEndian) {
476 uint32_t value = 0x12345678;
477 s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
478 hostByteOrder, lldb::eByteOrderLittle);
479 EXPECT_EQ(4U, s.GetWrittenBytes());
480 EXPECT_EQ("\x12\x34\x56\x78", TakeValue());
483 TEST_F(StreamTest, PutBytesAsRawHex8ToMixedEndian) {
484 uint32_t value = 0x12345678;
485 s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
486 hostByteOrder, lldb::eByteOrderPDP);
488 // FIXME: PDP byte order is not actually implemented but Stream just silently
489 // prints the value in some random byte order...
490 #if 0
491 EXPECT_EQ("34127856", TakeValue());
492 #endif
495 TEST_F(StreamTest, PutRawBytesToMixedEndian) {
496 uint32_t value = 0x12345678;
497 s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
498 lldb::eByteOrderInvalid, lldb::eByteOrderPDP);
500 // FIXME: PDP byte order is not actually implemented but Stream just silently
501 // prints the value in some random byte order...
502 #if 0
503 EXPECT_EQ("\x34\x12\x78\x56", TakeValue());
504 #endif
507 TEST_F(StreamTest, PutRawBytesZeroLenght) {
508 uint32_t value = 0x12345678;
510 s.PutRawBytes(static_cast<void *>(&value), 0, hostByteOrder,
511 lldb::eByteOrderLittle);
512 EXPECT_EQ(0U, s.GetWrittenBytes());
514 s.PutRawBytes(static_cast<void *>(&value), 0, hostByteOrder,
515 lldb::eByteOrderBig);
516 EXPECT_EQ(0U, s.GetWrittenBytes());
519 TEST_F(StreamTest, PutBytesAsRawHex8ZeroLenght) {
520 uint32_t value = 0x12345678;
522 s.PutBytesAsRawHex8(static_cast<void *>(&value), 0, hostByteOrder,
523 lldb::eByteOrderLittle);
524 EXPECT_EQ(0U, s.GetWrittenBytes());
526 s.PutBytesAsRawHex8(static_cast<void *>(&value), 0, hostByteOrder,
527 lldb::eByteOrderBig);
528 EXPECT_EQ(0U, s.GetWrittenBytes());
531 // ULEB128 support for binary streams.
533 TEST_F(BinaryStreamTest, PutULEB128OneByte) {
534 auto bytes = s.PutULEB128(0x74ULL);
535 EXPECT_EQ(1U, s.GetWrittenBytes());
536 EXPECT_EQ("\x74", TakeValue());
537 EXPECT_EQ(1U, bytes);
540 TEST_F(BinaryStreamTest, PutULEB128TwoBytes) {
541 auto bytes = s.PutULEB128(0x1985ULL);
542 EXPECT_EQ(2U, s.GetWrittenBytes());
543 EXPECT_EQ("\x85\x33", TakeValue());
544 EXPECT_EQ(2U, bytes);
547 TEST_F(BinaryStreamTest, PutULEB128ThreeBytes) {
548 auto bytes = s.PutULEB128(0x5023ULL);
549 EXPECT_EQ(3U, s.GetWrittenBytes());
550 EXPECT_EQ("\xA3\xA0\x1", TakeValue());
551 EXPECT_EQ(3U, bytes);
554 TEST_F(BinaryStreamTest, PutULEB128FourBytes) {
555 auto bytes = s.PutULEB128(0xA48032ULL);
556 EXPECT_EQ(4U, s.GetWrittenBytes());
557 EXPECT_EQ("\xB2\x80\x92\x5", TakeValue());
558 EXPECT_EQ(4U, bytes);
561 TEST_F(BinaryStreamTest, PutULEB128FiveBytes) {
562 auto bytes = s.PutULEB128(0x12345678ULL);
563 EXPECT_EQ(5U, s.GetWrittenBytes());
564 EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue());
565 EXPECT_EQ(5U, bytes);
568 TEST_F(BinaryStreamTest, PutULEB128SixBytes) {
569 auto bytes = s.PutULEB128(0xABFE3FAFDFULL);
570 EXPECT_EQ(6U, s.GetWrittenBytes());
571 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue());
572 EXPECT_EQ(6U, bytes);
575 TEST_F(BinaryStreamTest, PutULEB128SevenBytes) {
576 auto bytes = s.PutULEB128(0xDABFE3FAFDFULL);
577 EXPECT_EQ(7U, s.GetWrittenBytes());
578 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue());
579 EXPECT_EQ(7U, bytes);
582 TEST_F(BinaryStreamTest, PutULEB128EightBytes) {
583 auto bytes = s.PutULEB128(0x7CDABFE3FAFDFULL);
584 EXPECT_EQ(8U, s.GetWrittenBytes());
585 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue());
586 EXPECT_EQ(8U, bytes);
589 TEST_F(BinaryStreamTest, PutULEB128NineBytes) {
590 auto bytes = s.PutULEB128(0x327CDABFE3FAFDFULL);
591 EXPECT_EQ(9U, s.GetWrittenBytes());
592 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue());
593 EXPECT_EQ(9U, bytes);
596 TEST_F(BinaryStreamTest, PutULEB128MaxValue) {
597 auto bytes = s.PutULEB128(std::numeric_limits<uint64_t>::max());
598 EXPECT_EQ(10U, s.GetWrittenBytes());
599 EXPECT_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1", TakeValue());
600 EXPECT_EQ(10U, bytes);
603 TEST_F(BinaryStreamTest, PutULEB128Zero) {
604 auto bytes = s.PutULEB128(0x0U);
605 EXPECT_EQ(1U, s.GetWrittenBytes());
606 EXPECT_EQ(std::string("\0", 1), TakeValue());
607 EXPECT_EQ(1U, bytes);
610 TEST_F(BinaryStreamTest, PutULEB128One) {
611 auto bytes = s.PutULEB128(0x1U);
612 EXPECT_EQ(1U, s.GetWrittenBytes());
613 EXPECT_EQ("\x1", TakeValue());
614 EXPECT_EQ(1U, bytes);
617 // SLEB128 support for binary streams.
619 TEST_F(BinaryStreamTest, PutSLEB128OneByte) {
620 auto bytes = s.PutSLEB128(0x74LL);
621 EXPECT_EQ(2U, s.GetWrittenBytes());
622 EXPECT_EQ(std::string("\xF4\0", 2), TakeValue());
623 EXPECT_EQ(2U, bytes);
626 TEST_F(BinaryStreamTest, PutSLEB128TwoBytes) {
627 auto bytes = s.PutSLEB128(0x1985LL);
628 EXPECT_EQ(2U, s.GetWrittenBytes());
629 EXPECT_EQ("\x85\x33", TakeValue());
630 EXPECT_EQ(2U, bytes);
633 TEST_F(BinaryStreamTest, PutSLEB128ThreeBytes) {
634 auto bytes = s.PutSLEB128(0x5023LL);
635 EXPECT_EQ(3U, s.GetWrittenBytes());
636 EXPECT_EQ("\xA3\xA0\x1", TakeValue());
637 EXPECT_EQ(3U, bytes);
640 TEST_F(BinaryStreamTest, PutSLEB128FourBytes) {
641 auto bytes = s.PutSLEB128(0xA48032LL);
642 EXPECT_EQ(4U, s.GetWrittenBytes());
643 EXPECT_EQ("\xB2\x80\x92\x5", TakeValue());
644 EXPECT_EQ(4U, bytes);
647 TEST_F(BinaryStreamTest, PutSLEB128FiveBytes) {
648 auto bytes = s.PutSLEB128(0x12345678LL);
649 EXPECT_EQ(5U, s.GetWrittenBytes());
650 EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue());
651 EXPECT_EQ(5U, bytes);
654 TEST_F(BinaryStreamTest, PutSLEB128SixBytes) {
655 auto bytes = s.PutSLEB128(0xABFE3FAFDFLL);
656 EXPECT_EQ(6U, s.GetWrittenBytes());
657 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue());
658 EXPECT_EQ(6U, bytes);
661 TEST_F(BinaryStreamTest, PutSLEB128SevenBytes) {
662 auto bytes = s.PutSLEB128(0xDABFE3FAFDFLL);
663 EXPECT_EQ(7U, s.GetWrittenBytes());
664 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue());
665 EXPECT_EQ(7U, bytes);
668 TEST_F(BinaryStreamTest, PutSLEB128EightBytes) {
669 auto bytes = s.PutSLEB128(0x7CDABFE3FAFDFLL);
670 EXPECT_EQ(8U, s.GetWrittenBytes());
671 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue());
672 EXPECT_EQ(8U, bytes);
675 TEST_F(BinaryStreamTest, PutSLEB128NineBytes) {
676 auto bytes = s.PutSLEB128(0x327CDABFE3FAFDFLL);
677 EXPECT_EQ(9U, s.GetWrittenBytes());
678 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue());
679 EXPECT_EQ(9U, bytes);
682 TEST_F(BinaryStreamTest, PutSLEB128MaxValue) {
683 auto bytes = s.PutSLEB128(std::numeric_limits<int64_t>::max());
684 EXPECT_EQ(10U, s.GetWrittenBytes());
685 EXPECT_EQ(std::string("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\0", 10), TakeValue());
686 EXPECT_EQ(10U, bytes);
689 TEST_F(BinaryStreamTest, PutSLEB128Zero) {
690 auto bytes = s.PutSLEB128(0x0);
691 EXPECT_EQ(1U, s.GetWrittenBytes());
692 EXPECT_EQ(std::string("\0", 1), TakeValue());
693 EXPECT_EQ(1U, bytes);
696 TEST_F(BinaryStreamTest, PutSLEB128One) {
697 auto bytes = s.PutSLEB128(0x1);
698 EXPECT_EQ(1U, s.GetWrittenBytes());
699 EXPECT_EQ(std::string("\x1", 1), TakeValue());
700 EXPECT_EQ(1U, bytes);
703 // SLEB128/ULEB128 support for non-binary streams.
705 // The logic for this is very simple, so it should be enough to test some basic
706 // use cases.
708 TEST_F(StreamTest, PutULEB128) {
709 auto bytes = s.PutULEB128(0x74ULL);
710 EXPECT_EQ(4U, s.GetWrittenBytes());
711 EXPECT_EQ("0x74", TakeValue());
712 EXPECT_EQ(4U, bytes);
715 TEST_F(StreamTest, PutSLEB128) {
716 auto bytes = s.PutSLEB128(0x1985LL);
717 EXPECT_EQ(6U, s.GetWrittenBytes());
718 EXPECT_EQ("0x6533", TakeValue());
719 EXPECT_EQ(6U, bytes);