Jordan Rose | 2217648 | 2014-11-13 00:08:41 +0000 | [diff] [blame] | 1 | //===- BitstreamReaderTest.cpp - Tests for BitstreamReader ----------------===// |
| 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/Bitcode/BitstreamReader.h" |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Duncan P. N. Exon Smith | fbf768a | 2016-03-27 23:00:59 +0000 | [diff] [blame] | 12 | #include "llvm/Bitcode/BitstreamWriter.h" |
Jordan Rose | 2217648 | 2014-11-13 00:08:41 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | TEST(BitstreamReaderTest, AtEndOfStream) { |
| 20 | uint8_t Bytes[4] = { |
| 21 | 0x00, 0x01, 0x02, 0x03 |
| 22 | }; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 23 | BitstreamCursor Cursor(Bytes); |
Jordan Rose | 2217648 | 2014-11-13 00:08:41 +0000 | [diff] [blame] | 24 | |
| 25 | EXPECT_FALSE(Cursor.AtEndOfStream()); |
| 26 | (void)Cursor.Read(8); |
| 27 | EXPECT_FALSE(Cursor.AtEndOfStream()); |
| 28 | (void)Cursor.Read(24); |
| 29 | EXPECT_TRUE(Cursor.AtEndOfStream()); |
| 30 | |
| 31 | Cursor.JumpToBit(0); |
| 32 | EXPECT_FALSE(Cursor.AtEndOfStream()); |
| 33 | |
| 34 | Cursor.JumpToBit(32); |
| 35 | EXPECT_TRUE(Cursor.AtEndOfStream()); |
| 36 | } |
| 37 | |
| 38 | TEST(BitstreamReaderTest, AtEndOfStreamJump) { |
| 39 | uint8_t Bytes[4] = { |
| 40 | 0x00, 0x01, 0x02, 0x03 |
| 41 | }; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 42 | BitstreamCursor Cursor(Bytes); |
Jordan Rose | 2217648 | 2014-11-13 00:08:41 +0000 | [diff] [blame] | 43 | |
| 44 | Cursor.JumpToBit(32); |
| 45 | EXPECT_TRUE(Cursor.AtEndOfStream()); |
| 46 | } |
| 47 | |
| 48 | TEST(BitstreamReaderTest, AtEndOfStreamEmpty) { |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 49 | BitstreamCursor Cursor(ArrayRef<uint8_t>{}); |
Jordan Rose | 2217648 | 2014-11-13 00:08:41 +0000 | [diff] [blame] | 50 | |
| 51 | EXPECT_TRUE(Cursor.AtEndOfStream()); |
| 52 | } |
| 53 | |
Duncan P. N. Exon Smith | 2e87e9e | 2016-03-27 22:45:25 +0000 | [diff] [blame] | 54 | TEST(BitstreamReaderTest, getCurrentByteNo) { |
| 55 | uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03}; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 56 | SimpleBitstreamCursor Cursor(Bytes); |
Duncan P. N. Exon Smith | 2e87e9e | 2016-03-27 22:45:25 +0000 | [diff] [blame] | 57 | |
Peter Collingbourne | e851658 | 2016-11-02 00:08:19 +0000 | [diff] [blame] | 58 | for (unsigned I = 0, E = 32; I != E; ++I) { |
Duncan P. N. Exon Smith | 2e87e9e | 2016-03-27 22:45:25 +0000 | [diff] [blame] | 59 | EXPECT_EQ(I / 8, Cursor.getCurrentByteNo()); |
| 60 | (void)Cursor.Read(1); |
| 61 | } |
| 62 | EXPECT_EQ(4u, Cursor.getCurrentByteNo()); |
| 63 | } |
| 64 | |
| 65 | TEST(BitstreamReaderTest, getPointerToByte) { |
| 66 | uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 67 | SimpleBitstreamCursor Cursor(Bytes); |
Duncan P. N. Exon Smith | 2e87e9e | 2016-03-27 22:45:25 +0000 | [diff] [blame] | 68 | |
| 69 | for (unsigned I = 0, E = 8; I != E; ++I) { |
| 70 | EXPECT_EQ(Bytes + I, Cursor.getPointerToByte(I, 1)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | TEST(BitstreamReaderTest, getPointerToBit) { |
| 75 | uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 76 | SimpleBitstreamCursor Cursor(Bytes); |
Duncan P. N. Exon Smith | 2e87e9e | 2016-03-27 22:45:25 +0000 | [diff] [blame] | 77 | |
| 78 | for (unsigned I = 0, E = 8; I != E; ++I) { |
| 79 | EXPECT_EQ(Bytes + I, Cursor.getPointerToBit(I * 8, 1)); |
| 80 | } |
| 81 | } |
| 82 | |
Duncan P. N. Exon Smith | fbf768a | 2016-03-27 23:00:59 +0000 | [diff] [blame] | 83 | TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) { |
| 84 | SmallVector<uint8_t, 1> BlobData; |
| 85 | for (unsigned I = 0, E = 1024; I != E; ++I) |
| 86 | BlobData.push_back(I); |
| 87 | |
| 88 | // Try a bunch of different sizes. |
| 89 | const unsigned Magic = 0x12345678; |
| 90 | const unsigned BlockID = bitc::FIRST_APPLICATION_BLOCKID; |
| 91 | const unsigned RecordID = 1; |
| 92 | for (unsigned I = 0, BlobSize = 0, E = BlobData.size(); BlobSize < E; |
| 93 | BlobSize += ++I) { |
| 94 | StringRef BlobIn((const char *)BlobData.begin(), BlobSize); |
| 95 | |
| 96 | // Write the bitcode. |
| 97 | SmallVector<char, 1> Buffer; |
| 98 | unsigned AbbrevID; |
| 99 | { |
| 100 | BitstreamWriter Stream(Buffer); |
| 101 | Stream.Emit(Magic, 32); |
| 102 | Stream.EnterSubblock(BlockID, 3); |
| 103 | |
David Blaikie | 0581f9f | 2017-01-04 22:36:33 +0000 | [diff] [blame] | 104 | auto Abbrev = std::make_shared<BitCodeAbbrev>(); |
Duncan P. N. Exon Smith | fbf768a | 2016-03-27 23:00:59 +0000 | [diff] [blame] | 105 | Abbrev->Add(BitCodeAbbrevOp(RecordID)); |
| 106 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
David Blaikie | 0581f9f | 2017-01-04 22:36:33 +0000 | [diff] [blame] | 107 | AbbrevID = Stream.EmitAbbrev(std::move(Abbrev)); |
Duncan P. N. Exon Smith | fbf768a | 2016-03-27 23:00:59 +0000 | [diff] [blame] | 108 | unsigned Record[] = {RecordID}; |
| 109 | Stream.EmitRecordWithBlob(AbbrevID, makeArrayRef(Record), BlobIn); |
| 110 | |
| 111 | Stream.ExitBlock(); |
| 112 | } |
| 113 | |
| 114 | // Stream the buffer into the reader. |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 115 | BitstreamCursor Stream( |
Peter Collingbourne | e851658 | 2016-11-02 00:08:19 +0000 | [diff] [blame] | 116 | ArrayRef<uint8_t>((const uint8_t *)Buffer.begin(), Buffer.size())); |
Duncan P. N. Exon Smith | fbf768a | 2016-03-27 23:00:59 +0000 | [diff] [blame] | 117 | |
| 118 | // Header. Included in test so that we can run llvm-bcanalyzer to debug |
| 119 | // when there are problems. |
| 120 | ASSERT_EQ(Magic, Stream.Read(32)); |
| 121 | |
| 122 | // Block. |
| 123 | BitstreamEntry Entry = |
| 124 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); |
| 125 | ASSERT_EQ(BitstreamEntry::SubBlock, Entry.Kind); |
| 126 | ASSERT_EQ(BlockID, Entry.ID); |
| 127 | ASSERT_FALSE(Stream.EnterSubBlock(BlockID)); |
| 128 | |
| 129 | // Abbreviation. |
| 130 | Entry = Stream.advance(); |
| 131 | ASSERT_EQ(BitstreamEntry::Record, Entry.Kind); |
| 132 | ASSERT_EQ(AbbrevID, Entry.ID); |
| 133 | |
| 134 | // Record. |
| 135 | StringRef BlobOut; |
| 136 | SmallVector<uint64_t, 1> Record; |
| 137 | ASSERT_EQ(RecordID, Stream.readRecord(Entry.ID, Record, &BlobOut)); |
| 138 | EXPECT_TRUE(Record.empty()); |
| 139 | EXPECT_EQ(BlobIn, BlobOut); |
| 140 | } |
| 141 | } |
| 142 | |
Peter Collingbourne | 7cfe622 | 2016-11-02 02:58:47 +0000 | [diff] [blame] | 143 | TEST(BitstreamReaderTest, shortRead) { |
| 144 | uint8_t Bytes[] = {8, 7, 6, 5, 4, 3, 2, 1}; |
| 145 | for (unsigned I = 1; I != 8; ++I) { |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 146 | SimpleBitstreamCursor Cursor(ArrayRef<uint8_t>(Bytes, I)); |
Peter Collingbourne | 7cfe622 | 2016-11-02 02:58:47 +0000 | [diff] [blame] | 147 | EXPECT_EQ(8ull, Cursor.Read(8)); |
| 148 | } |
| 149 | } |
| 150 | |
Jordan Rose | 2217648 | 2014-11-13 00:08:41 +0000 | [diff] [blame] | 151 | } // end anonymous namespace |