forked from kellerkindt/asn1rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_enumerated.rs
100 lines (84 loc) · 2.74 KB
/
basic_enumerated.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
89
90
91
92
93
94
95
96
97
98
99
100
use asn1rs::syn::io::UperReader as NewUperReader;
use asn1rs::syn::io::UperWriter as NewUperWriter;
mod test_utils;
use test_utils::*;
asn_to_rust!(
r"BasicEnumerated DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
Basic ::= ENUMERATED {
abc,
def,
ghi
}
PredefinedNumbers ::= ENUMERATED {
abc(0),
def(5),
..., -- whatever reserved blubber comment
ghi(8),
jkl(9)
}
SomeEnum ::= ENUMERATED {
abc(0),
def(1),
ghi(2),
jkl(3),
mno(4),
qrs(15)
}
END"
);
fn serialize_uper(to_uper: impl Writable) -> (usize, Vec<u8>) {
let mut writer = NewUperWriter::default();
writer.write(&to_uper).unwrap();
let bits = writer.bit_len();
(bits, writer.into_bytes_vec())
}
fn deserialize_uper<T: Readable>(bytes: &[u8], bit_len: usize) -> T {
let mut reader = NewUperReader::from((bytes, bit_len));
reader.read::<T>().unwrap()
}
#[test]
fn test_predefined_numbers() {
assert_eq!((2, vec![0x00_u8]), serialize_uper(PredefinedNumbers::Abc));
assert_eq!((2, vec![0x40_u8]), serialize_uper(PredefinedNumbers::Def));
assert_eq!((8, vec![0x80_u8]), serialize_uper(PredefinedNumbers::Ghi));
assert_eq!((8, vec![0x81_u8]), serialize_uper(PredefinedNumbers::Jkl));
assert_eq!(PredefinedNumbers::Abc, deserialize_uper(&[0x00_u8], 2,));
assert_eq!(PredefinedNumbers::Def, deserialize_uper(&[0x40_u8], 2,));
assert_eq!(PredefinedNumbers::Ghi, deserialize_uper(&[0x80_u8], 8,));
assert_eq!(PredefinedNumbers::Jkl, deserialize_uper(&[0x81_u8], 8,));
}
#[test]
fn test_basic_variants_parsed() {
let _abc = Basic::Abc;
let _def = Basic::Def;
let _ghi = Basic::Ghi;
match Basic::Abc {
// this does not compile if there are additional unexpected variants
Basic::Abc | Basic::Def | Basic::Ghi => {}
}
}
#[test]
pub fn test_basic_uper() {
let mut writer = NewUperWriter::default();
writer.write(&Basic::Abc).unwrap();
writer.write(&Basic::Def).unwrap();
writer.write(&Basic::Ghi).unwrap();
assert_eq!(
&[
0b00 << 6 // Abc
| 0b01 << 4 // Def
| 0b10 << 2 // Ghi
],
writer.byte_content()
);
}
#[test]
fn test_some_enum_with_skipped_numbers() {
test_utils::serialize_and_deserialize_uper(3, &[0x00], &SomeEnum::Abc);
test_utils::serialize_and_deserialize_uper(3, &[0x20], &SomeEnum::Def);
test_utils::serialize_and_deserialize_uper(3, &[0x40], &SomeEnum::Ghi);
test_utils::serialize_and_deserialize_uper(3, &[0x60], &SomeEnum::Jkl);
test_utils::serialize_and_deserialize_uper(3, &[0x80], &SomeEnum::Mno);
test_utils::serialize_and_deserialize_uper(3, &[0xA0], &SomeEnum::Qrs);
}