blob: e7535f3e818eb0df90d35fa811d89473402fdc72 [file] [log] [blame]
Jordan Rose22176482014-11-13 00:08:41 +00001//===- 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 Carruth3c0d6072017-06-06 11:06:56 +000011#include "llvm/ADT/STLExtras.h"
Duncan P. N. Exon Smithfbf768a2016-03-27 23:00:59 +000012#include "llvm/Bitcode/BitstreamWriter.h"
Jordan Rose22176482014-11-13 00:08:41 +000013#include "gtest/gtest.h"
14
15using namespace llvm;
16
17namespace {
18
19TEST(BitstreamReaderTest, AtEndOfStream) {
20 uint8_t Bytes[4] = {
21 0x00, 0x01, 0x02, 0x03
22 };
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000023 BitstreamCursor Cursor(Bytes);
Jordan Rose22176482014-11-13 00:08:41 +000024
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
38TEST(BitstreamReaderTest, AtEndOfStreamJump) {
39 uint8_t Bytes[4] = {
40 0x00, 0x01, 0x02, 0x03
41 };
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000042 BitstreamCursor Cursor(Bytes);
Jordan Rose22176482014-11-13 00:08:41 +000043
44 Cursor.JumpToBit(32);
45 EXPECT_TRUE(Cursor.AtEndOfStream());
46}
47
48TEST(BitstreamReaderTest, AtEndOfStreamEmpty) {
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000049 BitstreamCursor Cursor(ArrayRef<uint8_t>{});
Jordan Rose22176482014-11-13 00:08:41 +000050
51 EXPECT_TRUE(Cursor.AtEndOfStream());
52}
53
Duncan P. N. Exon Smith2e87e9e2016-03-27 22:45:25 +000054TEST(BitstreamReaderTest, getCurrentByteNo) {
55 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03};
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000056 SimpleBitstreamCursor Cursor(Bytes);
Duncan P. N. Exon Smith2e87e9e2016-03-27 22:45:25 +000057
Peter Collingbournee8516582016-11-02 00:08:19 +000058 for (unsigned I = 0, E = 32; I != E; ++I) {
Duncan P. N. Exon Smith2e87e9e2016-03-27 22:45:25 +000059 EXPECT_EQ(I / 8, Cursor.getCurrentByteNo());
60 (void)Cursor.Read(1);
61 }
62 EXPECT_EQ(4u, Cursor.getCurrentByteNo());
63}
64
65TEST(BitstreamReaderTest, getPointerToByte) {
66 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000067 SimpleBitstreamCursor Cursor(Bytes);
Duncan P. N. Exon Smith2e87e9e2016-03-27 22:45:25 +000068
69 for (unsigned I = 0, E = 8; I != E; ++I) {
70 EXPECT_EQ(Bytes + I, Cursor.getPointerToByte(I, 1));
71 }
72}
73
74TEST(BitstreamReaderTest, getPointerToBit) {
75 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000076 SimpleBitstreamCursor Cursor(Bytes);
Duncan P. N. Exon Smith2e87e9e2016-03-27 22:45:25 +000077
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 Smithfbf768a2016-03-27 23:00:59 +000083TEST(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 Blaikie0581f9f2017-01-04 22:36:33 +0000104 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Duncan P. N. Exon Smithfbf768a2016-03-27 23:00:59 +0000105 Abbrev->Add(BitCodeAbbrevOp(RecordID));
106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikie0581f9f2017-01-04 22:36:33 +0000107 AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Duncan P. N. Exon Smithfbf768a2016-03-27 23:00:59 +0000108 unsigned Record[] = {RecordID};
109 Stream.EmitRecordWithBlob(AbbrevID, makeArrayRef(Record), BlobIn);
110
111 Stream.ExitBlock();
112 }
113
114 // Stream the buffer into the reader.
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000115 BitstreamCursor Stream(
Peter Collingbournee8516582016-11-02 00:08:19 +0000116 ArrayRef<uint8_t>((const uint8_t *)Buffer.begin(), Buffer.size()));
Duncan P. N. Exon Smithfbf768a2016-03-27 23:00:59 +0000117
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 Collingbourne7cfe6222016-11-02 02:58:47 +0000143TEST(BitstreamReaderTest, shortRead) {
144 uint8_t Bytes[] = {8, 7, 6, 5, 4, 3, 2, 1};
145 for (unsigned I = 1; I != 8; ++I) {
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000146 SimpleBitstreamCursor Cursor(ArrayRef<uint8_t>(Bytes, I));
Peter Collingbourne7cfe6222016-11-02 02:58:47 +0000147 EXPECT_EQ(8ull, Cursor.Read(8));
148 }
149}
150
Jordan Rose22176482014-11-13 00:08:41 +0000151} // end anonymous namespace