Debian Codegraph @

third_party/rust/rmp/tests/func/decode/null.rs

raw
1 use super::Cursor;
2
3 use rmp::decode::*;
4
5 #[test]
6 fn pass() {
7 let buf = [0xc0];
8 let mut cur = Cursor::new(&buf[..]);
9
10 assert_eq!((), read_nil(&mut cur).unwrap());
11 assert_eq!(1, cur.position());
12 }
13
14 #[test]
15 fn fail_invalid_marker() {
16 let buf = [0xc1];
17 let mut cur = Cursor::new(&buf[..]);
18
19 match read_nil(&mut cur) {
20 Err(ValueReadError::TypeMismatch(..)) => (),
21 other => panic!("unexpected result: {other:?}"),
22 }
23 assert_eq!(1, cur.position());
24 }
25
26 #[test]
27 fn fail_unexpected_eof() {
28 let buf = [];
29 let mut cur = Cursor::new(&buf[..]);
30
31 read_nil(&mut cur).err().unwrap();
32 assert_eq!(0, cur.position());
33 }
34
35 #[test]
36 #[cfg(feature = "std")]
37 fn interrupt_safe() {
38 use std::io::{Error, ErrorKind, Read};
39
40 struct MockRead { state_: u8 }
41
42 impl MockRead {
43 fn state(&self) -> u8 { self.state_ }
44 }
45
46 impl Read for MockRead {
47 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
48 if self.state_ == 0 {
49 self.state_ = 1;
50 Err(Error::new(ErrorKind::Interrupted, "interrupted"))
51 } else {
52 assert!(!buf.is_empty());
53
54 buf[0] = 0xc0;
55 Ok(1)
56 }
57 }
58 }
59
60 let mut cur = MockRead { state_: 0 };
61
62 // The function is interruption-safe, the first read should succeed.
63 read_nil(&mut cur).unwrap();
64
65 assert_eq!(1, cur.state());
66 }