add mark/reset feature to ByteBuf and RingBuf

This commit is contained in:
Dan Burkert
2015-04-11 15:18:32 -07:00
parent 40f1d1aa77
commit 4d645c7f53
4 changed files with 110 additions and 5 deletions
+4
View File
@@ -30,6 +30,10 @@ pub fn test_byte_buf_read_write() {
let mut buf = buf.flip();
let mut dst = [0; 5];
buf.mark();
assert_eq!(5, buf.read(&mut dst[..]).unwrap());
assert_eq!(b"hello", &dst);
buf.reset();
assert_eq!(5, buf.read(&mut dst[..]).unwrap());
assert_eq!(b"hello", &dst);
+25
View File
@@ -19,9 +19,16 @@ pub fn test_initial_buf_empty() {
assert_eq!(buf.bytes(), [1, 2, 3]);
let mut out = [0u8; 3];
buf.mark();
let bytes_read = buf.read(&mut out[..]).unwrap();;
assert_eq!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]);
buf.reset();
let bytes_read = buf.read(&mut out[..]).unwrap();;
assert_eq!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]);
assert_eq!(MutBuf::remaining(&buf), 16);
assert_eq!(Buf::remaining(&buf), 0);
}
@@ -39,6 +46,11 @@ fn test_wrapping_write() {
let bytes_written = buf.write(&[23;8][..]).unwrap();
assert_eq!(bytes_written, 8);
buf.mark();
let bytes_read = buf.read(&mut out[..]).unwrap();
assert_eq!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
buf.reset();
let bytes_read = buf.read(&mut out[..]).unwrap();
assert_eq!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
@@ -64,3 +76,16 @@ fn test_io_write_and_read() {
assert_eq!(bytes_read, 8);
assert_eq!(out, [2;8]);
}
#[test]
#[should_panic]
fn test_wrap_reset() {
use std::io::{Read, Write};
let mut buf = RingBuf::new(8);
buf.write(&[1, 2, 3, 4, 5, 6, 7]).unwrap();
buf.mark();
buf.read(&mut [0; 4]).unwrap();
buf.write(&[1, 2, 3, 4]).unwrap();
buf.reset();
}