Skip to content

Commit

Permalink
Add tests for 0-byte read propagation.
Browse files Browse the repository at this point in the history
The two `Some(0)' used to be `None' before the patch, a zero-byte long
read exhausting a reader (and thereafter) still produce a `None'.
  • Loading branch information
spaolacci committed Dec 23, 2013
1 parent cab6878 commit ee887d7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/libstd/io/buffered.rs
Expand Up @@ -322,6 +322,21 @@ mod test {
fn write(&mut self, _: &[u8]) { }
}

/// A dummy reader intended at testing short-reads propagation.
pub struct ShortReader {
priv lengths: ~[uint],
}

impl Reader for ShortReader {
fn read(&mut self, _: &mut [u8]) -> Option<uint> {
self.lengths.shift_opt()
}

fn eof(&mut self) -> bool {
self.lengths.len() == 0
}
}

#[test]
fn test_buffered_reader() {
let inner = MemReader::new(~[0, 1, 2, 3, 4]);
Expand Down Expand Up @@ -475,6 +490,19 @@ mod test {
assert_eq!(it.next(), None);
}

#[test]
fn test_short_reads() {
let inner = ShortReader{lengths: ~[0, 1, 2, 0, 1, 0]};
let mut reader = BufferedReader::new(inner);
let mut buf = [0, 0];
assert_eq!(reader.read(buf), Some(0));
assert_eq!(reader.read(buf), Some(1));
assert_eq!(reader.read(buf), Some(2));
assert_eq!(reader.read(buf), Some(0));
assert_eq!(reader.read(buf), Some(1));
assert_eq!(reader.read(buf), None);
}


#[bench]
fn bench_buffered_reader(bh: &mut Harness) {
Expand Down

5 comments on commit ee887d7

@bors
Copy link
Contributor

@bors bors commented on ee887d7 Dec 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at spaolacci@ee887d7

@bors
Copy link
Contributor

@bors bors commented on ee887d7 Dec 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging spaolacci/rust/0read = ee887d7 into auto

@bors
Copy link
Contributor

@bors bors commented on ee887d7 Dec 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaolacci/rust/0read = ee887d7 merged ok, testing candidate = 619c4fc

@bors
Copy link
Contributor

@bors bors commented on ee887d7 Dec 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on ee887d7 Dec 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 619c4fc

Please sign in to comment.