1 | #![warn (rust_2018_idioms)] |
2 | |
3 | use bytes::Buf; |
4 | #[cfg (feature = "std" )] |
5 | use std::io::IoSlice; |
6 | |
7 | #[test] |
8 | fn test_fresh_cursor_vec() { |
9 | let mut buf = &b"hello" [..]; |
10 | |
11 | assert_eq!(buf.remaining(), 5); |
12 | assert_eq!(buf.chunk(), b"hello" ); |
13 | |
14 | buf.advance(2); |
15 | |
16 | assert_eq!(buf.remaining(), 3); |
17 | assert_eq!(buf.chunk(), b"llo" ); |
18 | |
19 | buf.advance(3); |
20 | |
21 | assert_eq!(buf.remaining(), 0); |
22 | assert_eq!(buf.chunk(), b"" ); |
23 | } |
24 | |
25 | #[test] |
26 | fn test_get_u8() { |
27 | let mut buf = &b" \x21zomg" [..]; |
28 | assert_eq!(0x21, buf.get_u8()); |
29 | } |
30 | |
31 | #[test] |
32 | fn test_get_u16() { |
33 | let mut buf = &b" \x21\x54zomg" [..]; |
34 | assert_eq!(0x2154, buf.get_u16()); |
35 | let mut buf = &b" \x21\x54zomg" [..]; |
36 | assert_eq!(0x5421, buf.get_u16_le()); |
37 | } |
38 | |
39 | #[test] |
40 | #[should_panic ] |
41 | fn test_get_u16_buffer_underflow() { |
42 | let mut buf = &b" \x21" [..]; |
43 | buf.get_u16(); |
44 | } |
45 | |
46 | #[cfg (feature = "std" )] |
47 | #[test] |
48 | fn test_bufs_vec() { |
49 | let buf = &b"hello world" [..]; |
50 | |
51 | let b1: &[u8] = &mut []; |
52 | let b2: &[u8] = &mut []; |
53 | |
54 | let mut dst = [IoSlice::new(b1), IoSlice::new(b2)]; |
55 | |
56 | assert_eq!(1, buf.chunks_vectored(&mut dst[..])); |
57 | } |
58 | |
59 | #[test] |
60 | fn test_vec_deque() { |
61 | use std::collections::VecDeque; |
62 | |
63 | let mut buffer: VecDeque<u8> = VecDeque::new(); |
64 | buffer.extend(b"hello world" ); |
65 | assert_eq!(11, buffer.remaining()); |
66 | assert_eq!(b"hello world" , buffer.chunk()); |
67 | buffer.advance(6); |
68 | assert_eq!(b"world" , buffer.chunk()); |
69 | buffer.extend(b" piece" ); |
70 | let mut out = [0; 11]; |
71 | buffer.copy_to_slice(&mut out); |
72 | assert_eq!(b"world piece" , &out[..]); |
73 | } |
74 | |
75 | #[allow (unused_allocation)] // This is intentional. |
76 | #[test] |
77 | fn test_deref_buf_forwards() { |
78 | struct Special; |
79 | |
80 | impl Buf for Special { |
81 | fn remaining(&self) -> usize { |
82 | unreachable!("remaining" ); |
83 | } |
84 | |
85 | fn chunk(&self) -> &[u8] { |
86 | unreachable!("chunk" ); |
87 | } |
88 | |
89 | fn advance(&mut self, _: usize) { |
90 | unreachable!("advance" ); |
91 | } |
92 | |
93 | fn get_u8(&mut self) -> u8 { |
94 | // specialized! |
95 | b'x' |
96 | } |
97 | } |
98 | |
99 | // these should all use the specialized method |
100 | assert_eq!(Special.get_u8(), b'x' ); |
101 | assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x' ); |
102 | assert_eq!((Box::new(Special) as Box<dyn Buf>).get_u8(), b'x' ); |
103 | assert_eq!(Box::new(Special).get_u8(), b'x' ); |
104 | } |
105 | |
106 | #[test] |
107 | fn copy_to_bytes_less() { |
108 | let mut buf = &b"hello world" [..]; |
109 | |
110 | let bytes = buf.copy_to_bytes(5); |
111 | assert_eq!(bytes, &b"hello" [..]); |
112 | assert_eq!(buf, &b" world" [..]) |
113 | } |
114 | |
115 | #[test] |
116 | #[should_panic ] |
117 | fn copy_to_bytes_overflow() { |
118 | let mut buf = &b"hello world" [..]; |
119 | |
120 | let _bytes = buf.copy_to_bytes(12); |
121 | } |
122 | |