-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move libz-rs-sys tests into its own crate
- Loading branch information
1 parent
0cb9b35
commit 62b8703
Showing
26 changed files
with
299 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "test-libz-rs-sys" | ||
readme = "README.md" | ||
description.workspace = true | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
publish.workspace = true | ||
rust-version.workspace = true | ||
|
||
[dev-dependencies] | ||
quickcheck.workspace = true | ||
crc32fast = "1.3.2" | ||
|
||
zlib-rs = { workspace = true, default-features = false, features = ["std", "__internal-test"] } | ||
libz-rs-sys = { workspace = true, default-features = false, features = ["std", "rust-allocator", "testing-prefix"] } | ||
|
||
libloading.workspace = true | ||
dynamic-libz-sys.workspace = true | ||
|
||
libz-sys.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# tests for `libz-rs-sys` | ||
|
||
**why are these tests here?** | ||
|
||
Because C puts all symbols into one namespace. The `libz-rs-sys` crate defines the standard zlib api. But, we want to test against C implementations of that API. So we must, for testing, add a prefix to our names so that they don't clash with the symbols provided by the C implementation. | ||
|
||
In this crate, our symbols are always distinct through some macro/feature flag magic. That means we can freely test and benchmark without any danger of mixing up the symbols. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
use std::ffi::{c_int, c_uint}; | ||
|
||
// we use the libz_sys but configure zlib-ng in zlib compat mode | ||
use libz_sys as libz_ng_sys; | ||
|
||
use zlib_rs::{DeflateFlush, ReturnCode}; | ||
|
||
fn main() { | ||
let mut it = std::env::args(); | ||
|
||
// skips the program name | ||
let _ = it.next().unwrap(); | ||
|
||
let level: i32 = it.next().unwrap().parse().unwrap(); | ||
|
||
let mut dest_vec = vec![0u8; 1 << 28]; | ||
let mut dest_len = dest_vec.len(); | ||
|
||
match it.next().unwrap().as_str() { | ||
"ng" => { | ||
let path = it.next().unwrap(); | ||
let input = std::fs::read(path).unwrap(); | ||
|
||
let err = compress_ng(&mut dest_vec, &mut dest_len, &input, level); | ||
assert_eq!(ReturnCode::Ok, err); | ||
} | ||
"rs" => { | ||
let path = it.next().unwrap(); | ||
let input = std::fs::read(path).unwrap(); | ||
|
||
let err = compress_rs(&mut dest_vec, &mut dest_len, &input, level); | ||
assert_eq!(ReturnCode::Ok, err); | ||
} | ||
other => panic!("invalid input: {other:?}"), | ||
} | ||
} | ||
|
||
const METHOD: i32 = zlib_rs::c_api::Z_DEFLATED; | ||
const WINDOW_BITS: i32 = 15; | ||
const MEM_LEVEL: i32 = 8; | ||
const STRATEGY: i32 = zlib_rs::c_api::Z_DEFAULT_STRATEGY; | ||
|
||
fn compress_rs( | ||
dest: &mut [u8], | ||
dest_len: &mut usize, | ||
source: &[u8], | ||
// | ||
level: i32, | ||
) -> ReturnCode { | ||
use libz_rs_sys::{deflate, deflateEnd, deflateInit2_, z_stream, zlibVersion}; | ||
|
||
let mut stream = z_stream { | ||
next_in: source.as_ptr() as *mut u8, | ||
avail_in: 0, // for special logic in the first iteration | ||
total_in: 0, | ||
next_out: dest.as_mut_ptr(), | ||
avail_out: 0, // for special logic on the first iteration | ||
total_out: 0, | ||
msg: std::ptr::null_mut(), | ||
state: std::ptr::null_mut(), | ||
zalloc: Some(zlib_rs::allocate::zalloc_c), | ||
zfree: Some(zlib_rs::allocate::zfree_c), | ||
opaque: std::ptr::null_mut(), | ||
data_type: 0, | ||
adler: 0, | ||
reserved: 0, | ||
}; | ||
|
||
let err = { | ||
let strm: *mut z_stream = &mut stream; | ||
unsafe { | ||
deflateInit2_( | ||
strm, | ||
level, | ||
METHOD, | ||
WINDOW_BITS, | ||
MEM_LEVEL, | ||
STRATEGY, | ||
zlibVersion(), | ||
std::mem::size_of::<z_stream>() as c_int, | ||
) | ||
} | ||
}; | ||
|
||
if ReturnCode::from(err) != ReturnCode::Ok as _ { | ||
return ReturnCode::from(err); | ||
} | ||
|
||
let max = c_uint::MAX as usize; | ||
|
||
let mut left = dest.len(); | ||
let mut source_len = source.len(); | ||
|
||
loop { | ||
if stream.avail_out == 0 { | ||
stream.avail_out = Ord::min(left, max) as _; | ||
left -= stream.avail_out as usize; | ||
} | ||
|
||
if stream.avail_in == 0 { | ||
stream.avail_in = Ord::min(source_len, max) as _; | ||
source_len -= stream.avail_in as usize; | ||
} | ||
|
||
let flush = if source_len > 0 { | ||
DeflateFlush::NoFlush | ||
} else { | ||
DeflateFlush::Finish | ||
}; | ||
|
||
let err = unsafe { deflate(&mut stream, flush as i32) }; | ||
if ReturnCode::from(err) != ReturnCode::Ok { | ||
break; | ||
} | ||
} | ||
|
||
*dest_len = stream.total_out as _; | ||
|
||
unsafe { deflateEnd(&mut stream) }; | ||
|
||
ReturnCode::Ok | ||
} | ||
|
||
fn compress_ng( | ||
dest: &mut [u8], | ||
dest_len: &mut usize, | ||
source: &[u8], | ||
// | ||
level: i32, | ||
) -> ReturnCode { | ||
use libz_ng_sys::{deflate, deflateEnd, deflateInit2_, z_stream, zlibVersion}; | ||
|
||
let mut stream = z_stream { | ||
next_in: source.as_ptr() as *mut u8, | ||
avail_in: 0, // for special logic in the first iteration | ||
total_in: 0, | ||
next_out: dest.as_mut_ptr(), | ||
avail_out: 0, // for special logic on the first iteration | ||
total_out: 0, | ||
msg: std::ptr::null_mut(), | ||
state: std::ptr::null_mut(), | ||
zalloc: zlib_rs::allocate::zalloc_c, | ||
zfree: zlib_rs::allocate::zfree_c, | ||
opaque: std::ptr::null_mut(), | ||
data_type: 0, | ||
adler: 0, | ||
reserved: 0, | ||
}; | ||
|
||
let err = { | ||
let strm: *mut z_stream = &mut stream; | ||
unsafe { | ||
deflateInit2_( | ||
strm, | ||
level, | ||
METHOD, | ||
WINDOW_BITS, | ||
MEM_LEVEL, | ||
STRATEGY, | ||
zlibVersion(), | ||
std::mem::size_of::<z_stream>() as c_int, | ||
) | ||
} | ||
}; | ||
|
||
if ReturnCode::from(err) != ReturnCode::Ok as _ { | ||
return ReturnCode::from(err); | ||
} | ||
|
||
let max = c_uint::MAX as usize; | ||
|
||
let mut left = dest.len(); | ||
let mut source_len = source.len(); | ||
|
||
loop { | ||
if stream.avail_out == 0 { | ||
stream.avail_out = Ord::min(left, max) as _; | ||
left -= stream.avail_out as usize; | ||
} | ||
|
||
if stream.avail_in == 0 { | ||
stream.avail_in = Ord::min(source_len, max) as _; | ||
source_len -= stream.avail_in as usize; | ||
} | ||
|
||
let flush = if source_len > 0 { | ||
DeflateFlush::NoFlush | ||
} else { | ||
DeflateFlush::Finish | ||
}; | ||
|
||
let err = unsafe { deflate(&mut stream, flush as i32) }; | ||
if ReturnCode::from(err) != ReturnCode::Ok { | ||
break; | ||
} | ||
} | ||
|
||
*dest_len = stream.total_out as _; | ||
|
||
unsafe { deflateEnd(&mut stream) }; | ||
|
||
ReturnCode::Ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! a binary just so we can look at the optimized assembly | ||
|
||
// we use the libz_sys but configure zlib-ng in zlib compat mode | ||
use libz_sys as libz_ng_sys; | ||
|
||
fn main() { | ||
let mut it = std::env::args(); | ||
|
||
let _ = it.next().unwrap(); | ||
|
||
let mut dest_vec = vec![0u8; 1 << 28]; | ||
|
||
let mut dest_len = dest_vec.len() as std::ffi::c_ulong; | ||
let dest = dest_vec.as_mut_ptr(); | ||
|
||
match it.next().unwrap().as_str() { | ||
"ng" => { | ||
let path = it.next().unwrap(); | ||
let input = std::fs::read(&path).unwrap(); | ||
|
||
let source = input.as_ptr(); | ||
let source_len = input.len() as _; | ||
|
||
let err = unsafe { libz_ng_sys::uncompress(dest, &mut dest_len, source, source_len) }; | ||
assert_eq!(err, 0); | ||
} | ||
"rs" => { | ||
let path = it.next().unwrap(); | ||
let input = std::fs::read(&path).unwrap(); | ||
|
||
let source = input.as_ptr(); | ||
let source_len = input.len() as _; | ||
|
||
let err = unsafe { ::libz_rs_sys::uncompress(dest, &mut dest_len, source, source_len) }; | ||
assert_eq!(err, 0); | ||
} | ||
other => panic!("invalid option '{other}', expected one of 'rs' or 'ng'"), | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.