iscc-lib 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2027 -0
- data/Cargo.toml +17 -6
- data/README.md +6 -3
- data/core/CLAUDE.md +177 -0
- data/core/Cargo.lock +959 -0
- data/core/Cargo.toml +131 -0
- data/core/README.md +84 -0
- data/core/benches/benchmarks.rs +294 -0
- data/core/benches/iai_benches.rs +171 -0
- data/core/src/cdc.rs +488 -0
- data/core/src/codec.rs +2074 -0
- data/core/src/conformance.rs +754 -0
- data/core/src/dct.rs +148 -0
- data/core/src/lib.rs +2693 -0
- data/core/src/minhash.rs +286 -0
- data/core/src/simhash.rs +356 -0
- data/core/src/streaming.rs +688 -0
- data/core/src/types.rs +116 -0
- data/core/src/utils.rs +361 -0
- data/core/src/wtahash.rs +389 -0
- data/core/src/xxh32.rs +134 -0
- data/core/tests/codec_clean.rs +247 -0
- data/core/tests/data.json +645 -0
- data/core/tests/test_algorithm_primitives.rs +303 -0
- data/core/tests/test_seq.rs +565 -0
- data/core/tests/test_strict_base32.rs +158 -0
- data/core/tests/test_text_utils.rs +157 -0
- data/lib/iscc_lib/version.rb +1 -1
- data/src/lib.rs +108 -25
- metadata +43 -4
data/core/src/minhash.rs
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
//! MinHash algorithm for similarity-preserving hashing.
|
|
2
|
+
//!
|
|
3
|
+
//! Implements 64-dimensional MinHash with universal hash functions and
|
|
4
|
+
//! bit-interleaved compression, ported from `bio-codes/iscc-sum`.
|
|
5
|
+
|
|
6
|
+
use crate::{IsccError, IsccResult};
|
|
7
|
+
|
|
8
|
+
/// Maximum 64-bit unsigned value (2^64 - 1).
|
|
9
|
+
const MAXI64: u64 = u64::MAX;
|
|
10
|
+
|
|
11
|
+
/// Mersenne prime 2^61 - 1.
|
|
12
|
+
const MPRIME: u64 = (1 << 61) - 1;
|
|
13
|
+
|
|
14
|
+
/// Maximum hash value mask (2^32 - 1).
|
|
15
|
+
const MAXH: u64 = (1 << 32) - 1;
|
|
16
|
+
|
|
17
|
+
/// MinHash permutation parameter A (64 universal hash function multipliers).
|
|
18
|
+
const MPA: [u64; 64] = [
|
|
19
|
+
853146490016488653,
|
|
20
|
+
1849332765672628665,
|
|
21
|
+
1131688930666554379,
|
|
22
|
+
1936485333668353377,
|
|
23
|
+
890837126813020267,
|
|
24
|
+
1988249303247129861,
|
|
25
|
+
1408894512544874755,
|
|
26
|
+
2140251716176616185,
|
|
27
|
+
1755124413189049421,
|
|
28
|
+
1355916793659431597,
|
|
29
|
+
546586563822844083,
|
|
30
|
+
497603761441203021,
|
|
31
|
+
2000709902557454173,
|
|
32
|
+
1057597903350092207,
|
|
33
|
+
1576204252850880253,
|
|
34
|
+
2078784234495706739,
|
|
35
|
+
1022616668454863635,
|
|
36
|
+
2150082342606334489,
|
|
37
|
+
712341150087765807,
|
|
38
|
+
1511757510246096559,
|
|
39
|
+
1525853819909660573,
|
|
40
|
+
1263771796138990131,
|
|
41
|
+
1215963627200985263,
|
|
42
|
+
590069150281426443,
|
|
43
|
+
130824646248385081,
|
|
44
|
+
962725325544728503,
|
|
45
|
+
1702561325943522847,
|
|
46
|
+
296074222435072629,
|
|
47
|
+
490211158716051523,
|
|
48
|
+
1255327197241792767,
|
|
49
|
+
699458998727907367,
|
|
50
|
+
32930168991409845,
|
|
51
|
+
1985097843455124585,
|
|
52
|
+
362027841570125531,
|
|
53
|
+
1903252144040897835,
|
|
54
|
+
900391845076405289,
|
|
55
|
+
547470123601853551,
|
|
56
|
+
1689373724032359119,
|
|
57
|
+
845594231933442371,
|
|
58
|
+
400331968021206285,
|
|
59
|
+
174967108345233429,
|
|
60
|
+
876513700861085019,
|
|
61
|
+
505848386844809885,
|
|
62
|
+
1920468508342256199,
|
|
63
|
+
1292611725303815789,
|
|
64
|
+
963317239501343903,
|
|
65
|
+
1730880032297268007,
|
|
66
|
+
284614929850059717,
|
|
67
|
+
1185026248283273081,
|
|
68
|
+
2167288823816985197,
|
|
69
|
+
1214905315086686483,
|
|
70
|
+
1555253098157439857,
|
|
71
|
+
1048013650291539723,
|
|
72
|
+
1238618594841147605,
|
|
73
|
+
1213502582686547311,
|
|
74
|
+
286300733803129311,
|
|
75
|
+
1250358511639043529,
|
|
76
|
+
407534797452854371,
|
|
77
|
+
960869149538623787,
|
|
78
|
+
1722699901467253087,
|
|
79
|
+
1325704236119824319,
|
|
80
|
+
196979859428570839,
|
|
81
|
+
1669408735473259699,
|
|
82
|
+
781336617016068757,
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
/// MinHash permutation parameter B (64 universal hash function addends).
|
|
86
|
+
const MPB: [u64; 64] = [
|
|
87
|
+
1089606993368836715,
|
|
88
|
+
726972438868274737,
|
|
89
|
+
66204585613901025,
|
|
90
|
+
1078410179646709132,
|
|
91
|
+
1343470117098523467,
|
|
92
|
+
698653121981343911,
|
|
93
|
+
1248486536592473639,
|
|
94
|
+
1447963007834012793,
|
|
95
|
+
1034598851883537815,
|
|
96
|
+
1474008409379745934,
|
|
97
|
+
793773480906057541,
|
|
98
|
+
980501101461882479,
|
|
99
|
+
963941556313537655,
|
|
100
|
+
233651787311327325,
|
|
101
|
+
243905121737149907,
|
|
102
|
+
570269452476776142,
|
|
103
|
+
297633284648631084,
|
|
104
|
+
1516796967247398557,
|
|
105
|
+
1494795672066692649,
|
|
106
|
+
1728741177365151059,
|
|
107
|
+
1029197538967983408,
|
|
108
|
+
1660732464170610344,
|
|
109
|
+
1399769594446678069,
|
|
110
|
+
506465470557005705,
|
|
111
|
+
1279720146829545181,
|
|
112
|
+
860096419955634036,
|
|
113
|
+
411519685280832908,
|
|
114
|
+
69539191273403207,
|
|
115
|
+
1960489729088056217,
|
|
116
|
+
605092075716397684,
|
|
117
|
+
1017496016211653149,
|
|
118
|
+
1304834535101321372,
|
|
119
|
+
949013511180032347,
|
|
120
|
+
1142776242221098779,
|
|
121
|
+
576980004709031232,
|
|
122
|
+
1071272177143100544,
|
|
123
|
+
1494527341093835499,
|
|
124
|
+
1073290814142727850,
|
|
125
|
+
1285904200674942617,
|
|
126
|
+
1277176606329477335,
|
|
127
|
+
343788427301735585,
|
|
128
|
+
2100915269685487331,
|
|
129
|
+
1227711252031557450,
|
|
130
|
+
18593166391963377,
|
|
131
|
+
2101884148332688233,
|
|
132
|
+
191808277534686888,
|
|
133
|
+
2170124912729392024,
|
|
134
|
+
918430470748151293,
|
|
135
|
+
1831024560113812361,
|
|
136
|
+
1951365515851067694,
|
|
137
|
+
744352348473654499,
|
|
138
|
+
1921518311887826722,
|
|
139
|
+
2020165648600700886,
|
|
140
|
+
1764930142256726985,
|
|
141
|
+
1903893374912839788,
|
|
142
|
+
1449378957774802122,
|
|
143
|
+
1435825328374066345,
|
|
144
|
+
833197549717762813,
|
|
145
|
+
2238991044337210799,
|
|
146
|
+
748955638857938366,
|
|
147
|
+
1834583747494146901,
|
|
148
|
+
222012292803592982,
|
|
149
|
+
901238460725547841,
|
|
150
|
+
1501611130776083278,
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
/// Compute a 64-dimensional MinHash from 32-bit integer features.
|
|
154
|
+
///
|
|
155
|
+
/// Uses 64 universal hash functions parameterized by MPA/MPB to compute
|
|
156
|
+
/// the minimum hash value for each dimension. Returns `MAXH` for each
|
|
157
|
+
/// dimension when features is empty.
|
|
158
|
+
fn minhash(features: &[u32]) -> Vec<u64> {
|
|
159
|
+
MPA.iter()
|
|
160
|
+
.zip(MPB.iter())
|
|
161
|
+
.map(|(&a, &b)| {
|
|
162
|
+
features
|
|
163
|
+
.iter()
|
|
164
|
+
.map(|&f| (((a.wrapping_mul(f as u64).wrapping_add(b)) & MAXI64) % MPRIME) & MAXH)
|
|
165
|
+
.min()
|
|
166
|
+
.unwrap_or(MAXH)
|
|
167
|
+
})
|
|
168
|
+
.collect()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// Compress a MinHash vector by extracting and interleaving LSB bits.
|
|
172
|
+
///
|
|
173
|
+
/// Extracts `lsb` least-significant bits from each hash value. Iterates
|
|
174
|
+
/// bit positions 0..lsb, then over all hash values, packing bits MSB-first
|
|
175
|
+
/// into output bytes.
|
|
176
|
+
fn minhash_compress(mhash: &[u64], lsb: u32) -> Vec<u8> {
|
|
177
|
+
let total_bits = mhash.len() * lsb as usize;
|
|
178
|
+
let mut bits = vec![0u8; total_bits];
|
|
179
|
+
let mut bit_index = 0;
|
|
180
|
+
for bitpos in 0..lsb {
|
|
181
|
+
for &h in mhash {
|
|
182
|
+
bits[bit_index] = ((h >> bitpos) & 1) as u8;
|
|
183
|
+
bit_index += 1;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
let total_bytes = total_bits.div_ceil(8);
|
|
187
|
+
let mut out = vec![0u8; total_bytes];
|
|
188
|
+
for (i, &bit) in bits.iter().enumerate() {
|
|
189
|
+
if bit != 0 {
|
|
190
|
+
let byte_index = i / 8;
|
|
191
|
+
let bit_in_byte = 7 - (i % 8);
|
|
192
|
+
out[byte_index] |= 1 << bit_in_byte;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
out
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/// Compute a 256-bit MinHash digest from 32-bit integer features.
|
|
199
|
+
///
|
|
200
|
+
/// Validates that the feature list is non-empty, then delegates to
|
|
201
|
+
/// `alg_minhash_256_inner`. Returns `Err(IsccError::InvalidInput)` for an
|
|
202
|
+
/// empty list, where a minimum (and so a MinHash) is undefined.
|
|
203
|
+
pub fn alg_minhash_256(features: &[u32]) -> IsccResult<Vec<u8>> {
|
|
204
|
+
if features.is_empty() {
|
|
205
|
+
return Err(IsccError::InvalidInput("features must not be empty".into()));
|
|
206
|
+
}
|
|
207
|
+
Ok(alg_minhash_256_inner(features))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// Compute a 256-bit MinHash digest from 32-bit integer features (unchecked).
|
|
211
|
+
///
|
|
212
|
+
/// Internal variant for callers that always pass at least one feature.
|
|
213
|
+
/// Calls `minhash` to get a 64-dimensional hash vector, then compresses
|
|
214
|
+
/// with `lsb=4` to produce 32 bytes (64 × 4 bits = 256 bits).
|
|
215
|
+
pub(crate) fn alg_minhash_256_inner(features: &[u32]) -> Vec<u8> {
|
|
216
|
+
let mhash = minhash(features);
|
|
217
|
+
minhash_compress(&mhash, 4)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
#[cfg(test)]
|
|
221
|
+
mod tests {
|
|
222
|
+
use super::*;
|
|
223
|
+
|
|
224
|
+
#[test]
|
|
225
|
+
fn test_minhash_empty_features() {
|
|
226
|
+
let result = minhash(&[]);
|
|
227
|
+
assert_eq!(result.len(), 64);
|
|
228
|
+
assert!(result.iter().all(|&v| v == MAXH));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#[test]
|
|
232
|
+
fn test_minhash_single_feature() {
|
|
233
|
+
let result = minhash(&[42]);
|
|
234
|
+
assert_eq!(result.len(), 64);
|
|
235
|
+
// Each dimension should have a specific hash value
|
|
236
|
+
assert!(result.iter().all(|&v| v <= MAXH));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#[test]
|
|
240
|
+
fn test_minhash_compress_basic() {
|
|
241
|
+
// With lsb=1, extracting 1 bit from each of 64 hash values → 8 bytes
|
|
242
|
+
let mhash = vec![0u64; 64];
|
|
243
|
+
let result = minhash_compress(&mhash, 1);
|
|
244
|
+
assert_eq!(result.len(), 8);
|
|
245
|
+
assert!(result.iter().all(|&b| b == 0));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
#[test]
|
|
249
|
+
fn test_minhash_compress_all_ones() {
|
|
250
|
+
// All hash values are 0xFFFFFFFF, lsb=4 → 32 bytes, all bits set
|
|
251
|
+
let mhash = vec![MAXH; 64];
|
|
252
|
+
let result = minhash_compress(&mhash, 4);
|
|
253
|
+
assert_eq!(result.len(), 32);
|
|
254
|
+
assert!(result.iter().all(|&b| b == 0xFF));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#[test]
|
|
258
|
+
fn test_alg_minhash_256_empty() {
|
|
259
|
+
let err = alg_minhash_256(&[]).unwrap_err();
|
|
260
|
+
assert!(matches!(err, IsccError::InvalidInput(_)));
|
|
261
|
+
assert_eq!(err.to_string(), "invalid input: features must not be empty");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[test]
|
|
265
|
+
fn test_alg_minhash_256_single() {
|
|
266
|
+
let result = alg_minhash_256(&[1]).unwrap();
|
|
267
|
+
assert_eq!(result.len(), 32);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
#[test]
|
|
271
|
+
fn test_alg_minhash_256_matches_inner() {
|
|
272
|
+
let features = [100, 200, 300, 400, 500];
|
|
273
|
+
assert_eq!(
|
|
274
|
+
alg_minhash_256(&features).unwrap(),
|
|
275
|
+
alg_minhash_256_inner(&features)
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
#[test]
|
|
280
|
+
fn test_alg_minhash_256_deterministic() {
|
|
281
|
+
let features = vec![100, 200, 300, 400, 500];
|
|
282
|
+
let result1 = alg_minhash_256(&features).unwrap();
|
|
283
|
+
let result2 = alg_minhash_256(&features).unwrap();
|
|
284
|
+
assert_eq!(result1, result2);
|
|
285
|
+
}
|
|
286
|
+
}
|
data/core/src/simhash.rs
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
//! SimHash algorithm and sliding window utilities.
|
|
2
|
+
//!
|
|
3
|
+
//! Implements the SimHash similarity-preserving hash and sliding window
|
|
4
|
+
//! n-gram generation, ported from `iscc-core` `simhash.py` and `utils.py`.
|
|
5
|
+
|
|
6
|
+
use crate::{IsccError, IsccResult};
|
|
7
|
+
use std::cmp;
|
|
8
|
+
|
|
9
|
+
/// Compute a SimHash from a sequence of equal-length hash digests.
|
|
10
|
+
///
|
|
11
|
+
/// Validates that the list is non-empty and that all digests have equal
|
|
12
|
+
/// length, then delegates to `alg_simhash_inner`. Returns
|
|
13
|
+
/// `Err(IsccError::InvalidInput)` for an empty list or mismatched digest
|
|
14
|
+
/// lengths.
|
|
15
|
+
pub fn alg_simhash(hash_digests: &[impl AsRef<[u8]>]) -> IsccResult<Vec<u8>> {
|
|
16
|
+
if hash_digests.is_empty() {
|
|
17
|
+
return Err(IsccError::InvalidInput(
|
|
18
|
+
"hash_digests must not be empty".into(),
|
|
19
|
+
));
|
|
20
|
+
}
|
|
21
|
+
if hash_digests.len() >= 2 {
|
|
22
|
+
let expected_len = hash_digests[0].as_ref().len();
|
|
23
|
+
for (i, digest) in hash_digests.iter().enumerate().skip(1) {
|
|
24
|
+
if digest.as_ref().len() != expected_len {
|
|
25
|
+
return Err(IsccError::InvalidInput(format!(
|
|
26
|
+
"All hash digests must have equal length (expected {}, got {} at index {})",
|
|
27
|
+
expected_len,
|
|
28
|
+
digest.as_ref().len(),
|
|
29
|
+
i
|
|
30
|
+
)));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
Ok(alg_simhash_inner(hash_digests))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Compute a SimHash from a sequence of equal-length hash digests (unchecked).
|
|
38
|
+
///
|
|
39
|
+
/// Internal variant that skips length validation. Callers must guarantee
|
|
40
|
+
/// all digests have equal length. Returns 32 zero bytes for empty input.
|
|
41
|
+
pub(crate) fn alg_simhash_inner(hash_digests: &[impl AsRef<[u8]>]) -> Vec<u8> {
|
|
42
|
+
if hash_digests.is_empty() {
|
|
43
|
+
return vec![0u8; 32];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let n_bytes = hash_digests[0].as_ref().len();
|
|
47
|
+
let n_bits = n_bytes * 8;
|
|
48
|
+
let mut vector = vec![0u32; n_bits];
|
|
49
|
+
|
|
50
|
+
for digest in hash_digests {
|
|
51
|
+
let bytes = digest.as_ref();
|
|
52
|
+
for (i, count) in vector.iter_mut().enumerate() {
|
|
53
|
+
let byte_idx = i / 8;
|
|
54
|
+
let bit_idx = 7 - (i % 8); // MSB first
|
|
55
|
+
if (bytes[byte_idx] >> bit_idx) & 1 == 1 {
|
|
56
|
+
*count += 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Threshold: count * 2 >= n matches Python's count >= n / 2.0
|
|
62
|
+
let n = hash_digests.len() as u32;
|
|
63
|
+
let mut result = vec![0u8; n_bytes];
|
|
64
|
+
for (i, &count) in vector.iter().enumerate() {
|
|
65
|
+
if count * 2 >= n {
|
|
66
|
+
let byte_idx = i / 8;
|
|
67
|
+
let bit_idx = 7 - (i % 8);
|
|
68
|
+
result[byte_idx] |= 1 << bit_idx;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
result
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Generate sliding window n-grams from a string.
|
|
76
|
+
///
|
|
77
|
+
/// Returns overlapping substrings of `width` Unicode characters, advancing
|
|
78
|
+
/// by one character at a time. If the input is shorter than `width`, returns
|
|
79
|
+
/// a single element containing the full input.
|
|
80
|
+
///
|
|
81
|
+
/// Returns `Err(IsccError::InvalidInput)` if `width < 2`.
|
|
82
|
+
pub fn sliding_window(seq: &str, width: usize) -> IsccResult<Vec<String>> {
|
|
83
|
+
if width < 2 {
|
|
84
|
+
return Err(IsccError::InvalidInput(
|
|
85
|
+
"Sliding window width must be 2 or bigger.".into(),
|
|
86
|
+
));
|
|
87
|
+
}
|
|
88
|
+
let chars: Vec<char> = seq.chars().collect();
|
|
89
|
+
let len = chars.len();
|
|
90
|
+
let range = cmp::max(len.saturating_sub(width).saturating_add(1), 1);
|
|
91
|
+
Ok((0..range)
|
|
92
|
+
.map(|i| {
|
|
93
|
+
let end = cmp::min(i + width, len);
|
|
94
|
+
chars[i..end].iter().collect()
|
|
95
|
+
})
|
|
96
|
+
.collect())
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// Generate sliding window n-grams as borrowed string slices.
|
|
100
|
+
///
|
|
101
|
+
/// Returns overlapping substrings of `width` Unicode characters as `&str`
|
|
102
|
+
/// slices borrowing from the input, avoiding per-n-gram `String` allocation.
|
|
103
|
+
/// If the input is shorter than `width`, returns a single slice of the full input.
|
|
104
|
+
#[cfg(feature = "text-processing")]
|
|
105
|
+
pub(crate) fn sliding_window_strs(seq: &str, width: usize) -> Vec<&str> {
|
|
106
|
+
debug_assert!(width >= 2, "Sliding window width must be 2 or bigger.");
|
|
107
|
+
let char_indices: Vec<usize> = seq.char_indices().map(|(i, _)| i).collect();
|
|
108
|
+
let len = char_indices.len();
|
|
109
|
+
if len == 0 {
|
|
110
|
+
return vec![seq]; // empty string → single empty slice
|
|
111
|
+
}
|
|
112
|
+
let range = cmp::max(len.saturating_sub(width).saturating_add(1), 1);
|
|
113
|
+
(0..range)
|
|
114
|
+
.map(|i| {
|
|
115
|
+
let start = char_indices[i];
|
|
116
|
+
let end = if i + width >= len {
|
|
117
|
+
seq.len()
|
|
118
|
+
} else {
|
|
119
|
+
char_indices[i + width]
|
|
120
|
+
};
|
|
121
|
+
&seq[start..end]
|
|
122
|
+
})
|
|
123
|
+
.collect()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// Generate sliding window n-grams from a byte slice.
|
|
127
|
+
///
|
|
128
|
+
/// Returns overlapping sub-slices of `width` bytes, advancing by one byte
|
|
129
|
+
/// at a time. If the input is shorter than `width`, returns a single slice
|
|
130
|
+
/// of the full input.
|
|
131
|
+
#[cfg(feature = "meta-code")]
|
|
132
|
+
pub(crate) fn sliding_window_bytes(data: &[u8], width: usize) -> Vec<&[u8]> {
|
|
133
|
+
debug_assert!(width >= 2, "Sliding window width must be 2 or bigger.");
|
|
134
|
+
let len = data.len();
|
|
135
|
+
let range = cmp::max(len.saturating_sub(width).saturating_add(1), 1);
|
|
136
|
+
(0..range)
|
|
137
|
+
.map(|i| {
|
|
138
|
+
let end = cmp::min(i + width, len);
|
|
139
|
+
&data[i..end]
|
|
140
|
+
})
|
|
141
|
+
.collect()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#[cfg(test)]
|
|
145
|
+
mod tests {
|
|
146
|
+
use super::*;
|
|
147
|
+
|
|
148
|
+
#[test]
|
|
149
|
+
fn test_sliding_window_basic() {
|
|
150
|
+
assert_eq!(
|
|
151
|
+
sliding_window("Hello", 4).unwrap(),
|
|
152
|
+
vec!["Hell".to_string(), "ello".to_string()]
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#[test]
|
|
157
|
+
fn test_sliding_window_shorter_than_width() {
|
|
158
|
+
assert_eq!(sliding_window("ab", 3).unwrap(), vec!["ab".to_string()]);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#[test]
|
|
162
|
+
fn test_sliding_window_exact_width() {
|
|
163
|
+
assert_eq!(sliding_window("abc", 3).unwrap(), vec!["abc".to_string()]);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#[test]
|
|
167
|
+
fn test_sliding_window_empty() {
|
|
168
|
+
assert_eq!(sliding_window("", 3).unwrap(), vec!["".to_string()]);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#[test]
|
|
172
|
+
fn test_sliding_window_unicode() {
|
|
173
|
+
assert_eq!(
|
|
174
|
+
sliding_window("äöü", 2).unwrap(),
|
|
175
|
+
vec!["äö".to_string(), "öü".to_string()]
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#[test]
|
|
180
|
+
fn test_alg_simhash_single_digest() {
|
|
181
|
+
let digest = blake3::hash(b"test");
|
|
182
|
+
let result = alg_simhash(&[digest.as_bytes().to_vec()]).unwrap();
|
|
183
|
+
assert_eq!(result, digest.as_bytes().to_vec());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[test]
|
|
187
|
+
fn test_alg_simhash_empty() {
|
|
188
|
+
let empty: Vec<Vec<u8>> = vec![];
|
|
189
|
+
let err = alg_simhash(&empty).unwrap_err();
|
|
190
|
+
assert!(matches!(err, IsccError::InvalidInput(ref m) if m.contains("must not be empty")));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
#[test]
|
|
194
|
+
fn test_alg_simhash_inner_empty_returns_zeros() {
|
|
195
|
+
let empty: Vec<Vec<u8>> = vec![];
|
|
196
|
+
assert_eq!(alg_simhash_inner(&empty), vec![0u8; 32]);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
#[test]
|
|
200
|
+
fn test_alg_simhash_identical_digests() {
|
|
201
|
+
let digest = vec![0xFFu8; 32];
|
|
202
|
+
let result = alg_simhash(&[digest.clone(), digest.clone(), digest]).unwrap();
|
|
203
|
+
assert_eq!(result, vec![0xFFu8; 32]);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#[test]
|
|
207
|
+
fn test_alg_simhash_opposite_digests() {
|
|
208
|
+
// Two digests: all-ones and all-zeros. Threshold is count/2 = 1.
|
|
209
|
+
// Each bit has count 1 (from all-ones) or 0 (from all-zeros).
|
|
210
|
+
// vector[i] * 2 >= 2 → vector[i] >= 1. Bits from all-ones survive.
|
|
211
|
+
let ones = vec![0xFFu8; 32];
|
|
212
|
+
let zeros = vec![0x00u8; 32];
|
|
213
|
+
let result = alg_simhash(&[ones, zeros]).unwrap();
|
|
214
|
+
assert_eq!(result, vec![0xFFu8; 32]);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
#[test]
|
|
218
|
+
fn test_alg_simhash_mismatched_lengths_returns_error() {
|
|
219
|
+
// Mismatched digest lengths should return Err, not panic
|
|
220
|
+
let result = alg_simhash(&[vec![1u8, 2], vec![1u8, 2, 3]]);
|
|
221
|
+
assert!(result.is_err());
|
|
222
|
+
let msg = result.unwrap_err().to_string();
|
|
223
|
+
assert!(
|
|
224
|
+
msg.contains("equal length"),
|
|
225
|
+
"error message should mention equal length, got: {msg}"
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#[test]
|
|
230
|
+
fn test_sliding_window_width_too_small() {
|
|
231
|
+
let result = sliding_window("test", 1);
|
|
232
|
+
assert!(result.is_err());
|
|
233
|
+
let msg = result.unwrap_err().to_string();
|
|
234
|
+
assert!(
|
|
235
|
+
msg.contains("width must be 2"),
|
|
236
|
+
"error message should mention width constraint, got: {msg}"
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ---- sliding_window_strs tests ----
|
|
241
|
+
|
|
242
|
+
#[cfg(feature = "text-processing")]
|
|
243
|
+
#[test]
|
|
244
|
+
fn test_sliding_window_strs_basic() {
|
|
245
|
+
assert_eq!(sliding_window_strs("Hello", 4), vec!["Hell", "ello"]);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
#[cfg(feature = "text-processing")]
|
|
249
|
+
#[test]
|
|
250
|
+
fn test_sliding_window_strs_shorter_than_width() {
|
|
251
|
+
assert_eq!(sliding_window_strs("ab", 3), vec!["ab"]);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#[cfg(feature = "text-processing")]
|
|
255
|
+
#[test]
|
|
256
|
+
fn test_sliding_window_strs_exact_width() {
|
|
257
|
+
assert_eq!(sliding_window_strs("abc", 3), vec!["abc"]);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
#[cfg(feature = "text-processing")]
|
|
261
|
+
#[test]
|
|
262
|
+
fn test_sliding_window_strs_empty() {
|
|
263
|
+
assert_eq!(sliding_window_strs("", 3), vec![""]);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
#[cfg(feature = "text-processing")]
|
|
267
|
+
#[test]
|
|
268
|
+
fn test_sliding_window_strs_unicode() {
|
|
269
|
+
assert_eq!(sliding_window_strs("äöü", 2), vec!["äö", "öü"]);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#[cfg(feature = "text-processing")]
|
|
273
|
+
#[cfg(debug_assertions)]
|
|
274
|
+
#[test]
|
|
275
|
+
#[should_panic(expected = "width must be 2")]
|
|
276
|
+
fn test_sliding_window_strs_width_too_small() {
|
|
277
|
+
sliding_window_strs("test", 1);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
#[cfg(feature = "text-processing")]
|
|
281
|
+
#[test]
|
|
282
|
+
fn test_sliding_window_strs_matches_sliding_window() {
|
|
283
|
+
// Verify that sliding_window_strs produces identical content to sliding_window
|
|
284
|
+
let cases = vec![
|
|
285
|
+
("Hello World", 4),
|
|
286
|
+
("ab", 3),
|
|
287
|
+
("abc", 3),
|
|
288
|
+
("", 3),
|
|
289
|
+
("äöü", 2),
|
|
290
|
+
("Hello", 13),
|
|
291
|
+
("a longer test string for n-grams", 5),
|
|
292
|
+
];
|
|
293
|
+
for (input, width) in cases {
|
|
294
|
+
let strings = sliding_window(input, width).unwrap();
|
|
295
|
+
let strs = sliding_window_strs(input, width);
|
|
296
|
+
assert_eq!(
|
|
297
|
+
strings.len(),
|
|
298
|
+
strs.len(),
|
|
299
|
+
"length mismatch for input={input:?} width={width}"
|
|
300
|
+
);
|
|
301
|
+
for (s, sr) in strings.iter().zip(strs.iter()) {
|
|
302
|
+
assert_eq!(
|
|
303
|
+
s.as_str(),
|
|
304
|
+
*sr,
|
|
305
|
+
"content mismatch for input={input:?} width={width}"
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ---- sliding_window_bytes tests ----
|
|
312
|
+
|
|
313
|
+
#[cfg(feature = "meta-code")]
|
|
314
|
+
#[test]
|
|
315
|
+
fn test_sliding_window_bytes_basic() {
|
|
316
|
+
assert_eq!(
|
|
317
|
+
sliding_window_bytes(b"Hello", 4),
|
|
318
|
+
vec![&b"Hell"[..], &b"ello"[..]]
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#[cfg(feature = "meta-code")]
|
|
323
|
+
#[test]
|
|
324
|
+
fn test_sliding_window_bytes_shorter_than_width() {
|
|
325
|
+
assert_eq!(sliding_window_bytes(b"ab", 3), vec![&b"ab"[..]]);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
#[cfg(feature = "meta-code")]
|
|
329
|
+
#[test]
|
|
330
|
+
fn test_sliding_window_bytes_exact_width() {
|
|
331
|
+
assert_eq!(sliding_window_bytes(b"abc", 3), vec![&b"abc"[..]]);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
#[cfg(feature = "meta-code")]
|
|
335
|
+
#[test]
|
|
336
|
+
fn test_sliding_window_bytes_empty() {
|
|
337
|
+
assert_eq!(sliding_window_bytes(b"", 3), vec![&b""[..]]);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
#[cfg(feature = "meta-code")]
|
|
341
|
+
#[test]
|
|
342
|
+
fn test_sliding_window_bytes_width_4() {
|
|
343
|
+
assert_eq!(
|
|
344
|
+
sliding_window_bytes(b"abcdef", 4),
|
|
345
|
+
vec![&b"abcd"[..], &b"bcde"[..], &b"cdef"[..]]
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
#[cfg(feature = "meta-code")]
|
|
350
|
+
#[cfg(debug_assertions)]
|
|
351
|
+
#[test]
|
|
352
|
+
#[should_panic(expected = "width must be 2")]
|
|
353
|
+
fn test_sliding_window_bytes_width_too_small() {
|
|
354
|
+
sliding_window_bytes(b"test", 1);
|
|
355
|
+
}
|
|
356
|
+
}
|