1 //===- BitstreamWriterTest.cpp - Tests for BitstreamWriter ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Bitstream/BitstreamWriter.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Bitstream/BitCodeEnums.h"
13 #include "llvm/Bitstream/BitstreamReader.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Testing/Support/SupportHelpers.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
24 TEST(BitstreamWriterTest
, emitBlob
) {
25 SmallString
<64> Buffer
;
26 BitstreamWriter
W(Buffer
);
27 W
.emitBlob("str", /* ShouldEmitSize */ false);
28 EXPECT_EQ(StringRef("str\0", 4), Buffer
);
31 TEST(BitstreamWriterTest
, emitBlobWithSize
) {
32 SmallString
<64> Buffer
;
34 BitstreamWriter
W(Buffer
);
37 SmallString
<64> Expected
;
39 BitstreamWriter
W(Expected
);
47 EXPECT_EQ(Expected
.str(), Buffer
);
50 TEST(BitstreamWriterTest
, emitBlobEmpty
) {
51 SmallString
<64> Buffer
;
52 BitstreamWriter
W(Buffer
);
53 W
.emitBlob("", /* ShouldEmitSize */ false);
54 EXPECT_EQ(StringRef(""), Buffer
);
57 TEST(BitstreamWriterTest
, emitBlob4ByteAligned
) {
58 SmallString
<64> Buffer
;
59 BitstreamWriter
W(Buffer
);
60 W
.emitBlob("str0", /* ShouldEmitSize */ false);
61 EXPECT_EQ(StringRef("str0"), Buffer
);
64 class BitstreamWriterFlushTest
: public ::testing::TestWithParam
<int> {
66 // Any value after bitc::FIRST_APPLICATION_BLOCKID is good, but let's pick a
68 const unsigned BlkID
= bitc::FIRST_APPLICATION_BLOCKID
+ 17;
70 void write(StringRef TestFilePath
, int FlushThreshold
,
71 llvm::function_ref
<void(BitstreamWriter
&)> Action
) {
73 raw_fd_stream
Out(TestFilePath
, EC
);
75 BitstreamWriter
W(Out
, FlushThreshold
);
80 TEST_P(BitstreamWriterFlushTest
, simpleExample
) {
81 llvm::unittest::TempFile
TestFile("bitstream", "", "",
83 write(TestFile
.path(), GetParam(),
84 [&](BitstreamWriter
&W
) { W
.EmitVBR(42, 2); });
86 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MB
=
87 MemoryBuffer::getFile(TestFile
.path());
89 ASSERT_NE(*MB
, nullptr);
90 BitstreamCursor
Cursor((*MB
)->getBuffer());
91 auto V
= Cursor
.ReadVBR(2);
96 TEST_P(BitstreamWriterFlushTest
, subBlock
) {
97 llvm::unittest::TempFile
TestFile("bitstream", "", "",
99 write(TestFile
.path(), GetParam(), [&](BitstreamWriter
&W
) {
100 W
.EnterSubblock(BlkID
, 2);
104 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MB
=
105 MemoryBuffer::getFile(TestFile
.path());
107 ASSERT_NE(*MB
, nullptr);
108 BitstreamCursor
Cursor((*MB
)->getBuffer());
109 auto Blk
= Cursor
.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs
);
111 EXPECT_EQ(Blk
->Kind
, BitstreamEntry::SubBlock
);
112 EXPECT_EQ(Blk
->ID
, BlkID
);
113 EXPECT_FALSE(Cursor
.EnterSubBlock(BlkID
));
114 auto V
= Cursor
.ReadVBR(2);
117 // ReadBlockEnd() returns false if it actually read the block end.
118 EXPECT_FALSE(Cursor
.ReadBlockEnd());
119 EXPECT_TRUE(Cursor
.AtEndOfStream());
122 TEST_P(BitstreamWriterFlushTest
, blobRawRead
) {
123 llvm::unittest::TempFile
TestFile("bitstream", "", "",
125 write(TestFile
.path(), GetParam(), [&](BitstreamWriter
&W
) {
126 W
.emitBlob("str", /* ShouldEmitSize */ false);
129 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MB
=
130 MemoryBuffer::getFile(TestFile
.path());
132 ASSERT_NE(*MB
, nullptr);
133 EXPECT_EQ(StringRef("str\0", 4), (*MB
)->getBuffer());
136 INSTANTIATE_TEST_SUITE_P(BitstreamWriterFlushCases
, BitstreamWriterFlushTest
,
137 ::testing::Values(0, 1 /*MB*/));