mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-18 00:00:12 +02:00
Integrate with byteorder
This commit is contained in:
@@ -9,6 +9,7 @@ mod test_append;
|
||||
mod test_buf;
|
||||
mod test_buf_fill;
|
||||
mod test_byte_buf;
|
||||
mod test_mut_buf;
|
||||
mod test_ring;
|
||||
|
||||
// == Bytes
|
||||
|
||||
+19
-26
@@ -1,10 +1,9 @@
|
||||
use std::usize;
|
||||
use bytes::{Buf};
|
||||
use byteorder;
|
||||
use std::io::{Cursor};
|
||||
|
||||
#[test]
|
||||
pub fn test_fresh_cursor_vec() {
|
||||
use bytes::Buf;
|
||||
|
||||
let mut buf = Cursor::new(b"hello".to_vec());
|
||||
|
||||
assert_eq!(buf.remaining(), 5);
|
||||
@@ -27,27 +26,21 @@ pub fn test_fresh_cursor_vec() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_vec_as_mut_buf() {
|
||||
use bytes::MutBuf;
|
||||
|
||||
let mut buf = Vec::with_capacity(64);
|
||||
|
||||
assert_eq!(buf.remaining(), usize::MAX);
|
||||
|
||||
unsafe {
|
||||
assert!(buf.mut_bytes().len() >= 64);
|
||||
}
|
||||
|
||||
buf.copy_from(&b"zomg"[..]);
|
||||
|
||||
assert_eq!(&buf, b"zomg");
|
||||
|
||||
assert_eq!(buf.remaining(), usize::MAX - 4);
|
||||
assert_eq!(buf.capacity(), 64);
|
||||
|
||||
for _ in 0..16 {
|
||||
buf.copy_from(&b"zomg"[..]);
|
||||
}
|
||||
|
||||
assert_eq!(buf.len(), 68);
|
||||
pub fn test_read_u8() {
|
||||
let mut buf = Cursor::new(b"\x21zomg");
|
||||
assert_eq!(0x21, buf.read_u8());
|
||||
}
|
||||
|
||||
#[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>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_read_u16_buffer_underflow() {
|
||||
let mut buf = Cursor::new(b"\x21");
|
||||
buf.read_u16::<byteorder::BigEndian>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
use bytes::MutBuf;
|
||||
use byteorder;
|
||||
use std::usize;
|
||||
|
||||
#[test]
|
||||
pub fn test_vec_as_mut_buf() {
|
||||
let mut buf = Vec::with_capacity(64);
|
||||
|
||||
assert_eq!(buf.remaining(), usize::MAX);
|
||||
|
||||
unsafe {
|
||||
assert!(buf.mut_bytes().len() >= 64);
|
||||
}
|
||||
|
||||
buf.copy_from(&b"zomg"[..]);
|
||||
|
||||
assert_eq!(&buf, b"zomg");
|
||||
|
||||
assert_eq!(buf.remaining(), usize::MAX - 4);
|
||||
assert_eq!(buf.capacity(), 64);
|
||||
|
||||
for _ in 0..16 {
|
||||
buf.copy_from(&b"zomg"[..]);
|
||||
}
|
||||
|
||||
assert_eq!(buf.len(), 68);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_write_u8() {
|
||||
let mut buf = Vec::with_capacity(8);
|
||||
buf.write_u8(33);
|
||||
assert_eq!(b"\x21", &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_u16() {
|
||||
let mut buf = Vec::with_capacity(8);
|
||||
buf.write_u16::<byteorder::BigEndian>(8532);
|
||||
assert_eq!(b"\x21\x54", &buf[..]);
|
||||
|
||||
buf.clear();
|
||||
buf.write_u16::<byteorder::LittleEndian>(8532);
|
||||
assert_eq!(b"\x54\x21", &buf[..]);
|
||||
}
|
||||
Reference in New Issue
Block a user