Files
bytes/test/test_ring.rs
T

130 lines
3.4 KiB
Rust
Raw Normal View History

2016-09-23 12:05:32 -07:00
use bytes::{Buf, MutBuf};
use bytes::buf::RingBuf;
2015-04-09 23:18:16 +02:00
2016-09-25 22:40:11 -07:00
#[test]
pub fn test_ring_buf_is_send() {
fn is_send<T: Send>() {}
is_send::<RingBuf>();
}
2015-04-09 23:18:16 +02:00
#[test]
pub fn test_initial_buf_empty() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(16);
2015-04-09 23:18:16 +02:00
assert_eq!(MutBuf::remaining(&buf), 16);
assert_eq!(Buf::remaining(&buf), 0);
2016-08-05 21:54:29 -07:00
let bytes_written = buf.copy_from(&[1, 2, 3][..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_written, 3);
2016-08-05 21:54:29 -07:00
let bytes_written = buf.copy_from(&[][..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_written, 0);
assert_eq!(MutBuf::remaining(&buf), 13);
assert_eq!(Buf::remaining(&buf), 3);
assert_eq!(buf.bytes(), [1, 2, 3]);
let mut out = [0u8; 3];
2016-09-25 22:40:11 -07:00
let pos = buf.position();
2016-08-05 21:54:29 -07:00
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]);
2016-09-25 22:40:11 -07:00
buf.set_position(pos);
2016-08-05 21:54:29 -07:00
let bytes_read = buf.copy_to(&mut out[..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]);
2015-04-09 23:18:16 +02:00
assert_eq!(MutBuf::remaining(&buf), 16);
assert_eq!(Buf::remaining(&buf), 0);
}
#[test]
fn test_wrapping_write() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(16);
2015-04-09 23:18:16 +02:00
let mut out = [0;10];
2016-08-05 21:54:29 -07:00
buf.copy_from(&[42;12][..]);
let bytes_read = buf.copy_to(&mut out[..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_read, 10);
2016-08-05 21:54:29 -07:00
let bytes_written = buf.copy_from(&[23;8][..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_written, 8);
2016-09-25 22:40:11 -07:00
let pos = buf.position();
2016-08-05 21:54:29 -07:00
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
2016-09-25 22:40:11 -07:00
buf.set_position(pos);
2016-08-05 21:54:29 -07:00
let bytes_read = buf.copy_to(&mut out[..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
}
#[test]
fn test_io_write_and_read() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(16);
2016-06-08 14:48:49 -07:00
let mut out = [0u8;8];
2015-04-09 23:18:16 +02:00
2016-08-05 21:54:29 -07:00
let written = buf.copy_from(&[1;8][..]);
2015-04-09 23:18:16 +02:00
assert_eq!(written, 8);
2016-08-05 21:54:29 -07:00
buf.copy_to(&mut out[..]);
2015-04-09 23:18:16 +02:00
assert_eq!(out, [1;8]);
2016-08-05 21:54:29 -07:00
let written = buf.copy_from(&[2;8][..]);
2015-04-09 23:18:16 +02:00
assert_eq!(written, 8);
2016-08-05 21:54:29 -07:00
let bytes_read = buf.copy_to(&mut out[..]);
2015-04-09 23:18:16 +02:00
assert_eq!(bytes_read, 8);
assert_eq!(out, [2;8]);
}
#[test]
#[should_panic]
fn test_wrap_reset() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(8);
2016-08-05 21:54:29 -07:00
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]);
2016-09-25 22:40:11 -07:00
let pos = buf.position();
2016-08-05 21:54:29 -07:00
buf.copy_to(&mut [0; 4][..]);
buf.copy_from(&[1, 2, 3, 4][..]);
2016-09-25 22:40:11 -07:00
buf.set_position(pos);
}
2015-06-02 22:30:46 -07:00
#[test]
// Test that writes across a mark/reset are preserved.
fn test_mark_write() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(8);
2016-08-05 21:54:29 -07:00
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]);
2016-09-25 22:40:11 -07:00
let pos = buf.position();
2016-08-05 21:54:29 -07:00
buf.copy_from(&[8][..]);
2016-09-25 22:40:11 -07:00
buf.set_position(pos);
2015-06-02 22:30:46 -07:00
let mut buf2 = [0; 8];
2016-08-05 21:54:29 -07:00
buf.copy_to(&mut buf2[..]);
2015-06-02 22:30:46 -07:00
assert_eq!(buf2, [1, 2, 3, 4, 5, 6, 7, 8]);
}
#[test]
// Test that "RingBuf::reset" does not reset the length of a
// full buffer to zero.
fn test_reset_full() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(8);
2016-08-05 21:54:29 -07:00
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7, 8][..]);
2015-06-02 22:30:46 -07:00
assert_eq!(MutBuf::remaining(&buf), 0);
2016-09-25 22:40:11 -07:00
let pos = buf.position();
buf.set_position(pos);
2015-06-02 22:30:46 -07:00
assert_eq!(MutBuf::remaining(&buf), 0);
}
2015-09-04 10:27:20 +02:00
#[test]
// Test that "RingBuf::clear" does the full reset
fn test_clear() {
2016-09-20 13:42:24 -07:00
let mut buf = RingBuf::with_capacity(8);
2016-08-05 21:54:29 -07:00
buf.copy_from(&[0; 8][..]);
2015-09-04 10:27:20 +02:00
assert_eq!(MutBuf::remaining(&buf), 0);
assert_eq!(Buf::remaining(&buf), 8);
buf.clear();
assert_eq!(MutBuf::remaining(&buf), 8);
assert_eq!(Buf::remaining(&buf), 0);
}