Skip to content

Commit 1bdfd4d

Browse files
committed
add test for ZDICT_trainFromBuffer_cover
1 parent bcc8106 commit 1bdfd4d

File tree

5 files changed

+116
-6
lines changed

5 files changed

+116
-6
lines changed

c2rust-lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub mod lib {
3838
mod zstdmt_compress;
3939
} // mod compress
4040
pub mod decompress;
41-
mod dictBuilder {
41+
pub(crate) mod dictBuilder {
4242
pub(crate) mod cover;
4343
pub(crate) mod divsufsort;
4444
pub(crate) mod fastcover;
@@ -68,7 +68,11 @@ pub use crate::lib::decompress::{
6868

6969
pub use crate::lib::common::zstd_common::{ZSTD_getErrorName, ZSTD_isError};
7070

71-
pub use crate::lib::zdict::{ZDICT_getErrorName, ZDICT_isError, ZDICT_trainFromBuffer};
71+
pub use crate::lib::dictBuilder::cover::ZDICT_trainFromBuffer_cover;
72+
pub use crate::lib::zdict::{
73+
experimental::ZDICT_cover_params_t, ZDICT_getErrorName, ZDICT_isError, ZDICT_params_t,
74+
ZDICT_trainFromBuffer,
75+
};
7276

7377
pub use crate::lib::compress::zstd_compress::{
7478
ZSTD_compressBound, ZSTD_compress_usingDict, ZSTD_createCCtx, ZSTD_freeCCtx,

lib/dictBuilder/cover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ unsafe fn COVER_map_init(map: *mut COVER_map_t, size: u32) -> core::ffi::c_int {
130130
}
131131
static COVER_prime4bytes: u32 = 2654435761;
132132
unsafe fn COVER_map_hash(map: *mut COVER_map_t, key: u32) -> u32 {
133-
(key * COVER_prime4bytes) >> 32u32.wrapping_sub((*map).sizeLog)
133+
(key.wrapping_mul(COVER_prime4bytes)) >> 32u32.wrapping_sub((*map).sizeLog)
134134
}
135135
unsafe fn COVER_map_index(map: *mut COVER_map_t, key: u32) -> u32 {
136136
let hash = COVER_map_hash(map, key);

test-libzstd-rs-sys/src/decompress.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::ffi::CStr;
1+
use std::ffi::{c_void, CStr};
22

33
use crate::assert_eq_rs_c;
44

55
macro_rules! decompress_stream {
66
($compressed:expr, $dict:expr) => {
77
unsafe {
8-
use core::ffi::c_void;
9-
108
// Allocate and initialize a decompression context
119
let dctx = ZSTD_createDCtx();
1210
if dctx.is_null() {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use crate::assert_eq_rs_c;
2+
use std::ffi::{c_void, CStr};
3+
4+
#[test]
5+
#[cfg_attr(target_vendor = "apple", ignore = "test aborted with signal 10")]
6+
fn test_train_from_buffer_cover() {
7+
let input_data = "The quick brown fox jumps high";
8+
9+
assert_eq_rs_c!({
10+
let samples: [&str; 16] = [
11+
"The quick brown fox jumps over the lazy dog",
12+
"The quick brown fox jumps high",
13+
"The slow turtle crawls under the energetic cat",
14+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
15+
"Pack my box with five dozen liquor jugs",
16+
"Bright vixens jump; dozy fowl quack",
17+
"Sphinx of black quartz, judge my vow",
18+
"How razorback-jumping frogs can level six piqued gymnasts",
19+
"Crazy Fredrick bought many very exquisite opal jewels",
20+
"Five quacking zephyrs jolt my wax bed",
21+
"Jackdaws love my big sphinx of quartz",
22+
"Two driven jocks help fax my big quiz",
23+
"The wizard quickly jinxed the gnomes before they vaporized",
24+
"Quick zephyrs blow, vexing daft Jim",
25+
"Heavy boxes perform quick waltzes and jigs",
26+
"Jovial harpooned sharks quizzed exotic men drinking water",
27+
];
28+
29+
let mut sample_data = Vec::new();
30+
31+
let mut sample_sizes = Vec::new();
32+
33+
for &s in &samples {
34+
sample_data.extend_from_slice(s.as_bytes());
35+
36+
sample_sizes.push(s.len());
37+
}
38+
39+
let dict_capacity = 16 * 1024;
40+
41+
let mut dict_buffer = vec![0u8; dict_capacity];
42+
43+
let params = ZDICT_cover_params_t {
44+
k: 200,
45+
d: 8,
46+
steps: 4,
47+
nbThreads: 1,
48+
splitPoint: 75.0,
49+
shrinkDict: 0,
50+
shrinkDictMaxRegression: 1,
51+
zParams: ZDICT_params_t {
52+
compressionLevel: 3,
53+
notificationLevel: 0,
54+
dictID: 0,
55+
},
56+
};
57+
58+
let dict_size = ZDICT_trainFromBuffer_cover(
59+
dict_buffer.as_mut_ptr() as *mut c_void,
60+
dict_buffer.len(),
61+
sample_data.as_ptr() as *const c_void,
62+
sample_sizes.as_ptr(),
63+
sample_sizes.len() as u32,
64+
params,
65+
);
66+
67+
assert_eq!(
68+
ZDICT_isError(dict_size),
69+
0,
70+
"Dict training failed {:?}",
71+
CStr::from_ptr(ZDICT_getErrorName(dict_size)).to_str(),
72+
);
73+
74+
dict_buffer.truncate(dict_size);
75+
76+
println!("Dictionary size: {}", dict_size);
77+
78+
let cctx = ZSTD_createCCtx();
79+
80+
assert!(!cctx.is_null());
81+
82+
let max_compressed_size = ZSTD_compressBound(input_data.len());
83+
84+
let mut compressed = vec![0u8; max_compressed_size];
85+
86+
let compressed_size = ZSTD_compress_usingDict(
87+
cctx,
88+
compressed.as_mut_ptr() as *mut c_void,
89+
compressed.len(),
90+
input_data.as_bytes().as_ptr() as *const c_void,
91+
input_data.len(),
92+
dict_buffer.as_ptr() as *const c_void,
93+
dict_buffer.len(),
94+
3, // compression level
95+
);
96+
97+
assert_eq!(ZSTD_isError(compressed_size), 0, "Compression failed");
98+
99+
compressed.truncate(compressed_size);
100+
101+
println!("Compressed size: {}", compressed_size);
102+
103+
ZSTD_freeCCtx(cctx);
104+
105+
(compressed, dict_buffer)
106+
});
107+
}

test-libzstd-rs-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[cfg(test)]
22
mod decompress;
3+
mod dict_builder;
34

45
#[macro_export]
56
macro_rules! assert_eq_rs_c {

0 commit comments

Comments
 (0)