Explicitly test BitstreamReader::setArtificialByteLimit, NFC
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Mon, 28 Mar 2016 20:39:41 +0000 (20:39 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Mon, 28 Mar 2016 20:39:41 +0000 (20:39 +0000)
Explicitly check that artificial byte limit is rounded correctly by
exposing BitstreamReader::Size through a new accessor, getSizeIfKnown.

The original code for rounding (from r264547) wasn't obviously correct,
and even though r264623 cleaned it up (by calling llvm::alignTo) I think
it's worth testing.

llvm-svn: 264650

llvm/include/llvm/Bitcode/BitstreamReader.h
llvm/unittests/Bitcode/BitstreamReaderTest.cpp

index f664339..628a83c 100644 (file)
@@ -373,6 +373,9 @@ public:
     if (!Size || Size > Limit)
       Size = Limit;
   }
+
+  /// Return the Size, if known.
+  uint64_t getSizeIfKnown() const { return Size; }
 };
 
 /// When advancing through a bitstream cursor, each advance can discover a few
index c99239b..2be774c 100644 (file)
@@ -121,6 +121,7 @@ TEST(BitstreamReaderTest, setArtificialByteLimit) {
   SimpleBitstreamCursor Cursor(Reader);
 
   Cursor.setArtificialByteLimit(8);
+  EXPECT_EQ(8u, Cursor.getSizeIfKnown());
   while (!Cursor.AtEndOfStream())
     (void)Cursor.Read(1);
 
@@ -134,6 +135,7 @@ TEST(BitstreamReaderTest, setArtificialByteLimitNotWordBoundary) {
   SimpleBitstreamCursor Cursor(Reader);
 
   Cursor.setArtificialByteLimit(5);
+  EXPECT_EQ(8u, Cursor.getSizeIfKnown());
   while (!Cursor.AtEndOfStream())
     (void)Cursor.Read(1);
 
@@ -148,11 +150,13 @@ TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEnd) {
 
   // The size of the memory object isn't known yet.  Set it too high and
   // confirm that we don't read too far.
-  Cursor.setArtificialByteLimit(20);
+  Cursor.setArtificialByteLimit(24);
+  EXPECT_EQ(24u, Cursor.getSizeIfKnown());
   while (!Cursor.AtEndOfStream())
     (void)Cursor.Read(1);
 
   EXPECT_EQ(12u, Cursor.getCurrentByteNo());
+  EXPECT_EQ(12u, Cursor.getSizeIfKnown());
 }
 
 TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEndKnown) {
@@ -165,9 +169,11 @@ TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEndKnown) {
   while (!Cursor.AtEndOfStream())
     (void)Cursor.Read(1);
   EXPECT_EQ(12u, Cursor.getCurrentByteNo());
+  EXPECT_EQ(12u, Cursor.getSizeIfKnown());
 
   Cursor.setArtificialByteLimit(20);
   EXPECT_TRUE(Cursor.AtEndOfStream());
+  EXPECT_EQ(12u, Cursor.getSizeIfKnown());
 }
 
 TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) {