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