From 4fd32da7faa624bfdd47c12e5a0a8587824f65bb Mon Sep 17 00:00:00 2001 From: oyvindln Date: Sat, 3 Feb 2024 22:00:54 +0100 Subject: [PATCH] fix(miniz_oxide): fix tests when with-alloc is not enabled (running with --no-default-features) and make add test run of it to ci --- .github/workflows/test.yml | 1 + miniz_oxide/src/deflate/core.rs | 5 +---- miniz_oxide/src/inflate/core.rs | 12 +++++++---- miniz_oxide/src/inflate/mod.rs | 2 +- miniz_oxide/src/inflate/stream.rs | 2 +- miniz_oxide/src/lib.rs | 34 ++++++++++++++++--------------- miniz_oxide/tests/test.rs | 2 ++ 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cde56868..dd721da9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,6 +36,7 @@ jobs: toolchain: ${{ matrix.rust }} - run: cargo test --manifest-path ./miniz_oxide/Cargo.toml - run: cargo test --manifest-path ./miniz_oxide/Cargo.toml --features simd + - run: cargo test --manifest-path ./miniz_oxide/Cargo.toml --no-default-features - run: cargo build --manifest-path ./miniz_oxide/Cargo.toml --no-default-features - run: cargo test diff --git a/miniz_oxide/src/deflate/core.rs b/miniz_oxide/src/deflate/core.rs index 04140048..b0a532dc 100644 --- a/miniz_oxide/src/deflate/core.rs +++ b/miniz_oxide/src/deflate/core.rs @@ -563,10 +563,7 @@ impl<'a> CallbackBuf<'a> { params: &mut ParamsOxide, ) -> i32 { if saved_output.local { - let n = cmp::min( - saved_output.pos, - self.out_buf.len() - params.out_buf_ofs, - ); + let n = cmp::min(saved_output.pos, self.out_buf.len() - params.out_buf_ofs); (self.out_buf[params.out_buf_ofs..params.out_buf_ofs + n]) .copy_from_slice(¶ms.local_buf.b[..n]); diff --git a/miniz_oxide/src/inflate/core.rs b/miniz_oxide/src/inflate/core.rs index 2ca56431..9b90596e 100644 --- a/miniz_oxide/src/inflate/core.rs +++ b/miniz_oxide/src/inflate/core.rs @@ -1826,7 +1826,7 @@ mod test { let mut b = DecompressorOxide::new(); const LEN: usize = 32; - let mut b_buf = vec![0; LEN]; + let mut b_buf = [0; LEN]; // This should fail with the out buffer being to small. let b_status = tinfl_decompress_oxide(&mut b, &encoded[..], b_buf.as_mut_slice(), flags); @@ -1844,6 +1844,7 @@ mod test { assert_eq!(b_status.0, TINFLStatus::Done); } + #[cfg(feature = "with-alloc")] #[test] fn raw_block() { const LEN: usize = 64; @@ -1868,7 +1869,7 @@ mod test { let mut b = DecompressorOxide::new(); - let mut b_buf = vec![0; LEN]; + let mut b_buf = [0; LEN]; let b_status = tinfl_decompress_oxide(&mut b, &encoded[..], b_buf.as_mut_slice(), flags); assert_eq!(b_buf[..b_status.2], text[..]); @@ -1910,6 +1911,8 @@ mod test { assert_eq!(masked_lookup(dt, 20), (5, 5)); } + // Only run this test with alloc enabled as it uses a larger buffer. + #[cfg(feature = "with-alloc")] fn check_result(input: &[u8], expected_status: TINFLStatus, expected_state: State, zlib: bool) { let mut r = DecompressorOxide::default(); let mut output_buf = vec![0; 1024 * 32]; @@ -1925,6 +1928,7 @@ mod test { assert_eq!(expected_state, r.state); } + #[cfg(feature = "with-alloc")] #[test] fn bogus_input() { use self::check_result as cr; @@ -2011,7 +2015,7 @@ mod test { | TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; let mut r = DecompressorOxide::new(); - let mut output_buf = vec![]; + let mut output_buf: [u8; 0] = []; // Check that we handle an empty buffer properly and not panicking. // https://github.com/Frommi/miniz_oxide/issues/23 let res = decompress(&mut r, &encoded, &mut output_buf, 0, flags); @@ -2025,7 +2029,7 @@ mod test { ]; let flags = TINFL_FLAG_COMPUTE_ADLER32; let mut r = DecompressorOxide::new(); - let mut output_buf = vec![]; + let mut output_buf: [u8; 0] = []; // Check that we handle an empty buffer properly and not panicking. // https://github.com/Frommi/miniz_oxide/issues/23 let res = decompress(&mut r, &encoded, &mut output_buf, 0, flags); diff --git a/miniz_oxide/src/inflate/mod.rs b/miniz_oxide/src/inflate/mod.rs index a4fa5cf8..3f787e72 100644 --- a/miniz_oxide/src/inflate/mod.rs +++ b/miniz_oxide/src/inflate/mod.rs @@ -280,7 +280,7 @@ pub fn decompress_slice_iter_to_slice<'out, 'inp>( Err(TINFLStatus::FailedCannotMakeProgress) } -#[cfg(test)] +#[cfg(all(test, feature = "with-alloc"))] mod test { use super::{ decompress_slice_iter_to_slice, decompress_to_vec_zlib, decompress_to_vec_zlib_with_limit, diff --git a/miniz_oxide/src/inflate/stream.rs b/miniz_oxide/src/inflate/stream.rs index 6caef2cf..5463ab0f 100644 --- a/miniz_oxide/src/inflate/stream.rs +++ b/miniz_oxide/src/inflate/stream.rs @@ -370,7 +370,7 @@ fn push_dict_out(state: &mut InflateState, next_out: &mut &mut [u8]) -> usize { n } -#[cfg(test)] +#[cfg(all(test, feature = "with-alloc"))] mod test { use super::{inflate, InflateState}; use crate::{DataFormat, MZFlush, MZStatus}; diff --git a/miniz_oxide/src/lib.rs b/miniz_oxide/src/lib.rs index 0e30dc03..6cd4ed37 100644 --- a/miniz_oxide/src/lib.rs +++ b/miniz_oxide/src/lib.rs @@ -3,23 +3,25 @@ //! Used a rust back-end for the //! [flate2](https://github.com/alexcrichton/flate2-rs) crate. //! -//! # Usage -//! ## Simple compression/decompression: -//! ``` rust -//! -//! use miniz_oxide::inflate::decompress_to_vec; -//! use miniz_oxide::deflate::compress_to_vec; -//! -//! fn roundtrip(data: &[u8]) { -//! let compressed = compress_to_vec(data, 6); -//! let decompressed = decompress_to_vec(compressed.as_slice()).expect("Failed to decompress!"); -//! # let _ = decompressed; -//! } -//! -//! # roundtrip(b"Test_data test data lalalal blabla"); -//! -//! ``` +#![cfg_attr( + feature = "with-alloc", + doc = r##" +# Usage +## Simple compression/decompression: +``` rust + +use miniz_oxide::inflate::decompress_to_vec; +use miniz_oxide::deflate::compress_to_vec; + +fn roundtrip(data: &[u8]) { + let compressed = compress_to_vec(data, 6); + let decompressed = decompress_to_vec(compressed.as_slice()).expect("Failed to decompress!"); +# let _ = decompressed; +} +# roundtrip(b"Test_data test data lalalal blabla"); +"## +)] #![forbid(unsafe_code)] #![cfg_attr(not(feature = "std"), no_std)] diff --git a/miniz_oxide/tests/test.rs b/miniz_oxide/tests/test.rs index 16d2c9d9..8f447a86 100644 --- a/miniz_oxide/tests/test.rs +++ b/miniz_oxide/tests/test.rs @@ -1,3 +1,5 @@ +// Disable these tests for now unless alloc is enabled since we're only testing with the *_vec functions in here. +#![cfg(feature = "with-alloc")] extern crate miniz_oxide; use std::io::Read;