Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 1 | //===-- StreamTest.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Utility/StreamString.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | using namespace lldb_private; |
| 13 | |
| 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; |
| 26 | } |
| 27 | }; |
| 28 | } |
| 29 | |
| 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); |
| 35 | } |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | TEST_F(StreamTest, ChangingByteOrder) { |
| 40 | s.SetByteOrder(lldb::eByteOrderPDP); |
| 41 | EXPECT_EQ(lldb::eByteOrderPDP, s.GetByteOrder()); |
| 42 | } |
| 43 | |
| 44 | TEST_F(StreamTest, PutChar) { |
| 45 | s.PutChar('a'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 46 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 47 | EXPECT_EQ("a", TakeValue()); |
| 48 | |
| 49 | s.PutChar('1'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 50 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 51 | EXPECT_EQ("1", TakeValue()); |
| 52 | } |
| 53 | |
| 54 | TEST_F(StreamTest, PutCharWhitespace) { |
| 55 | s.PutChar(' '); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 56 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 57 | EXPECT_EQ(" ", TakeValue()); |
| 58 | |
| 59 | s.PutChar('\n'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 60 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 61 | EXPECT_EQ("\n", TakeValue()); |
| 62 | |
| 63 | s.PutChar('\r'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 64 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 65 | EXPECT_EQ("\r", TakeValue()); |
| 66 | |
| 67 | s.PutChar('\t'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 68 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 69 | EXPECT_EQ("\t", TakeValue()); |
| 70 | } |
| 71 | |
| 72 | TEST_F(StreamTest, PutCString) { |
| 73 | s.PutCString(""); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 74 | EXPECT_EQ(0U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 75 | EXPECT_EQ("", TakeValue()); |
| 76 | |
| 77 | s.PutCString("foobar"); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 78 | EXPECT_EQ(6U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 79 | EXPECT_EQ("foobar", TakeValue()); |
| 80 | |
| 81 | s.PutCString(" "); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 82 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 83 | EXPECT_EQ(" ", TakeValue()); |
| 84 | } |
| 85 | |
| 86 | TEST_F(StreamTest, PutCStringWithStringRef) { |
| 87 | s.PutCString(llvm::StringRef("")); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 88 | EXPECT_EQ(0U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 89 | EXPECT_EQ("", TakeValue()); |
| 90 | |
| 91 | s.PutCString(llvm::StringRef("foobar")); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 92 | EXPECT_EQ(6U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 93 | EXPECT_EQ("foobar", TakeValue()); |
| 94 | |
| 95 | s.PutCString(llvm::StringRef(" ")); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 96 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 97 | EXPECT_EQ(" ", TakeValue()); |
| 98 | } |
| 99 | |
| 100 | TEST_F(StreamTest, QuotedCString) { |
| 101 | s.QuotedCString("foo"); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 102 | EXPECT_EQ(5U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 103 | EXPECT_EQ(R"("foo")", TakeValue()); |
| 104 | |
| 105 | s.QuotedCString("ba r"); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 106 | EXPECT_EQ(6U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 107 | EXPECT_EQ(R"("ba r")", TakeValue()); |
| 108 | |
| 109 | s.QuotedCString(" "); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 110 | EXPECT_EQ(3U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 111 | EXPECT_EQ(R"(" ")", TakeValue()); |
| 112 | } |
| 113 | |
| 114 | TEST_F(StreamTest, PutCharNull) { |
| 115 | s.PutChar('\0'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 116 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 117 | EXPECT_EQ(std::string("\0", 1), TakeValue()); |
| 118 | |
| 119 | s.PutChar('a'); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 120 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 121 | EXPECT_EQ(std::string("a", 1), TakeValue()); |
| 122 | } |
| 123 | |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame^] | 124 | TEST_F(StreamTest, PutStringAsRawHex8) { |
| 125 | s.PutStringAsRawHex8(""); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 126 | EXPECT_EQ(0U, s.GetWrittenBytes()); |
Raphael Isemann | f4590de | 2018-08-01 21:07:18 +0000 | [diff] [blame] | 127 | EXPECT_EQ("", TakeValue()); |
| 128 | |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame^] | 129 | s.PutStringAsRawHex8("foobar"); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 130 | EXPECT_EQ(12U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 131 | EXPECT_EQ("666f6f626172", TakeValue()); |
| 132 | |
Pavel Labath | 7f815a9 | 2019-02-12 14:28:55 +0000 | [diff] [blame^] | 133 | s.PutStringAsRawHex8(" "); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 134 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 135 | EXPECT_EQ("20", TakeValue()); |
| 136 | } |
| 137 | |
| 138 | TEST_F(StreamTest, PutHex8) { |
| 139 | s.PutHex8((uint8_t)55); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 140 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 141 | EXPECT_EQ("37", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 142 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 143 | s.PutHex8(std::numeric_limits<uint8_t>::max()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 144 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 145 | EXPECT_EQ("ff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 146 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 147 | s.PutHex8((uint8_t)0); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 148 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 149 | EXPECT_EQ("00", TakeValue()); |
| 150 | } |
| 151 | |
| 152 | TEST_F(StreamTest, PutNHex8) { |
| 153 | s.PutNHex8(0, (uint8_t)55); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 154 | EXPECT_EQ(0U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 155 | EXPECT_EQ("", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 156 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 157 | s.PutNHex8(1, (uint8_t)55); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 158 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 159 | EXPECT_EQ("37", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 160 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 161 | s.PutNHex8(2, (uint8_t)55); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 162 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 163 | EXPECT_EQ("3737", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 164 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 165 | s.PutNHex8(1, (uint8_t)56); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 166 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 167 | EXPECT_EQ("38", TakeValue()); |
| 168 | } |
| 169 | |
| 170 | TEST_F(StreamTest, PutHex16ByteOrderLittle) { |
| 171 | s.PutHex16(0x1234U, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 172 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 173 | EXPECT_EQ("3412", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 174 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 175 | s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 176 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 177 | EXPECT_EQ("ffff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 178 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 179 | s.PutHex16(0U, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 180 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 181 | EXPECT_EQ("0000", TakeValue()); |
| 182 | } |
| 183 | |
| 184 | TEST_F(StreamTest, PutHex16ByteOrderBig) { |
| 185 | s.PutHex16(0x1234U, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 186 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 187 | EXPECT_EQ("1234", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 188 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 189 | s.PutHex16(std::numeric_limits<uint16_t>::max(), lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 190 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 191 | EXPECT_EQ("ffff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 192 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 193 | s.PutHex16(0U, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 194 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 195 | EXPECT_EQ("0000", TakeValue()); |
| 196 | } |
| 197 | |
| 198 | TEST_F(StreamTest, PutHex32ByteOrderLittle) { |
| 199 | s.PutHex32(0x12345678U, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 200 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 201 | EXPECT_EQ("78563412", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 202 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 203 | s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 204 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 205 | EXPECT_EQ("ffffffff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 206 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 207 | s.PutHex32(0U, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 208 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 209 | EXPECT_EQ("00000000", TakeValue()); |
| 210 | } |
| 211 | |
| 212 | TEST_F(StreamTest, PutHex32ByteOrderBig) { |
| 213 | s.PutHex32(0x12345678U, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 214 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 215 | EXPECT_EQ("12345678", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 216 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 217 | s.PutHex32(std::numeric_limits<uint32_t>::max(), lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 218 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 219 | EXPECT_EQ("ffffffff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 220 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 221 | s.PutHex32(0U, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 222 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 223 | EXPECT_EQ("00000000", TakeValue()); |
| 224 | } |
| 225 | |
| 226 | TEST_F(StreamTest, PutHex64ByteOrderLittle) { |
| 227 | s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 228 | EXPECT_EQ(16U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 229 | EXPECT_EQ("efcdab9078563412", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 230 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 231 | s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 232 | EXPECT_EQ(16U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 233 | EXPECT_EQ("ffffffffffffffff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 234 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 235 | s.PutHex64(0U, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 236 | EXPECT_EQ(16U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 237 | EXPECT_EQ("0000000000000000", TakeValue()); |
| 238 | } |
| 239 | |
| 240 | TEST_F(StreamTest, PutHex64ByteOrderBig) { |
| 241 | s.PutHex64(0x1234567890ABCDEFU, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 242 | EXPECT_EQ(16U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 243 | EXPECT_EQ("1234567890abcdef", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 244 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 245 | s.PutHex64(std::numeric_limits<uint64_t>::max(), lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 246 | EXPECT_EQ(16U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 247 | EXPECT_EQ("ffffffffffffffff", TakeValue()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 248 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 249 | s.PutHex64(0U, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 250 | EXPECT_EQ(16U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 251 | EXPECT_EQ("0000000000000000", TakeValue()); |
| 252 | } |
| 253 | |
Raphael Isemann | 0bb8d83 | 2018-08-01 17:12:58 +0000 | [diff] [blame] | 254 | TEST_F(StreamTest, PutMaxHex64ByteOrderBig) { |
| 255 | std::size_t bytes; |
| 256 | bytes = s.PutMaxHex64(0x12U, 1, lldb::eByteOrderBig); |
| 257 | EXPECT_EQ(2U, bytes); |
| 258 | bytes = s.PutMaxHex64(0x1234U, 2, lldb::eByteOrderBig); |
| 259 | EXPECT_EQ(4U, bytes); |
| 260 | bytes = s.PutMaxHex64(0x12345678U, 4, lldb::eByteOrderBig); |
| 261 | EXPECT_EQ(8U, bytes); |
| 262 | bytes = s.PutMaxHex64(0x1234567890ABCDEFU, 8, lldb::eByteOrderBig); |
| 263 | EXPECT_EQ(16U, bytes); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 264 | EXPECT_EQ(30U, s.GetWrittenBytes()); |
Raphael Isemann | 0bb8d83 | 2018-08-01 17:12:58 +0000 | [diff] [blame] | 265 | EXPECT_EQ("121234123456781234567890abcdef", TakeValue()); |
| 266 | } |
| 267 | |
| 268 | TEST_F(StreamTest, PutMaxHex64ByteOrderLittle) { |
| 269 | std::size_t bytes; |
| 270 | bytes = s.PutMaxHex64(0x12U, 1, lldb::eByteOrderLittle); |
| 271 | EXPECT_EQ(2U, bytes); |
| 272 | bytes = s.PutMaxHex64(0x1234U, 2, lldb::eByteOrderLittle); |
| 273 | EXPECT_EQ(4U, bytes); |
| 274 | bytes = s.PutMaxHex64(0x12345678U, 4, lldb::eByteOrderLittle); |
| 275 | EXPECT_EQ(8U, bytes); |
| 276 | bytes = s.PutMaxHex64(0x1234567890ABCDEFU, 8, lldb::eByteOrderLittle); |
| 277 | EXPECT_EQ(16U, bytes); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 278 | EXPECT_EQ(30U, s.GetWrittenBytes()); |
Raphael Isemann | 0bb8d83 | 2018-08-01 17:12:58 +0000 | [diff] [blame] | 279 | EXPECT_EQ("12341278563412efcdab9078563412", TakeValue()); |
| 280 | } |
| 281 | |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 282 | //------------------------------------------------------------------------------ |
| 283 | // Shift operator tests. |
| 284 | //------------------------------------------------------------------------------ |
| 285 | |
| 286 | TEST_F(StreamTest, ShiftOperatorChars) { |
| 287 | s << 'a' << 'b'; |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 288 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 289 | EXPECT_EQ("ab", TakeValue()); |
| 290 | } |
| 291 | |
| 292 | TEST_F(StreamTest, ShiftOperatorStrings) { |
| 293 | s << "cstring\n"; |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 294 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 295 | s << llvm::StringRef("llvm::StringRef\n"); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 296 | EXPECT_EQ(24U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 297 | EXPECT_EQ("cstring\nllvm::StringRef\n", TakeValue()); |
| 298 | } |
| 299 | |
| 300 | TEST_F(StreamTest, ShiftOperatorInts) { |
| 301 | s << std::numeric_limits<int8_t>::max() << " "; |
| 302 | s << std::numeric_limits<int16_t>::max() << " "; |
| 303 | s << std::numeric_limits<int32_t>::max() << " "; |
| 304 | s << std::numeric_limits<int64_t>::max(); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 305 | EXPECT_EQ(40U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 306 | EXPECT_EQ("127 32767 2147483647 9223372036854775807", TakeValue()); |
| 307 | } |
| 308 | |
| 309 | TEST_F(StreamTest, ShiftOperatorUInts) { |
| 310 | s << std::numeric_limits<uint8_t>::max() << " "; |
| 311 | s << std::numeric_limits<uint16_t>::max() << " "; |
| 312 | s << std::numeric_limits<uint32_t>::max() << " "; |
| 313 | s << std::numeric_limits<uint64_t>::max(); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 314 | EXPECT_EQ(33U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 315 | EXPECT_EQ("ff ffff ffffffff ffffffffffffffff", TakeValue()); |
| 316 | } |
| 317 | |
| 318 | TEST_F(StreamTest, ShiftOperatorPtr) { |
| 319 | // This test is a bit tricky because pretty much everything related to |
| 320 | // pointer printing seems to lead to UB or IB. So let's make the most basic |
| 321 | // test that just checks that we print *something*. This way we at least know |
| 322 | // that pointer printing doesn't do really bad things (e.g. crashing, reading |
| 323 | // OOB/uninitialized memory which the sanitizers would spot). |
| 324 | |
| 325 | // Shift our own pointer to the output. |
| 326 | int i = 3; |
| 327 | int *ptr = &i; |
| 328 | s << ptr; |
| 329 | |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 330 | EXPECT_NE(0U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 331 | EXPECT_TRUE(!TakeValue().empty()); |
| 332 | } |
| 333 | |
| 334 | TEST_F(StreamTest, PutPtr) { |
| 335 | // See the ShiftOperatorPtr test for the rationale. |
| 336 | int i = 3; |
| 337 | int *ptr = &i; |
| 338 | s.PutPointer(ptr); |
| 339 | |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 340 | EXPECT_NE(0U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 341 | EXPECT_TRUE(!TakeValue().empty()); |
| 342 | } |
| 343 | |
| 344 | // Alias to make it more clear that 'invalid' means for the Stream interface |
| 345 | // that it should use the host byte order. |
| 346 | const static auto hostByteOrder = lldb::eByteOrderInvalid; |
| 347 | |
| 348 | //------------------------------------------------------------------------------ |
| 349 | // PutRawBytes/PutBytesAsRawHex tests. |
| 350 | //------------------------------------------------------------------------------ |
| 351 | |
| 352 | TEST_F(StreamTest, PutBytesAsRawHex8ToBigEndian) { |
| 353 | uint32_t value = 0x12345678; |
| 354 | s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value), |
| 355 | hostByteOrder, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 356 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 357 | EXPECT_EQ("78563412", TakeValue()); |
| 358 | } |
| 359 | |
| 360 | TEST_F(StreamTest, PutRawBytesToBigEndian) { |
| 361 | uint32_t value = 0x12345678; |
| 362 | s.PutRawBytes(static_cast<void*>(&value), sizeof(value), |
| 363 | hostByteOrder, lldb::eByteOrderBig); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 364 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 365 | EXPECT_EQ("\x78\x56\x34\x12", TakeValue()); |
| 366 | } |
| 367 | |
| 368 | TEST_F(StreamTest, PutBytesAsRawHex8ToLittleEndian) { |
| 369 | uint32_t value = 0x12345678; |
| 370 | s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value), |
| 371 | hostByteOrder, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 372 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 373 | EXPECT_EQ("12345678", TakeValue()); |
| 374 | } |
| 375 | |
| 376 | TEST_F(StreamTest, PutRawBytesToLittleEndian) { |
| 377 | uint32_t value = 0x12345678; |
| 378 | s.PutRawBytes(static_cast<void*>(&value), sizeof(value), |
| 379 | hostByteOrder, lldb::eByteOrderLittle); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 380 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 381 | EXPECT_EQ("\x12\x34\x56\x78", TakeValue()); |
| 382 | } |
| 383 | |
| 384 | TEST_F(StreamTest, PutBytesAsRawHex8ToMixedEndian) { |
| 385 | uint32_t value = 0x12345678; |
| 386 | s.PutBytesAsRawHex8(static_cast<void*>(&value), sizeof(value), |
| 387 | hostByteOrder, lldb::eByteOrderPDP); |
| 388 | |
| 389 | // FIXME: PDP byte order is not actually implemented but Stream just silently |
| 390 | // prints the value in some random byte order... |
| 391 | #if 0 |
| 392 | EXPECT_EQ("34127856", TakeValue()); |
| 393 | #endif |
| 394 | } |
| 395 | |
| 396 | TEST_F(StreamTest, PutRawBytesToMixedEndian) { |
| 397 | uint32_t value = 0x12345678; |
| 398 | s.PutRawBytes(static_cast<void*>(&value), sizeof(value), |
| 399 | lldb::eByteOrderInvalid, lldb::eByteOrderPDP); |
| 400 | |
| 401 | // FIXME: PDP byte order is not actually implemented but Stream just silently |
| 402 | // prints the value in some random byte order... |
| 403 | #if 0 |
| 404 | EXPECT_EQ("\x34\x12\x78\x56", TakeValue()); |
| 405 | #endif |
| 406 | } |
| 407 | |
| 408 | //------------------------------------------------------------------------------ |
| 409 | // ULEB128 support for binary streams. |
| 410 | //------------------------------------------------------------------------------ |
| 411 | |
| 412 | TEST_F(BinaryStreamTest, PutULEB128OneByte) { |
| 413 | auto bytes = s.PutULEB128(0x74ULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 414 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 415 | EXPECT_EQ("\x74", TakeValue()); |
| 416 | EXPECT_EQ(1U, bytes); |
| 417 | } |
| 418 | |
| 419 | TEST_F(BinaryStreamTest, PutULEB128TwoBytes) { |
| 420 | auto bytes = s.PutULEB128(0x1985ULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 421 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 422 | EXPECT_EQ("\x85\x33", TakeValue()); |
| 423 | EXPECT_EQ(2U, bytes); |
| 424 | } |
| 425 | |
| 426 | TEST_F(BinaryStreamTest, PutULEB128ThreeBytes) { |
| 427 | auto bytes = s.PutULEB128(0x5023ULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 428 | EXPECT_EQ(3U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 429 | EXPECT_EQ("\xA3\xA0\x1", TakeValue()); |
| 430 | EXPECT_EQ(3U, bytes); |
| 431 | } |
| 432 | |
| 433 | TEST_F(BinaryStreamTest, PutULEB128FourBytes) { |
| 434 | auto bytes = s.PutULEB128(0xA48032ULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 435 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 436 | EXPECT_EQ("\xB2\x80\x92\x5", TakeValue()); |
| 437 | EXPECT_EQ(4U, bytes); |
| 438 | } |
| 439 | |
| 440 | TEST_F(BinaryStreamTest, PutULEB128FiveBytes) { |
| 441 | auto bytes = s.PutULEB128(0x12345678ULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 442 | EXPECT_EQ(5U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 443 | EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue()); |
| 444 | EXPECT_EQ(5U, bytes); |
| 445 | } |
| 446 | |
| 447 | TEST_F(BinaryStreamTest, PutULEB128SixBytes) { |
| 448 | auto bytes = s.PutULEB128(0xABFE3FAFDFULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 449 | EXPECT_EQ(6U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 450 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue()); |
| 451 | EXPECT_EQ(6U, bytes); |
| 452 | } |
| 453 | |
| 454 | TEST_F(BinaryStreamTest, PutULEB128SevenBytes) { |
| 455 | auto bytes = s.PutULEB128(0xDABFE3FAFDFULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 456 | EXPECT_EQ(7U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 457 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue()); |
| 458 | EXPECT_EQ(7U, bytes); |
| 459 | } |
| 460 | |
| 461 | TEST_F(BinaryStreamTest, PutULEB128EightBytes) { |
| 462 | auto bytes = s.PutULEB128(0x7CDABFE3FAFDFULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 463 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 464 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue()); |
| 465 | EXPECT_EQ(8U, bytes); |
| 466 | } |
| 467 | |
| 468 | TEST_F(BinaryStreamTest, PutULEB128NineBytes) { |
| 469 | auto bytes = s.PutULEB128(0x327CDABFE3FAFDFULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 470 | EXPECT_EQ(9U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 471 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue()); |
| 472 | EXPECT_EQ(9U, bytes); |
| 473 | } |
| 474 | |
| 475 | TEST_F(BinaryStreamTest, PutULEB128MaxValue) { |
| 476 | auto bytes = s.PutULEB128(std::numeric_limits<uint64_t>::max()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 477 | EXPECT_EQ(10U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 478 | EXPECT_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1", TakeValue()); |
| 479 | EXPECT_EQ(10U, bytes); |
| 480 | } |
| 481 | |
| 482 | TEST_F(BinaryStreamTest, PutULEB128Zero) { |
| 483 | auto bytes = s.PutULEB128(0x0U); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 484 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 485 | EXPECT_EQ(std::string("\0", 1), TakeValue()); |
| 486 | EXPECT_EQ(1U, bytes); |
| 487 | } |
| 488 | |
| 489 | TEST_F(BinaryStreamTest, PutULEB128One) { |
| 490 | auto bytes = s.PutULEB128(0x1U); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 491 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 492 | EXPECT_EQ("\x1", TakeValue()); |
| 493 | EXPECT_EQ(1U, bytes); |
| 494 | } |
| 495 | |
| 496 | //------------------------------------------------------------------------------ |
| 497 | // SLEB128 support for binary streams. |
| 498 | //------------------------------------------------------------------------------ |
| 499 | |
| 500 | TEST_F(BinaryStreamTest, PutSLEB128OneByte) { |
| 501 | auto bytes = s.PutSLEB128(0x74LL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 502 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 503 | EXPECT_EQ(std::string("\xF4\0", 2), TakeValue()); |
| 504 | EXPECT_EQ(2U, bytes); |
| 505 | } |
| 506 | |
| 507 | TEST_F(BinaryStreamTest, PutSLEB128TwoBytes) { |
| 508 | auto bytes = s.PutSLEB128(0x1985LL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 509 | EXPECT_EQ(2U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 510 | EXPECT_EQ("\x85\x33", TakeValue()); |
| 511 | EXPECT_EQ(2U, bytes); |
| 512 | } |
| 513 | |
| 514 | TEST_F(BinaryStreamTest, PutSLEB128ThreeBytes) { |
| 515 | auto bytes = s.PutSLEB128(0x5023LL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 516 | EXPECT_EQ(3U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 517 | EXPECT_EQ("\xA3\xA0\x1", TakeValue()); |
| 518 | EXPECT_EQ(3U, bytes); |
| 519 | } |
| 520 | |
| 521 | TEST_F(BinaryStreamTest, PutSLEB128FourBytes) { |
| 522 | auto bytes = s.PutSLEB128(0xA48032LL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 523 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 524 | EXPECT_EQ("\xB2\x80\x92\x5", TakeValue()); |
| 525 | EXPECT_EQ(4U, bytes); |
| 526 | } |
| 527 | |
| 528 | TEST_F(BinaryStreamTest, PutSLEB128FiveBytes) { |
| 529 | auto bytes = s.PutSLEB128(0x12345678LL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 530 | EXPECT_EQ(5U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 531 | EXPECT_EQ("\xF8\xAC\xD1\x91\x1", TakeValue()); |
| 532 | EXPECT_EQ(5U, bytes); |
| 533 | } |
| 534 | |
| 535 | TEST_F(BinaryStreamTest, PutSLEB128SixBytes) { |
| 536 | auto bytes = s.PutSLEB128(0xABFE3FAFDFLL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 537 | EXPECT_EQ(6U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 538 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\x15", TakeValue()); |
| 539 | EXPECT_EQ(6U, bytes); |
| 540 | } |
| 541 | |
| 542 | TEST_F(BinaryStreamTest, PutSLEB128SevenBytes) { |
| 543 | auto bytes = s.PutSLEB128(0xDABFE3FAFDFLL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 544 | EXPECT_EQ(7U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 545 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\x3", TakeValue()); |
| 546 | EXPECT_EQ(7U, bytes); |
| 547 | } |
| 548 | |
| 549 | TEST_F(BinaryStreamTest, PutSLEB128EightBytes) { |
| 550 | auto bytes = s.PutSLEB128(0x7CDABFE3FAFDFLL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 551 | EXPECT_EQ(8U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 552 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x3", TakeValue()); |
| 553 | EXPECT_EQ(8U, bytes); |
| 554 | } |
| 555 | |
| 556 | TEST_F(BinaryStreamTest, PutSLEB128NineBytes) { |
| 557 | auto bytes = s.PutSLEB128(0x327CDABFE3FAFDFLL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 558 | EXPECT_EQ(9U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 559 | EXPECT_EQ("\xDF\xDF\xFE\xF1\xBF\xB5\xF3\x93\x3", TakeValue()); |
| 560 | EXPECT_EQ(9U, bytes); |
| 561 | } |
| 562 | |
| 563 | TEST_F(BinaryStreamTest, PutSLEB128MaxValue) { |
| 564 | auto bytes = s.PutSLEB128(std::numeric_limits<int64_t>::max()); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 565 | EXPECT_EQ(10U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 566 | EXPECT_EQ(std::string("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\0", 10), TakeValue()); |
| 567 | EXPECT_EQ(10U, bytes); |
| 568 | } |
| 569 | |
| 570 | TEST_F(BinaryStreamTest, PutSLEB128Zero) { |
| 571 | auto bytes = s.PutSLEB128(0x0); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 572 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 573 | EXPECT_EQ(std::string("\0", 1), TakeValue()); |
| 574 | EXPECT_EQ(1U, bytes); |
| 575 | } |
| 576 | |
| 577 | TEST_F(BinaryStreamTest, PutSLEB128One) { |
| 578 | auto bytes = s.PutSLEB128(0x1); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 579 | EXPECT_EQ(1U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 580 | EXPECT_EQ(std::string("\x1", 1), TakeValue()); |
| 581 | EXPECT_EQ(1U, bytes); |
| 582 | } |
| 583 | |
| 584 | //------------------------------------------------------------------------------ |
| 585 | // SLEB128/ULEB128 support for non-binary streams. |
| 586 | //------------------------------------------------------------------------------ |
| 587 | |
| 588 | // The logic for this is very simple, so it should be enough to test some basic |
| 589 | // use cases. |
| 590 | |
| 591 | TEST_F(StreamTest, PutULEB128) { |
| 592 | auto bytes = s.PutULEB128(0x74ULL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 593 | EXPECT_EQ(4U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 594 | EXPECT_EQ("0x74", TakeValue()); |
| 595 | EXPECT_EQ(4U, bytes); |
| 596 | } |
| 597 | |
| 598 | TEST_F(StreamTest, PutSLEB128) { |
| 599 | auto bytes = s.PutSLEB128(0x1985LL); |
Raphael Isemann | 92b1673 | 2018-08-02 16:38:34 +0000 | [diff] [blame] | 600 | EXPECT_EQ(6U, s.GetWrittenBytes()); |
Raphael Isemann | b1dfad0 | 2018-08-01 06:04:48 +0000 | [diff] [blame] | 601 | EXPECT_EQ("0x6533", TakeValue()); |
| 602 | EXPECT_EQ(6U, bytes); |
| 603 | } |