WIP - Implement abstractions for working with bytes

This commit is contained in:
Carl Lerche
2015-02-09 21:24:53 -08:00
parent 52aeab40ee
commit 3c4ebb85f0
16 changed files with 2447 additions and 10 deletions
+46
View File
@@ -0,0 +1,46 @@
use bytes::ByteBuf;
use bytes::traits::*;
#[test]
pub fn test_initial_buf_empty() {
let buf = ByteBuf::mut_with_capacity(100);
assert!(buf.capacity() == 128);
assert!(buf.remaining() == 128);
let buf = buf.flip();
assert!(buf.remaining() == 0);
let buf = buf.flip();
assert!(buf.remaining() == 128);
}
#[test]
pub fn test_byte_buf_read_write() {
let mut buf = ByteBuf::mut_with_capacity(32);
buf.write(b"hello world").unwrap();
assert_eq!(21, buf.remaining());
buf.write(b" goodbye").unwrap();
assert_eq!(13, buf.remaining());
let mut buf = buf.flip();
let mut dst = [0; 5];
assert_eq!(5, buf.read(dst.as_mut_slice()).unwrap());
assert_eq!(b"hello", dst);
assert_eq!(5, buf.read(dst.as_mut_slice()).unwrap());
assert_eq!(b" worl", dst);
let mut dst = [0; 2];
assert_eq!(2, buf.read(dst.as_mut_slice()).unwrap());
assert_eq!(b"d ", dst);
let mut dst = [0; 7];
assert_eq!(7, buf.read(dst.as_mut_slice()).unwrap());
assert_eq!(b"goodbye", dst);
}