llvm.org GIT mirror llvm / 00d9a54
[PDB] Add an additional test for BinaryStreamRef. A bug was uncovered where if you have a StreamRef whose ViewOffset is > 0, then when you call readLongestContiguousChunk it will succeed even when it shouldn't, and it always return you a buffer that was taken as if the ViewOffset was 0. Fixed this bug and added a test for it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296556 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
2 changed file(s) with 60 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
121121 if (auto EC = checkOffset(Offset, 1))
122122 return EC;
123123
124 if (auto EC = Stream->readLongestContiguousChunk(Offset, Buffer))
124 if (auto EC =
125 Stream->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
125126 return EC;
126127 // This StreamRef might refer to a smaller window over a larger stream. In
127128 // that case we will have read out more bytes than we should return, because
134134 void SetUp() override {
135135 Streams.clear();
136136 Streams.resize(NumStreams);
137 for (int I = 0; I < NumStreams; ++I)
138 Streams[I].IsContiguous = (I % 2 == 0);
139
137140 InputData.clear();
138141 OutputData.clear();
139142 }
140143
141144 protected:
142145 struct StreamPair {
146 bool IsContiguous;
143147 std::unique_ptr Input;
144148 std::unique_ptr Output;
145149 };
209213 };
210214
211215 // Tests that a we can read from a BinaryByteStream without a StreamReader.
212 TEST_F(BinaryStreamTest, BinaryByteStreamProperties) {
216 TEST_F(BinaryStreamTest, BinaryByteStreamBounds) {
213217 std::vector InputData = {1, 2, 3, 4, 5};
214218 initializeInput(InputData);
215219
228232 }
229233 }
230234
235 TEST_F(BinaryStreamTest, StreamRefBounds) {
236 std::vector InputData = {1, 2, 3, 4, 5};
237 initializeInput(InputData);
238
239 for (const auto &Stream : Streams) {
240 ArrayRef Buffer;
241 BinaryStreamRef Ref(*Stream.Input);
242
243 // Read 1 byte from offset 2 should work
244 ASSERT_EQ(InputData.size(), Ref.getLength());
245 ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer));
246 EXPECT_EQ(makeArrayRef(InputData).slice(2, 1), Buffer);
247
248 // Reading everything from offset 2 on.
249 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
250 if (Stream.IsContiguous)
251 EXPECT_EQ(makeArrayRef(InputData).slice(2), Buffer);
252 else
253 EXPECT_FALSE(Buffer.empty());
254
255 // Reading 6 bytes from offset 0 is too big.
256 EXPECT_ERROR(Ref.readBytes(0, 6, Buffer));
257 EXPECT_ERROR(Ref.readLongestContiguousChunk(6, Buffer));
258
259 // Reading 1 byte from offset 2 after dropping 1 byte is the same as reading
260 // 1 byte from offset 3.
261 Ref = Ref.drop_front(1);
262 ASSERT_NO_ERROR(Ref.readBytes(2, 1, Buffer));
263 if (Stream.IsContiguous)
264 EXPECT_EQ(makeArrayRef(InputData).slice(3, 1), Buffer);
265 else
266 EXPECT_FALSE(Buffer.empty());
267
268 // Reading everything from offset 2 on after dropping 1 byte.
269 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
270 if (Stream.IsContiguous)
271 EXPECT_EQ(makeArrayRef(InputData).slice(3), Buffer);
272 else
273 EXPECT_FALSE(Buffer.empty());
274
275 // Reading 2 bytes from offset 2 after dropping 2 bytes is the same as
276 // reading 2 bytes from offset 4, and should fail.
277 Ref = Ref.drop_front(1);
278 EXPECT_ERROR(Ref.readBytes(2, 2, Buffer));
279
280 // But if we read the longest contiguous chunk instead, we should still
281 // get the 1 byte at the end.
282 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
283 EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer);
284 }
285 }
286
231287 // Test that we can write to a BinaryStream without a StreamWriter.
232 TEST_F(BinaryStreamTest, MutableBinaryByteStreamProperties) {
288 TEST_F(BinaryStreamTest, MutableBinaryByteStreamBounds) {
233289 std::vector InputData = {'T', 'e', 's', 't', '\0'};
234290 initializeInput(InputData);
235291 initializeOutput(InputData.size());