@@ -62,10 +62,103 @@ impl<R: Read> Decoder<R> {
62
62
}
63
63
}
64
64
65
- // TODO: Parse tests.
66
65
/// Decode a single packet from a slice of bytes.
67
66
pub fn from_slice ( s : & [ u8 ] ) -> Result < Packet > {
68
67
let mut d = Decoder :: new ( Cursor :: new ( Vec :: from ( s) ) ) ;
69
68
d. read_packet ( )
70
69
}
71
70
71
+ #[ cfg( test) ]
72
+ mod tests {
73
+ use super :: from_slice;
74
+ use error:: { Error , ErrorKind , Result } ;
75
+ use packet:: { Kind , Packet } ;
76
+ use std:: io;
77
+
78
+ #[ test]
79
+ fn header ( ) {
80
+ let p = decode_one ( & [ 0x01 , 0x11 ] ) ;
81
+ assert_eq ! ( p. header, 0x01 ) ;
82
+ }
83
+
84
+ #[ test]
85
+ fn instrumentation_payload_1_byte ( ) {
86
+ let p = decode_one ( & [ 0x01 , 0x11 ] ) ;
87
+ match p. kind {
88
+ Kind :: Instrumentation ( ref i) => {
89
+ assert_eq ! ( * i. payload, [ 0x11 ] ) ;
90
+ } ,
91
+ _ => panic ! ( )
92
+ }
93
+ }
94
+
95
+ #[ test]
96
+ fn instrumentation_payload_2_bytes ( ) {
97
+ let p = decode_one ( & [ 0x02 , 0x11 , 0x12 ] ) ;
98
+ match p. kind {
99
+ Kind :: Instrumentation ( ref i) => {
100
+ assert_eq ! ( * i. payload, [ 0x11 , 0x12 ] ) ;
101
+ } ,
102
+ _ => panic ! ( )
103
+ }
104
+ }
105
+
106
+ #[ test]
107
+ fn instrumentation_payload_4_bytes ( ) {
108
+ let p = decode_one ( & [ 0x03 , 0x11 , 0x12 , 0x13 , 0x14 ] ) ;
109
+ match p. kind {
110
+ Kind :: Instrumentation ( ref i) => {
111
+ assert_eq ! ( * i. payload, [ 0x11 , 0x12 , 0x13 , 0x14 ] ) ;
112
+ } ,
113
+ _ => panic ! ( )
114
+ }
115
+ }
116
+
117
+ #[ test]
118
+ fn instrumentation_stim_port ( ) {
119
+ let p = decode_one ( & [ 0b00000_001 , 0x11 ] ) ;
120
+ match p. kind {
121
+ Kind :: Instrumentation ( ref i) => {
122
+ assert_eq ! ( i. port, 0 ) ;
123
+ } ,
124
+ _ => panic ! ( )
125
+ }
126
+
127
+ let p = decode_one ( & [ 0b11111_001 , 0x11 ] ) ;
128
+ match p. kind {
129
+ Kind :: Instrumentation ( ref i) => {
130
+ assert_eq ! ( i. port, 31 ) ;
131
+ } ,
132
+ _ => panic ! ( )
133
+ }
134
+ }
135
+
136
+ #[ test]
137
+ fn unknown_header ( ) {
138
+ let p = try_decode_one ( & [ 0x00 ] ) ;
139
+ match p {
140
+ Err ( Error ( ErrorKind :: UnknownHeader ( 0x00 ) , _) ) => ( ) ,
141
+ _ => panic ! ( ) ,
142
+ }
143
+ }
144
+
145
+ #[ test]
146
+ fn eof_before_payload ( ) {
147
+ let p = try_decode_one ( & [ 0x01 /* Missing payload bytes */ ] ) ;
148
+ match p {
149
+ Err ( Error ( ErrorKind :: Io ( ref e) , _) )
150
+ if e. kind ( ) == io:: ErrorKind :: UnexpectedEof => ( ) ,
151
+ _ => panic ! ( ) ,
152
+ }
153
+ }
154
+
155
+ fn decode_one ( data : & [ u8 ] ) -> Packet {
156
+ try_decode_one ( data) . unwrap ( )
157
+ }
158
+
159
+ fn try_decode_one ( data : & [ u8 ] ) -> Result < Packet > {
160
+ let p = from_slice ( data) ;
161
+ println ! ( "{:#?}" , p) ;
162
+ p
163
+ }
164
+ }
0 commit comments