2020-07-10 01:12:39 +09:00
|
|
|
#![warn(rust_2018_idioms)]
|
2016-11-01 08:14:31 -07:00
|
|
|
|
2020-10-19 15:48:23 -07:00
|
|
|
use bytes::buf::UninitSlice;
|
2020-05-22 13:17:30 +09:00
|
|
|
use bytes::{BufMut, BytesMut};
|
2020-05-21 19:18:20 -07:00
|
|
|
use core::fmt::Write;
|
2020-05-22 13:17:30 +09:00
|
|
|
use core::usize;
|
2016-08-31 12:07:44 -07:00
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 08:14:31 -07:00
|
|
|
fn test_vec_as_mut_buf() {
|
2016-08-31 12:07:44 -07:00
|
|
|
let mut buf = Vec::with_capacity(64);
|
|
|
|
|
|
2021-08-07 08:22:18 +02:00
|
|
|
assert_eq!(buf.remaining_mut(), isize::MAX as usize);
|
2016-08-31 12:07:44 -07:00
|
|
|
|
2020-12-18 11:04:31 -08:00
|
|
|
assert!(buf.chunk_mut().len() >= 64);
|
2016-08-31 12:07:44 -07:00
|
|
|
|
2017-02-16 11:15:46 -08:00
|
|
|
buf.put(&b"zomg"[..]);
|
2016-08-31 12:07:44 -07:00
|
|
|
|
|
|
|
|
assert_eq!(&buf, b"zomg");
|
|
|
|
|
|
2021-08-07 08:22:18 +02:00
|
|
|
assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(buf.capacity(), 64);
|
|
|
|
|
|
|
|
|
|
for _ in 0..16 {
|
2017-02-16 11:15:46 -08:00
|
|
|
buf.put(&b"zomg"[..]);
|
2016-08-31 12:07:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert_eq!(buf.len(), 68);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 18:43:53 +01:00
|
|
|
#[test]
|
|
|
|
|
fn test_vec_put_bytes() {
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
buf.push(17);
|
|
|
|
|
buf.put_bytes(19, 2);
|
|
|
|
|
assert_eq!([17, 19, 19], &buf[..]);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 12:07:44 -07:00
|
|
|
#[test]
|
2016-11-01 08:14:31 -07:00
|
|
|
fn test_put_u8() {
|
2016-08-31 12:07:44 -07:00
|
|
|
let mut buf = Vec::with_capacity(8);
|
2019-08-27 22:09:43 +02:00
|
|
|
buf.put_u8(33);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(b"\x21", &buf[..]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 08:14:31 -07:00
|
|
|
fn test_put_u16() {
|
2016-08-31 12:07:44 -07:00
|
|
|
let mut buf = Vec::with_capacity(8);
|
2018-03-12 09:27:09 -07:00
|
|
|
buf.put_u16(8532);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(b"\x21\x54", &buf[..]);
|
|
|
|
|
|
|
|
|
|
buf.clear();
|
2018-03-12 09:27:09 -07:00
|
|
|
buf.put_u16_le(8532);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(b"\x54\x21", &buf[..]);
|
|
|
|
|
}
|
2016-11-21 15:43:59 -08:00
|
|
|
|
2021-08-08 18:43:09 +01:00
|
|
|
#[test]
|
|
|
|
|
fn test_put_int() {
|
|
|
|
|
let mut buf = Vec::with_capacity(8);
|
|
|
|
|
buf.put_int(0x1020304050607080, 3);
|
|
|
|
|
assert_eq!(b"\x60\x70\x80", &buf[..]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn test_put_int_nbytes_overflow() {
|
|
|
|
|
let mut buf = Vec::with_capacity(8);
|
|
|
|
|
buf.put_int(0x1020304050607080, 9);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_put_int_le() {
|
|
|
|
|
let mut buf = Vec::with_capacity(8);
|
|
|
|
|
buf.put_int_le(0x1020304050607080, 3);
|
|
|
|
|
assert_eq!(b"\x80\x70\x60", &buf[..]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn test_put_int_le_nbytes_overflow() {
|
|
|
|
|
let mut buf = Vec::with_capacity(8);
|
|
|
|
|
buf.put_int_le(0x1020304050607080, 9);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-30 16:14:54 -07:00
|
|
|
#[test]
|
2020-03-13 16:50:04 -05:00
|
|
|
#[should_panic(expected = "cannot advance")]
|
2017-04-30 16:14:54 -07:00
|
|
|
fn test_vec_advance_mut() {
|
2020-03-13 16:50:04 -05:00
|
|
|
// Verify fix for #354
|
2017-04-30 16:14:54 -07:00
|
|
|
let mut buf = Vec::with_capacity(8);
|
|
|
|
|
unsafe {
|
|
|
|
|
buf.advance_mut(12);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 15:43:59 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_clone() {
|
2017-02-15 14:48:49 -08:00
|
|
|
let mut buf = BytesMut::with_capacity(100);
|
2016-11-21 15:43:59 -08:00
|
|
|
buf.write_str("this is a test").unwrap();
|
|
|
|
|
let buf2 = buf.clone();
|
|
|
|
|
|
2019-07-09 18:02:23 +07:00
|
|
|
buf.write_str(" of our emergency broadcast system").unwrap();
|
2017-02-15 14:48:49 -08:00
|
|
|
assert!(buf != buf2);
|
2016-11-21 15:43:59 -08:00
|
|
|
}
|
2017-02-28 11:15:35 -08:00
|
|
|
|
2019-06-07 12:31:10 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_mut_slice() {
|
|
|
|
|
let mut v = vec![0, 0, 0, 0];
|
|
|
|
|
let mut s = &mut v[..];
|
|
|
|
|
s.put_u32(42);
|
2021-02-16 03:35:01 +03:00
|
|
|
|
|
|
|
|
assert_eq!(s.len(), 0);
|
|
|
|
|
assert_eq!(&v, &[0, 0, 0, 42]);
|
2019-06-07 12:31:10 -07:00
|
|
|
}
|
2019-12-06 16:33:04 -08:00
|
|
|
|
2021-08-08 18:43:53 +01:00
|
|
|
#[test]
|
|
|
|
|
fn test_slice_put_bytes() {
|
|
|
|
|
let mut v = [0, 0, 0, 0];
|
|
|
|
|
let mut s = &mut v[..];
|
|
|
|
|
s.put_u8(17);
|
|
|
|
|
s.put_bytes(19, 2);
|
|
|
|
|
assert_eq!(1, s.remaining_mut());
|
|
|
|
|
assert_eq!(&[17, 19, 19, 0], &v[..]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 16:33:04 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_deref_bufmut_forwards() {
|
|
|
|
|
struct Special;
|
|
|
|
|
|
2020-10-16 15:45:38 -07:00
|
|
|
unsafe impl BufMut for Special {
|
2019-12-06 16:33:04 -08:00
|
|
|
fn remaining_mut(&self) -> usize {
|
|
|
|
|
unreachable!("remaining_mut");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 11:04:31 -08:00
|
|
|
fn chunk_mut(&mut self) -> &mut UninitSlice {
|
|
|
|
|
unreachable!("chunk_mut");
|
2019-12-06 16:33:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn advance_mut(&mut self, _: usize) {
|
|
|
|
|
unreachable!("advance");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn put_u8(&mut self, _: u8) {
|
|
|
|
|
// specialized!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// these should all use the specialized method
|
|
|
|
|
Special.put_u8(b'x');
|
|
|
|
|
(&mut Special as &mut dyn BufMut).put_u8(b'x');
|
|
|
|
|
(Box::new(Special) as Box<dyn BufMut>).put_u8(b'x');
|
|
|
|
|
Box::new(Special).put_u8(b'x');
|
|
|
|
|
}
|
2020-10-19 15:48:23 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn write_byte_panics_if_out_of_bounds() {
|
|
|
|
|
let mut data = [b'b', b'a', b'r'];
|
|
|
|
|
|
|
|
|
|
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
|
|
|
|
|
slice.write_byte(4, b'f');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn copy_from_slice_panics_if_different_length_1() {
|
|
|
|
|
let mut data = [b'b', b'a', b'r'];
|
|
|
|
|
|
|
|
|
|
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
|
|
|
|
|
slice.copy_from_slice(b"a");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn copy_from_slice_panics_if_different_length_2() {
|
|
|
|
|
let mut data = [b'b', b'a', b'r'];
|
|
|
|
|
|
|
|
|
|
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
|
|
|
|
|
slice.copy_from_slice(b"abcd");
|
|
|
|
|
}
|