1 use super::*;
2 use crate::error::InputError;
3 use crate::Partial;
4
5 #[test]
6 /// Take the `bits` function and assert that remaining bytes are correctly returned, if the
7 /// previous bytes are fully consumed
test_complete_byte_consumption_bits()8 fn test_complete_byte_consumption_bits() {
9 let input = &[0x12, 0x34, 0x56, 0x78][..];
10
11 // Take 3 bit slices with sizes [4, 8, 4].
12 let result: IResult<&[u8], (u8, u8, u8)> =
13 bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize)))
14 .parse_peek(input);
15
16 let output = result.expect("We take 2 bytes and the input is longer than 2 bytes");
17
18 let remaining = output.0;
19 assert_eq!(remaining, [0x56, 0x78]);
20
21 let parsed = output.1;
22 assert_eq!(parsed.0, 0x01);
23 assert_eq!(parsed.1, 0x23);
24 assert_eq!(parsed.2, 0x04);
25 }
26
27 #[test]
28 /// Take the `bits` function and assert that remaining bytes are correctly returned, if the
29 /// previous bytes are NOT fully consumed. Partially consumed bytes are supposed to be dropped.
30 /// I.e. if we consume 1.5 bytes of 4 bytes, 2 bytes will be returned, bits 13-16 will be
31 /// dropped.
test_partial_byte_consumption_bits()32 fn test_partial_byte_consumption_bits() {
33 let input = &[0x12, 0x34, 0x56, 0x78][..];
34
35 // Take bit slices with sizes [4, 8].
36 let result: IResult<&[u8], (u8, u8)> =
37 bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize)))
38 .parse_peek(input);
39
40 let output = result.expect("We take 1.5 bytes and the input is longer than 2 bytes");
41
42 let remaining = output.0;
43 assert_eq!(remaining, [0x56, 0x78]);
44
45 let parsed = output.1;
46 assert_eq!(parsed.0, 0x01);
47 assert_eq!(parsed.1, 0x23);
48 }
49
50 #[test]
51 #[cfg(feature = "std")]
52 /// Ensure that in Incomplete error is thrown, if too few bytes are passed for a given parser.
test_incomplete_bits()53 fn test_incomplete_bits() {
54 let input = Partial::new(&[0x12][..]);
55
56 // Take bit slices with sizes [4, 8].
57 let result: IResult<_, (u8, u8)> =
58 bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input);
59
60 assert!(result.is_err());
61 let error = result.err().unwrap();
62 assert_eq!("Parsing requires 2 bytes/chars", error.to_string());
63 }
64
65 #[test]
test_take_complete_0()66 fn test_take_complete_0() {
67 let input = &[0b00010010][..];
68 let count = 0usize;
69 assert_eq!(count, 0usize);
70 let offset = 0usize;
71
72 let result: crate::IResult<(&[u8], usize), usize> = take(count).parse_peek((input, offset));
73
74 assert_eq!(result, Ok(((input, offset), 0)));
75 }
76
77 #[test]
test_take_complete_eof()78 fn test_take_complete_eof() {
79 let input = &[0b00010010][..];
80
81 let result: crate::IResult<(&[u8], usize), usize> = take(1usize).parse_peek((input, 8));
82
83 assert_eq!(
84 result,
85 Err(crate::error::ErrMode::Backtrack(InputError::new(
86 (input, 8),
87 ErrorKind::Eof
88 )))
89 );
90 }
91
92 #[test]
test_take_complete_span_over_multiple_bytes()93 fn test_take_complete_span_over_multiple_bytes() {
94 let input = &[0b00010010, 0b00110100, 0b11111111, 0b11111111][..];
95
96 let result: crate::IResult<(&[u8], usize), usize> = take(24usize).parse_peek((input, 4));
97
98 assert_eq!(
99 result,
100 Ok((([0b11111111].as_ref(), 4), 0b1000110100111111111111))
101 );
102 }
103
104 #[test]
test_take_partial_0()105 fn test_take_partial_0() {
106 let input = Partial::new(&[][..]);
107 let count = 0usize;
108 assert_eq!(count, 0usize);
109 let offset = 0usize;
110
111 let result: crate::IResult<(_, usize), usize> = take(count).parse_peek((input, offset));
112
113 assert_eq!(result, Ok(((input, offset), 0)));
114 }
115
116 #[test]
test_tag_partial_ok()117 fn test_tag_partial_ok() {
118 let input = Partial::new(&[0b00011111][..]);
119 let offset = 0usize;
120 let bits_to_take = 4usize;
121 let value_to_tag = 0b0001;
122
123 let result: crate::IResult<(_, usize), usize> =
124 tag(value_to_tag, bits_to_take).parse_peek((input, offset));
125
126 assert_eq!(result, Ok(((input, bits_to_take), value_to_tag)));
127 }
128
129 #[test]
test_tag_partial_err()130 fn test_tag_partial_err() {
131 let input = Partial::new(&[0b00011111][..]);
132 let offset = 0usize;
133 let bits_to_take = 4usize;
134 let value_to_tag = 0b1111;
135
136 let result: crate::IResult<(_, usize), usize> =
137 tag(value_to_tag, bits_to_take).parse_peek((input, offset));
138
139 assert_eq!(
140 result,
141 Err(crate::error::ErrMode::Backtrack(InputError::new(
142 (input, offset),
143 ErrorKind::Tag
144 )))
145 );
146 }
147
148 #[test]
test_bool_0_complete()149 fn test_bool_0_complete() {
150 let input = [0b10000000].as_ref();
151
152 let result: crate::IResult<(&[u8], usize), bool> = bool.parse_peek((input, 0));
153
154 assert_eq!(result, Ok(((input, 1), true)));
155 }
156
157 #[test]
test_bool_eof_complete()158 fn test_bool_eof_complete() {
159 let input = [0b10000000].as_ref();
160
161 let result: crate::IResult<(&[u8], usize), bool> = bool.parse_peek((input, 8));
162
163 assert_eq!(
164 result,
165 Err(crate::error::ErrMode::Backtrack(InputError::new(
166 (input, 8),
167 ErrorKind::Eof
168 )))
169 );
170 }
171
172 #[test]
test_bool_0_partial()173 fn test_bool_0_partial() {
174 let input = Partial::new([0b10000000].as_ref());
175
176 let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool.parse_peek((input, 0));
177
178 assert_eq!(result, Ok(((input, 1), true)));
179 }
180
181 #[test]
test_bool_eof_partial()182 fn test_bool_eof_partial() {
183 let input = Partial::new([0b10000000].as_ref());
184
185 let result: crate::IResult<(Partial<&[u8]>, usize), bool> = bool.parse_peek((input, 8));
186
187 assert_eq!(
188 result,
189 Err(crate::error::ErrMode::Incomplete(Needed::new(1)))
190 );
191 }
192