-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_modifier_byteorder.rs
88 lines (68 loc) · 2.29 KB
/
test_modifier_byteorder.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use jbytes::{
Bytes, BufRead,
ByteDecode
};
use jbytes_derive::{ByteDecode, ByteEncode};
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
// default: big-endian byte order.
pub struct ByteOrderExample1 {
pub a: u16,
#[jbytes(byteorder = "LE")] // set little-endian byte order.
pub b: u16,
}
#[test]
fn test_byteorder_example1() {
let bytes = Bytes::new([0x00, 0x01, 0x00, 0x02]);
let value = ByteOrderExample1 { a: 0x0001, b: 0x0200};
assert_eq!(ByteOrderExample1::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x00, 0x01, 0x00, 0x02]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
#[jbytes(byteorder = "LE")]
pub struct ByteOrderExample2 {
pub a: u16,
pub b: u16,
}
#[test]
fn test_byteorder_example2() {
let bytes = Bytes::new([0x00, 0x01, 0x00, 0x02]);
let value = ByteOrderExample2 { a: 0x0100, b: 0x0200};
assert_eq!(ByteOrderExample2::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x00, 0x01, 0x00, 0x02]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
// default: big-endian byte order.
pub struct ByteOrderExample3 {
pub a: u8,
#[jbytes(byteorder = "a")] // BE=0, LE=1
pub b: u16,
}
#[test]
fn test_byteorder_example3() {
let bytes = Bytes::new([0x01, 0x00, 0x02]);
let value = ByteOrderExample3 { a: 0x01, b: 0x0200};
assert_eq!(ByteOrderExample3::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x01, 0x00, 0x02]);
}
#[derive(Debug, PartialEq, Eq, ByteEncode, ByteDecode)]
pub enum ByteOrderEnumExample1 {
#[jbytes(branch_value=1)]
Read {
a: u16,
#[jbytes(byteorder = "LE")] // set little-endian byte order.
b: u16,
},
#[jbytes(branch_default)]
Unknown,
}
#[test]
fn test_byteorder_enum_example1() {
let bytes = Bytes::new([0x01, 0x00, 0x01, 0x00, 0x02]);
let value = ByteOrderEnumExample1::Read { a: 0x0001, b: 0x0200};
assert_eq!(ByteOrderEnumExample1::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining_len(), 0);
assert_eq!(*jbytes::encode(value).unwrap(), [0x01, 0x00, 0x01, 0x00, 0x02]);
}