Files
bytes/test/test_buf.rs
T

60 lines
1.4 KiB
Rust
Raw Normal View History

2016-08-31 12:07:44 -07:00
use bytes::{Buf};
use byteorder;
use std::io::{Cursor};
2016-09-02 17:28:47 +02:00
use std::vec::{Vec};
#[test]
pub 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-08-31 12:07:44 -07:00
pub fn test_read_u8() {
let mut buf = Cursor::new(b"\x21zomg");
assert_eq!(0x21, buf.read_u8());
}
2016-08-31 12:07:44 -07:00
#[test]
fn test_read_u16() {
let buf = b"\x21\x54zomg";
assert_eq!(0x2154, Cursor::new(buf).read_u16::<byteorder::BigEndian>());
assert_eq!(0x5421, Cursor::new(buf).read_u16::<byteorder::LittleEndian>());
}
2016-08-31 12:07:44 -07:00
#[test]
#[should_panic]
fn test_read_u16_buffer_underflow() {
let mut buf = Cursor::new(b"\x21");
buf.read_u16::<byteorder::BigEndian>();
}
2016-09-02 17:28:47 +02:00
#[test]
fn test_vec_sink_capacity() {
2016-09-23 12:05:32 -07:00
use bytes::buf::Sink;
2016-09-02 17:28:47 +02:00
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");
sink.copy_from(&mut source);
assert!(sink.len() <= sink.capacity(), "Length {} must be less than or equal to capacity {}", sink.len(), sink.capacity());
}