Skip to content

Commit 8014309

Browse files
committed
test ZDICT_trainFromBuffer_fastCover
1 parent c0d7e3a commit 8014309

File tree

2 files changed

+111
-22
lines changed

2 files changed

+111
-22
lines changed

c2rust-lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ pub use crate::lib::common::zstd_common::{
140140
};
141141

142142
pub use crate::lib::dictBuilder::cover::ZDICT_trainFromBuffer_cover;
143+
pub use crate::lib::dictBuilder::fastcover::ZDICT_trainFromBuffer_fastCover;
143144
pub use crate::lib::zdict::{
144-
experimental::ZDICT_cover_params_t, ZDICT_getDictID, ZDICT_getErrorName, ZDICT_isError,
145-
ZDICT_params_t, ZDICT_trainFromBuffer,
145+
experimental::{ZDICT_cover_params_t, ZDICT_fastCover_params_t},
146+
ZDICT_getDictID, ZDICT_getErrorName, ZDICT_isError, ZDICT_params_t, ZDICT_trainFromBuffer,
146147
};
147148

148149
pub use crate::lib::compress::zstd_compress::{

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

Lines changed: 108 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
use crate::assert_eq_rs_c;
22
use std::ffi::{c_void, CStr};
33

4+
const SAMPLES: [&str; 16] = [
5+
"The quick brown fox jumps over the lazy dog",
6+
"The quick brown fox jumps high",
7+
"The slow turtle crawls under the energetic cat",
8+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
9+
"Pack my box with five dozen liquor jugs",
10+
"Bright vixens jump; dozy fowl quack",
11+
"Sphinx of black quartz, judge my vow",
12+
"How razorback-jumping frogs can level six piqued gymnasts",
13+
"Crazy Fredrick bought many very exquisite opal jewels",
14+
"Five quacking zephyrs jolt my wax bed",
15+
"Jackdaws love my big sphinx of quartz",
16+
"Two driven jocks help fax my big quiz",
17+
"The wizard quickly jinxed the gnomes before they vaporized",
18+
"Quick zephyrs blow, vexing daft Jim",
19+
"Heavy boxes perform quick waltzes and jigs",
20+
"Jovial harpooned sharks quizzed exotic men drinking water",
21+
];
22+
423
#[test]
524
#[cfg(not(target_family = "wasm"))]
625
fn test_train_from_buffer_cover() {
726
let input_data = "The quick brown fox jumps high";
827

928
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-
2929
let mut sample_data = Vec::new();
3030

3131
let mut sample_sizes = Vec::new();
3232

33-
for &s in &samples {
33+
for &s in &SAMPLES {
3434
sample_data.extend_from_slice(s.as_bytes());
3535

3636
sample_sizes.push(s.len());
@@ -105,3 +105,91 @@ fn test_train_from_buffer_cover() {
105105
(compressed, dict_buffer)
106106
});
107107
}
108+
109+
#[test]
110+
#[cfg(not(target_family = "wasm"))]
111+
fn test_train_from_buffer_fastcover() {
112+
let input_data = "The quick brown fox jumps high";
113+
114+
assert_eq_rs_c!({
115+
let mut sample_data = Vec::new();
116+
117+
let mut sample_sizes = Vec::new();
118+
119+
for &s in &SAMPLES {
120+
sample_data.extend_from_slice(s.as_bytes());
121+
122+
sample_sizes.push(s.len());
123+
}
124+
125+
let dict_capacity = 16 * 1024;
126+
127+
let mut dict_buffer = vec![0u8; dict_capacity];
128+
129+
let params = ZDICT_fastCover_params_t {
130+
k: 200,
131+
d: 8,
132+
f: 20,
133+
steps: 4,
134+
nbThreads: 1,
135+
splitPoint: 75.0,
136+
shrinkDict: 0,
137+
shrinkDictMaxRegression: 1,
138+
zParams: ZDICT_params_t {
139+
compressionLevel: 3,
140+
notificationLevel: 0,
141+
dictID: 0,
142+
},
143+
accel: 1,
144+
};
145+
146+
let dict_size = ZDICT_trainFromBuffer_fastCover(
147+
dict_buffer.as_mut_ptr() as *mut c_void,
148+
dict_buffer.len(),
149+
sample_data.as_ptr() as *const c_void,
150+
sample_sizes.as_ptr(),
151+
sample_sizes.len() as u32,
152+
params,
153+
);
154+
155+
assert_eq!(
156+
ZDICT_isError(dict_size),
157+
0,
158+
"Dict training failed {:?}",
159+
CStr::from_ptr(ZDICT_getErrorName(dict_size)).to_str(),
160+
);
161+
162+
dict_buffer.truncate(dict_size);
163+
164+
println!("Dictionary size: {}", dict_size);
165+
166+
let cctx = ZSTD_createCCtx();
167+
168+
assert!(!cctx.is_null());
169+
170+
let max_compressed_size = ZSTD_compressBound(input_data.len());
171+
172+
let mut compressed = vec![0u8; max_compressed_size];
173+
174+
let compressed_size = ZSTD_compress_usingDict(
175+
cctx,
176+
compressed.as_mut_ptr() as *mut c_void,
177+
compressed.len(),
178+
input_data.as_bytes().as_ptr() as *const c_void,
179+
input_data.len(),
180+
dict_buffer.as_ptr() as *const c_void,
181+
dict_buffer.len(),
182+
3, // compression level
183+
);
184+
185+
assert_eq!(ZSTD_isError(compressed_size), 0, "Compression failed");
186+
187+
compressed.truncate(compressed_size);
188+
189+
println!("Compressed size: {}", compressed_size);
190+
191+
ZSTD_freeCCtx(cctx);
192+
193+
(compressed, dict_buffer)
194+
});
195+
}

0 commit comments

Comments
 (0)