| 1 |
#![warn(rust_2018_idioms)] |
| 2 |
|
| 3 |
use bytes::Buf; |
| 4 |
#[cfg(feature = "std")] |
| 5 |
use std::io::IoSlice; |
| 6 |
|
| 7 |
#[test] |
| 8 |
fn test_fresh_cursor_vec() { |
| 9 |
let mut buf = &b"hello"[..]; |
| 10 |
|
| 11 |
assert_eq!(buf.remaining(), 5); |
| 12 |
assert_eq!(buf.chunk(), b"hello"); |
| 13 |
|
| 14 |
buf.advance(2); |
| 15 |
|
| 16 |
assert_eq!(buf.remaining(), 3); |
| 17 |
assert_eq!(buf.chunk(), b"llo"); |
| 18 |
|
| 19 |
buf.advance(3); |
| 20 |
|
| 21 |
assert_eq!(buf.remaining(), 0); |
| 22 |
assert_eq!(buf.chunk(), b""); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_get_u8() { |
| 27 |
let mut buf = &b"\x21zomg"[..]; |
| 28 |
assert_eq!(0x21, buf.get_u8()); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn test_get_u16() { |
| 33 |
let mut buf = &b"\x21\x54zomg"[..]; |
| 34 |
assert_eq!(0x2154, buf.get_u16()); |
| 35 |
let mut buf = &b"\x21\x54zomg"[..]; |
| 36 |
assert_eq!(0x5421, buf.get_u16_le()); |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
#[should_panic] |
| 41 |
fn test_get_u16_buffer_underflow() { |
| 42 |
let mut buf = &b"\x21"[..]; |
| 43 |
buf.get_u16(); |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(feature = "std")] |
| 47 |
#[test] |
| 48 |
fn test_bufs_vec() { |
| 49 |
let buf = &b"hello world"[..]; |
| 50 |
|
| 51 |
let b1: &[u8] = &mut []; |
| 52 |
let b2: &[u8] = &mut []; |
| 53 |
|
| 54 |
let mut dst = [IoSlice::new(b1), IoSlice::new(b2)]; |
| 55 |
|
| 56 |
assert_eq!(1, buf.chunks_vectored(&mut dst[..])); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn test_vec_deque() { |
| 61 |
use std::collections::VecDeque; |
| 62 |
|
| 63 |
let mut buffer: VecDeque<u8> = VecDeque::new(); |
| 64 |
buffer.extend(b"hello world"); |
| 65 |
assert_eq!(11, buffer.remaining()); |
| 66 |
assert_eq!(b"hello world", buffer.chunk()); |
| 67 |
buffer.advance(6); |
| 68 |
assert_eq!(b"world", buffer.chunk()); |
| 69 |
buffer.extend(b" piece"); |
| 70 |
let mut out = [0; 11]; |
| 71 |
buffer.copy_to_slice(&mut out); |
| 72 |
assert_eq!(b"world piece", &out[..]); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn test_deref_buf_forwards() { |
| 77 |
struct Special; |
| 78 |
|
| 79 |
impl Buf for Special { |
| 80 |
fn remaining(&self) -> usize { |
| 81 |
unreachable!("remaining"); |
| 82 |
} |
| 83 |
|
| 84 |
fn chunk(&self) -> &[u8] { |
| 85 |
unreachable!("chunk"); |
| 86 |
} |
| 87 |
|
| 88 |
fn advance(&mut self, _: usize) { |
| 89 |
unreachable!("advance"); |
| 90 |
} |
| 91 |
|
| 92 |
fn get_u8(&mut self) -> u8 { |
| 93 |
// specialized! |
| 94 |
b'x' |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// these should all use the specialized method |
| 99 |
assert_eq!(Special.get_u8(), b'x'); |
| 100 |
assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x'); |
| 101 |
assert_eq!((Box::new(Special) as Box<dyn Buf>).get_u8(), b'x'); |
| 102 |
assert_eq!(Box::new(Special).get_u8(), b'x'); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn copy_to_bytes_less() { |
| 107 |
let mut buf = &b"hello world"[..]; |
| 108 |
|
| 109 |
let bytes = buf.copy_to_bytes(5); |
| 110 |
assert_eq!(bytes, &b"hello"[..]); |
| 111 |
assert_eq!(buf, &b" world"[..]) |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
#[should_panic] |
| 116 |
fn copy_to_bytes_overflow() { |
| 117 |
let mut buf = &b"hello world"[..]; |
| 118 |
|
| 119 |
let _bytes = buf.copy_to_bytes(12); |
| 120 |
} |