1 #![warn(rust_2018_idioms)]
2
3 use bytes::buf::Buf;
4 use bytes::Bytes;
5
6 #[test]
long_take()7 fn long_take() {
8 // Tests that get a take with a size greater than the buffer length will not
9 // overrun the buffer. Regression test for #138.
10 let buf = b"hello world".take(100);
11 assert_eq!(11, buf.remaining());
12 assert_eq!(b"hello world", buf.chunk());
13 }
14
15 #[test]
take_copy_to_bytes()16 fn take_copy_to_bytes() {
17 let mut abcd = Bytes::copy_from_slice(b"abcd");
18 let abcd_ptr = abcd.as_ptr();
19 let mut take = (&mut abcd).take(2);
20 let a = take.copy_to_bytes(1);
21 assert_eq!(Bytes::copy_from_slice(b"a"), a);
22 // assert `to_bytes` did not allocate
23 assert_eq!(abcd_ptr, a.as_ptr());
24 assert_eq!(Bytes::copy_from_slice(b"bcd"), abcd);
25 }
26
27 #[test]
28 #[should_panic]
take_copy_to_bytes_panics()29 fn take_copy_to_bytes_panics() {
30 let abcd = Bytes::copy_from_slice(b"abcd");
31 abcd.take(2).copy_to_bytes(3);
32 }
33
34 #[cfg(feature = "std")]
35 #[test]
take_chunks_vectored()36 fn take_chunks_vectored() {
37 fn chain() -> impl Buf {
38 Bytes::from([1, 2, 3].to_vec()).chain(Bytes::from([4, 5, 6].to_vec()))
39 }
40
41 {
42 let mut dst = [std::io::IoSlice::new(&[]); 2];
43 let take = chain().take(0);
44 assert_eq!(take.chunks_vectored(&mut dst), 0);
45 }
46
47 {
48 let mut dst = [std::io::IoSlice::new(&[]); 2];
49 let take = chain().take(1);
50 assert_eq!(take.chunks_vectored(&mut dst), 1);
51 assert_eq!(&*dst[0], &[1]);
52 }
53
54 {
55 let mut dst = [std::io::IoSlice::new(&[]); 2];
56 let take = chain().take(3);
57 assert_eq!(take.chunks_vectored(&mut dst), 1);
58 assert_eq!(&*dst[0], &[1, 2, 3]);
59 }
60
61 {
62 let mut dst = [std::io::IoSlice::new(&[]); 2];
63 let take = chain().take(4);
64 assert_eq!(take.chunks_vectored(&mut dst), 2);
65 assert_eq!(&*dst[0], &[1, 2, 3]);
66 assert_eq!(&*dst[1], &[4]);
67 }
68
69 {
70 let mut dst = [std::io::IoSlice::new(&[]); 2];
71 let take = chain().take(6);
72 assert_eq!(take.chunks_vectored(&mut dst), 2);
73 assert_eq!(&*dst[0], &[1, 2, 3]);
74 assert_eq!(&*dst[1], &[4, 5, 6]);
75 }
76
77 {
78 let mut dst = [std::io::IoSlice::new(&[]); 2];
79 let take = chain().take(7);
80 assert_eq!(take.chunks_vectored(&mut dst), 2);
81 assert_eq!(&*dst[0], &[1, 2, 3]);
82 assert_eq!(&*dst[1], &[4, 5, 6]);
83 }
84 }
85