blob: 940d49fdfdb7df21acd84a2ab983f2b42c0a7aa8 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- StreamTest.cpp ----------------------------------------------------===//
Raphael Isemannb1dfad02018-08-01 06:04:48 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Raphael Isemannb1dfad02018-08-01 06:04:48 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Utility/StreamString.h"
10#include "gtest/gtest.h"
11
12using namespace lldb_private;
13
14namespace {
15struct 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;
26 }
27};
28}
29
30namespace {
31// A StreamTest where we expect the Stream output to be binary.
32struct BinaryStreamTest : StreamTest {
33 void SetUp() override {
34 s.GetFlags().Set(Stream::eBinary);
35 }
36};
37}
38
Raphael Isemann16d20132019-12-04 10:27:18 +010039TEST_F(StreamTest, AddressPrefix) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010040 DumpAddress(s.AsRawOstream(), 0x1, 1, "foo");
Raphael Isemann16d20132019-12-04 10:27:18 +010041 EXPECT_EQ("foo0x01", TakeValue());
42}
43
44TEST_F(StreamTest, AddressEmptyPrefix) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010045 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr);
Raphael Isemann16d20132019-12-04 10:27:18 +010046 EXPECT_EQ("0x01", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010047 DumpAddress(s.AsRawOstream(), 0x1, 1, "");
Raphael Isemann16d20132019-12-04 10:27:18 +010048 EXPECT_EQ("0x01", TakeValue());
49}
50
51TEST_F(StreamTest, AddressSuffix) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010052 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, "foo");
Raphael Isemann16d20132019-12-04 10:27:18 +010053 EXPECT_EQ("0x01foo", TakeValue());
54}
55
56TEST_F(StreamTest, AddressNoSuffix) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010057 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, nullptr);
Raphael Isemann16d20132019-12-04 10:27:18 +010058 EXPECT_EQ("0x01", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010059 DumpAddress(s.AsRawOstream(), 0x1, 1, nullptr, "");
Raphael Isemann16d20132019-12-04 10:27:18 +010060 EXPECT_EQ("0x01", TakeValue());
61}
62
63TEST_F(StreamTest, AddressPrefixAndSuffix) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010064 DumpAddress(s.AsRawOstream(), 0x1, 1, "foo", "bar");
Raphael Isemann16d20132019-12-04 10:27:18 +010065 EXPECT_EQ("foo0x01bar", TakeValue());
66}
67
68TEST_F(StreamTest, AddressSize) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010069 DumpAddress(s.AsRawOstream(), 0x0, 0);
Raphael Isemann16d20132019-12-04 10:27:18 +010070 EXPECT_EQ("0x0", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010071 DumpAddress(s.AsRawOstream(), 0x1, 0);
Raphael Isemann16d20132019-12-04 10:27:18 +010072 EXPECT_EQ("0x1", TakeValue());
73
Raphael Isemann1462f5a2019-12-05 14:41:09 +010074 DumpAddress(s.AsRawOstream(), 0x1, 1);
Raphael Isemann16d20132019-12-04 10:27:18 +010075 EXPECT_EQ("0x01", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010076 DumpAddress(s.AsRawOstream(), 0xf1, 1);
Raphael Isemann16d20132019-12-04 10:27:18 +010077 EXPECT_EQ("0xf1", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010078 DumpAddress(s.AsRawOstream(), 0xff, 1);
Raphael Isemann16d20132019-12-04 10:27:18 +010079 EXPECT_EQ("0xff", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010080 DumpAddress(s.AsRawOstream(), 0x100, 1);
Raphael Isemann16d20132019-12-04 10:27:18 +010081 EXPECT_EQ("0x100", TakeValue());
82
Raphael Isemann1462f5a2019-12-05 14:41:09 +010083 DumpAddress(s.AsRawOstream(), 0xf00, 4);
Raphael Isemann16d20132019-12-04 10:27:18 +010084 EXPECT_EQ("0x00000f00", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010085 DumpAddress(s.AsRawOstream(), 0x100, 8);
Raphael Isemann16d20132019-12-04 10:27:18 +010086 EXPECT_EQ("0x0000000000000100", TakeValue());
Raphael Isemann16d20132019-12-04 10:27:18 +010087}
88
89TEST_F(StreamTest, AddressRange) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010090 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 2);
Raphael Isemann16d20132019-12-04 10:27:18 +010091 EXPECT_EQ("[0x0100-0x0101)", TakeValue());
92}
93
94TEST_F(StreamTest, AddressRangeEmptyRange) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +010095 DumpAddressRange(s.AsRawOstream(), 0x100, 0x100, 2);
Raphael Isemann16d20132019-12-04 10:27:18 +010096 EXPECT_EQ("[0x0100-0x0100)", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +010097 DumpAddressRange(s.AsRawOstream(), 0x0, 0x0, 2);
Raphael Isemann16d20132019-12-04 10:27:18 +010098 EXPECT_EQ("[0x0000-0x0000)", TakeValue());
99}
100
101TEST_F(StreamTest, AddressRangeInvalidRange) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100102 DumpAddressRange(s.AsRawOstream(), 0x100, 0x0FF, 2);
Raphael Isemann16d20132019-12-04 10:27:18 +0100103 EXPECT_EQ("[0x0100-0x00ff)", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100104 DumpAddressRange(s.AsRawOstream(), 0x100, 0x0, 2);
Raphael Isemann16d20132019-12-04 10:27:18 +0100105 EXPECT_EQ("[0x0100-0x0000)", TakeValue());
106}
107
108TEST_F(StreamTest, AddressRangeSize) {
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100109 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 0);
Raphael Isemann16d20132019-12-04 10:27:18 +0100110 EXPECT_EQ("[0x100-0x101)", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100111 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 2);
Raphael Isemann16d20132019-12-04 10:27:18 +0100112 EXPECT_EQ("[0x0100-0x0101)", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100113 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 4);
Raphael Isemann16d20132019-12-04 10:27:18 +0100114 EXPECT_EQ("[0x00000100-0x00000101)", TakeValue());
115
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100116 DumpAddressRange(s.AsRawOstream(), 0x100, 0x101, 4);
Raphael Isemann16d20132019-12-04 10:27:18 +0100117 EXPECT_EQ("[0x00000100-0x00000101)", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100118 DumpAddressRange(s.AsRawOstream(), 0x1, 0x101, 4);
Raphael Isemann16d20132019-12-04 10:27:18 +0100119 EXPECT_EQ("[0x00000001-0x00000101)", TakeValue());
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100120 DumpAddressRange(s.AsRawOstream(), 0x101, 0x1, 4);
Raphael Isemann16d20132019-12-04 10:27:18 +0100121 EXPECT_EQ("[0x00000101-0x00000001)", TakeValue());
122
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100123 DumpAddressRange(s.AsRawOstream(), 0x1, 0x101, 1);
Raphael Isemann16d20132019-12-04 10:27:18 +0100124 EXPECT_EQ("[0x01-0x101)", TakeValue());
125}
126
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000127TEST_F(StreamTest, ChangingByteOrder) {
128 s.SetByteOrder(lldb::eByteOrderPDP);
129 EXPECT_EQ(lldb::eByteOrderPDP, s.GetByteOrder());
130}
131
Raphael Isemann5b61f782020-02-11 11:12:54 +0100132TEST_F(StreamTest, SetIndentLevel) {
133 s.Indent("a");
134 EXPECT_EQ("a", TakeValue());
135
136 s.SetIndentLevel(3);
137 s.Indent("a");
138 EXPECT_EQ(" a", TakeValue());
139
140 s.SetIndentLevel(2);
141 s.Indent("a");
142 EXPECT_EQ(" a", TakeValue());
143
144 s.SetIndentLevel(0);
145 s.Indent("a");
146 EXPECT_EQ("a", TakeValue());
147}
148
149TEST_F(StreamTest, Indent) {
150 s.SetIndentLevel(2);
Raphael Isemann651936e2020-02-11 13:04:48 +0100151 const char *nullptr_cstring = nullptr;
152 s.Indent(nullptr_cstring);
Raphael Isemann5b61f782020-02-11 11:12:54 +0100153 EXPECT_EQ(" ", TakeValue());
154
155 s.Indent("");
156 EXPECT_EQ(" ", TakeValue());
157
158 s.Indent(" ");
159 EXPECT_EQ(" ", TakeValue());
160
161 s.Indent(" aa");
162 EXPECT_EQ(" aa", TakeValue());
163}
164
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000165TEST_F(StreamTest, PutChar) {
166 s.PutChar('a');
Raphael Isemann92b16732018-08-02 16:38:34 +0000167 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000168 EXPECT_EQ("a", TakeValue());
169
170 s.PutChar('1');
Raphael Isemann92b16732018-08-02 16:38:34 +0000171 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000172 EXPECT_EQ("1", TakeValue());
173}
174
175TEST_F(StreamTest, PutCharWhitespace) {
176 s.PutChar(' ');
Raphael Isemann92b16732018-08-02 16:38:34 +0000177 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000178 EXPECT_EQ(" ", TakeValue());
179
180 s.PutChar('\n');
Raphael Isemann92b16732018-08-02 16:38:34 +0000181 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000182 EXPECT_EQ("\n", TakeValue());
183
184 s.PutChar('\r');
Raphael Isemann92b16732018-08-02 16:38:34 +0000185 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000186 EXPECT_EQ("\r", TakeValue());
187
188 s.PutChar('\t');
Raphael Isemann92b16732018-08-02 16:38:34 +0000189 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000190 EXPECT_EQ("\t", TakeValue());
191}
192
193TEST_F(StreamTest, PutCString) {
194 s.PutCString("");
Raphael Isemann92b16732018-08-02 16:38:34 +0000195 EXPECT_EQ(0U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000196 EXPECT_EQ("", TakeValue());
197
198 s.PutCString("foobar");
Raphael Isemann92b16732018-08-02 16:38:34 +0000199 EXPECT_EQ(6U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000200 EXPECT_EQ("foobar", TakeValue());
201
202 s.PutCString(" ");
Raphael Isemann92b16732018-08-02 16:38:34 +0000203 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000204 EXPECT_EQ(" ", TakeValue());
205}
206
207TEST_F(StreamTest, PutCStringWithStringRef) {
208 s.PutCString(llvm::StringRef(""));
Raphael Isemann92b16732018-08-02 16:38:34 +0000209 EXPECT_EQ(0U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000210 EXPECT_EQ("", TakeValue());
211
212 s.PutCString(llvm::StringRef("foobar"));
Raphael Isemann92b16732018-08-02 16:38:34 +0000213 EXPECT_EQ(6U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000214 EXPECT_EQ("foobar", TakeValue());
215
216 s.PutCString(llvm::StringRef(" "));
Raphael Isemann92b16732018-08-02 16:38:34 +0000217 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000218 EXPECT_EQ(" ", TakeValue());
219}
220
221TEST_F(StreamTest, QuotedCString) {
222 s.QuotedCString("foo");
Raphael Isemann92b16732018-08-02 16:38:34 +0000223 EXPECT_EQ(5U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000224 EXPECT_EQ(R"("foo")", TakeValue());
225
226 s.QuotedCString("ba r");
Raphael Isemann92b16732018-08-02 16:38:34 +0000227 EXPECT_EQ(6U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000228 EXPECT_EQ(R"("ba r")", TakeValue());
229
230 s.QuotedCString(" ");
Raphael Isemann92b16732018-08-02 16:38:34 +0000231 EXPECT_EQ(3U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000232 EXPECT_EQ(R"(" ")", TakeValue());
233}
234
235TEST_F(StreamTest, PutCharNull) {
236 s.PutChar('\0');
Raphael Isemann92b16732018-08-02 16:38:34 +0000237 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000238 EXPECT_EQ(std::string("\0", 1), TakeValue());
239
240 s.PutChar('a');
Raphael Isemann92b16732018-08-02 16:38:34 +0000241 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000242 EXPECT_EQ(std::string("a", 1), TakeValue());
243}
244
Pavel Labath7f815a92019-02-12 14:28:55 +0000245TEST_F(StreamTest, PutStringAsRawHex8) {
246 s.PutStringAsRawHex8("");
Raphael Isemann92b16732018-08-02 16:38:34 +0000247 EXPECT_EQ(0U, s.GetWrittenBytes());
Raphael Isemannf4590de2018-08-01 21:07:18 +0000248 EXPECT_EQ("", TakeValue());
249
Pavel Labath7f815a92019-02-12 14:28:55 +0000250 s.PutStringAsRawHex8("foobar");
Raphael Isemann92b16732018-08-02 16:38:34 +0000251 EXPECT_EQ(12U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000252 EXPECT_EQ("666f6f626172", TakeValue());
253
Pavel Labath7f815a92019-02-12 14:28:55 +0000254 s.PutStringAsRawHex8(" ");
Raphael Isemann92b16732018-08-02 16:38:34 +0000255 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000256 EXPECT_EQ("20", TakeValue());
257}
258
259TEST_F(StreamTest, PutHex8) {
260 s.PutHex8((uint8_t)55);
Raphael Isemann92b16732018-08-02 16:38:34 +0000261 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000262 EXPECT_EQ("37", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000263
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000264 s.PutHex8(std::numeric_limits<uint8_t>::max());
Raphael Isemann92b16732018-08-02 16:38:34 +0000265 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000266 EXPECT_EQ("ff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000267
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000268 s.PutHex8((uint8_t)0);
Raphael Isemann92b16732018-08-02 16:38:34 +0000269 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000270 EXPECT_EQ("00", TakeValue());
271}
272
273TEST_F(StreamTest, PutNHex8) {
274 s.PutNHex8(0, (uint8_t)55);
Raphael Isemann92b16732018-08-02 16:38:34 +0000275 EXPECT_EQ(0U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000276 EXPECT_EQ("", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000277
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000278 s.PutNHex8(1, (uint8_t)55);
Raphael Isemann92b16732018-08-02 16:38:34 +0000279 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000280 EXPECT_EQ("37", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000281
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000282 s.PutNHex8(2, (uint8_t)55);
Raphael Isemann92b16732018-08-02 16:38:34 +0000283 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000284 EXPECT_EQ("3737", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000285
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000286 s.PutNHex8(1, (uint8_t)56);
Raphael Isemann92b16732018-08-02 16:38:34 +0000287 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000288 EXPECT_EQ("38", TakeValue());
289}
290
291TEST_F(StreamTest, PutHex16ByteOrderLittle) {
292 s.PutHex16(0x1234U, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000293 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000294 EXPECT_EQ("3412", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000295
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000296 s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000297 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000298 EXPECT_EQ("ffff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000299
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000300 s.PutHex16(0U, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000301 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000302 EXPECT_EQ("0000", TakeValue());
303}
304
305TEST_F(StreamTest, PutHex16ByteOrderBig) {
306 s.PutHex16(0x1234U, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000307 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000308 EXPECT_EQ("1234", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000309
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000310 s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000311 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000312 EXPECT_EQ("ffff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000313
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000314 s.PutHex16(0U, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000315 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000316 EXPECT_EQ("0000", TakeValue());
317}
318
319TEST_F(StreamTest, PutHex32ByteOrderLittle) {
320 s.PutHex32(0x12345678U, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000321 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000322 EXPECT_EQ("78563412", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000323
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000324 s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000325 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000326 EXPECT_EQ("ffffffff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000327
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000328 s.PutHex32(0U, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000329 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000330 EXPECT_EQ("00000000", TakeValue());
331}
332
333TEST_F(StreamTest, PutHex32ByteOrderBig) {
334 s.PutHex32(0x12345678U, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000335 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000336 EXPECT_EQ("12345678", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000337
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000338 s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000339 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000340 EXPECT_EQ("ffffffff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000341
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000342 s.PutHex32(0U, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000343 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000344 EXPECT_EQ("00000000", TakeValue());
345}
346
347TEST_F(StreamTest, PutHex64ByteOrderLittle) {
348 s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000349 EXPECT_EQ(16U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000350 EXPECT_EQ("efcdab9078563412", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000351
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000352 s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000353 EXPECT_EQ(16U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000354 EXPECT_EQ("ffffffffffffffff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000355
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000356 s.PutHex64(0U, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000357 EXPECT_EQ(16U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000358 EXPECT_EQ("0000000000000000", TakeValue());
359}
360
361TEST_F(StreamTest, PutHex64ByteOrderBig) {
362 s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000363 EXPECT_EQ(16U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000364 EXPECT_EQ("1234567890abcdef", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000365
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000366 s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000367 EXPECT_EQ(16U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000368 EXPECT_EQ("ffffffffffffffff", TakeValue());
Raphael Isemann92b16732018-08-02 16:38:34 +0000369
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000370 s.PutHex64(0U, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000371 EXPECT_EQ(16U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000372 EXPECT_EQ("0000000000000000", TakeValue());
373}
374
Raphael Isemann0bb8d832018-08-01 17:12:58 +0000375TEST_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);
Raphael Isemann92b16732018-08-02 16:38:34 +0000385 EXPECT_EQ(30U, s.GetWrittenBytes());
Raphael Isemann0bb8d832018-08-01 17:12:58 +0000386 EXPECT_EQ("121234123456781234567890abcdef", TakeValue());
387}
388
389TEST_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);
Raphael Isemann92b16732018-08-02 16:38:34 +0000399 EXPECT_EQ(30U, s.GetWrittenBytes());
Raphael Isemann0bb8d832018-08-01 17:12:58 +0000400 EXPECT_EQ("12341278563412efcdab9078563412", TakeValue());
401}
402
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000403// Shift operator tests.
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000404
405TEST_F(StreamTest, ShiftOperatorChars) {
406 s << 'a' << 'b';
Raphael Isemann92b16732018-08-02 16:38:34 +0000407 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000408 EXPECT_EQ("ab", TakeValue());
409}
410
411TEST_F(StreamTest, ShiftOperatorStrings) {
412 s << "cstring\n";
Raphael Isemann92b16732018-08-02 16:38:34 +0000413 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000414 s << llvm::StringRef("llvm::StringRef\n");
Raphael Isemann92b16732018-08-02 16:38:34 +0000415 EXPECT_EQ(24U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000416 EXPECT_EQ("cstring\nllvm::StringRef\n", TakeValue());
417}
418
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000419TEST_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).
425
426 // Shift our own pointer to the output.
427 int i = 3;
428 int *ptr = &i;
429 s << ptr;
430
Raphael Isemann92b16732018-08-02 16:38:34 +0000431 EXPECT_NE(0U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000432 EXPECT_TRUE(!TakeValue().empty());
433}
434
435TEST_F(StreamTest, PutPtr) {
436 // See the ShiftOperatorPtr test for the rationale.
437 int i = 3;
438 int *ptr = &i;
439 s.PutPointer(ptr);
440
Raphael Isemann92b16732018-08-02 16:38:34 +0000441 EXPECT_NE(0U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000442 EXPECT_TRUE(!TakeValue().empty());
443}
444
445// Alias to make it more clear that 'invalid' means for the Stream interface
446// that it should use the host byte order.
447const static auto hostByteOrder = lldb::eByteOrderInvalid;
448
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000449// PutRawBytes/PutBytesAsRawHex tests.
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000450
451TEST_F(StreamTest, PutBytesAsRawHex8ToBigEndian) {
452 uint32_t value = 0x12345678;
453 s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
454 hostByteOrder, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000455 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000456 EXPECT_EQ("78563412", TakeValue());
457}
458
459TEST_F(StreamTest, PutRawBytesToBigEndian) {
460 uint32_t value = 0x12345678;
461 s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
462 hostByteOrder, lldb::eByteOrderBig);
Raphael Isemann92b16732018-08-02 16:38:34 +0000463 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000464 EXPECT_EQ("\x78\x56\x34\x12", TakeValue());
465}
466
467TEST_F(StreamTest, PutBytesAsRawHex8ToLittleEndian) {
468 uint32_t value = 0x12345678;
469 s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
470 hostByteOrder, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000471 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000472 EXPECT_EQ("12345678", TakeValue());
473}
474
475TEST_F(StreamTest, PutRawBytesToLittleEndian) {
476 uint32_t value = 0x12345678;
477 s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
478 hostByteOrder, lldb::eByteOrderLittle);
Raphael Isemann92b16732018-08-02 16:38:34 +0000479 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000480 EXPECT_EQ("\x12\x34\x56\x78", TakeValue());
481}
482
483TEST_F(StreamTest, PutBytesAsRawHex8ToMixedEndian) {
484 uint32_t value = 0x12345678;
485 s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value),
486 hostByteOrder, lldb::eByteOrderPDP);
487
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
493}
494
495TEST_F(StreamTest, PutRawBytesToMixedEndian) {
496 uint32_t value = 0x12345678;
497 s.PutRawBytes(static_cast<void*>(&value), sizeof(value),
498 lldb::eByteOrderInvalid, lldb::eByteOrderPDP);
499
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
505}
506
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000507// ULEB128 support for binary streams.
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000508
509TEST_F(BinaryStreamTest, PutULEB128OneByte) {
510 auto bytes = s.PutULEB128(0x74ULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000511 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000512 EXPECT_EQ("\x74", TakeValue());
513 EXPECT_EQ(1U, bytes);
514}
515
516TEST_F(BinaryStreamTest, PutULEB128TwoBytes) {
517 auto bytes = s.PutULEB128(0x1985ULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000518 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000519 EXPECT_EQ("\x85\x33", TakeValue());
520 EXPECT_EQ(2U, bytes);
521}
522
523TEST_F(BinaryStreamTest, PutULEB128ThreeBytes) {
524 auto bytes = s.PutULEB128(0x5023ULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000525 EXPECT_EQ(3U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000526 EXPECT_EQ("\xA3\xA0\x1", TakeValue());
527 EXPECT_EQ(3U, bytes);
528}
529
530TEST_F(BinaryStreamTest, PutULEB128FourBytes) {
531 auto bytes = s.PutULEB128(0xA48032ULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000532 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000533 EXPECT_EQ("\xB2\x80\x92\x5", TakeValue());
534 EXPECT_EQ(4U, bytes);
535}
536
537TEST_F(BinaryStreamTest, PutULEB128FiveBytes) {
538 auto bytes = s.PutULEB128(0x12345678ULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000539 EXPECT_EQ(5U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000540 EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue());
541 EXPECT_EQ(5U, bytes);
542}
543
544TEST_F(BinaryStreamTest, PutULEB128SixBytes) {
545 auto bytes = s.PutULEB128(0xABFE3FAFDFULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000546 EXPECT_EQ(6U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000547 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue());
548 EXPECT_EQ(6U, bytes);
549}
550
551TEST_F(BinaryStreamTest, PutULEB128SevenBytes) {
552 auto bytes = s.PutULEB128(0xDABFE3FAFDFULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000553 EXPECT_EQ(7U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000554 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue());
555 EXPECT_EQ(7U, bytes);
556}
557
558TEST_F(BinaryStreamTest, PutULEB128EightBytes) {
559 auto bytes = s.PutULEB128(0x7CDABFE3FAFDFULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000560 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000561 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue());
562 EXPECT_EQ(8U, bytes);
563}
564
565TEST_F(BinaryStreamTest, PutULEB128NineBytes) {
566 auto bytes = s.PutULEB128(0x327CDABFE3FAFDFULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000567 EXPECT_EQ(9U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000568 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue());
569 EXPECT_EQ(9U, bytes);
570}
571
572TEST_F(BinaryStreamTest, PutULEB128MaxValue) {
573 auto bytes = s.PutULEB128(std::numeric_limits<uint64_t>::max());
Raphael Isemann92b16732018-08-02 16:38:34 +0000574 EXPECT_EQ(10U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000575 EXPECT_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1", TakeValue());
576 EXPECT_EQ(10U, bytes);
577}
578
579TEST_F(BinaryStreamTest, PutULEB128Zero) {
580 auto bytes = s.PutULEB128(0x0U);
Raphael Isemann92b16732018-08-02 16:38:34 +0000581 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000582 EXPECT_EQ(std::string("\0", 1), TakeValue());
583 EXPECT_EQ(1U, bytes);
584}
585
586TEST_F(BinaryStreamTest, PutULEB128One) {
587 auto bytes = s.PutULEB128(0x1U);
Raphael Isemann92b16732018-08-02 16:38:34 +0000588 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000589 EXPECT_EQ("\x1", TakeValue());
590 EXPECT_EQ(1U, bytes);
591}
592
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000593// SLEB128 support for binary streams.
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000594
595TEST_F(BinaryStreamTest, PutSLEB128OneByte) {
596 auto bytes = s.PutSLEB128(0x74LL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000597 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000598 EXPECT_EQ(std::string("\xF4\0", 2), TakeValue());
599 EXPECT_EQ(2U, bytes);
600}
601
602TEST_F(BinaryStreamTest, PutSLEB128TwoBytes) {
603 auto bytes = s.PutSLEB128(0x1985LL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000604 EXPECT_EQ(2U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000605 EXPECT_EQ("\x85\x33", TakeValue());
606 EXPECT_EQ(2U, bytes);
607}
608
609TEST_F(BinaryStreamTest, PutSLEB128ThreeBytes) {
610 auto bytes = s.PutSLEB128(0x5023LL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000611 EXPECT_EQ(3U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000612 EXPECT_EQ("\xA3\xA0\x1", TakeValue());
613 EXPECT_EQ(3U, bytes);
614}
615
616TEST_F(BinaryStreamTest, PutSLEB128FourBytes) {
617 auto bytes = s.PutSLEB128(0xA48032LL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000618 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000619 EXPECT_EQ("\xB2\x80\x92\x5", TakeValue());
620 EXPECT_EQ(4U, bytes);
621}
622
623TEST_F(BinaryStreamTest, PutSLEB128FiveBytes) {
624 auto bytes = s.PutSLEB128(0x12345678LL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000625 EXPECT_EQ(5U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000626 EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue());
627 EXPECT_EQ(5U, bytes);
628}
629
630TEST_F(BinaryStreamTest, PutSLEB128SixBytes) {
631 auto bytes = s.PutSLEB128(0xABFE3FAFDFLL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000632 EXPECT_EQ(6U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000633 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue());
634 EXPECT_EQ(6U, bytes);
635}
636
637TEST_F(BinaryStreamTest, PutSLEB128SevenBytes) {
638 auto bytes = s.PutSLEB128(0xDABFE3FAFDFLL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000639 EXPECT_EQ(7U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000640 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue());
641 EXPECT_EQ(7U, bytes);
642}
643
644TEST_F(BinaryStreamTest, PutSLEB128EightBytes) {
645 auto bytes = s.PutSLEB128(0x7CDABFE3FAFDFLL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000646 EXPECT_EQ(8U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000647 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue());
648 EXPECT_EQ(8U, bytes);
649}
650
651TEST_F(BinaryStreamTest, PutSLEB128NineBytes) {
652 auto bytes = s.PutSLEB128(0x327CDABFE3FAFDFLL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000653 EXPECT_EQ(9U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000654 EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue());
655 EXPECT_EQ(9U, bytes);
656}
657
658TEST_F(BinaryStreamTest, PutSLEB128MaxValue) {
659 auto bytes = s.PutSLEB128(std::numeric_limits<int64_t>::max());
Raphael Isemann92b16732018-08-02 16:38:34 +0000660 EXPECT_EQ(10U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000661 EXPECT_EQ(std::string("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\0", 10), TakeValue());
662 EXPECT_EQ(10U, bytes);
663}
664
665TEST_F(BinaryStreamTest, PutSLEB128Zero) {
666 auto bytes = s.PutSLEB128(0x0);
Raphael Isemann92b16732018-08-02 16:38:34 +0000667 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000668 EXPECT_EQ(std::string("\0", 1), TakeValue());
669 EXPECT_EQ(1U, bytes);
670}
671
672TEST_F(BinaryStreamTest, PutSLEB128One) {
673 auto bytes = s.PutSLEB128(0x1);
Raphael Isemann92b16732018-08-02 16:38:34 +0000674 EXPECT_EQ(1U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000675 EXPECT_EQ(std::string("\x1", 1), TakeValue());
676 EXPECT_EQ(1U, bytes);
677}
678
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000679// SLEB128/ULEB128 support for non-binary streams.
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000680
681// The logic for this is very simple, so it should be enough to test some basic
682// use cases.
683
684TEST_F(StreamTest, PutULEB128) {
685 auto bytes = s.PutULEB128(0x74ULL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000686 EXPECT_EQ(4U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000687 EXPECT_EQ("0x74", TakeValue());
688 EXPECT_EQ(4U, bytes);
689}
690
691TEST_F(StreamTest, PutSLEB128) {
692 auto bytes = s.PutSLEB128(0x1985LL);
Raphael Isemann92b16732018-08-02 16:38:34 +0000693 EXPECT_EQ(6U, s.GetWrittenBytes());
Raphael Isemannb1dfad02018-08-01 06:04:48 +0000694 EXPECT_EQ("0x6533", TakeValue());
695 EXPECT_EQ(6U, bytes);
696}