summaryrefslogtreecommitdiffstats
path: root/library/std/src/io/util
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /library/std/src/io/util
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/src/io/util')
-rw-r--r--library/std/src/io/util/tests.rs147
1 files changed, 147 insertions, 0 deletions
diff --git a/library/std/src/io/util/tests.rs b/library/std/src/io/util/tests.rs
new file mode 100644
index 000000000..08972a59a
--- /dev/null
+++ b/library/std/src/io/util/tests.rs
@@ -0,0 +1,147 @@
+use crate::cmp::{max, min};
+use crate::io::prelude::*;
+use crate::io::{
+ copy, empty, repeat, sink, BufWriter, Empty, ReadBuf, Repeat, Result, SeekFrom, Sink,
+ DEFAULT_BUF_SIZE,
+};
+
+use crate::mem::MaybeUninit;
+
+#[test]
+fn copy_copies() {
+ let mut r = repeat(0).take(4);
+ let mut w = sink();
+ assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
+
+ let mut r = repeat(0).take(1 << 17);
+ assert_eq!(copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(), 1 << 17);
+}
+
+struct ShortReader {
+ cap: usize,
+ read_size: usize,
+ observed_buffer: usize,
+}
+
+impl Read for ShortReader {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+ let bytes = min(self.cap, self.read_size);
+ self.cap -= bytes;
+ self.observed_buffer = max(self.observed_buffer, buf.len());
+ Ok(bytes)
+ }
+}
+
+struct WriteObserver {
+ observed_buffer: usize,
+}
+
+impl Write for WriteObserver {
+ fn write(&mut self, buf: &[u8]) -> Result<usize> {
+ self.observed_buffer = max(self.observed_buffer, buf.len());
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> Result<()> {
+ Ok(())
+ }
+}
+
+#[test]
+fn copy_specializes_bufwriter() {
+ let cap = 117 * 1024;
+ let buf_sz = 16 * 1024;
+ let mut r = ShortReader { cap, observed_buffer: 0, read_size: 1337 };
+ let mut w = BufWriter::with_capacity(buf_sz, WriteObserver { observed_buffer: 0 });
+ assert_eq!(
+ copy(&mut r, &mut w).unwrap(),
+ cap as u64,
+ "expected the whole capacity to be copied"
+ );
+ assert_eq!(r.observed_buffer, buf_sz, "expected a large buffer to be provided to the reader");
+ assert!(w.get_mut().observed_buffer > DEFAULT_BUF_SIZE, "expected coalesced writes");
+}
+
+#[test]
+fn sink_sinks() {
+ let mut s = sink();
+ assert_eq!(s.write(&[]).unwrap(), 0);
+ assert_eq!(s.write(&[0]).unwrap(), 1);
+ assert_eq!(s.write(&[0; 1024]).unwrap(), 1024);
+ assert_eq!(s.by_ref().write(&[0; 1024]).unwrap(), 1024);
+}
+
+#[test]
+fn empty_reads() {
+ let mut e = empty();
+ assert_eq!(e.read(&mut []).unwrap(), 0);
+ assert_eq!(e.read(&mut [0]).unwrap(), 0);
+ assert_eq!(e.read(&mut [0; 1024]).unwrap(), 0);
+ assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
+
+ let mut buf = [];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+
+ let mut buf = [MaybeUninit::uninit()];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+
+ let mut buf = [MaybeUninit::uninit(); 1024];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+
+ let mut buf = [MaybeUninit::uninit(); 1024];
+ let mut buf = ReadBuf::uninit(&mut buf);
+ e.by_ref().read_buf(&mut buf).unwrap();
+ assert_eq!(buf.filled_len(), 0);
+ assert_eq!(buf.initialized_len(), 0);
+}
+
+#[test]
+fn empty_seeks() {
+ let mut e = empty();
+ assert!(matches!(e.seek(SeekFrom::Start(0)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Start(1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Start(u64::MAX)), Ok(0)));
+
+ assert!(matches!(e.seek(SeekFrom::End(i64::MIN)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(-1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(0)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::End(i64::MAX)), Ok(0)));
+
+ assert!(matches!(e.seek(SeekFrom::Current(i64::MIN)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(-1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(0)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(1)), Ok(0)));
+ assert!(matches!(e.seek(SeekFrom::Current(i64::MAX)), Ok(0)));
+}
+
+#[test]
+fn repeat_repeats() {
+ let mut r = repeat(4);
+ let mut b = [0; 1024];
+ assert_eq!(r.read(&mut b).unwrap(), 1024);
+ assert!(b.iter().all(|b| *b == 4));
+}
+
+#[test]
+fn take_some_bytes() {
+ assert_eq!(repeat(4).take(100).bytes().count(), 100);
+ assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
+ assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
+}
+
+#[allow(dead_code)]
+fn const_utils() {
+ const _: Empty = empty();
+ const _: Repeat = repeat(b'c');
+ const _: Sink = sink();
+}