Skip to content

Commit e89e2e4

Browse files
committed
Added test stubs
1 parent 2c3024b commit e89e2e4

File tree

1 file changed

+86
-23
lines changed

1 file changed

+86
-23
lines changed

src/libstd/io/buffered.rs

+86-23
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
971971
return match bufs.iter().find(|buf| !buf.is_empty()) {
972972
Some(buf) => self.write(buf),
973973
None => Ok(0),
974-
}
974+
};
975975
}
976976

977977
// Find the buffer containing the last newline
@@ -1321,7 +1321,11 @@ mod tests {
13211321

13221322
impl Read for ShortReader {
13231323
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
1324-
if self.lengths.is_empty() { Ok(0) } else { Ok(self.lengths.remove(0)) }
1324+
if self.lengths.is_empty() {
1325+
Ok(0)
1326+
} else {
1327+
Ok(self.lengths.remove(0))
1328+
}
13251329
}
13261330
}
13271331

@@ -1742,27 +1746,48 @@ mod tests {
17421746
b.iter(|| BufWriter::new(io::sink()));
17431747
}
17441748

1745-
struct AcceptOneThenFail {
1746-
written: bool,
1749+
#[derive(Default, Clone)]
1750+
struct ProgrammableSink {
1751+
// Writes append to this slice
1752+
buffer: Vec<u8>,
1753+
1754+
// Flushes set this flag
17471755
flushed: bool,
1756+
1757+
// If true, writes & flushes will always be an error
1758+
return_error: bool,
1759+
1760+
// If set, only up to this number of bytes will be written in a single
1761+
// call to `write`
1762+
accept_prefix: Option<usize>,
1763+
1764+
// If set, counts down with each write, and writes return an error
1765+
// when it hits 0
1766+
max_writes: Option<usize>,
17481767
}
17491768

1750-
impl Write for AcceptOneThenFail {
1769+
impl Write for ProgrammableSink {
17511770
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
1752-
if !self.written {
1753-
assert_eq!(data, b"a\nb\n");
1754-
self.written = true;
1755-
Ok(data.len())
1771+
if self.return_error {
1772+
Err(io::Error::new(io::ErrorKind::Other, "test"))
17561773
} else {
1757-
Err(io::Error::new(io::ErrorKind::NotFound, "test"))
1774+
let len = match self.accept_prefix {
1775+
None => data.len(),
1776+
Some(prefix) => prefix.min(prefix),
1777+
};
1778+
let data = &data[..len];
1779+
self.buffer.extend_from_slice(data);
1780+
Ok(len)
17581781
}
17591782
}
17601783

17611784
fn flush(&mut self) -> io::Result<()> {
1762-
assert!(self.written);
1763-
assert!(!self.flushed);
1764-
self.flushed = true;
1765-
Err(io::Error::new(io::ErrorKind::Other, "test"))
1785+
if self.return_error {
1786+
Err(io::Error::new(io::ErrorKind::Other, "test"))
1787+
} else {
1788+
self.flushed = true;
1789+
Ok(())
1790+
}
17661791
}
17671792
}
17681793

@@ -1777,15 +1802,7 @@ mod tests {
17771802
/// Regression test for #37807
17781803
#[test]
17791804
fn erroneous_flush_retried() {
1780-
let a = AcceptOneThenFail { written: false, flushed: false };
1781-
1782-
let mut l = LineWriter::new(a);
1783-
assert_eq!(l.write(b"a\nb\na").unwrap(), 4);
1784-
assert!(l.get_ref().written);
1785-
assert!(l.get_ref().flushed);
1786-
l.get_mut().flushed = false;
1787-
1788-
assert_eq!(l.write(b"a").unwrap_err().kind(), io::ErrorKind::Other)
1805+
todo!()
17891806
}
17901807

17911808
#[test]
@@ -1895,4 +1912,50 @@ mod tests {
18951912
io::Error::new(io::ErrorKind::Other, "x")
18961913
}
18971914
}
1915+
1916+
/// Test that, given this input:
1917+
///
1918+
/// Line 1\n
1919+
/// Line 2\n
1920+
/// Line 3\n
1921+
/// Line 4
1922+
///
1923+
/// And given a result that only writes to midway through Line 2
1924+
///
1925+
/// That only up to the end of Line 3 is buffered
1926+
///
1927+
/// This behavior is desirable because it prevents flushing partial lines
1928+
#[test]
1929+
fn test_partial_write_buffers_line() {
1930+
todo!()
1931+
}
1932+
1933+
/// Test that, given this input:
1934+
///
1935+
/// Line 1\n
1936+
/// Line 2\n
1937+
/// Line 3
1938+
///
1939+
/// And given that the full write of lines 1 and 2 was successful
1940+
/// That data up to Line 3 is buffered
1941+
#[test]
1942+
fn test_partial_line_buffered_after_line_write() {
1943+
todo!()
1944+
}
1945+
1946+
/// Test that, given a partial line that exceeds the length of
1947+
/// LineBuffer's buffer (that is, without a trailing newline), that that
1948+
/// line is written to the inner writer
1949+
#[test]
1950+
fn test_long_line_flushed() {
1951+
todo!()
1952+
}
1953+
1954+
/// Test that, given a very long partial line *after* successfully
1955+
/// flushing a complete line, that that line is buffered unconditionally,
1956+
/// and no additional writes take place
1957+
#[test]
1958+
fn test_long_tail_not_flushed() {
1959+
todo!()
1960+
}
18981961
}

0 commit comments

Comments
 (0)