Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/BinaryStreamTest.cpp -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/DebugInfo/MSF/BinaryByteStream.h" |
| 11 | #include "llvm/DebugInfo/MSF/BinaryItemStream.h" |
| 12 | #include "llvm/DebugInfo/MSF/BinaryStreamArray.h" |
| 13 | #include "llvm/DebugInfo/MSF/BinaryStreamReader.h" |
| 14 | #include "llvm/DebugInfo/MSF/BinaryStreamRef.h" |
| 15 | #include "llvm/DebugInfo/MSF/BinaryStreamWriter.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | #include <unordered_map> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::support; |
| 22 | |
| 23 | #define EXPECT_NO_ERROR(Err) \ |
| 24 | { \ |
| 25 | auto E = Err; \ |
| 26 | EXPECT_FALSE(static_cast<bool>(E)); \ |
| 27 | if (E) \ |
| 28 | consumeError(std::move(E)); \ |
| 29 | } |
| 30 | |
| 31 | #define ASSERT_NO_ERROR(Err) \ |
| 32 | { \ |
| 33 | auto E = Err; \ |
| 34 | ASSERT_FALSE(static_cast<bool>(E)); \ |
| 35 | if (E) \ |
| 36 | consumeError(std::move(E)); \ |
| 37 | } |
| 38 | |
| 39 | #define EXPECT_ERROR(Err) \ |
| 40 | { \ |
| 41 | auto E = Err; \ |
| 42 | EXPECT_TRUE(static_cast<bool>(E)); \ |
| 43 | if (E) \ |
| 44 | consumeError(std::move(E)); \ |
| 45 | } |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | class DiscontiguousStream : public WritableBinaryStream { |
| 50 | public: |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 51 | DiscontiguousStream(MutableArrayRef<uint8_t> Data, endianness Endian, |
| 52 | uint32_t Align) |
| 53 | : Data(Data), PartitionIndex(alignDown(Data.size() / 2, Align)), |
| 54 | Endian(Endian) {} |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 55 | |
| 56 | endianness getEndian() const override { return Endian; } |
| 57 | |
| 58 | Error readBytes(uint32_t Offset, uint32_t Size, |
| 59 | ArrayRef<uint8_t> &Buffer) override { |
| 60 | if (auto EC = checkOffset(Offset, Size)) |
| 61 | return EC; |
| 62 | uint32_t S = startIndex(Offset); |
| 63 | auto Ref = Data.drop_front(S); |
| 64 | if (Ref.size() >= Size) { |
| 65 | Buffer = Ref.take_front(Size); |
| 66 | return Error::success(); |
| 67 | } |
| 68 | |
| 69 | uint32_t BytesLeft = Size - Ref.size(); |
| 70 | uint8_t *Ptr = Allocator.Allocate<uint8_t>(Size); |
| 71 | ::memcpy(Ptr, Ref.data(), Ref.size()); |
| 72 | ::memcpy(Ptr + Ref.size(), Data.data(), BytesLeft); |
| 73 | Buffer = makeArrayRef<uint8_t>(Ptr, Size); |
| 74 | return Error::success(); |
| 75 | } |
| 76 | |
| 77 | Error readLongestContiguousChunk(uint32_t Offset, |
| 78 | ArrayRef<uint8_t> &Buffer) override { |
| 79 | if (auto EC = checkOffset(Offset, 1)) |
| 80 | return EC; |
| 81 | uint32_t S = startIndex(Offset); |
| 82 | Buffer = Data.drop_front(S); |
| 83 | return Error::success(); |
| 84 | } |
| 85 | |
| 86 | uint32_t getLength() override { return Data.size(); } |
| 87 | |
| 88 | Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> SrcData) override { |
| 89 | if (auto EC = checkOffset(Offset, SrcData.size())) |
| 90 | return EC; |
| 91 | if (SrcData.empty()) |
| 92 | return Error::success(); |
| 93 | |
| 94 | uint32_t S = startIndex(Offset); |
| 95 | MutableArrayRef<uint8_t> Ref(Data); |
| 96 | Ref = Ref.drop_front(S); |
| 97 | if (Ref.size() >= SrcData.size()) { |
| 98 | ::memcpy(Ref.data(), SrcData.data(), SrcData.size()); |
| 99 | return Error::success(); |
| 100 | } |
| 101 | |
| 102 | uint32_t BytesLeft = SrcData.size() - Ref.size(); |
| 103 | ::memcpy(Ref.data(), SrcData.data(), Ref.size()); |
| 104 | ::memcpy(&Data[0], SrcData.data() + Ref.size(), BytesLeft); |
| 105 | return Error::success(); |
| 106 | } |
| 107 | Error commit() override { return Error::success(); } |
| 108 | |
| 109 | private: |
| 110 | uint32_t startIndex(uint32_t Offset) const { |
| 111 | return (Offset + PartitionIndex) % Data.size(); |
| 112 | } |
| 113 | |
| 114 | uint32_t endIndex(uint32_t Offset, uint32_t Size) const { |
| 115 | return (startIndex(Offset) + Size - 1) % Data.size(); |
| 116 | } |
| 117 | |
| 118 | // Buffer is organized like this: |
| 119 | // ------------------------------------------------- |
| 120 | // | N/2 | N/2+1 | ... | N-1 | 0 | 1 | ... | N-2-1 | |
| 121 | // ------------------------------------------------- |
| 122 | // So reads from the beginning actually come from the middle. |
| 123 | MutableArrayRef<uint8_t> Data; |
| 124 | uint32_t PartitionIndex = 0; |
| 125 | endianness Endian; |
| 126 | BumpPtrAllocator Allocator; |
| 127 | }; |
| 128 | |
| 129 | constexpr endianness Endians[] = { big, little, native }; |
| 130 | constexpr uint32_t NumEndians = llvm::array_lengthof(Endians); |
| 131 | constexpr uint32_t NumStreams = 2 * NumEndians; |
| 132 | |
| 133 | class BinaryStreamTest : public testing::Test { |
| 134 | |
| 135 | public: |
| 136 | BinaryStreamTest() {} |
| 137 | |
| 138 | void SetUp() override { |
| 139 | Streams.clear(); |
| 140 | Streams.resize(NumStreams); |
| 141 | for (uint32_t I = 0; I < NumStreams; ++I) |
| 142 | Streams[I].IsContiguous = (I % 2 == 0); |
| 143 | |
| 144 | InputData.clear(); |
| 145 | OutputData.clear(); |
| 146 | } |
| 147 | |
| 148 | protected: |
| 149 | struct StreamPair { |
| 150 | bool IsContiguous; |
| 151 | std::unique_ptr<BinaryStream> Input; |
| 152 | std::unique_ptr<WritableBinaryStream> Output; |
| 153 | }; |
| 154 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 155 | void initializeInput(ArrayRef<uint8_t> Input, uint32_t Align) { |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 156 | InputData = Input; |
| 157 | |
| 158 | BrokenInputData.resize(InputData.size()); |
| 159 | if (!Input.empty()) { |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 160 | uint32_t PartitionIndex = alignDown(InputData.size() / 2, Align); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 161 | uint32_t RightBytes = InputData.size() - PartitionIndex; |
| 162 | uint32_t LeftBytes = PartitionIndex; |
| 163 | if (RightBytes > 0) |
| 164 | ::memcpy(&BrokenInputData[PartitionIndex], Input.data(), RightBytes); |
| 165 | if (LeftBytes > 0) |
| 166 | ::memcpy(&BrokenInputData[0], Input.data() + RightBytes, LeftBytes); |
| 167 | } |
| 168 | |
| 169 | for (uint32_t I = 0; I < NumEndians; ++I) { |
| 170 | auto InByteStream = |
| 171 | llvm::make_unique<BinaryByteStream>(InputData, Endians[I]); |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 172 | auto InBrokenStream = llvm::make_unique<DiscontiguousStream>( |
| 173 | BrokenInputData, Endians[I], Align); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 174 | |
| 175 | Streams[I * 2].Input = std::move(InByteStream); |
| 176 | Streams[I * 2 + 1].Input = std::move(InBrokenStream); |
| 177 | } |
| 178 | } |
| 179 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 180 | void initializeOutput(uint32_t Size, uint32_t Align) { |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 181 | OutputData.resize(Size); |
| 182 | BrokenOutputData.resize(Size); |
| 183 | |
| 184 | for (uint32_t I = 0; I < NumEndians; ++I) { |
| 185 | Streams[I * 2].Output = |
| 186 | llvm::make_unique<MutableBinaryByteStream>(OutputData, Endians[I]); |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 187 | Streams[I * 2 + 1].Output = llvm::make_unique<DiscontiguousStream>( |
| 188 | BrokenOutputData, Endians[I], Align); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 192 | void initializeOutputFromInput(uint32_t Align) { |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 193 | for (uint32_t I = 0; I < NumEndians; ++I) { |
| 194 | Streams[I * 2].Output = |
| 195 | llvm::make_unique<MutableBinaryByteStream>(InputData, Endians[I]); |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 196 | Streams[I * 2 + 1].Output = llvm::make_unique<DiscontiguousStream>( |
| 197 | BrokenInputData, Endians[I], Align); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 201 | void initializeInputFromOutput(uint32_t Align) { |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 202 | for (uint32_t I = 0; I < NumEndians; ++I) { |
| 203 | Streams[I * 2].Input = |
| 204 | llvm::make_unique<BinaryByteStream>(OutputData, Endians[I]); |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 205 | Streams[I * 2 + 1].Input = llvm::make_unique<DiscontiguousStream>( |
| 206 | BrokenOutputData, Endians[I], Align); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | std::vector<uint8_t> InputData; |
| 211 | std::vector<uint8_t> BrokenInputData; |
| 212 | |
| 213 | std::vector<uint8_t> OutputData; |
| 214 | std::vector<uint8_t> BrokenOutputData; |
| 215 | |
| 216 | std::vector<StreamPair> Streams; |
| 217 | }; |
| 218 | |
| 219 | // Tests that a we can read from a BinaryByteStream without a StreamReader. |
| 220 | TEST_F(BinaryStreamTest, BinaryByteStreamBounds) { |
| 221 | std::vector<uint8_t> InputData = {1, 2, 3, 4, 5}; |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 222 | initializeInput(InputData, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 223 | |
| 224 | for (auto &Stream : Streams) { |
| 225 | ArrayRef<uint8_t> Buffer; |
| 226 | |
| 227 | // 1. If the read fits it should work. |
| 228 | ASSERT_EQ(InputData.size(), Stream.Input->getLength()); |
| 229 | ASSERT_NO_ERROR(Stream.Input->readBytes(2, 1, Buffer)); |
| 230 | EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer); |
| 231 | ASSERT_NO_ERROR(Stream.Input->readBytes(0, 4, Buffer)); |
| 232 | EXPECT_EQ(makeArrayRef(InputData).slice(0, 4), Buffer); |
| 233 | |
| 234 | // 2. Reading past the bounds of the input should fail. |
| 235 | EXPECT_ERROR(Stream.Input->readBytes(4, 2, Buffer)); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | TEST_F(BinaryStreamTest, StreamRefBounds) { |
| 240 | std::vector<uint8_t> InputData = {1, 2, 3, 4, 5}; |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 241 | initializeInput(InputData, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 242 | |
| 243 | for (const auto &Stream : Streams) { |
| 244 | ArrayRef<uint8_t> Buffer; |
| 245 | BinaryStreamRef Ref(*Stream.Input); |
| 246 | |
| 247 | // Read 1 byte from offset 2 should work |
| 248 | ASSERT_EQ(InputData.size(), Ref.getLength()); |
| 249 | ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer)); |
| 250 | EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer); |
| 251 | |
| 252 | // Reading everything from offset 2 on. |
| 253 | ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer)); |
| 254 | if (Stream.IsContiguous) |
| 255 | EXPECT_EQ(makeArrayRef(InputData).slice(2), Buffer); |
| 256 | else |
| 257 | EXPECT_FALSE(Buffer.empty()); |
| 258 | |
| 259 | // Reading 6 bytes from offset 0 is too big. |
| 260 | EXPECT_ERROR(Ref.readBytes(0, 6, Buffer)); |
| 261 | EXPECT_ERROR(Ref.readLongestContiguousChunk(6, Buffer)); |
| 262 | |
| 263 | // Reading 1 byte from offset 2 after dropping 1 byte is the same as reading |
| 264 | // 1 byte from offset 3. |
| 265 | Ref = Ref.drop_front(1); |
| 266 | ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer)); |
| 267 | if (Stream.IsContiguous) |
| 268 | EXPECT_EQ(makeArrayRef(InputData).slice(3, 1), Buffer); |
| 269 | else |
| 270 | EXPECT_FALSE(Buffer.empty()); |
| 271 | |
| 272 | // Reading everything from offset 2 on after dropping 1 byte. |
| 273 | ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer)); |
| 274 | if (Stream.IsContiguous) |
| 275 | EXPECT_EQ(makeArrayRef(InputData).slice(3), Buffer); |
| 276 | else |
| 277 | EXPECT_FALSE(Buffer.empty()); |
| 278 | |
| 279 | // Reading 2 bytes from offset 2 after dropping 2 bytes is the same as |
| 280 | // reading 2 bytes from offset 4, and should fail. |
| 281 | Ref = Ref.drop_front(1); |
| 282 | EXPECT_ERROR(Ref.readBytes(2, 2, Buffer)); |
| 283 | |
| 284 | // But if we read the longest contiguous chunk instead, we should still |
| 285 | // get the 1 byte at the end. |
| 286 | ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer)); |
| 287 | EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Test that we can write to a BinaryStream without a StreamWriter. |
| 292 | TEST_F(BinaryStreamTest, MutableBinaryByteStreamBounds) { |
| 293 | std::vector<uint8_t> InputData = {'T', 'e', 's', 't', '\0'}; |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 294 | initializeInput(InputData, 1); |
| 295 | initializeOutput(InputData.size(), 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 296 | |
| 297 | // For every combination of input stream and output stream. |
| 298 | for (auto &Stream : Streams) { |
| 299 | MutableArrayRef<uint8_t> Buffer; |
| 300 | ASSERT_EQ(InputData.size(), Stream.Input->getLength()); |
| 301 | |
| 302 | // 1. Try two reads that are supposed to work. One from offset 0, and one |
| 303 | // from the middle. |
| 304 | uint32_t Offsets[] = {0, 3}; |
| 305 | for (auto Offset : Offsets) { |
| 306 | uint32_t ExpectedSize = Stream.Input->getLength() - Offset; |
| 307 | |
| 308 | // Read everything from Offset until the end of the input data. |
| 309 | ArrayRef<uint8_t> Data; |
| 310 | ASSERT_NO_ERROR(Stream.Input->readBytes(Offset, ExpectedSize, Data)); |
| 311 | ASSERT_EQ(ExpectedSize, Data.size()); |
| 312 | |
| 313 | // Then write it to the destination. |
| 314 | ASSERT_NO_ERROR(Stream.Output->writeBytes(0, Data)); |
| 315 | |
| 316 | // Then we read back what we wrote, it should match the corresponding |
| 317 | // slice of the original input data. |
| 318 | ArrayRef<uint8_t> Data2; |
| 319 | ASSERT_NO_ERROR(Stream.Output->readBytes(Offset, ExpectedSize, Data2)); |
| 320 | EXPECT_EQ(makeArrayRef(InputData).drop_front(Offset), Data2); |
| 321 | } |
| 322 | |
| 323 | std::vector<uint8_t> BigData = {0, 1, 2, 3, 4}; |
| 324 | // 2. If the write is too big, it should fail. |
| 325 | EXPECT_ERROR(Stream.Output->writeBytes(3, BigData)); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Test that FixedStreamArray works correctly. |
| 330 | TEST_F(BinaryStreamTest, FixedStreamArray) { |
| 331 | std::vector<uint32_t> Ints = {90823, 12908, 109823, 209823}; |
| 332 | ArrayRef<uint8_t> IntBytes(reinterpret_cast<uint8_t *>(Ints.data()), |
| 333 | Ints.size() * sizeof(uint32_t)); |
| 334 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 335 | initializeInput(IntBytes, alignof(uint32_t)); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 336 | |
| 337 | for (auto &Stream : Streams) { |
| 338 | MutableArrayRef<uint8_t> Buffer; |
| 339 | ASSERT_EQ(InputData.size(), Stream.Input->getLength()); |
| 340 | |
| 341 | FixedStreamArray<uint32_t> Array(*Stream.Input); |
| 342 | auto Iter = Array.begin(); |
| 343 | ASSERT_EQ(Ints[0], *Iter++); |
| 344 | ASSERT_EQ(Ints[1], *Iter++); |
| 345 | ASSERT_EQ(Ints[2], *Iter++); |
| 346 | ASSERT_EQ(Ints[3], *Iter++); |
| 347 | ASSERT_EQ(Array.end(), Iter); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Test that VarStreamArray works correctly. |
| 352 | TEST_F(BinaryStreamTest, VarStreamArray) { |
| 353 | StringLiteral Strings("1. Test2. Longer Test3. Really Long Test4. Super " |
| 354 | "Extra Longest Test Of All"); |
| 355 | ArrayRef<uint8_t> StringBytes( |
| 356 | reinterpret_cast<const uint8_t *>(Strings.data()), Strings.size()); |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 357 | initializeInput(StringBytes, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 358 | |
| 359 | struct StringExtractor { |
| 360 | public: |
| 361 | Error operator()(BinaryStreamRef Stream, uint32_t &Len, StringRef &Item) { |
| 362 | if (Index == 0) |
| 363 | Len = strlen("1. Test"); |
| 364 | else if (Index == 1) |
| 365 | Len = strlen("2. Longer Test"); |
| 366 | else if (Index == 2) |
| 367 | Len = strlen("3. Really Long Test"); |
| 368 | else |
| 369 | Len = strlen("4. Super Extra Longest Test Of All"); |
| 370 | ArrayRef<uint8_t> Bytes; |
| 371 | if (auto EC = Stream.readBytes(0, Len, Bytes)) |
| 372 | return EC; |
| 373 | Item = |
| 374 | StringRef(reinterpret_cast<const char *>(Bytes.data()), Bytes.size()); |
| 375 | ++Index; |
| 376 | return Error::success(); |
| 377 | } |
| 378 | |
| 379 | private: |
| 380 | uint32_t Index = 0; |
| 381 | }; |
| 382 | |
| 383 | for (auto &Stream : Streams) { |
| 384 | VarStreamArray<StringRef, StringExtractor> Array(*Stream.Input); |
| 385 | auto Iter = Array.begin(); |
| 386 | ASSERT_EQ("1. Test", *Iter++); |
| 387 | ASSERT_EQ("2. Longer Test", *Iter++); |
| 388 | ASSERT_EQ("3. Really Long Test", *Iter++); |
| 389 | ASSERT_EQ("4. Super Extra Longest Test Of All", *Iter++); |
| 390 | ASSERT_EQ(Array.end(), Iter); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | TEST_F(BinaryStreamTest, StreamReaderBounds) { |
| 395 | std::vector<uint8_t> Bytes; |
| 396 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 397 | initializeInput(Bytes, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 398 | for (auto &Stream : Streams) { |
| 399 | StringRef S; |
| 400 | BinaryStreamReader Reader(*Stream.Input); |
| 401 | EXPECT_EQ(0U, Reader.bytesRemaining()); |
| 402 | EXPECT_ERROR(Reader.readFixedString(S, 1)); |
| 403 | } |
| 404 | |
| 405 | Bytes.resize(5); |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 406 | initializeInput(Bytes, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 407 | for (auto &Stream : Streams) { |
| 408 | StringRef S; |
| 409 | BinaryStreamReader Reader(*Stream.Input); |
| 410 | EXPECT_EQ(Bytes.size(), Reader.bytesRemaining()); |
| 411 | EXPECT_NO_ERROR(Reader.readFixedString(S, 5)); |
| 412 | EXPECT_ERROR(Reader.readFixedString(S, 6)); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | TEST_F(BinaryStreamTest, StreamReaderIntegers) { |
| 417 | support::ulittle64_t Little{908234}; |
| 418 | support::ubig32_t Big{28907823}; |
| 419 | short NS = 2897; |
| 420 | int NI = -89723; |
| 421 | unsigned long NUL = 902309023UL; |
| 422 | constexpr uint32_t Size = |
| 423 | sizeof(Little) + sizeof(Big) + sizeof(NS) + sizeof(NI) + sizeof(NUL); |
| 424 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 425 | initializeOutput(Size, alignof(support::ulittle64_t)); |
| 426 | initializeInputFromOutput(alignof(support::ulittle64_t)); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 427 | |
| 428 | for (auto &Stream : Streams) { |
| 429 | BinaryStreamWriter Writer(*Stream.Output); |
| 430 | ASSERT_NO_ERROR(Writer.writeObject(Little)); |
| 431 | ASSERT_NO_ERROR(Writer.writeObject(Big)); |
| 432 | ASSERT_NO_ERROR(Writer.writeInteger(NS)); |
| 433 | ASSERT_NO_ERROR(Writer.writeInteger(NI)); |
| 434 | ASSERT_NO_ERROR(Writer.writeInteger(NUL)); |
| 435 | |
| 436 | const support::ulittle64_t *Little2; |
| 437 | const support::ubig32_t *Big2; |
| 438 | short NS2; |
| 439 | int NI2; |
| 440 | unsigned long NUL2; |
| 441 | |
| 442 | // 1. Reading fields individually. |
| 443 | BinaryStreamReader Reader(*Stream.Input); |
| 444 | ASSERT_NO_ERROR(Reader.readObject(Little2)); |
| 445 | ASSERT_NO_ERROR(Reader.readObject(Big2)); |
| 446 | ASSERT_NO_ERROR(Reader.readInteger(NS2)); |
| 447 | ASSERT_NO_ERROR(Reader.readInteger(NI2)); |
| 448 | ASSERT_NO_ERROR(Reader.readInteger(NUL2)); |
| 449 | ASSERT_EQ(0U, Reader.bytesRemaining()); |
| 450 | |
| 451 | EXPECT_EQ(Little, *Little2); |
| 452 | EXPECT_EQ(Big, *Big2); |
| 453 | EXPECT_EQ(NS, NS2); |
| 454 | EXPECT_EQ(NI, NI2); |
| 455 | EXPECT_EQ(NUL, NUL2); |
| 456 | } |
| 457 | } |
| 458 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 459 | TEST_F(BinaryStreamTest, StreamReaderIntegerArray) { |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 460 | // 1. Arrays of integers |
| 461 | std::vector<int> Ints = {1, 2, 3, 4, 5}; |
| 462 | ArrayRef<uint8_t> IntBytes(reinterpret_cast<uint8_t *>(&Ints[0]), |
| 463 | Ints.size() * sizeof(int)); |
| 464 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 465 | initializeInput(IntBytes, alignof(int)); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 466 | for (auto &Stream : Streams) { |
| 467 | BinaryStreamReader Reader(*Stream.Input); |
| 468 | ArrayRef<int> IntsRef; |
| 469 | ASSERT_NO_ERROR(Reader.readArray(IntsRef, Ints.size())); |
| 470 | ASSERT_EQ(0U, Reader.bytesRemaining()); |
| 471 | EXPECT_EQ(makeArrayRef(Ints), IntsRef); |
| 472 | |
| 473 | Reader.setOffset(0); |
| 474 | FixedStreamArray<int> FixedIntsRef; |
| 475 | ASSERT_NO_ERROR(Reader.readArray(FixedIntsRef, Ints.size())); |
| 476 | ASSERT_EQ(0U, Reader.bytesRemaining()); |
| 477 | ASSERT_EQ(Ints, std::vector<int>(FixedIntsRef.begin(), FixedIntsRef.end())); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | TEST_F(BinaryStreamTest, StreamReaderEnum) { |
| 482 | enum class MyEnum : int64_t { Foo = -10, Bar = 0, Baz = 10 }; |
| 483 | |
| 484 | std::vector<MyEnum> Enums = {MyEnum::Bar, MyEnum::Baz, MyEnum::Foo}; |
| 485 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 486 | initializeOutput(Enums.size() * sizeof(MyEnum), alignof(MyEnum)); |
| 487 | initializeInputFromOutput(alignof(MyEnum)); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 488 | for (auto &Stream : Streams) { |
| 489 | BinaryStreamWriter Writer(*Stream.Output); |
| 490 | for (auto Value : Enums) |
| 491 | ASSERT_NO_ERROR(Writer.writeEnum(Value)); |
| 492 | |
| 493 | BinaryStreamReader Reader(*Stream.Input); |
| 494 | |
| 495 | ArrayRef<MyEnum> Array; |
| 496 | FixedStreamArray<MyEnum> FSA; |
| 497 | |
| 498 | for (size_t I = 0; I < Enums.size(); ++I) { |
| 499 | MyEnum Value; |
| 500 | ASSERT_NO_ERROR(Reader.readEnum(Value)); |
| 501 | EXPECT_EQ(Enums[I], Value); |
| 502 | } |
| 503 | ASSERT_EQ(0U, Reader.bytesRemaining()); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | TEST_F(BinaryStreamTest, StreamReaderObject) { |
| 508 | struct Foo { |
| 509 | int X; |
| 510 | double Y; |
| 511 | char Z; |
| 512 | }; |
| 513 | |
| 514 | std::vector<Foo> Foos; |
| 515 | Foos.push_back({-42, 42.42, 42}); |
| 516 | Foos.push_back({100, 3.1415, static_cast<char>(-89)}); |
| 517 | |
| 518 | const uint8_t *Bytes = reinterpret_cast<const uint8_t *>(&Foos[0]); |
| 519 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 520 | initializeInput(makeArrayRef(Bytes, 2 * sizeof(Foo)), alignof(Foo)); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 521 | |
| 522 | for (auto &Stream : Streams) { |
| 523 | // 1. Reading object pointers. |
| 524 | BinaryStreamReader Reader(*Stream.Input); |
| 525 | const Foo *FPtrOut = nullptr; |
| 526 | const Foo *GPtrOut = nullptr; |
| 527 | ASSERT_NO_ERROR(Reader.readObject(FPtrOut)); |
| 528 | ASSERT_NO_ERROR(Reader.readObject(GPtrOut)); |
| 529 | EXPECT_EQ(0U, Reader.bytesRemaining()); |
| 530 | EXPECT_EQ(0, ::memcmp(&Foos[0], FPtrOut, sizeof(Foo))); |
| 531 | EXPECT_EQ(0, ::memcmp(&Foos[1], GPtrOut, sizeof(Foo))); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | TEST_F(BinaryStreamTest, StreamReaderStrings) { |
| 536 | std::vector<uint8_t> Bytes = {'O', 'n', 'e', '\0', 'T', 'w', 'o', |
| 537 | '\0', 'T', 'h', 'r', 'e', 'e', '\0', |
| 538 | 'F', 'o', 'u', 'r', '\0'}; |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 539 | initializeInput(Bytes, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 540 | |
| 541 | for (auto &Stream : Streams) { |
| 542 | BinaryStreamReader Reader(*Stream.Input); |
| 543 | |
| 544 | StringRef S1; |
| 545 | StringRef S2; |
| 546 | StringRef S3; |
| 547 | StringRef S4; |
| 548 | ASSERT_NO_ERROR(Reader.readCString(S1)); |
| 549 | ASSERT_NO_ERROR(Reader.readCString(S2)); |
| 550 | ASSERT_NO_ERROR(Reader.readCString(S3)); |
| 551 | ASSERT_NO_ERROR(Reader.readCString(S4)); |
| 552 | ASSERT_EQ(0U, Reader.bytesRemaining()); |
| 553 | |
| 554 | EXPECT_EQ("One", S1); |
| 555 | EXPECT_EQ("Two", S2); |
| 556 | EXPECT_EQ("Three", S3); |
| 557 | EXPECT_EQ("Four", S4); |
| 558 | |
| 559 | S1 = S2 = S3 = S4 = ""; |
| 560 | Reader.setOffset(0); |
| 561 | ASSERT_NO_ERROR(Reader.readFixedString(S1, 3)); |
| 562 | ASSERT_NO_ERROR(Reader.skip(1)); |
| 563 | ASSERT_NO_ERROR(Reader.readFixedString(S2, 3)); |
| 564 | ASSERT_NO_ERROR(Reader.skip(1)); |
| 565 | ASSERT_NO_ERROR(Reader.readFixedString(S3, 5)); |
| 566 | ASSERT_NO_ERROR(Reader.skip(1)); |
| 567 | ASSERT_NO_ERROR(Reader.readFixedString(S4, 4)); |
| 568 | ASSERT_NO_ERROR(Reader.skip(1)); |
| 569 | ASSERT_EQ(0U, Reader.bytesRemaining()); |
| 570 | |
| 571 | EXPECT_EQ("One", S1); |
| 572 | EXPECT_EQ("Two", S2); |
| 573 | EXPECT_EQ("Three", S3); |
| 574 | EXPECT_EQ("Four", S4); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | TEST_F(BinaryStreamTest, StreamWriterBounds) { |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 579 | initializeOutput(5, 1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 580 | |
| 581 | for (auto &Stream : Streams) { |
| 582 | BinaryStreamWriter Writer(*Stream.Output); |
| 583 | |
| 584 | // 1. Can write a string that exactly fills the buffer. |
| 585 | EXPECT_EQ(5U, Writer.bytesRemaining()); |
| 586 | EXPECT_NO_ERROR(Writer.writeFixedString("abcde")); |
| 587 | EXPECT_EQ(0U, Writer.bytesRemaining()); |
| 588 | |
| 589 | // 2. Can write an empty string even when you're full |
| 590 | EXPECT_NO_ERROR(Writer.writeFixedString("")); |
| 591 | EXPECT_ERROR(Writer.writeFixedString("a")); |
| 592 | |
| 593 | // 3. Can't write a string that is one character too long. |
| 594 | Writer.setOffset(0); |
| 595 | EXPECT_ERROR(Writer.writeFixedString("abcdef")); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | TEST_F(BinaryStreamTest, StreamWriterIntegerArrays) { |
| 600 | // 3. Arrays of integers |
| 601 | std::vector<int> SourceInts = {1, 2, 3, 4, 5}; |
| 602 | ArrayRef<uint8_t> SourceBytes(reinterpret_cast<uint8_t *>(&SourceInts[0]), |
| 603 | SourceInts.size() * sizeof(int)); |
| 604 | |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 605 | initializeInput(SourceBytes, alignof(int)); |
| 606 | initializeOutputFromInput(alignof(int)); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 607 | |
| 608 | for (auto &Stream : Streams) { |
| 609 | BinaryStreamReader Reader(*Stream.Input); |
| 610 | BinaryStreamWriter Writer(*Stream.Output); |
| 611 | ArrayRef<int> Ints; |
| 612 | ArrayRef<int> Ints2; |
| 613 | // First read them, then write them, then read them back. |
| 614 | ASSERT_NO_ERROR(Reader.readArray(Ints, SourceInts.size())); |
| 615 | ASSERT_NO_ERROR(Writer.writeArray(Ints)); |
| 616 | |
| 617 | BinaryStreamReader ReaderBacker(*Stream.Output); |
| 618 | ASSERT_NO_ERROR(ReaderBacker.readArray(Ints2, SourceInts.size())); |
| 619 | |
| 620 | EXPECT_EQ(makeArrayRef(SourceInts), Ints2); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | TEST_F(BinaryStreamTest, StringWriterStrings) { |
| 625 | StringRef Strings[] = {"First", "Second", "Third", "Fourth"}; |
| 626 | |
| 627 | size_t Length = 0; |
| 628 | for (auto S : Strings) |
| 629 | Length += S.size() + 1; |
Zachary Turner | d2fd4ae | 2017-03-01 19:29:11 +0000 | [diff] [blame^] | 630 | initializeOutput(Length, 1); |
| 631 | initializeInputFromOutput(1); |
Zachary Turner | 081b1bc | 2017-03-01 17:22:36 +0000 | [diff] [blame] | 632 | |
| 633 | for (auto &Stream : Streams) { |
| 634 | BinaryStreamWriter Writer(*Stream.Output); |
| 635 | for (auto S : Strings) |
| 636 | ASSERT_NO_ERROR(Writer.writeCString(S)); |
| 637 | std::vector<StringRef> InStrings; |
| 638 | BinaryStreamReader Reader(*Stream.Input); |
| 639 | while (!Reader.empty()) { |
| 640 | StringRef S; |
| 641 | ASSERT_NO_ERROR(Reader.readCString(S)); |
| 642 | InStrings.push_back(S); |
| 643 | } |
| 644 | EXPECT_EQ(makeArrayRef(Strings), makeArrayRef(InStrings)); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | namespace { |
| 650 | struct BinaryItemStreamObject { |
| 651 | explicit BinaryItemStreamObject(ArrayRef<uint8_t> Bytes) : Bytes(Bytes) {} |
| 652 | |
| 653 | ArrayRef<uint8_t> Bytes; |
| 654 | }; |
| 655 | } |
| 656 | |
| 657 | namespace llvm { |
| 658 | template <> struct BinaryItemTraits<BinaryItemStreamObject> { |
| 659 | static size_t length(const BinaryItemStreamObject &Item) { |
| 660 | return Item.Bytes.size(); |
| 661 | } |
| 662 | |
| 663 | static ArrayRef<uint8_t> bytes(const BinaryItemStreamObject &Item) { |
| 664 | return Item.Bytes; |
| 665 | } |
| 666 | }; |
| 667 | } |
| 668 | |
| 669 | namespace { |
| 670 | |
| 671 | TEST_F(BinaryStreamTest, BinaryItemStream) { |
| 672 | std::vector<BinaryItemStreamObject> Objects; |
| 673 | |
| 674 | struct Foo { |
| 675 | int X; |
| 676 | double Y; |
| 677 | }; |
| 678 | std::vector<Foo> Foos = {{1, 1.0}, {2, 2.0}, {3, 3.0}}; |
| 679 | BumpPtrAllocator Allocator; |
| 680 | for (const auto &F : Foos) { |
| 681 | uint8_t *Ptr = Allocator.Allocate<uint8_t>(sizeof(Foo)); |
| 682 | MutableArrayRef<uint8_t> Buffer(Ptr, sizeof(Foo)); |
| 683 | MutableBinaryByteStream Stream(Buffer, llvm::support::big); |
| 684 | BinaryStreamWriter Writer(Stream); |
| 685 | ASSERT_NO_ERROR(Writer.writeObject(F)); |
| 686 | Objects.push_back(BinaryItemStreamObject(Buffer)); |
| 687 | } |
| 688 | |
| 689 | BinaryItemStream<BinaryItemStreamObject> ItemStream(big); |
| 690 | ItemStream.setItems(Objects); |
| 691 | BinaryStreamReader Reader(ItemStream); |
| 692 | |
| 693 | for (const auto &F : Foos) { |
| 694 | const Foo *F2; |
| 695 | ASSERT_NO_ERROR(Reader.readObject(F2)); |
| 696 | |
| 697 | EXPECT_EQ(F.X, F2->X); |
| 698 | EXPECT_DOUBLE_EQ(F.Y, F2->Y); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | } // end anonymous namespace |