Skip to content

Commit c673ce6

Browse files
committed
test(decompression-plz): state machine
1 parent 245b901 commit c673ce6

File tree

1 file changed

+76
-38
lines changed

1 file changed

+76
-38
lines changed

decompression-plz/src/state.rs

Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -217,61 +217,99 @@ mod tests {
217217

218218
use crate::tests::*;
219219

220-
fn test_all_compression(mut einfo: Vec<EncodingInfo>) {
221-
let compressed = all_compressed_data();
222-
let input = BytesMut::from(&compressed[..]);
220+
// ----- Main
221+
fn assert_mainonly_finish(
222+
main: &[u8],
223+
extra: Option<&[u8]>,
224+
encoding_info: &mut Vec<EncodingInfo>,
225+
) {
223226
let mut buf = BytesMut::new();
224-
let result = runner(&input, None, &mut einfo, &mut buf).unwrap();
225-
if let State::EndMainOnly(main) = result {
226-
assert_eq!(main, "hello world");
227-
} else {
228-
panic!()
227+
let mut state =
228+
State::start(main, extra, encoding_info, (&mut buf).writer());
229+
assert!(matches!(state, State::MainOnly(_)));
230+
231+
match state.try_next().unwrap() {
232+
State::EndMainOnly(out) => {
233+
assert_eq!(out, "hello world");
234+
}
235+
other => panic!("Expected EndMainOnly, got {other:?}"),
229236
}
230237
}
231238

232-
// ----- Main
233239
#[test]
234-
fn test_state_main_only_single_header() {
235-
let einfo = all_encoding_info_single_header();
236-
test_all_compression(einfo);
240+
fn test_state_main_only_single_compression() {
241+
let mut info =
242+
vec![EncodingInfo::new(0, vec![ContentEncoding::Brotli])];
243+
let compressed = compress_brotli(INPUT);
244+
assert_mainonly_finish(&compressed, None, &mut info);
237245
}
238246

239247
#[test]
240-
fn test_state_main_only_multi_header() {
241-
let einfo = all_encoding_info_multi_header();
242-
test_all_compression(einfo);
248+
fn test_state_main_only_multi_compression_single_header() {
249+
let mut info = all_encoding_info_single_header();
250+
let compressed = all_compressed_data();
251+
assert_mainonly_finish(&compressed, None, &mut info);
243252
}
244253

245-
// ----- Main + Extra
246254
#[test]
247-
fn test_state_main_extra_compressed_together_single_header() {
248-
let mut einfo = all_encoding_info_single_header();
255+
fn test_state_main_only_multi_compression_multi_header() {
256+
let mut info = all_encoding_info_multi_header();
249257
let compressed = all_compressed_data();
250-
let main = &compressed[..compressed.len() / 2];
251-
let extra = &compressed[compressed.len() / 2..];
258+
assert_mainonly_finish(&compressed, None, &mut info);
259+
}
260+
261+
// ----- Extra
262+
263+
// Main + Extra
264+
fn assert_main_plus_extra_flow(
265+
enc_info: &mut Vec<EncodingInfo>,
266+
compressed: &[u8],
267+
) {
268+
let mid = compressed.len() / 2;
269+
let main_slice = &compressed[..mid];
270+
let extra_slice = &compressed[mid..];
271+
252272
let mut buf = BytesMut::new();
253-
let result =
254-
runner(&main, Some(&extra), &mut einfo, &mut buf).unwrap();
255-
if let State::EndMainPlusExtra(main) = result {
256-
assert_eq!(main, "hello world");
257-
} else {
258-
panic!()
273+
let mut state = State::start(
274+
main_slice,
275+
Some(extra_slice),
276+
enc_info,
277+
(&mut buf).writer(),
278+
);
279+
280+
state = state
281+
.try_next()
282+
.expect("first transition failed");
283+
assert!(matches!(state, State::ExtraPlusMainTry(_)));
284+
285+
match state
286+
.try_next()
287+
.expect("second transition failed")
288+
{
289+
State::EndMainPlusExtra(val) => assert_eq!(val, "hello world"),
290+
other => panic!("Expected EndMainPlusExtra, got {:?}", other),
259291
}
260292
}
261293

262294
#[test]
263-
fn test_state_main_extra_compressed_together_multi_header() {
264-
let mut einfo = all_encoding_info_multi_header();
295+
fn test_state_main_plus_extra_single_compression() {
296+
let mut info =
297+
vec![EncodingInfo::new(0, vec![ContentEncoding::Brotli])];
298+
let compressed = compress_brotli(INPUT);
299+
assert_main_plus_extra_flow(&mut info, &compressed);
300+
}
301+
302+
#[test]
303+
fn test_state_main_plus_extra_compressed_together_single_header() {
304+
let mut info = all_encoding_info_single_header();
265305
let compressed = all_compressed_data();
266-
let main = &compressed[..compressed.len() / 2];
267-
let extra = &compressed[compressed.len() / 2..];
268-
let mut buf = BytesMut::new();
269-
let result =
270-
runner(&main, Some(&extra), &mut einfo, &mut buf).unwrap();
271-
if let State::EndMainPlusExtra(main) = result {
272-
assert_eq!(main, "hello world");
273-
} else {
274-
panic!()
275-
}
306+
assert_main_plus_extra_flow(&mut info, &compressed);
307+
}
308+
309+
#[test]
310+
fn test_state_main_plus_extra_compressed_together_multi_header() {
311+
let mut info = all_encoding_info_multi_header();
312+
let compressed = all_compressed_data();
313+
assert_main_plus_extra_flow(&mut info, &compressed);
276314
}
277315
}

0 commit comments

Comments
 (0)