2024-09-20 14:13:55 +00:00
|
|
|
#![expect(unused_crate_dependencies, reason = "outer test module")]
|
|
|
|
|
2024-06-24 01:30:47 +00:00
|
|
|
use cuprate_epee_encoding::{epee_object, from_bytes, to_bytes};
|
2024-01-29 22:32:16 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct T {
|
|
|
|
val: Option<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
epee_object!(
|
|
|
|
T,
|
|
|
|
val: Option<u8>,
|
|
|
|
);
|
|
|
|
|
|
|
|
#[test]
|
2024-01-30 16:09:54 +00:00
|
|
|
#[allow(clippy::useless_asref)]
|
2024-01-29 22:32:16 +00:00
|
|
|
fn optional_val_not_in_data() {
|
|
|
|
let bytes: &[u8] = b"\x01\x11\x01\x01\x01\x01\x02\x01\x01\x00";
|
2024-01-30 16:09:54 +00:00
|
|
|
let t: T = from_bytes(&mut bytes.as_ref()).unwrap();
|
2024-01-29 22:32:16 +00:00
|
|
|
let bytes2 = to_bytes(t.clone()).unwrap();
|
|
|
|
assert_eq!(bytes, bytes2);
|
|
|
|
assert!(t.val.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn optional_val_in_data() {
|
|
|
|
let bytes = [
|
|
|
|
0x01, 0x11, 0x01, 0x1, 0x01, 0x01, 0x02, 0x1, 0x1, 0x04, 0x03, b'v', b'a', b'l', 0x08, 21,
|
|
|
|
];
|
2024-01-30 16:09:54 +00:00
|
|
|
let t: T = from_bytes(&mut &bytes[..]).unwrap();
|
2024-01-29 22:32:16 +00:00
|
|
|
let bytes2 = to_bytes(t.clone()).unwrap();
|
2024-09-20 14:13:55 +00:00
|
|
|
assert_eq!(bytes.as_slice(), &*bytes2);
|
2024-01-29 22:32:16 +00:00
|
|
|
assert_eq!(t.val.unwrap(), 21);
|
|
|
|
}
|