Skip to content

Commit 3400206

Browse files
committed
Test more chunk splits
1 parent 719f7d1 commit 3400206

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

tests/stall.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,58 @@ fn test_truncated_file() {
5353
}
5454
}
5555

56+
#[track_caller]
57+
fn decode_chopped_anim(r: ChoppedReader) {
58+
let frames = gif::DecodeOptions::new().read_info(r).unwrap()
59+
.into_iter().enumerate()
60+
.map(|(n, f)| f.expect(&n.to_string()))
61+
.count();
62+
assert_eq!(frames, 14);
63+
}
64+
5665
#[test]
5766
fn one_byte_at_a_time() {
58-
let r = OneByte {
67+
decode_chopped_anim(ChoppedReader {
68+
chunk_len: 1,
5969
data: include_bytes!("../tests/samples/moon_impact.gif"),
60-
};
61-
let frames = gif::DecodeOptions::new().read_info(r).unwrap()
62-
.into_iter().enumerate().map(|(n, f)| {
63-
f.expect(&n.to_string())
64-
}).count();
65-
assert_eq!(frames, 14);
70+
});
71+
}
72+
73+
#[test]
74+
fn two_bytes_at_a_time() {
75+
decode_chopped_anim(ChoppedReader {
76+
chunk_len: 2,
77+
data: include_bytes!("../tests/samples/moon_impact.gif"),
78+
});
6679
}
6780

68-
struct OneByte<'a> {
81+
#[test]
82+
fn three_bytes_at_a_time() {
83+
decode_chopped_anim(ChoppedReader {
84+
chunk_len: 3,
85+
data: include_bytes!("../tests/samples/moon_impact.gif"),
86+
});
87+
}
88+
89+
struct ChoppedReader<'a> {
90+
chunk_len: usize,
6991
data: &'a [u8],
7092
}
7193

72-
impl io::BufRead for OneByte<'_> {
94+
impl io::BufRead for ChoppedReader<'_> {
7395
fn fill_buf(&mut self) -> io::Result<&[u8]> {
74-
Ok(&self.data[..self.data.len().min(1)])
96+
Ok(&self.data[..self.data.len().min(self.chunk_len)])
7597
}
98+
7699
fn consume(&mut self, n: usize) {
77-
debug_assert!(n <= 1);
100+
debug_assert!(n <= self.chunk_len);
78101
self.data = &self.data[n..];
79102
}
80103
}
81104

82-
impl io::Read for OneByte<'_> {
105+
impl io::Read for ChoppedReader<'_> {
83106
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
84-
let n = self.data.len().min(buf.len()).min(1);
107+
let n = self.data.len().min(buf.len()).min(self.chunk_len);
85108
buf[..n].copy_from_slice(&self.data[..n]);
86109
self.data = &self.data[n..];
87110
Ok(n)

0 commit comments

Comments
 (0)