BitstreamReaderTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "gtest/gtest.h"
  13. using namespace llvm;
  14. namespace {
  15. TEST(BitstreamReaderTest, AtEndOfStream) {
  16. uint8_t Bytes[4] = {
  17. 0x00, 0x01, 0x02, 0x03
  18. };
  19. BitstreamReader Reader(Bytes);
  20. BitstreamCursor Cursor(Reader);
  21. EXPECT_FALSE(Cursor.AtEndOfStream());
  22. (void)Cursor.Read(8);
  23. EXPECT_FALSE(Cursor.AtEndOfStream());
  24. (void)Cursor.Read(24);
  25. EXPECT_TRUE(Cursor.AtEndOfStream());
  26. Cursor.JumpToBit(0);
  27. EXPECT_FALSE(Cursor.AtEndOfStream());
  28. Cursor.JumpToBit(32);
  29. EXPECT_TRUE(Cursor.AtEndOfStream());
  30. }
  31. TEST(BitstreamReaderTest, AtEndOfStreamJump) {
  32. uint8_t Bytes[4] = {
  33. 0x00, 0x01, 0x02, 0x03
  34. };
  35. BitstreamReader Reader(Bytes);
  36. BitstreamCursor Cursor(Reader);
  37. Cursor.JumpToBit(32);
  38. EXPECT_TRUE(Cursor.AtEndOfStream());
  39. }
  40. TEST(BitstreamReaderTest, AtEndOfStreamEmpty) {
  41. BitstreamReader Reader(ArrayRef<uint8_t>{});
  42. BitstreamCursor Cursor(Reader);
  43. EXPECT_TRUE(Cursor.AtEndOfStream());
  44. }
  45. TEST(BitstreamReaderTest, getCurrentByteNo) {
  46. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03};
  47. BitstreamReader Reader(Bytes);
  48. SimpleBitstreamCursor Cursor(Reader);
  49. for (unsigned I = 0, E = 32; I != E; ++I) {
  50. EXPECT_EQ(I / 8, Cursor.getCurrentByteNo());
  51. (void)Cursor.Read(1);
  52. }
  53. EXPECT_EQ(4u, Cursor.getCurrentByteNo());
  54. }
  55. TEST(BitstreamReaderTest, getPointerToByte) {
  56. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  57. BitstreamReader Reader(Bytes);
  58. SimpleBitstreamCursor Cursor(Reader);
  59. for (unsigned I = 0, E = 8; I != E; ++I) {
  60. EXPECT_EQ(Bytes + I, Cursor.getPointerToByte(I, 1));
  61. }
  62. }
  63. TEST(BitstreamReaderTest, getPointerToBit) {
  64. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  65. BitstreamReader Reader(Bytes);
  66. SimpleBitstreamCursor Cursor(Reader);
  67. for (unsigned I = 0, E = 8; I != E; ++I) {
  68. EXPECT_EQ(Bytes + I, Cursor.getPointerToBit(I * 8, 1));
  69. }
  70. }
  71. TEST(BitstreamReaderTest, jumpToPointer) {
  72. uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  73. BitstreamReader Reader(Bytes);
  74. SimpleBitstreamCursor Cursor(Reader);
  75. for (unsigned I : {0, 6, 2, 7}) {
  76. Cursor.jumpToPointer(Bytes + I);
  77. EXPECT_EQ(I, Cursor.getCurrentByteNo());
  78. }
  79. }
  80. TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) {
  81. SmallVector<uint8_t, 1> BlobData;
  82. for (unsigned I = 0, E = 1024; I != E; ++I)
  83. BlobData.push_back(I);
  84. // Try a bunch of different sizes.
  85. const unsigned Magic = 0x12345678;
  86. const unsigned BlockID = bitc::FIRST_APPLICATION_BLOCKID;
  87. const unsigned RecordID = 1;
  88. for (unsigned I = 0, BlobSize = 0, E = BlobData.size(); BlobSize < E;
  89. BlobSize += ++I) {
  90. StringRef BlobIn((const char *)BlobData.begin(), BlobSize);
  91. // Write the bitcode.
  92. SmallVector<char, 1> Buffer;
  93. unsigned AbbrevID;
  94. {
  95. BitstreamWriter Stream(Buffer);
  96. Stream.Emit(Magic, 32);
  97. Stream.EnterSubblock(BlockID, 3);
  98. BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
  99. Abbrev->Add(BitCodeAbbrevOp(RecordID));
  100. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
  101. AbbrevID = Stream.EmitAbbrev(Abbrev);
  102. unsigned Record[] = {RecordID};
  103. Stream.EmitRecordWithBlob(AbbrevID, makeArrayRef(Record), BlobIn);
  104. Stream.ExitBlock();
  105. }
  106. // Stream the buffer into the reader.
  107. BitstreamReader R(
  108. ArrayRef<uint8_t>((const uint8_t *)Buffer.begin(), Buffer.size()));
  109. BitstreamCursor Stream(R);
  110. // Header. Included in test so that we can run llvm-bcanalyzer to debug
  111. // when there are problems.
  112. ASSERT_EQ(Magic, Stream.Read(32));
  113. // Block.
  114. BitstreamEntry Entry =
  115. Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
  116. ASSERT_EQ(BitstreamEntry::SubBlock, Entry.Kind);
  117. ASSERT_EQ(BlockID, Entry.ID);
  118. ASSERT_FALSE(Stream.EnterSubBlock(BlockID));
  119. // Abbreviation.
  120. Entry = Stream.advance();
  121. ASSERT_EQ(BitstreamEntry::Record, Entry.Kind);
  122. ASSERT_EQ(AbbrevID, Entry.ID);
  123. // Record.
  124. StringRef BlobOut;
  125. SmallVector<uint64_t, 1> Record;
  126. ASSERT_EQ(RecordID, Stream.readRecord(Entry.ID, Record, &BlobOut));
  127. EXPECT_TRUE(Record.empty());
  128. EXPECT_EQ(BlobIn, BlobOut);
  129. }
  130. }
  131. } // end anonymous namespace