BitstreamReaderTest.cpp 5.1 KB

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