Files
bytes/test/test_slice_buf.rs
T

68 lines
1.6 KiB
Rust
Raw Normal View History

2016-08-05 21:42:07 -07:00
use bytes::{Buf, MutBuf};
2016-09-23 22:53:52 -07:00
use bytes::buf::SliceBuf;
#[test]
pub fn test_initial_buf_empty() {
2016-09-23 22:53:52 -07:00
let buf = SliceBuf::with_capacity(100);
assert!(buf.capacity() == 128);
2016-09-23 22:53:52 -07:00
assert!(buf.remaining_write() == 128);
assert!(buf.remaining_read() == 0);
}
#[test]
2016-09-23 22:53:52 -07:00
pub fn test_slice_buf_bytes() {
let mut buf = SliceBuf::with_capacity(32);
2016-08-05 21:54:29 -07:00
buf.copy_from(&b"hello "[..]);
assert_eq!(&b"hello "[..], buf.bytes());
2016-08-05 21:54:29 -07:00
buf.copy_from(&b"world"[..]);
assert_eq!(&b"hello world"[..], buf.bytes());
}
#[test]
pub fn test_byte_buf_read_write() {
2016-09-23 22:53:52 -07:00
let mut buf = SliceBuf::with_capacity(32);
2016-08-05 21:54:29 -07:00
buf.copy_from(&b"hello world"[..]);
2016-09-23 22:53:52 -07:00
assert_eq!(21, buf.remaining_write());
2016-08-05 21:54:29 -07:00
buf.copy_from(&b" goodbye"[..]);
2016-09-23 22:53:52 -07:00
assert_eq!(13, buf.remaining_write());
let mut dst = [0; 5];
2016-09-23 22:53:52 -07:00
let pos = buf.position();
2016-08-05 21:54:29 -07:00
assert_eq!(5, buf.copy_to(&mut dst[..]));
assert_eq!(b"hello", &dst);
2016-09-23 22:53:52 -07:00
buf.set_position(pos);
2016-08-05 21:54:29 -07:00
assert_eq!(5, buf.copy_to(&mut dst[..]));
2015-03-23 17:12:39 -07:00
assert_eq!(b"hello", &dst);
2016-08-05 21:54:29 -07:00
assert_eq!(5, buf.copy_to(&mut dst[..]));
2015-03-23 17:12:39 -07:00
assert_eq!(b" worl", &dst);
let mut dst = [0; 2];
2016-08-05 21:54:29 -07:00
assert_eq!(2, buf.copy_to(&mut dst[..]));
2015-03-23 17:12:39 -07:00
assert_eq!(b"d ", &dst);
let mut dst = [0; 7];
2016-08-05 21:54:29 -07:00
assert_eq!(7, buf.copy_to(&mut dst[..]));
2015-03-23 17:12:39 -07:00
assert_eq!(b"goodbye", &dst);
2016-09-23 22:53:52 -07:00
assert_eq!(13, buf.remaining_write());
2016-08-05 21:54:29 -07:00
buf.copy_from(&b" have fun"[..]);
2016-09-23 22:53:52 -07:00
assert_eq!(4, buf.remaining_write());
2016-09-23 22:53:52 -07:00
assert_eq!(buf.bytes(), b" have fun");
buf.set_position(0);
assert_eq!(buf.bytes(), b"hello world goodbye have fun");
2016-09-23 22:53:52 -07:00
buf.clear();
assert_eq!(buf.bytes(), b"");
}