llvm.org GIT mirror llvm / 75f9dc9
Explicitly test BitstreamReader::setArtificialByteLimit, NFC 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264650 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
2 changed file(s) with 28 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
372372 if (!Size || Size > Limit)
373373 Size = Limit;
374374 }
375
376 /// Return the Size, if known.
377 uint64_t getSizeIfKnown() const { return Size; }
375378 };
376379
377380 /// When advancing through a bitstream cursor, each advance can discover a few
120120 SimpleBitstreamCursor Cursor(Reader);
121121
122122 Cursor.setArtificialByteLimit(8);
123 EXPECT_EQ(8u, Cursor.getSizeIfKnown());
123124 while (!Cursor.AtEndOfStream())
124125 (void)Cursor.Read(1);
125126
133134 SimpleBitstreamCursor Cursor(Reader);
134135
135136 Cursor.setArtificialByteLimit(5);
137 EXPECT_EQ(8u, Cursor.getSizeIfKnown());
136138 while (!Cursor.AtEndOfStream())
137139 (void)Cursor.Read(1);
138140
147149
148150 // The size of the memory object isn't known yet. Set it too high and
149151 // confirm that we don't read too far.
152 Cursor.setArtificialByteLimit(24);
153 EXPECT_EQ(24u, Cursor.getSizeIfKnown());
154 while (!Cursor.AtEndOfStream())
155 (void)Cursor.Read(1);
156
157 EXPECT_EQ(12u, Cursor.getCurrentByteNo());
158 EXPECT_EQ(12u, Cursor.getSizeIfKnown());
159 }
160
161 TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEndKnown) {
162 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
163 0x08, 0x09, 0x0a, 0x0b};
164 BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
165 SimpleBitstreamCursor Cursor(Reader);
166
167 // Save the size of the memory object in the cursor.
168 while (!Cursor.AtEndOfStream())
169 (void)Cursor.Read(1);
170 EXPECT_EQ(12u, Cursor.getCurrentByteNo());
171 EXPECT_EQ(12u, Cursor.getSizeIfKnown());
172
150173 Cursor.setArtificialByteLimit(20);
151 while (!Cursor.AtEndOfStream())
152 (void)Cursor.Read(1);
153
154 EXPECT_EQ(12u, Cursor.getCurrentByteNo());
155 }
156
157 TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEndKnown) {
158 uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
159 0x08, 0x09, 0x0a, 0x0b};
160 BitstreamReader Reader(std::begin(Bytes), std::end(Bytes));
161 SimpleBitstreamCursor Cursor(Reader);
162
163 // Save the size of the memory object in the cursor.
164 while (!Cursor.AtEndOfStream())
165 (void)Cursor.Read(1);
166 EXPECT_EQ(12u, Cursor.getCurrentByteNo());
167
168 Cursor.setArtificialByteLimit(20);
169 EXPECT_TRUE(Cursor.AtEndOfStream());
174 EXPECT_TRUE(Cursor.AtEndOfStream());
175 EXPECT_EQ(12u, Cursor.getSizeIfKnown());
170176 }
171177
172178 TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) {