Skip to content

Commit

Permalink
fix(miniz_oxide): fix tests when with-alloc is not enabled (running w…
Browse files Browse the repository at this point in the history
…ith --no-default-features) and make add test run of it to ci
  • Loading branch information
oyvindln committed Feb 3, 2024
1 parent 657c5b2 commit 4fd32da
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions miniz_oxide/src/deflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params.local_buf.b[..n]);

Expand Down
12 changes: 8 additions & 4 deletions miniz_oxide/src/inflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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[..]);
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion miniz_oxide/src/inflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion miniz_oxide/src/inflate/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
34 changes: 18 additions & 16 deletions miniz_oxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
2 changes: 2 additions & 0 deletions miniz_oxide/tests/test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 4fd32da

Please sign in to comment.