| 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] |
| 9 |
fn test_vec_as_mut_buf() { |
| 10 |
let mut buf = Vec::with_capacity(64); |
| 11 |
|
| 12 |
assert_eq!(buf.remaining_mut(), isize::MAX as usize); |
| 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(), isize::MAX as usize - 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] |
| 31 |
fn test_vec_put_bytes() { |
| 32 |
let mut buf = Vec::new(); |
| 33 |
buf.push(17); |
| 34 |
buf.put_bytes(19, 2); |
| 35 |
assert_eq!([17, 19, 19], &buf[..]); |
| 36 |
} |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn test_put_u8() { |
| 40 |
let mut buf = Vec::with_capacity(8); |
| 41 |
buf.put_u8(33); |
| 42 |
assert_eq!(b"\x21", &buf[..]); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_put_u16() { |
| 47 |
let mut buf = Vec::with_capacity(8); |
| 48 |
buf.put_u16(8532); |
| 49 |
assert_eq!(b"\x21\x54", &buf[..]); |
| 50 |
|
| 51 |
buf.clear(); |
| 52 |
buf.put_u16_le(8532); |
| 53 |
assert_eq!(b"\x54\x21", &buf[..]); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn test_put_int() { |
| 58 |
let mut buf = Vec::with_capacity(8); |
| 59 |
buf.put_int(0x1020304050607080, 3); |
| 60 |
assert_eq!(b"\x60\x70\x80", &buf[..]); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
#[should_panic] |
| 65 |
fn test_put_int_nbytes_overflow() { |
| 66 |
let mut buf = Vec::with_capacity(8); |
| 67 |
buf.put_int(0x1020304050607080, 9); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_put_int_le() { |
| 72 |
let mut buf = Vec::with_capacity(8); |
| 73 |
buf.put_int_le(0x1020304050607080, 3); |
| 74 |
assert_eq!(b"\x80\x70\x60", &buf[..]); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
#[should_panic] |
| 79 |
fn test_put_int_le_nbytes_overflow() { |
| 80 |
let mut buf = Vec::with_capacity(8); |
| 81 |
buf.put_int_le(0x1020304050607080, 9); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
#[should_panic(expected = "cannot advance")] |
| 86 |
fn test_vec_advance_mut() { |
| 87 |
// Verify fix for #354 |
| 88 |
let mut buf = Vec::with_capacity(8); |
| 89 |
unsafe { |
| 90 |
buf.advance_mut(12); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn test_clone() { |
| 96 |
let mut buf = BytesMut::with_capacity(100); |
| 97 |
buf.write_str("this is a test").unwrap(); |
| 98 |
let buf2 = buf.clone(); |
| 99 |
|
| 100 |
buf.write_str(" of our emergency broadcast system").unwrap(); |
| 101 |
assert!(buf != buf2); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn test_mut_slice() { |
| 106 |
let mut v = vec![0, 0, 0, 0]; |
| 107 |
let mut s = &mut v[..]; |
| 108 |
s.put_u32(42); |
| 109 |
|
| 110 |
assert_eq!(s.len(), 0); |
| 111 |
assert_eq!(&v, &[0, 0, 0, 42]); |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_slice_put_bytes() { |
| 116 |
let mut v = [0, 0, 0, 0]; |
| 117 |
let mut s = &mut v[..]; |
| 118 |
s.put_u8(17); |
| 119 |
s.put_bytes(19, 2); |
| 120 |
assert_eq!(1, s.remaining_mut()); |
| 121 |
assert_eq!(&[17, 19, 19, 0], &v[..]); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_deref_bufmut_forwards() { |
| 126 |
struct Special; |
| 127 |
|
| 128 |
unsafe impl BufMut for Special { |
| 129 |
fn remaining_mut(&self) -> usize { |
| 130 |
unreachable!("remaining_mut"); |
| 131 |
} |
| 132 |
|
| 133 |
fn chunk_mut(&mut self) -> &mut UninitSlice { |
| 134 |
unreachable!("chunk_mut"); |
| 135 |
} |
| 136 |
|
| 137 |
unsafe fn advance_mut(&mut self, _: usize) { |
| 138 |
unreachable!("advance"); |
| 139 |
} |
| 140 |
|
| 141 |
fn put_u8(&mut self, _: u8) { |
| 142 |
// specialized! |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// these should all use the specialized method |
| 147 |
Special.put_u8(b'x'); |
| 148 |
(&mut Special as &mut dyn BufMut).put_u8(b'x'); |
| 149 |
(Box::new(Special) as Box<dyn BufMut>).put_u8(b'x'); |
| 150 |
Box::new(Special).put_u8(b'x'); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
#[should_panic] |
| 155 |
fn write_byte_panics_if_out_of_bounds() { |
| 156 |
let mut data = [b'b', b'a', b'r']; |
| 157 |
|
| 158 |
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; |
| 159 |
slice.write_byte(4, b'f'); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
#[should_panic] |
| 164 |
fn copy_from_slice_panics_if_different_length_1() { |
| 165 |
let mut data = [b'b', b'a', b'r']; |
| 166 |
|
| 167 |
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; |
| 168 |
slice.copy_from_slice(b"a"); |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
#[should_panic] |
| 173 |
fn copy_from_slice_panics_if_different_length_2() { |
| 174 |
let mut data = [b'b', b'a', b'r']; |
| 175 |
|
| 176 |
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; |
| 177 |
slice.copy_from_slice(b"abcd"); |
| 178 |
} |