xref: /llvm-project/lldb/unittests/Utility/StreamTest.cpp (revision 920146316da1e55017d46cfd62be783419da03d5)
180814287SRaphael Isemann //===-- StreamTest.cpp ----------------------------------------------------===//
2b1dfad06SRaphael Isemann //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b1dfad06SRaphael Isemann //
7b1dfad06SRaphael Isemann //===----------------------------------------------------------------------===//
8b1dfad06SRaphael Isemann 
9b1dfad06SRaphael Isemann #include "lldb/Utility/StreamString.h"
10b1dfad06SRaphael Isemann #include "gtest/gtest.h"
11b1dfad06SRaphael Isemann 
12b1dfad06SRaphael Isemann using namespace lldb_private;
13b1dfad06SRaphael Isemann 
14b1dfad06SRaphael Isemann namespace {
15b1dfad06SRaphael Isemann struct StreamTest : ::testing::Test {
16b1dfad06SRaphael Isemann   // Note: Stream is an abstract class, so we use StreamString to test it. To
17b1dfad06SRaphael Isemann   // make it easier to change this later, only methods in this class explicitly
18b1dfad06SRaphael Isemann   // refer to the StringStream class.
19b1dfad06SRaphael Isemann   StreamString s;
20b1dfad06SRaphael Isemann   // We return here a std::string because that way gtest can print better
21b1dfad06SRaphael Isemann   // assertion messages.
TakeValue__anonb7d904710111::StreamTest22b1dfad06SRaphael Isemann   std::string TakeValue() {
23b1dfad06SRaphael Isemann     std::string result = s.GetString().str();
24b1dfad06SRaphael Isemann     s.Clear();
25b1dfad06SRaphael Isemann     return result;
26b1dfad06SRaphael Isemann   }
27b1dfad06SRaphael Isemann };
28b1dfad06SRaphael Isemann }
29b1dfad06SRaphael Isemann 
30b1dfad06SRaphael Isemann namespace {
31b1dfad06SRaphael Isemann // A StreamTest where we expect the Stream output to be binary.
32b1dfad06SRaphael Isemann struct BinaryStreamTest : StreamTest {
SetUp__anonb7d904710211::BinaryStreamTest33b1dfad06SRaphael Isemann   void SetUp() override {
34b1dfad06SRaphael Isemann     s.GetFlags().Set(Stream::eBinary);
35b1dfad06SRaphael Isemann   }
36b1dfad06SRaphael Isemann };
37b1dfad06SRaphael Isemann }
38b1dfad06SRaphael Isemann 
TEST_F(StreamTest,AddressPrefix)3916d20130SRaphael Isemann TEST_F(StreamTest, AddressPrefix) {
401462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, "foo");
4116d20130SRaphael Isemann   EXPECT_EQ("foo0x01", TakeValue());
4216d20130SRaphael Isemann }
4316d20130SRaphael Isemann 
TEST_F(StreamTest,AddressEmptyPrefix)4416d20130SRaphael Isemann TEST_F(StreamTest, AddressEmptyPrefix) {
451462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr);
4616d20130SRaphael Isemann   EXPECT_EQ("0x01", TakeValue());
471462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, "");
4816d20130SRaphael Isemann   EXPECT_EQ("0x01", TakeValue());
4916d20130SRaphael Isemann }
5016d20130SRaphael Isemann 
TEST_F(StreamTest,AddressSuffix)5116d20130SRaphael Isemann TEST_F(StreamTest, AddressSuffix) {
521462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, "foo");
5316d20130SRaphael Isemann   EXPECT_EQ("0x01foo", TakeValue());
5416d20130SRaphael Isemann }
5516d20130SRaphael Isemann 
TEST_F(StreamTest,AddressNoSuffix)5616d20130SRaphael Isemann TEST_F(StreamTest, AddressNoSuffix) {
571462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, nullptr);
5816d20130SRaphael Isemann   EXPECT_EQ("0x01", TakeValue());
591462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, "");
6016d20130SRaphael Isemann   EXPECT_EQ("0x01", TakeValue());
6116d20130SRaphael Isemann }
6216d20130SRaphael Isemann 
TEST_F(StreamTest,AddressPrefixAndSuffix)6316d20130SRaphael Isemann TEST_F(StreamTest, AddressPrefixAndSuffix) {
641462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1, "foo", "bar");
6516d20130SRaphael Isemann   EXPECT_EQ("foo0x01bar", TakeValue());
6616d20130SRaphael Isemann }
6716d20130SRaphael Isemann 
TEST_F(StreamTest,AddressSize)6816d20130SRaphael Isemann TEST_F(StreamTest, AddressSize) {
691462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x0, 0);
7016d20130SRaphael Isemann   EXPECT_EQ("0x0", TakeValue());
711462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 0);
7216d20130SRaphael Isemann   EXPECT_EQ("0x1", TakeValue());
7316d20130SRaphael Isemann 
741462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x1, 1);
7516d20130SRaphael Isemann   EXPECT_EQ("0x01", TakeValue());
761462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0xf1, 1);
7716d20130SRaphael Isemann   EXPECT_EQ("0xf1", TakeValue());
781462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0xff, 1);
7916d20130SRaphael Isemann   EXPECT_EQ("0xff", TakeValue());
801462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x100, 1);
8116d20130SRaphael Isemann   EXPECT_EQ("0x100", TakeValue());
8216d20130SRaphael Isemann 
831462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0xf00, 4);
8416d20130SRaphael Isemann   EXPECT_EQ("0x00000f00", TakeValue());
851462f5a4SRaphael Isemann   DumpAddress(s.AsRawOstream(), 0x100, 8);
8616d20130SRaphael Isemann   EXPECT_EQ("0x0000000000000100", TakeValue());
8716d20130SRaphael Isemann }
8816d20130SRaphael Isemann 
TEST_F(StreamTest,AddressRange)8916d20130SRaphael Isemann TEST_F(StreamTest, AddressRange) {
901462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 2);
9116d20130SRaphael Isemann   EXPECT_EQ("[0x0100-0x0101)", TakeValue());
9216d20130SRaphael Isemann }
9316d20130SRaphael Isemann 
TEST_F(StreamTest,AddressRangeEmptyRange)9416d20130SRaphael Isemann TEST_F(StreamTest, AddressRangeEmptyRange) {
951462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x100, 2);
9616d20130SRaphael Isemann   EXPECT_EQ("[0x0100-0x0100)", TakeValue());
971462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x0, 0x0, 2);
9816d20130SRaphael Isemann   EXPECT_EQ("[0x0000-0x0000)", TakeValue());
9916d20130SRaphael Isemann }
10016d20130SRaphael Isemann 
TEST_F(StreamTest,AddressRangeInvalidRange)10116d20130SRaphael Isemann TEST_F(StreamTest, AddressRangeInvalidRange) {
1021462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x0FF, 2);
10316d20130SRaphael Isemann   EXPECT_EQ("[0x0100-0x00ff)", TakeValue());
1041462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x0, 2);
10516d20130SRaphael Isemann   EXPECT_EQ("[0x0100-0x0000)", TakeValue());
10616d20130SRaphael Isemann }
10716d20130SRaphael Isemann 
TEST_F(StreamTest,AddressRangeSize)10816d20130SRaphael Isemann TEST_F(StreamTest, AddressRangeSize) {
1091462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 0);
11016d20130SRaphael Isemann   EXPECT_EQ("[0x100-0x101)", TakeValue());
1111462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 2);
11216d20130SRaphael Isemann   EXPECT_EQ("[0x0100-0x0101)", TakeValue());
1131462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 4);
11416d20130SRaphael Isemann   EXPECT_EQ("[0x00000100-0x00000101)", TakeValue());
11516d20130SRaphael Isemann 
1161462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 4);
11716d20130SRaphael Isemann   EXPECT_EQ("[0x00000100-0x00000101)", TakeValue());
1181462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x1, 0x101, 4);
11916d20130SRaphael Isemann   EXPECT_EQ("[0x00000001-0x00000101)", TakeValue());
1201462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x101, 0x1, 4);
12116d20130SRaphael Isemann   EXPECT_EQ("[0x00000101-0x00000001)", TakeValue());
12216d20130SRaphael Isemann 
1231462f5a4SRaphael Isemann   DumpAddressRange(s.AsRawOstream(), 0x1, 0x101, 1);
12416d20130SRaphael Isemann   EXPECT_EQ("[0x01-0x101)", TakeValue());
12516d20130SRaphael Isemann }
12616d20130SRaphael Isemann 
TEST_F(StreamTest,ChangingByteOrder)127b1dfad06SRaphael Isemann TEST_F(StreamTest, ChangingByteOrder) {
128b1dfad06SRaphael Isemann   s.SetByteOrder(lldb::eByteOrderPDP);
129b1dfad06SRaphael Isemann   EXPECT_EQ(lldb::eByteOrderPDP, s.GetByteOrder());
130b1dfad06SRaphael Isemann }
131b1dfad06SRaphael Isemann 
TEST_F(StreamTest,SetIndentLevel)1325b61f78aSRaphael Isemann TEST_F(StreamTest, SetIndentLevel) {
1335b61f78aSRaphael Isemann   s.Indent("a");
1345b61f78aSRaphael Isemann   EXPECT_EQ("a", TakeValue());
1355b61f78aSRaphael Isemann 
1365b61f78aSRaphael Isemann   s.SetIndentLevel(3);
1375b61f78aSRaphael Isemann   s.Indent("a");
1385b61f78aSRaphael Isemann   EXPECT_EQ("   a", TakeValue());
1395b61f78aSRaphael Isemann 
1405b61f78aSRaphael Isemann   s.SetIndentLevel(2);
1415b61f78aSRaphael Isemann   s.Indent("a");
1425b61f78aSRaphael Isemann   EXPECT_EQ("  a", TakeValue());
1435b61f78aSRaphael Isemann 
1445b61f78aSRaphael Isemann   s.SetIndentLevel(0);
1455b61f78aSRaphael Isemann   s.Indent("a");
1465b61f78aSRaphael Isemann   EXPECT_EQ("a", TakeValue());
1475b61f78aSRaphael Isemann }
1485b61f78aSRaphael Isemann 
TEST_F(StreamTest,Indent)1495b61f78aSRaphael Isemann TEST_F(StreamTest, Indent) {
1505b61f78aSRaphael Isemann   s.SetIndentLevel(2);
151651936e5SRaphael Isemann   const char *nullptr_cstring = nullptr;
152651936e5SRaphael Isemann   s.Indent(nullptr_cstring);
1535b61f78aSRaphael Isemann   EXPECT_EQ("  ", TakeValue());
1545b61f78aSRaphael Isemann 
1555b61f78aSRaphael Isemann   s.Indent("");
1565b61f78aSRaphael Isemann   EXPECT_EQ("  ", TakeValue());
1575b61f78aSRaphael Isemann 
1585b61f78aSRaphael Isemann   s.Indent(" ");
1595b61f78aSRaphael Isemann   EXPECT_EQ("   ", TakeValue());
1605b61f78aSRaphael Isemann 
1615b61f78aSRaphael Isemann   s.Indent(" aa");
1625b61f78aSRaphael Isemann   EXPECT_EQ("   aa", TakeValue());
1635b61f78aSRaphael Isemann }
1645b61f78aSRaphael Isemann 
TEST_F(StreamTest,PutChar)165b1dfad06SRaphael Isemann TEST_F(StreamTest, PutChar) {
166b1dfad06SRaphael Isemann   s.PutChar('a');
16792b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
168b1dfad06SRaphael Isemann   EXPECT_EQ("a", TakeValue());
169b1dfad06SRaphael Isemann 
170b1dfad06SRaphael Isemann   s.PutChar('1');
17192b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
172b1dfad06SRaphael Isemann   EXPECT_EQ("1", TakeValue());
173b1dfad06SRaphael Isemann }
174b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutCharWhitespace)175b1dfad06SRaphael Isemann TEST_F(StreamTest, PutCharWhitespace) {
176b1dfad06SRaphael Isemann   s.PutChar(' ');
17792b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
178b1dfad06SRaphael Isemann   EXPECT_EQ(" ", TakeValue());
179b1dfad06SRaphael Isemann 
180b1dfad06SRaphael Isemann   s.PutChar('\n');
18192b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
182b1dfad06SRaphael Isemann   EXPECT_EQ("\n", TakeValue());
183b1dfad06SRaphael Isemann 
184b1dfad06SRaphael Isemann   s.PutChar('\r');
18592b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
186b1dfad06SRaphael Isemann   EXPECT_EQ("\r", TakeValue());
187b1dfad06SRaphael Isemann 
188b1dfad06SRaphael Isemann   s.PutChar('\t');
18992b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
190b1dfad06SRaphael Isemann   EXPECT_EQ("\t", TakeValue());
191b1dfad06SRaphael Isemann }
192b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutCString)193b1dfad06SRaphael Isemann TEST_F(StreamTest, PutCString) {
194b1dfad06SRaphael Isemann   s.PutCString("");
19592b16738SRaphael Isemann   EXPECT_EQ(0U, s.GetWrittenBytes());
196b1dfad06SRaphael Isemann   EXPECT_EQ("", TakeValue());
197b1dfad06SRaphael Isemann 
198b1dfad06SRaphael Isemann   s.PutCString("foobar");
19992b16738SRaphael Isemann   EXPECT_EQ(6U, s.GetWrittenBytes());
200b1dfad06SRaphael Isemann   EXPECT_EQ("foobar", TakeValue());
201b1dfad06SRaphael Isemann 
202b1dfad06SRaphael Isemann   s.PutCString(" ");
20392b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
204b1dfad06SRaphael Isemann   EXPECT_EQ(" ", TakeValue());
205b1dfad06SRaphael Isemann }
206b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutCStringWithStringRef)207b1dfad06SRaphael Isemann TEST_F(StreamTest, PutCStringWithStringRef) {
208b1dfad06SRaphael Isemann   s.PutCString(llvm::StringRef(""));
20992b16738SRaphael Isemann   EXPECT_EQ(0U, s.GetWrittenBytes());
210b1dfad06SRaphael Isemann   EXPECT_EQ("", TakeValue());
211b1dfad06SRaphael Isemann 
212b1dfad06SRaphael Isemann   s.PutCString(llvm::StringRef("foobar"));
21392b16738SRaphael Isemann   EXPECT_EQ(6U, s.GetWrittenBytes());
214b1dfad06SRaphael Isemann   EXPECT_EQ("foobar", TakeValue());
215b1dfad06SRaphael Isemann 
216b1dfad06SRaphael Isemann   s.PutCString(llvm::StringRef(" "));
21792b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
218b1dfad06SRaphael Isemann   EXPECT_EQ(" ", TakeValue());
219b1dfad06SRaphael Isemann }
220b1dfad06SRaphael Isemann 
TEST_F(StreamTest,QuotedCString)221b1dfad06SRaphael Isemann TEST_F(StreamTest, QuotedCString) {
222b1dfad06SRaphael Isemann   s.QuotedCString("foo");
22392b16738SRaphael Isemann   EXPECT_EQ(5U, s.GetWrittenBytes());
224b1dfad06SRaphael Isemann   EXPECT_EQ(R"("foo")", TakeValue());
225b1dfad06SRaphael Isemann 
226b1dfad06SRaphael Isemann   s.QuotedCString("ba r");
22792b16738SRaphael Isemann   EXPECT_EQ(6U, s.GetWrittenBytes());
228b1dfad06SRaphael Isemann   EXPECT_EQ(R"("ba r")", TakeValue());
229b1dfad06SRaphael Isemann 
230b1dfad06SRaphael Isemann   s.QuotedCString(" ");
23192b16738SRaphael Isemann   EXPECT_EQ(3U, s.GetWrittenBytes());
232b1dfad06SRaphael Isemann   EXPECT_EQ(R"(" ")", TakeValue());
233b1dfad06SRaphael Isemann }
234b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutCharNull)235b1dfad06SRaphael Isemann TEST_F(StreamTest, PutCharNull) {
236b1dfad06SRaphael Isemann   s.PutChar('\0');
23792b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
238b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("\0", 1), TakeValue());
239b1dfad06SRaphael Isemann 
240b1dfad06SRaphael Isemann   s.PutChar('a');
24192b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
242b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("a", 1), TakeValue());
243b1dfad06SRaphael Isemann }
244b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutStringAsRawHex8)2457f815a9aSPavel Labath TEST_F(StreamTest, PutStringAsRawHex8) {
2467f815a9aSPavel Labath   s.PutStringAsRawHex8("");
24792b16738SRaphael Isemann   EXPECT_EQ(0U, s.GetWrittenBytes());
248f4590de9SRaphael Isemann   EXPECT_EQ("", TakeValue());
249f4590de9SRaphael Isemann 
2507f815a9aSPavel Labath   s.PutStringAsRawHex8("foobar");
25192b16738SRaphael Isemann   EXPECT_EQ(12U, s.GetWrittenBytes());
252b1dfad06SRaphael Isemann   EXPECT_EQ("666f6f626172", TakeValue());
253b1dfad06SRaphael Isemann 
2547f815a9aSPavel Labath   s.PutStringAsRawHex8(" ");
25592b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
256b1dfad06SRaphael Isemann   EXPECT_EQ("20", TakeValue());
257b1dfad06SRaphael Isemann }
258b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex8)259b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex8) {
260b1dfad06SRaphael Isemann   s.PutHex8((uint8_t)55);
26192b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
262b1dfad06SRaphael Isemann   EXPECT_EQ("37", TakeValue());
26392b16738SRaphael Isemann 
264b1dfad06SRaphael Isemann   s.PutHex8(std::numeric_limits<uint8_t>::max());
26592b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
266b1dfad06SRaphael Isemann   EXPECT_EQ("ff", TakeValue());
26792b16738SRaphael Isemann 
268b1dfad06SRaphael Isemann   s.PutHex8((uint8_t)0);
26992b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
270b1dfad06SRaphael Isemann   EXPECT_EQ("00", TakeValue());
271b1dfad06SRaphael Isemann }
272b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutNHex8)273b1dfad06SRaphael Isemann TEST_F(StreamTest, PutNHex8) {
274b1dfad06SRaphael Isemann   s.PutNHex8(0, (uint8_t)55);
27592b16738SRaphael Isemann   EXPECT_EQ(0U, s.GetWrittenBytes());
276b1dfad06SRaphael Isemann   EXPECT_EQ("", TakeValue());
27792b16738SRaphael Isemann 
278b1dfad06SRaphael Isemann   s.PutNHex8(1, (uint8_t)55);
27992b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
280b1dfad06SRaphael Isemann   EXPECT_EQ("37", TakeValue());
28192b16738SRaphael Isemann 
282b1dfad06SRaphael Isemann   s.PutNHex8(2, (uint8_t)55);
28392b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
284b1dfad06SRaphael Isemann   EXPECT_EQ("3737", TakeValue());
28592b16738SRaphael Isemann 
286b1dfad06SRaphael Isemann   s.PutNHex8(1, (uint8_t)56);
28792b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
288b1dfad06SRaphael Isemann   EXPECT_EQ("38", TakeValue());
289b1dfad06SRaphael Isemann }
290b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex16ByteOrderLittle)291b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex16ByteOrderLittle) {
292b1dfad06SRaphael Isemann   s.PutHex16(0x1234U, lldb::eByteOrderLittle);
29392b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
294b1dfad06SRaphael Isemann   EXPECT_EQ("3412", TakeValue());
29592b16738SRaphael Isemann 
296b1dfad06SRaphael Isemann   s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderLittle);
29792b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
298b1dfad06SRaphael Isemann   EXPECT_EQ("ffff", TakeValue());
29992b16738SRaphael Isemann 
300b1dfad06SRaphael Isemann   s.PutHex16(0U, lldb::eByteOrderLittle);
30192b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
302b1dfad06SRaphael Isemann   EXPECT_EQ("0000", TakeValue());
303b1dfad06SRaphael Isemann }
304b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex16ByteOrderBig)305b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex16ByteOrderBig) {
306b1dfad06SRaphael Isemann   s.PutHex16(0x1234U, lldb::eByteOrderBig);
30792b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
308b1dfad06SRaphael Isemann   EXPECT_EQ("1234", TakeValue());
30992b16738SRaphael Isemann 
310b1dfad06SRaphael Isemann   s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderBig);
31192b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
312b1dfad06SRaphael Isemann   EXPECT_EQ("ffff", TakeValue());
31392b16738SRaphael Isemann 
314b1dfad06SRaphael Isemann   s.PutHex16(0U, lldb::eByteOrderBig);
31592b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
316b1dfad06SRaphael Isemann   EXPECT_EQ("0000", TakeValue());
317b1dfad06SRaphael Isemann }
318b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex32ByteOrderLittle)319b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex32ByteOrderLittle) {
320b1dfad06SRaphael Isemann   s.PutHex32(0x12345678U, lldb::eByteOrderLittle);
32192b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
322b1dfad06SRaphael Isemann   EXPECT_EQ("78563412", TakeValue());
32392b16738SRaphael Isemann 
324b1dfad06SRaphael Isemann   s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderLittle);
32592b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
326b1dfad06SRaphael Isemann   EXPECT_EQ("ffffffff", TakeValue());
32792b16738SRaphael Isemann 
328b1dfad06SRaphael Isemann   s.PutHex32(0U, lldb::eByteOrderLittle);
32992b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
330b1dfad06SRaphael Isemann   EXPECT_EQ("00000000", TakeValue());
331b1dfad06SRaphael Isemann }
332b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex32ByteOrderBig)333b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex32ByteOrderBig) {
334b1dfad06SRaphael Isemann   s.PutHex32(0x12345678U, lldb::eByteOrderBig);
33592b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
336b1dfad06SRaphael Isemann   EXPECT_EQ("12345678", TakeValue());
33792b16738SRaphael Isemann 
338b1dfad06SRaphael Isemann   s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderBig);
33992b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
340b1dfad06SRaphael Isemann   EXPECT_EQ("ffffffff", TakeValue());
34192b16738SRaphael Isemann 
342b1dfad06SRaphael Isemann   s.PutHex32(0U, lldb::eByteOrderBig);
34392b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
344b1dfad06SRaphael Isemann   EXPECT_EQ("00000000", TakeValue());
345b1dfad06SRaphael Isemann }
346b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex64ByteOrderLittle)347b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex64ByteOrderLittle) {
348b1dfad06SRaphael Isemann   s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderLittle);
34992b16738SRaphael Isemann   EXPECT_EQ(16U, s.GetWrittenBytes());
350b1dfad06SRaphael Isemann   EXPECT_EQ("efcdab9078563412", TakeValue());
35192b16738SRaphael Isemann 
352b1dfad06SRaphael Isemann   s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderLittle);
35392b16738SRaphael Isemann   EXPECT_EQ(16U, s.GetWrittenBytes());
354b1dfad06SRaphael Isemann   EXPECT_EQ("ffffffffffffffff", TakeValue());
35592b16738SRaphael Isemann 
356b1dfad06SRaphael Isemann   s.PutHex64(0U, lldb::eByteOrderLittle);
35792b16738SRaphael Isemann   EXPECT_EQ(16U, s.GetWrittenBytes());
358b1dfad06SRaphael Isemann   EXPECT_EQ("0000000000000000", TakeValue());
359b1dfad06SRaphael Isemann }
360b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutHex64ByteOrderBig)361b1dfad06SRaphael Isemann TEST_F(StreamTest, PutHex64ByteOrderBig) {
362b1dfad06SRaphael Isemann   s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderBig);
36392b16738SRaphael Isemann   EXPECT_EQ(16U, s.GetWrittenBytes());
364b1dfad06SRaphael Isemann   EXPECT_EQ("1234567890abcdef", TakeValue());
36592b16738SRaphael Isemann 
366b1dfad06SRaphael Isemann   s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderBig);
36792b16738SRaphael Isemann   EXPECT_EQ(16U, s.GetWrittenBytes());
368b1dfad06SRaphael Isemann   EXPECT_EQ("ffffffffffffffff", TakeValue());
36992b16738SRaphael Isemann 
370b1dfad06SRaphael Isemann   s.PutHex64(0U, lldb::eByteOrderBig);
37192b16738SRaphael Isemann   EXPECT_EQ(16U, s.GetWrittenBytes());
372b1dfad06SRaphael Isemann   EXPECT_EQ("0000000000000000", TakeValue());
373b1dfad06SRaphael Isemann }
374b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutMaxHex64ByteOrderBig)3750bb8d83cSRaphael Isemann TEST_F(StreamTest, PutMaxHex64ByteOrderBig) {
3760bb8d83cSRaphael Isemann   std::size_t bytes;
3770bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x12U, 1, lldb::eByteOrderBig);
3780bb8d83cSRaphael Isemann   EXPECT_EQ(2U, bytes);
3790bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x1234U, 2, lldb::eByteOrderBig);
3800bb8d83cSRaphael Isemann   EXPECT_EQ(4U, bytes);
3810bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x12345678U, 4, lldb::eByteOrderBig);
3820bb8d83cSRaphael Isemann   EXPECT_EQ(8U, bytes);
3830bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x1234567890ABCDEFU, 8, lldb::eByteOrderBig);
3840bb8d83cSRaphael Isemann   EXPECT_EQ(16U, bytes);
38592b16738SRaphael Isemann   EXPECT_EQ(30U, s.GetWrittenBytes());
3860bb8d83cSRaphael Isemann   EXPECT_EQ("121234123456781234567890abcdef", TakeValue());
3870bb8d83cSRaphael Isemann }
3880bb8d83cSRaphael Isemann 
TEST_F(StreamTest,PutMaxHex64ByteOrderLittle)3890bb8d83cSRaphael Isemann TEST_F(StreamTest, PutMaxHex64ByteOrderLittle) {
3900bb8d83cSRaphael Isemann   std::size_t bytes;
3910bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x12U, 1, lldb::eByteOrderLittle);
3920bb8d83cSRaphael Isemann   EXPECT_EQ(2U, bytes);
3930bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x1234U, 2, lldb::eByteOrderLittle);
3940bb8d83cSRaphael Isemann   EXPECT_EQ(4U, bytes);
3950bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x12345678U, 4, lldb::eByteOrderLittle);
3960bb8d83cSRaphael Isemann   EXPECT_EQ(8U, bytes);
3970bb8d83cSRaphael Isemann   bytes = s.PutMaxHex64(0x1234567890ABCDEFU, 8, lldb::eByteOrderLittle);
3980bb8d83cSRaphael Isemann   EXPECT_EQ(16U, bytes);
39992b16738SRaphael Isemann   EXPECT_EQ(30U, s.GetWrittenBytes());
4000bb8d83cSRaphael Isemann   EXPECT_EQ("12341278563412efcdab9078563412", TakeValue());
4010bb8d83cSRaphael Isemann }
4020bb8d83cSRaphael Isemann 
403b1dfad06SRaphael Isemann // Shift operator tests.
404b1dfad06SRaphael Isemann 
TEST_F(StreamTest,ShiftOperatorChars)405b1dfad06SRaphael Isemann TEST_F(StreamTest, ShiftOperatorChars) {
406b1dfad06SRaphael Isemann   s << 'a' << 'b';
40792b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
408b1dfad06SRaphael Isemann   EXPECT_EQ("ab", TakeValue());
409b1dfad06SRaphael Isemann }
410b1dfad06SRaphael Isemann 
TEST_F(StreamTest,ShiftOperatorStrings)411b1dfad06SRaphael Isemann TEST_F(StreamTest, ShiftOperatorStrings) {
412b1dfad06SRaphael Isemann   s << "cstring\n";
41392b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
414b1dfad06SRaphael Isemann   s << llvm::StringRef("llvm::StringRef\n");
41592b16738SRaphael Isemann   EXPECT_EQ(24U, s.GetWrittenBytes());
416b1dfad06SRaphael Isemann   EXPECT_EQ("cstring\nllvm::StringRef\n", TakeValue());
417b1dfad06SRaphael Isemann }
418b1dfad06SRaphael Isemann 
TEST_F(StreamTest,ShiftOperatorPtr)419b1dfad06SRaphael Isemann TEST_F(StreamTest, ShiftOperatorPtr) {
420b1dfad06SRaphael Isemann   // This test is a bit tricky because pretty much everything related to
421b1dfad06SRaphael Isemann   // pointer printing seems to lead to UB or IB. So let's make the most basic
422b1dfad06SRaphael Isemann   // test that just checks that we print *something*. This way we at least know
423b1dfad06SRaphael Isemann   // that pointer printing doesn't do really bad things (e.g. crashing, reading
424b1dfad06SRaphael Isemann   // OOB/uninitialized memory which the sanitizers would spot).
425b1dfad06SRaphael Isemann 
426b1dfad06SRaphael Isemann   // Shift our own pointer to the output.
427b1dfad06SRaphael Isemann   int i = 3;
428b1dfad06SRaphael Isemann   int *ptr = &i;
429b1dfad06SRaphael Isemann   s << ptr;
430b1dfad06SRaphael Isemann 
43192b16738SRaphael Isemann   EXPECT_NE(0U, s.GetWrittenBytes());
432b1dfad06SRaphael Isemann   EXPECT_TRUE(!TakeValue().empty());
433b1dfad06SRaphael Isemann }
434b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutPtr)435b1dfad06SRaphael Isemann TEST_F(StreamTest, PutPtr) {
436b1dfad06SRaphael Isemann   // See the ShiftOperatorPtr test for the rationale.
437b1dfad06SRaphael Isemann   int i = 3;
438b1dfad06SRaphael Isemann   int *ptr = &i;
439b1dfad06SRaphael Isemann   s.PutPointer(ptr);
440b1dfad06SRaphael Isemann 
44192b16738SRaphael Isemann   EXPECT_NE(0U, s.GetWrittenBytes());
442b1dfad06SRaphael Isemann   EXPECT_TRUE(!TakeValue().empty());
443b1dfad06SRaphael Isemann }
444b1dfad06SRaphael Isemann 
445b1dfad06SRaphael Isemann // Alias to make it more clear that 'invalid' means for the Stream interface
446b1dfad06SRaphael Isemann // that it should use the host byte order.
447b1dfad06SRaphael Isemann const static auto hostByteOrder = lldb::eByteOrderInvalid;
448b1dfad06SRaphael Isemann 
449b1dfad06SRaphael Isemann // PutRawBytes/PutBytesAsRawHex tests.
450b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutBytesAsRawHex8ToBigEndian)451b1dfad06SRaphael Isemann TEST_F(StreamTest, PutBytesAsRawHex8ToBigEndian) {
452b1dfad06SRaphael Isemann   uint32_t value = 0x12345678;
453b1dfad06SRaphael Isemann   s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
454b1dfad06SRaphael Isemann                       hostByteOrder, lldb::eByteOrderBig);
45592b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
456b1dfad06SRaphael Isemann   EXPECT_EQ("78563412", TakeValue());
457b1dfad06SRaphael Isemann }
458b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutRawBytesToBigEndian)459b1dfad06SRaphael Isemann TEST_F(StreamTest, PutRawBytesToBigEndian) {
460b1dfad06SRaphael Isemann   uint32_t value = 0x12345678;
461b1dfad06SRaphael Isemann   s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
462b1dfad06SRaphael Isemann                       hostByteOrder, lldb::eByteOrderBig);
46392b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
464b1dfad06SRaphael Isemann   EXPECT_EQ("\x78\x56\x34\x12", TakeValue());
465b1dfad06SRaphael Isemann }
466b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutBytesAsRawHex8ToLittleEndian)467b1dfad06SRaphael Isemann TEST_F(StreamTest, PutBytesAsRawHex8ToLittleEndian) {
468b1dfad06SRaphael Isemann   uint32_t value = 0x12345678;
469b1dfad06SRaphael Isemann   s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
470b1dfad06SRaphael Isemann                       hostByteOrder, lldb::eByteOrderLittle);
47192b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
472b1dfad06SRaphael Isemann   EXPECT_EQ("12345678", TakeValue());
473b1dfad06SRaphael Isemann }
474b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutRawBytesToLittleEndian)475b1dfad06SRaphael Isemann TEST_F(StreamTest, PutRawBytesToLittleEndian) {
476b1dfad06SRaphael Isemann   uint32_t value = 0x12345678;
477b1dfad06SRaphael Isemann   s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
478b1dfad06SRaphael Isemann                       hostByteOrder, lldb::eByteOrderLittle);
47992b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
480b1dfad06SRaphael Isemann   EXPECT_EQ("\x12\x34\x56\x78", TakeValue());
481b1dfad06SRaphael Isemann }
482b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutBytesAsRawHex8ToMixedEndian)483b1dfad06SRaphael Isemann TEST_F(StreamTest, PutBytesAsRawHex8ToMixedEndian) {
484b1dfad06SRaphael Isemann   uint32_t value = 0x12345678;
485b1dfad06SRaphael Isemann   s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
486b1dfad06SRaphael Isemann                       hostByteOrder, lldb::eByteOrderPDP);
487b1dfad06SRaphael Isemann 
488b1dfad06SRaphael Isemann   // FIXME: PDP byte order is not actually implemented but Stream just silently
489b1dfad06SRaphael Isemann   // prints the value in some random byte order...
490b1dfad06SRaphael Isemann #if 0
491b1dfad06SRaphael Isemann   EXPECT_EQ("34127856", TakeValue());
492b1dfad06SRaphael Isemann #endif
493b1dfad06SRaphael Isemann }
494b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutRawBytesToMixedEndian)495b1dfad06SRaphael Isemann TEST_F(StreamTest, PutRawBytesToMixedEndian) {
496b1dfad06SRaphael Isemann   uint32_t value = 0x12345678;
497b1dfad06SRaphael Isemann   s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
498b1dfad06SRaphael Isemann                       lldb::eByteOrderInvalid, lldb::eByteOrderPDP);
499b1dfad06SRaphael Isemann 
500b1dfad06SRaphael Isemann   // FIXME: PDP byte order is not actually implemented but Stream just silently
501b1dfad06SRaphael Isemann   // prints the value in some random byte order...
502b1dfad06SRaphael Isemann #if 0
503b1dfad06SRaphael Isemann   EXPECT_EQ("\x34\x12\x78\x56", TakeValue());
504b1dfad06SRaphael Isemann #endif
505b1dfad06SRaphael Isemann }
506b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutRawBytesZeroLenght)507*92014631SJonas Devlieghere TEST_F(StreamTest, PutRawBytesZeroLenght) {
508*92014631SJonas Devlieghere   uint32_t value = 0x12345678;
509*92014631SJonas Devlieghere 
510*92014631SJonas Devlieghere   s.PutRawBytes(static_cast<void *>(&value), 0, hostByteOrder,
511*92014631SJonas Devlieghere                 lldb::eByteOrderLittle);
512*92014631SJonas Devlieghere   EXPECT_EQ(0U, s.GetWrittenBytes());
513*92014631SJonas Devlieghere 
514*92014631SJonas Devlieghere   s.PutRawBytes(static_cast<void *>(&value), 0, hostByteOrder,
515*92014631SJonas Devlieghere                 lldb::eByteOrderBig);
516*92014631SJonas Devlieghere   EXPECT_EQ(0U, s.GetWrittenBytes());
517*92014631SJonas Devlieghere }
518*92014631SJonas Devlieghere 
TEST_F(StreamTest,PutBytesAsRawHex8ZeroLenght)519*92014631SJonas Devlieghere TEST_F(StreamTest, PutBytesAsRawHex8ZeroLenght) {
520*92014631SJonas Devlieghere   uint32_t value = 0x12345678;
521*92014631SJonas Devlieghere 
522*92014631SJonas Devlieghere   s.PutBytesAsRawHex8(static_cast<void *>(&value), 0, hostByteOrder,
523*92014631SJonas Devlieghere                       lldb::eByteOrderLittle);
524*92014631SJonas Devlieghere   EXPECT_EQ(0U, s.GetWrittenBytes());
525*92014631SJonas Devlieghere 
526*92014631SJonas Devlieghere   s.PutBytesAsRawHex8(static_cast<void *>(&value), 0, hostByteOrder,
527*92014631SJonas Devlieghere                       lldb::eByteOrderBig);
528*92014631SJonas Devlieghere   EXPECT_EQ(0U, s.GetWrittenBytes());
529*92014631SJonas Devlieghere }
530*92014631SJonas Devlieghere 
531b1dfad06SRaphael Isemann // ULEB128 support for binary streams.
532b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128OneByte)533b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128OneByte) {
534b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x74ULL);
53592b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
536b1dfad06SRaphael Isemann   EXPECT_EQ("\x74", TakeValue());
537b1dfad06SRaphael Isemann   EXPECT_EQ(1U, bytes);
538b1dfad06SRaphael Isemann }
539b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128TwoBytes)540b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128TwoBytes) {
541b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x1985ULL);
54292b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
543b1dfad06SRaphael Isemann   EXPECT_EQ("\x85\x33", TakeValue());
544b1dfad06SRaphael Isemann   EXPECT_EQ(2U, bytes);
545b1dfad06SRaphael Isemann }
546b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128ThreeBytes)547b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128ThreeBytes) {
548b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x5023ULL);
54992b16738SRaphael Isemann   EXPECT_EQ(3U, s.GetWrittenBytes());
550b1dfad06SRaphael Isemann   EXPECT_EQ("\xA3\xA0\x1", TakeValue());
551b1dfad06SRaphael Isemann   EXPECT_EQ(3U, bytes);
552b1dfad06SRaphael Isemann }
553b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128FourBytes)554b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128FourBytes) {
555b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0xA48032ULL);
55692b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
557b1dfad06SRaphael Isemann   EXPECT_EQ("\xB2\x80\x92\x5", TakeValue());
558b1dfad06SRaphael Isemann   EXPECT_EQ(4U, bytes);
559b1dfad06SRaphael Isemann }
560b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128FiveBytes)561b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128FiveBytes) {
562b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x12345678ULL);
56392b16738SRaphael Isemann   EXPECT_EQ(5U, s.GetWrittenBytes());
564b1dfad06SRaphael Isemann   EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue());
565b1dfad06SRaphael Isemann   EXPECT_EQ(5U, bytes);
566b1dfad06SRaphael Isemann }
567b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128SixBytes)568b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128SixBytes) {
569b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0xABFE3FAFDFULL);
57092b16738SRaphael Isemann   EXPECT_EQ(6U, s.GetWrittenBytes());
571b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue());
572b1dfad06SRaphael Isemann   EXPECT_EQ(6U, bytes);
573b1dfad06SRaphael Isemann }
574b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128SevenBytes)575b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128SevenBytes) {
576b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0xDABFE3FAFDFULL);
57792b16738SRaphael Isemann   EXPECT_EQ(7U, s.GetWrittenBytes());
578b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue());
579b1dfad06SRaphael Isemann   EXPECT_EQ(7U, bytes);
580b1dfad06SRaphael Isemann }
581b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128EightBytes)582b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128EightBytes) {
583b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x7CDABFE3FAFDFULL);
58492b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
585b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue());
586b1dfad06SRaphael Isemann   EXPECT_EQ(8U, bytes);
587b1dfad06SRaphael Isemann }
588b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128NineBytes)589b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128NineBytes) {
590b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x327CDABFE3FAFDFULL);
59192b16738SRaphael Isemann   EXPECT_EQ(9U, s.GetWrittenBytes());
592b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue());
593b1dfad06SRaphael Isemann   EXPECT_EQ(9U, bytes);
594b1dfad06SRaphael Isemann }
595b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128MaxValue)596b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128MaxValue) {
597b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(std::numeric_limits<uint64_t>::max());
59892b16738SRaphael Isemann   EXPECT_EQ(10U, s.GetWrittenBytes());
599b1dfad06SRaphael Isemann   EXPECT_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1", TakeValue());
600b1dfad06SRaphael Isemann   EXPECT_EQ(10U, bytes);
601b1dfad06SRaphael Isemann }
602b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128Zero)603b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128Zero) {
604b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x0U);
60592b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
606b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("\0", 1), TakeValue());
607b1dfad06SRaphael Isemann   EXPECT_EQ(1U, bytes);
608b1dfad06SRaphael Isemann }
609b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutULEB128One)610b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutULEB128One) {
611b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x1U);
61292b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
613b1dfad06SRaphael Isemann   EXPECT_EQ("\x1", TakeValue());
614b1dfad06SRaphael Isemann   EXPECT_EQ(1U, bytes);
615b1dfad06SRaphael Isemann }
616b1dfad06SRaphael Isemann 
617b1dfad06SRaphael Isemann // SLEB128 support for binary streams.
618b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128OneByte)619b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128OneByte) {
620b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x74LL);
62192b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
622b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("\xF4\0", 2), TakeValue());
623b1dfad06SRaphael Isemann   EXPECT_EQ(2U, bytes);
624b1dfad06SRaphael Isemann }
625b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128TwoBytes)626b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128TwoBytes) {
627b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x1985LL);
62892b16738SRaphael Isemann   EXPECT_EQ(2U, s.GetWrittenBytes());
629b1dfad06SRaphael Isemann   EXPECT_EQ("\x85\x33", TakeValue());
630b1dfad06SRaphael Isemann   EXPECT_EQ(2U, bytes);
631b1dfad06SRaphael Isemann }
632b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128ThreeBytes)633b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128ThreeBytes) {
634b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x5023LL);
63592b16738SRaphael Isemann   EXPECT_EQ(3U, s.GetWrittenBytes());
636b1dfad06SRaphael Isemann   EXPECT_EQ("\xA3\xA0\x1", TakeValue());
637b1dfad06SRaphael Isemann   EXPECT_EQ(3U, bytes);
638b1dfad06SRaphael Isemann }
639b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128FourBytes)640b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128FourBytes) {
641b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0xA48032LL);
64292b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
643b1dfad06SRaphael Isemann   EXPECT_EQ("\xB2\x80\x92\x5", TakeValue());
644b1dfad06SRaphael Isemann   EXPECT_EQ(4U, bytes);
645b1dfad06SRaphael Isemann }
646b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128FiveBytes)647b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128FiveBytes) {
648b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x12345678LL);
64992b16738SRaphael Isemann   EXPECT_EQ(5U, s.GetWrittenBytes());
650b1dfad06SRaphael Isemann   EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue());
651b1dfad06SRaphael Isemann   EXPECT_EQ(5U, bytes);
652b1dfad06SRaphael Isemann }
653b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128SixBytes)654b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128SixBytes) {
655b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0xABFE3FAFDFLL);
65692b16738SRaphael Isemann   EXPECT_EQ(6U, s.GetWrittenBytes());
657b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue());
658b1dfad06SRaphael Isemann   EXPECT_EQ(6U, bytes);
659b1dfad06SRaphael Isemann }
660b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128SevenBytes)661b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128SevenBytes) {
662b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0xDABFE3FAFDFLL);
66392b16738SRaphael Isemann   EXPECT_EQ(7U, s.GetWrittenBytes());
664b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue());
665b1dfad06SRaphael Isemann   EXPECT_EQ(7U, bytes);
666b1dfad06SRaphael Isemann }
667b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128EightBytes)668b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128EightBytes) {
669b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x7CDABFE3FAFDFLL);
67092b16738SRaphael Isemann   EXPECT_EQ(8U, s.GetWrittenBytes());
671b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue());
672b1dfad06SRaphael Isemann   EXPECT_EQ(8U, bytes);
673b1dfad06SRaphael Isemann }
674b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128NineBytes)675b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128NineBytes) {
676b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x327CDABFE3FAFDFLL);
67792b16738SRaphael Isemann   EXPECT_EQ(9U, s.GetWrittenBytes());
678b1dfad06SRaphael Isemann   EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue());
679b1dfad06SRaphael Isemann   EXPECT_EQ(9U, bytes);
680b1dfad06SRaphael Isemann }
681b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128MaxValue)682b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128MaxValue) {
683b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(std::numeric_limits<int64_t>::max());
68492b16738SRaphael Isemann   EXPECT_EQ(10U, s.GetWrittenBytes());
685b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\0", 10), TakeValue());
686b1dfad06SRaphael Isemann   EXPECT_EQ(10U, bytes);
687b1dfad06SRaphael Isemann }
688b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128Zero)689b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128Zero) {
690b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x0);
69192b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
692b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("\0", 1), TakeValue());
693b1dfad06SRaphael Isemann   EXPECT_EQ(1U, bytes);
694b1dfad06SRaphael Isemann }
695b1dfad06SRaphael Isemann 
TEST_F(BinaryStreamTest,PutSLEB128One)696b1dfad06SRaphael Isemann TEST_F(BinaryStreamTest, PutSLEB128One) {
697b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x1);
69892b16738SRaphael Isemann   EXPECT_EQ(1U, s.GetWrittenBytes());
699b1dfad06SRaphael Isemann   EXPECT_EQ(std::string("\x1", 1), TakeValue());
700b1dfad06SRaphael Isemann   EXPECT_EQ(1U, bytes);
701b1dfad06SRaphael Isemann }
702b1dfad06SRaphael Isemann 
703b1dfad06SRaphael Isemann // SLEB128/ULEB128 support for non-binary streams.
704b1dfad06SRaphael Isemann 
705b1dfad06SRaphael Isemann // The logic for this is very simple, so it should be enough to test some basic
706b1dfad06SRaphael Isemann // use cases.
707b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutULEB128)708b1dfad06SRaphael Isemann TEST_F(StreamTest, PutULEB128) {
709b1dfad06SRaphael Isemann   auto bytes = s.PutULEB128(0x74ULL);
71092b16738SRaphael Isemann   EXPECT_EQ(4U, s.GetWrittenBytes());
711b1dfad06SRaphael Isemann   EXPECT_EQ("0x74", TakeValue());
712b1dfad06SRaphael Isemann   EXPECT_EQ(4U, bytes);
713b1dfad06SRaphael Isemann }
714b1dfad06SRaphael Isemann 
TEST_F(StreamTest,PutSLEB128)715b1dfad06SRaphael Isemann TEST_F(StreamTest, PutSLEB128) {
716b1dfad06SRaphael Isemann   auto bytes = s.PutSLEB128(0x1985LL);
71792b16738SRaphael Isemann   EXPECT_EQ(6U, s.GetWrittenBytes());
718b1dfad06SRaphael Isemann   EXPECT_EQ("0x6533", TakeValue());
719b1dfad06SRaphael Isemann   EXPECT_EQ(6U, bytes);
720b1dfad06SRaphael Isemann }
721