1 #![warn(rust_2018_idioms)]
2
3 use bytes::buf::UninitSlice;
4 use bytes::{BufMut, BytesMut};
5 use core::fmt::Write;
6 use core::usize;
7
8 #[test]
test_vec_as_mut_buf()9 fn test_vec_as_mut_buf() {
10 let mut buf = Vec::with_capacity(64);
11
12 assert_eq!(buf.remaining_mut(), usize::MAX);
13
14 assert!(buf.chunk_mut().len() >= 64);
15
16 buf.put(&b"zomg"[..]);
17
18 assert_eq!(&buf, b"zomg");
19
20 assert_eq!(buf.remaining_mut(), usize::MAX - 4);
21 assert_eq!(buf.capacity(), 64);
22
23 for _ in 0..16 {
24 buf.put(&b"zomg"[..]);
25 }
26
27 assert_eq!(buf.len(), 68);
28 }
29
30 #[test]
test_put_u8()31 fn test_put_u8() {
32 let mut buf = Vec::with_capacity(8);
33 buf.put_u8(33);
34 assert_eq!(b"\x21", &buf[..]);
35 }
36
37 #[test]
test_put_u16()38 fn test_put_u16() {
39 let mut buf = Vec::with_capacity(8);
40 buf.put_u16(8532);
41 assert_eq!(b"\x21\x54", &buf[..]);
42
43 buf.clear();
44 buf.put_u16_le(8532);
45 assert_eq!(b"\x54\x21", &buf[..]);
46 }
47
48 #[test]
49 #[should_panic(expected = "cannot advance")]
test_vec_advance_mut()50 fn test_vec_advance_mut() {
51 // Verify fix for #354
52 let mut buf = Vec::with_capacity(8);
53 unsafe {
54 buf.advance_mut(12);
55 }
56 }
57
58 #[test]
test_clone()59 fn test_clone() {
60 let mut buf = BytesMut::with_capacity(100);
61 buf.write_str("this is a test").unwrap();
62 let buf2 = buf.clone();
63
64 buf.write_str(" of our emergency broadcast system").unwrap();
65 assert!(buf != buf2);
66 }
67
68 #[test]
test_mut_slice()69 fn test_mut_slice() {
70 let mut v = vec![0, 0, 0, 0];
71 let mut s = &mut v[..];
72 s.put_u32(42);
73 }
74
75 #[test]
test_deref_bufmut_forwards()76 fn test_deref_bufmut_forwards() {
77 struct Special;
78
79 unsafe impl BufMut for Special {
80 fn remaining_mut(&self) -> usize {
81 unreachable!("remaining_mut");
82 }
83
84 fn chunk_mut(&mut self) -> &mut UninitSlice {
85 unreachable!("chunk_mut");
86 }
87
88 unsafe fn advance_mut(&mut self, _: usize) {
89 unreachable!("advance");
90 }
91
92 fn put_u8(&mut self, _: u8) {
93 // specialized!
94 }
95 }
96
97 // these should all use the specialized method
98 Special.put_u8(b'x');
99 (&mut Special as &mut dyn BufMut).put_u8(b'x');
100 (Box::new(Special) as Box<dyn BufMut>).put_u8(b'x');
101 Box::new(Special).put_u8(b'x');
102 }
103
104 #[test]
105 #[should_panic]
write_byte_panics_if_out_of_bounds()106 fn write_byte_panics_if_out_of_bounds() {
107 let mut data = [b'b', b'a', b'r'];
108
109 let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
110 slice.write_byte(4, b'f');
111 }
112
113 #[test]
114 #[should_panic]
copy_from_slice_panics_if_different_length_1()115 fn copy_from_slice_panics_if_different_length_1() {
116 let mut data = [b'b', b'a', b'r'];
117
118 let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
119 slice.copy_from_slice(b"a");
120 }
121
122 #[test]
123 #[should_panic]
copy_from_slice_panics_if_different_length_2()124 fn copy_from_slice_panics_if_different_length_2() {
125 let mut data = [b'b', b'a', b'r'];
126
127 let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
128 slice.copy_from_slice(b"abcd");
129 }
130