|
1 | 1 | use crate::assert_eq_rs_c; |
2 | 2 | use std::ffi::{c_void, CStr}; |
3 | 3 |
|
| 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 | + |
4 | 23 | #[test] |
5 | 24 | #[cfg(not(target_family = "wasm"))] |
6 | 25 | fn test_train_from_buffer_cover() { |
7 | 26 | let input_data = "The quick brown fox jumps high"; |
8 | 27 |
|
9 | 28 | 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 | 29 | let mut sample_data = Vec::new(); |
30 | 30 |
|
31 | 31 | let mut sample_sizes = Vec::new(); |
32 | 32 |
|
33 | | - for &s in &samples { |
| 33 | + for &s in &SAMPLES { |
34 | 34 | sample_data.extend_from_slice(s.as_bytes()); |
35 | 35 |
|
36 | 36 | sample_sizes.push(s.len()); |
@@ -105,3 +105,91 @@ fn test_train_from_buffer_cover() { |
105 | 105 | (compressed, dict_buffer) |
106 | 106 | }); |
107 | 107 | } |
| 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