BitstreamReaderTest.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/Bitcode/BitstreamReader.h"
  11. #include "llvm/Bitcode/BitstreamWriter.h"
  12. #include "llvm/Support/StreamingMemoryObject.h"
  13. #include "gtest/gtest.h"
  14. using namespace llvm;
  15. namespace {
  16. class BufferStreamer : public DataStreamer {
  17. StringRef Buffer;
  18. public:
  19. BufferStreamer(StringRef Buffer) : Buffer(Buffer) {}
  20. size_t GetBytes(unsigned char *OutBuffer, size_t Length) override {
  21. if (Length >= Buffer.size())
  22. Length = Buffer.size();
  23. std::copy(Buffer.begin(), Buffer.begin() + Length, OutBuffer);
  24. Buffer = Buffer.drop_front(Length);
  25. return Length;
  26. }
  27. };
  28. TEST(BitstreamReaderTest, AtEndOfStream) {
  29. uint8_t Bytes[4] = {
  30. 0x00, 0x01, 0x02, 0x03
  31. };
  32. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  33. BitstreamCursor Cursor(Reader);
  34. EXPECT_FALSE(Cursor.AtEndOfStream());
  35. (void)Cursor.Read(8);
  36. EXPECT_FALSE(Cursor.AtEndOfStream());
  37. (void)Cursor.Read(24);
  38. EXPECT_TRUE(Cursor.AtEndOfStream());
  39. Cursor.JumpToBit(0);
  40. EXPECT_FALSE(Cursor.AtEndOfStream());
  41. Cursor.JumpToBit(32);
  42. EXPECT_TRUE(Cursor.AtEndOfStream());
  43. }
  44. TEST(BitstreamReaderTest, AtEndOfStreamJump) {
  45. uint8_t Bytes[4] = {
  46. 0x00, 0x01, 0x02, 0x03
  47. };
  48. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  49. BitstreamCursor Cursor(Reader);
  50. Cursor.JumpToBit(32);
  51. EXPECT_TRUE(Cursor.AtEndOfStream());
  52. }
  53. TEST(BitstreamReaderTest, AtEndOfStreamEmpty) {
  54. uint8_t Dummy = 0xFF;
  55. BitstreamReader Reader(&Dummy, &Dummy);
  56. BitstreamCursor Cursor(Reader);
  57. EXPECT_TRUE(Cursor.AtEndOfStream());
  58. }
  59. TEST(BitstreamReaderTest, getCurrentByteNo) {
  60. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03};
  61. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  62. SimpleBitstreamCursor Cursor(Reader);
  63. for (unsigned I = 0, E = 33; I != E; ++I) {
  64. EXPECT_EQ(I / 8, Cursor.getCurrentByteNo());
  65. (void)Cursor.Read(1);
  66. }
  67. EXPECT_EQ(4u, Cursor.getCurrentByteNo());
  68. }
  69. TEST(BitstreamReaderTest, getPointerToByte) {
  70. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  71. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  72. SimpleBitstreamCursor Cursor(Reader);
  73. for (unsigned I = 0, E = 8; I != E; ++I) {
  74. EXPECT_EQ(Bytes + I, Cursor.getPointerToByte(I, 1));
  75. }
  76. }
  77. TEST(BitstreamReaderTest, getPointerToBit) {
  78. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  79. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  80. SimpleBitstreamCursor Cursor(Reader);
  81. for (unsigned I = 0, E = 8; I != E; ++I) {
  82. EXPECT_EQ(Bytes + I, Cursor.getPointerToBit(I * 8, 1));
  83. }
  84. }
  85. TEST(BitstreamReaderTest, jumpToPointer) {
  86. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  87. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  88. SimpleBitstreamCursor Cursor(Reader);
  89. for (unsigned I : {0, 6, 2, 7}) {
  90. Cursor.jumpToPointer(Bytes + I);
  91. EXPECT_EQ(I, Cursor.getCurrentByteNo());
  92. }
  93. }
  94. TEST(BitstreamReaderTest, setArtificialByteLimit) {
  95. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  96. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
  97. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  98. SimpleBitstreamCursor Cursor(Reader);
  99. Cursor.setArtificialByteLimit(8);
  100. while (!Cursor.AtEndOfStream())
  101. (void)Cursor.Read(1);
  102. EXPECT_EQ(8u, Cursor.getCurrentByteNo());
  103. }
  104. TEST(BitstreamReaderTest, setArtificialByteLimitNotWordBoundary) {
  105. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  106. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
  107. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  108. SimpleBitstreamCursor Cursor(Reader);
  109. Cursor.setArtificialByteLimit(5);
  110. while (!Cursor.AtEndOfStream())
  111. (void)Cursor.Read(1);
  112. EXPECT_EQ(8u, Cursor.getCurrentByteNo());
  113. }
  114. TEST(BitstreamReaderTest, setArtificialByteLimitNot4ByteBoundary) {
  115. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  116. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
  117. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  118. SimpleBitstreamCursor Cursor(Reader);
  119. Cursor.setArtificialByteLimit(5);
  120. while (!Cursor.AtEndOfStream())
  121. (void)Cursor.Read(1);
  122. EXPECT_EQ(8u, Cursor.getCurrentByteNo());
  123. }
  124. TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEnd) {
  125. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  126. 0x08, 0x09, 0x0a, 0x0b};
  127. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  128. SimpleBitstreamCursor Cursor(Reader);
  129. // The size of the memory object isn't known yet. Set it too high and
  130. // confirm that we don't read too far.
  131. Cursor.setArtificialByteLimit(20);
  132. while (!Cursor.AtEndOfStream())
  133. (void)Cursor.Read(1);
  134. EXPECT_EQ(12u, Cursor.getCurrentByteNo());
  135. }
  136. TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEndKnown) {
  137. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  138. 0x08, 0x09, 0x0a, 0x0b};
  139. BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
  140. SimpleBitstreamCursor Cursor(Reader);
  141. // Save the size of the memory object in the cursor.
  142. while (!Cursor.AtEndOfStream())
  143. (void)Cursor.Read(1);
  144. EXPECT_EQ(12u, Cursor.getCurrentByteNo());
  145. Cursor.setArtificialByteLimit(20);
  146. EXPECT_TRUE(Cursor.AtEndOfStream());
  147. }
  148. TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) {
  149. SmallVector<uint8_t, 1> BlobData;
  150. for (unsigned I = 0, E = 1024; I != E; ++I)
  151. BlobData.push_back(I);
  152. // Try a bunch of different sizes.
  153. const unsigned Magic = 0x12345678;
  154. const unsigned BlockID = bitc::FIRST_APPLICATION_BLOCKID;
  155. const unsigned RecordID = 1;
  156. for (unsigned I = 0, BlobSize = 0, E = BlobData.size(); BlobSize < E;
  157. BlobSize += ++I) {
  158. StringRef BlobIn((const char *)BlobData.begin(), BlobSize);
  159. // Write the bitcode.
  160. SmallVector<char, 1> Buffer;
  161. unsigned AbbrevID;
  162. {
  163. BitstreamWriter Stream(Buffer);
  164. Stream.Emit(Magic, 32);
  165. Stream.EnterSubblock(BlockID, 3);
  166. BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
  167. Abbrev->Add(BitCodeAbbrevOp(RecordID));
  168. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
  169. AbbrevID = Stream.EmitAbbrev(Abbrev);
  170. unsigned Record[] = {RecordID};
  171. Stream.EmitRecordWithBlob(AbbrevID, makeArrayRef(Record), BlobIn);
  172. Stream.ExitBlock();
  173. }
  174. // Stream the buffer into the reader.
  175. BitstreamReader R(llvm::make_unique<StreamingMemoryObject>(
  176. llvm::make_unique<BufferStreamer>(
  177. StringRef(Buffer.begin(), Buffer.size()))));
  178. BitstreamCursor Stream(R);
  179. // Header. Included in test so that we can run llvm-bcanalyzer to debug
  180. // when there are problems.
  181. ASSERT_EQ(Magic, Stream.Read(32));
  182. // Block.
  183. BitstreamEntry Entry =
  184. Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
  185. ASSERT_EQ(BitstreamEntry::SubBlock, Entry.Kind);
  186. ASSERT_EQ(BlockID, Entry.ID);
  187. ASSERT_FALSE(Stream.EnterSubBlock(BlockID));
  188. // Abbreviation.
  189. Entry = Stream.advance();
  190. ASSERT_EQ(BitstreamEntry::Record, Entry.Kind);
  191. ASSERT_EQ(AbbrevID, Entry.ID);
  192. // Record.
  193. StringRef BlobOut;
  194. SmallVector<uint64_t, 1> Record;
  195. ASSERT_EQ(RecordID, Stream.readRecord(Entry.ID, Record, &BlobOut));
  196. EXPECT_TRUE(Record.empty());
  197. EXPECT_EQ(BlobIn, BlobOut);
  198. }
  199. }
  200. } // end anonymous namespace