BitstreamReaderTest.cpp 4.2 KB

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