Add test that impl Seek for BufReader correctly invalidates buffer between seeks

This commit is contained in:
Chris Gregory 2019-05-25 15:02:35 -05:00
parent fc45382c12
commit f27ec0f05f
1 changed files with 34 additions and 0 deletions

View File

@ -1162,6 +1162,40 @@ mod tests {
assert_eq!(reader.get_ref().pos, expected);
}
fn test_buffered_reader_seek_underflow_discard_buffer_between_seeks() {
// gimmick reader that returns Err after first seek
struct ErrAfterFirstSeekReader {
first_seek: bool,
}
impl Read for ErrAfterFirstSeekReader {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
for x in &mut *buf {
*x = 0;
}
Ok(buf.len())
}
}
impl Seek for ErrAfterFirstSeekReader {
fn seek(&mut self, _: SeekFrom) -> io::Result<u64> {
if self.first_seek {
self.first_seek = false;
Ok(0)
} else {
Err(io::Error::new(io::ErrorKind::Other, "oh no!"))
}
}
}
let mut reader = BufReader::with_capacity(5, ErrAfterFirstSeekReader { first_seek: true });
assert_eq!(reader.fill_buf().ok(), Some(&[0, 0, 0, 0, 0][..]));
// The following seek will require two underlying seeks. The first will
// succeed but the second will fail. This should still invalidate the
// buffer.
assert!(reader.seek(SeekFrom::Current(i64::min_value())).is_err());
assert_eq!(reader.buffer().len(), 0);
}
#[test]
fn test_buffered_writer() {
let inner = Vec::new();