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"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Bitcode/BitstreamWriter.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16
17 namespace {
18
TEST(BitstreamReaderTest,AtEndOfStream)19 TEST(BitstreamReaderTest, AtEndOfStream) {
20 uint8_t Bytes[4] = {
21 0x00, 0x01, 0x02, 0x03
22 };
23 BitstreamCursor Cursor(Bytes);
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
TEST(BitstreamReaderTest,AtEndOfStreamJump)38 TEST(BitstreamReaderTest, AtEndOfStreamJump) {
39 uint8_t Bytes[4] = {
40 0x00, 0x01, 0x02, 0x03
41 };
42 BitstreamCursor Cursor(Bytes);
43
44 Cursor.JumpToBit(32);
45 EXPECT_TRUE(Cursor.AtEndOfStream());
46 }
47
TEST(BitstreamReaderTest,AtEndOfStreamEmpty)48 TEST(BitstreamReaderTest, AtEndOfStreamEmpty) {
49 BitstreamCursor Cursor(ArrayRef<uint8_t>{});
50
51 EXPECT_TRUE(Cursor.AtEndOfStream());
52 }
53
TEST(BitstreamReaderTest,getCurrentByteNo)54 TEST(BitstreamReaderTest, getCurrentByteNo) {
55 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03};
56 SimpleBitstreamCursor Cursor(Bytes);
57
58 for (unsigned I = 0, E = 32; I != E; ++I) {
59 EXPECT_EQ(I / 8, Cursor.getCurrentByteNo());
60 (void)Cursor.Read(1);
61 }
62 EXPECT_EQ(4u, Cursor.getCurrentByteNo());
63 }
64
TEST(BitstreamReaderTest,getPointerToByte)65 TEST(BitstreamReaderTest, getPointerToByte) {
66 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
67 SimpleBitstreamCursor Cursor(Bytes);
68
69 for (unsigned I = 0, E = 8; I != E; ++I) {
70 EXPECT_EQ(Bytes + I, Cursor.getPointerToByte(I, 1));
71 }
72 }
73
TEST(BitstreamReaderTest,getPointerToBit)74 TEST(BitstreamReaderTest, getPointerToBit) {
75 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
76 SimpleBitstreamCursor Cursor(Bytes);
77
78 for (unsigned I = 0, E = 8; I != E; ++I) {
79 EXPECT_EQ(Bytes + I, Cursor.getPointerToBit(I * 8, 1));
80 }
81 }
82
TEST(BitstreamReaderTest,readRecordWithBlobWhileStreaming)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
104 auto Abbrev = std::make_shared<BitCodeAbbrev>();
105 Abbrev->Add(BitCodeAbbrevOp(RecordID));
106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
107 AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
108 unsigned Record[] = {RecordID};
109 Stream.EmitRecordWithBlob(AbbrevID, makeArrayRef(Record), BlobIn);
110
111 Stream.ExitBlock();
112 }
113
114 // Stream the buffer into the reader.
115 BitstreamCursor Stream(
116 ArrayRef<uint8_t>((const uint8_t *)Buffer.begin(), Buffer.size()));
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
TEST(BitstreamReaderTest,shortRead)143 TEST(BitstreamReaderTest, shortRead) {
144 uint8_t Bytes[] = {8, 7, 6, 5, 4, 3, 2, 1};
145 for (unsigned I = 1; I != 8; ++I) {
146 SimpleBitstreamCursor Cursor(ArrayRef<uint8_t>(Bytes, I));
147 EXPECT_EQ(8ull, Cursor.Read(8));
148 }
149 }
150
151 } // end anonymous namespace
152