-
Notifications
You must be signed in to change notification settings - Fork 267
/
test_buf.rs
67 lines (53 loc) · 1.34 KB
/
test_buf.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
extern crate bytes;
extern crate byteorder;
#[cfg(feature = "iovec")]
extern crate iovec;
#[cfg(feature = "std")]
use bytes::Buf;
#[cfg(feature = "iovec")]
use iovec::IoVec;
#[cfg(feature = "std")]
use std::io::Cursor;
#[test]
#[cfg(feature = "std")]
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"");
}
#[test]
#[cfg(feature = "std")]
fn test_get_u8() {
let mut buf = Cursor::new(b"\x21zomg");
assert_eq!(0x21, buf.get_u8());
}
#[test]
#[cfg(feature = "std")]
fn test_get_u16() {
let buf = b"\x21\x54zomg";
assert_eq!(0x2154, Cursor::new(buf).get_u16_be());
assert_eq!(0x5421, Cursor::new(buf).get_u16_le());
}
#[test]
#[should_panic]
#[cfg(feature = "std")]
fn test_get_u16_buffer_underflow() {
let mut buf = Cursor::new(b"\x21");
buf.get_u16_be();
}
#[test]
#[cfg(all(feature = "iovec", feature = "std"))]
fn test_bufs_vec() {
let buf = Cursor::new(b"hello world");
let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
let mut dst: [&IoVec; 2] =
[b1.into(), b2.into()];
assert_eq!(1, buf.bytes_vec(&mut dst[..]));
}