xref: /llvm-project/llvm/unittests/Bitstream/BitstreamWriterTest.cpp (revision 1def2579e10dd84405465f403e8c31acebff0c97)
1 //===- BitstreamWriterTest.cpp - Tests for BitstreamWriter ----------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Bitstream/BitstreamWriter.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 namespace {
17 
18 TEST(BitstreamWriterTest, emitBlob) {
19   SmallString<64> Buffer;
20   BitstreamWriter W(Buffer);
21   W.emitBlob("str", /* ShouldEmitSize */ false);
22   EXPECT_EQ(StringRef("str\0", 4), Buffer);
23 }
24 
25 TEST(BitstreamWriterTest, emitBlobWithSize) {
26   SmallString<64> Buffer;
27   {
28     BitstreamWriter W(Buffer);
29     W.emitBlob("str");
30   }
31   SmallString<64> Expected;
32   {
33     BitstreamWriter W(Expected);
34     W.EmitVBR(3, 6);
35     W.FlushToWord();
36     W.Emit('s', 8);
37     W.Emit('t', 8);
38     W.Emit('r', 8);
39     W.Emit(0, 8);
40   }
41   EXPECT_EQ(Expected.str(), Buffer);
42 }
43 
44 TEST(BitstreamWriterTest, emitBlobEmpty) {
45   SmallString<64> Buffer;
46   BitstreamWriter W(Buffer);
47   W.emitBlob("", /* ShouldEmitSize */ false);
48   EXPECT_EQ(StringRef(""), Buffer);
49 }
50 
51 TEST(BitstreamWriterTest, emitBlob4ByteAligned) {
52   SmallString<64> Buffer;
53   BitstreamWriter W(Buffer);
54   W.emitBlob("str0", /* ShouldEmitSize */ false);
55   EXPECT_EQ(StringRef("str0"), Buffer);
56 }
57 
58 } // end namespace
59