summaryrefslogtreecommitdiffstats
path: root/vendor/bytes/tests/test_chain.rs
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 /vendor/bytes/tests/test_chain.rs
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 'vendor/bytes/tests/test_chain.rs')
-rw-r--r--vendor/bytes/tests/test_chain.rs134
1 files changed, 134 insertions, 0 deletions
diff --git a/vendor/bytes/tests/test_chain.rs b/vendor/bytes/tests/test_chain.rs
new file mode 100644
index 000000000..500ccd4a8
--- /dev/null
+++ b/vendor/bytes/tests/test_chain.rs
@@ -0,0 +1,134 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::{Buf, BufMut, Bytes};
+#[cfg(feature = "std")]
+use std::io::IoSlice;
+
+#[test]
+fn collect_two_bufs() {
+ let a = Bytes::from(&b"hello"[..]);
+ let b = Bytes::from(&b"world"[..]);
+
+ let res = a.chain(b).copy_to_bytes(10);
+ assert_eq!(res, &b"helloworld"[..]);
+}
+
+#[test]
+fn writing_chained() {
+ let mut a = [0u8; 64];
+ let mut b = [0u8; 64];
+
+ {
+ let mut buf = (&mut a[..]).chain_mut(&mut b[..]);
+
+ for i in 0u8..128 {
+ buf.put_u8(i);
+ }
+ }
+
+ for i in 0..64 {
+ let expect = i as u8;
+ assert_eq!(expect, a[i]);
+ assert_eq!(expect + 64, b[i]);
+ }
+}
+
+#[test]
+fn iterating_two_bufs() {
+ let a = Bytes::from(&b"hello"[..]);
+ let b = Bytes::from(&b"world"[..]);
+
+ let res: Vec<u8> = a.chain(b).into_iter().collect();
+ assert_eq!(res, &b"helloworld"[..]);
+}
+
+#[cfg(feature = "std")]
+#[test]
+fn vectored_read() {
+ let a = Bytes::from(&b"hello"[..]);
+ let b = Bytes::from(&b"world"[..]);
+
+ let mut buf = a.chain(b);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(2, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"hello"[..]);
+ assert_eq!(iovecs[1][..], b"world"[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+
+ buf.advance(2);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(2, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"llo"[..]);
+ assert_eq!(iovecs[1][..], b"world"[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+
+ buf.advance(3);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(1, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"world"[..]);
+ assert_eq!(iovecs[1][..], b""[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+
+ buf.advance(3);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(1, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"ld"[..]);
+ assert_eq!(iovecs[1][..], b""[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+}