BitstreamReaderTest.cpp 4.4 KB

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