Files
bytes/tests/test_buf.rs
T

59 lines
1.4 KiB
Rust
Raw Normal View History

2016-11-01 08:14:31 -07:00
extern crate bytes;
extern crate byteorder;
use bytes::{Buf, Sink};
use std::io::Cursor;
#[test]
2016-11-01 08:14:31 -07:00
fn test_fresh_cursor_vec() {
let mut buf = Cursor::new(b"hello".to_vec());
assert_eq!(buf.remaining(), 5);
assert_eq!(buf.bytes(), b"hello");
buf.advance(2);
assert_eq!(buf.remaining(), 3);
assert_eq!(buf.bytes(), b"llo");
buf.advance(3);
assert_eq!(buf.remaining(), 0);
assert_eq!(buf.bytes(), b"");
buf.advance(1);
assert_eq!(buf.remaining(), 0);
assert_eq!(buf.bytes(), b"");
}
#[test]
2016-11-01 08:14:31 -07:00
fn test_get_u8() {
2016-08-31 12:07:44 -07:00
let mut buf = Cursor::new(b"\x21zomg");
2016-11-01 08:14:31 -07:00
assert_eq!(0x21, buf.get_u8());
2016-08-31 12:07:44 -07:00
}
2016-08-31 12:07:44 -07:00
#[test]
2016-11-01 08:14:31 -07:00
fn test_get_u16() {
2016-08-31 12:07:44 -07:00
let buf = b"\x21\x54zomg";
2016-11-01 08:14:31 -07:00
assert_eq!(0x2154, Cursor::new(buf).get_u16::<byteorder::BigEndian>());
assert_eq!(0x5421, Cursor::new(buf).get_u16::<byteorder::LittleEndian>());
2016-08-31 12:07:44 -07:00
}
2016-08-31 12:07:44 -07:00
#[test]
#[should_panic]
2016-11-01 08:14:31 -07:00
fn test_get_u16_buffer_underflow() {
2016-08-31 12:07:44 -07:00
let mut buf = Cursor::new(b"\x21");
2016-11-01 08:14:31 -07:00
buf.get_u16::<byteorder::BigEndian>();
}
2016-09-02 17:28:47 +02:00
#[test]
fn test_vec_sink_capacity() {
let mut sink: Vec<u8> = Vec::new();
sink.reserve(16);
assert!(sink.capacity() >= 16, "Capacity {} must be at least 16", sink.capacity());
let mut source = Cursor::new(b"0123456789abcdef0123456789abcdef");
2016-10-07 15:37:12 -07:00
sink.sink(&mut source);
2016-09-02 17:28:47 +02:00
assert!(sink.len() <= sink.capacity(), "Length {} must be less than or equal to capacity {}", sink.len(), sink.capacity());
}