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
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
//! Differential tests for ISCC input cleaning across the public codec entry points.
|
|
2
|
+
//!
|
|
3
|
+
//! Verifies that the hyphen-grouped canonical display form, whitespace padding,
|
|
4
|
+
//! and a case-insensitive `iscc:` scheme are all accepted and produce output
|
|
5
|
+
//! identical to the plain (bare base32) form — matching `iscc_core`'s `iscc_clean`.
|
|
6
|
+
|
|
7
|
+
use iscc_lib::{
|
|
8
|
+
IsccError, encode_seq, gen_iscc_code_v0, gen_mixed_code_v0, iscc_decode, iscc_decompose,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/// Insert a `-` every four characters to build the canonical hyphen-grouped form.
|
|
12
|
+
fn hyphenate(s: &str) -> String {
|
|
13
|
+
s.chars()
|
|
14
|
+
.collect::<Vec<_>>()
|
|
15
|
+
.chunks(4)
|
|
16
|
+
.map(|c| c.iter().collect::<String>())
|
|
17
|
+
.collect::<Vec<_>>()
|
|
18
|
+
.join("-")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// A valid composite ISCC-CODE (plain body, no `ISCC:` prefix, no dashes),
|
|
22
|
+
/// produced from the `test_0000_standard` conformance units.
|
|
23
|
+
const COMPOSITE: &str = "KACYPXW445FTYNJ3CYSXHAFJMA2HUWULUNRFE3BLHRSCXYH2M5AEGQY";
|
|
24
|
+
|
|
25
|
+
/// The four ISCC-UNITs that compose `COMPOSITE`.
|
|
26
|
+
const UNITS: [&str; 4] = [
|
|
27
|
+
"AAAYPXW445FTYNJ3",
|
|
28
|
+
"EAARMJLTQCUWAND2",
|
|
29
|
+
"GABVVC5DMJJGYKZ4ZBYVNYABFFYXG",
|
|
30
|
+
"IADWIK7A7JTUAQ2D6QARX7OBEIK3OOUAM42LOBLCZ4ZOGDLRHMDL6TQ",
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/// Content-Code units for the `gen_mixed_code_v0` differential check.
|
|
34
|
+
const MIXED_UNITS: [&str; 4] = [
|
|
35
|
+
"EUA6GIKXN42IQV3S",
|
|
36
|
+
"EIAUKMOUIOYZCKA5",
|
|
37
|
+
"EQA6JK5IEKO6E732",
|
|
38
|
+
"EIAU2XRWOT4AKMTZ",
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
#[test]
|
|
42
|
+
fn decompose_ignores_hyphens() {
|
|
43
|
+
let plain = iscc_decompose(COMPOSITE).unwrap();
|
|
44
|
+
let hyphenated = iscc_decompose(&hyphenate(COMPOSITE)).unwrap();
|
|
45
|
+
assert_eq!(plain, hyphenated);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#[test]
|
|
49
|
+
fn decode_ignores_scheme_and_whitespace() {
|
|
50
|
+
let plain = iscc_decode(COMPOSITE).unwrap();
|
|
51
|
+
let padded = iscc_decode(&format!(" iscc:{COMPOSITE} ")).unwrap();
|
|
52
|
+
assert_eq!(plain, padded);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#[test]
|
|
56
|
+
fn decode_accepts_hyphenated_form() {
|
|
57
|
+
let plain = iscc_decode(COMPOSITE).unwrap();
|
|
58
|
+
let hyphenated = iscc_decode(&hyphenate(COMPOSITE)).unwrap();
|
|
59
|
+
assert_eq!(plain, hyphenated);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#[test]
|
|
63
|
+
fn gen_iscc_code_accepts_cleaned_forms() {
|
|
64
|
+
let plain = gen_iscc_code_v0(&UNITS, false).unwrap().iscc;
|
|
65
|
+
|
|
66
|
+
// Hyphenated + padded + lowercase-scheme variants of each unit.
|
|
67
|
+
let variants: Vec<String> = UNITS
|
|
68
|
+
.iter()
|
|
69
|
+
.map(|u| format!(" iscc:{} ", hyphenate(u)))
|
|
70
|
+
.collect();
|
|
71
|
+
let refs: Vec<&str> = variants.iter().map(String::as_str).collect();
|
|
72
|
+
let cleaned = gen_iscc_code_v0(&refs, false).unwrap().iscc;
|
|
73
|
+
|
|
74
|
+
assert_eq!(plain, cleaned);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
#[test]
|
|
78
|
+
fn gen_mixed_code_accepts_cleaned_forms() {
|
|
79
|
+
let plain = gen_mixed_code_v0(&MIXED_UNITS, 64).unwrap().iscc;
|
|
80
|
+
|
|
81
|
+
let variants: Vec<String> = MIXED_UNITS
|
|
82
|
+
.iter()
|
|
83
|
+
.map(|u| format!(" iscc:{} ", hyphenate(u)))
|
|
84
|
+
.collect();
|
|
85
|
+
let refs: Vec<&str> = variants.iter().map(String::as_str).collect();
|
|
86
|
+
let cleaned = gen_mixed_code_v0(&refs, 64).unwrap().iscc;
|
|
87
|
+
|
|
88
|
+
assert_eq!(plain, cleaned);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#[test]
|
|
92
|
+
fn uppercase_scheme_still_accepted() {
|
|
93
|
+
// The existing `ISCC:` uppercase form must keep working.
|
|
94
|
+
let plain = iscc_decode(COMPOSITE).unwrap();
|
|
95
|
+
let prefixed = iscc_decode(&format!("ISCC:{COMPOSITE}")).unwrap();
|
|
96
|
+
assert_eq!(plain, prefixed);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// Inputs that clean to an empty string. The reference `iscc_clean` errors on
|
|
100
|
+
/// these (`code[0]` IndexError in the one-part branch; downstream for `"iscc:"`),
|
|
101
|
+
/// and `decode_base32("")` returns `Ok(empty)`, so without an explicit guard
|
|
102
|
+
/// `iscc_decompose` would silently return `Ok([])` for malformed input.
|
|
103
|
+
const EMPTY_CLEANED: [&str; 4] = [" ", "-", "iscc:", "----"];
|
|
104
|
+
|
|
105
|
+
#[test]
|
|
106
|
+
fn decompose_rejects_empty_cleaned_input() {
|
|
107
|
+
for input in EMPTY_CLEANED {
|
|
108
|
+
let result = iscc_decompose(input);
|
|
109
|
+
assert!(
|
|
110
|
+
matches!(result, Err(IsccError::InvalidInput(_))),
|
|
111
|
+
"iscc_decompose({input:?}) should error, got {result:?}"
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#[test]
|
|
117
|
+
fn decode_rejects_empty_cleaned_input() {
|
|
118
|
+
// `iscc_decode` routes through `iscc_normalize`, exercising the same guard.
|
|
119
|
+
for input in EMPTY_CLEANED {
|
|
120
|
+
let result = iscc_decode(input);
|
|
121
|
+
assert!(
|
|
122
|
+
matches!(result, Err(IsccError::InvalidInput(_))),
|
|
123
|
+
"iscc_decode({input:?}) should error, got {result:?}"
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#[test]
|
|
129
|
+
fn gen_iscc_code_rejects_empty_cleaned_unit() {
|
|
130
|
+
// A whitespace/dash-only unit must not be accepted as a valid ISCC-UNIT.
|
|
131
|
+
let units = [" ", "----"];
|
|
132
|
+
let result = gen_iscc_code_v0(&units, false);
|
|
133
|
+
assert!(
|
|
134
|
+
matches!(result, Err(IsccError::InvalidInput(_))),
|
|
135
|
+
"gen_iscc_code_v0 should error on empty-cleaned units, got {result:?}"
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#[test]
|
|
140
|
+
fn gen_mixed_code_rejects_empty_cleaned_unit() {
|
|
141
|
+
let units = [" ", "----", "iscc:", "-"];
|
|
142
|
+
let result = gen_mixed_code_v0(&units, 64);
|
|
143
|
+
assert!(
|
|
144
|
+
matches!(result, Err(IsccError::InvalidInput(_))),
|
|
145
|
+
"gen_mixed_code_v0 should error on empty-cleaned units, got {result:?}"
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/// A minimal valid ISCC-UNIT used for the whitespace-parity checks.
|
|
150
|
+
const UNIT_32: &str = "ISCC:GAAAAAAAAA";
|
|
151
|
+
|
|
152
|
+
/// The ASCII information separators that Python `str.strip()` removes but Rust
|
|
153
|
+
/// `str::trim` keeps.
|
|
154
|
+
const INFO_SEPARATORS: [char; 4] = ['\u{1c}', '\u{1d}', '\u{1e}', '\u{1f}'];
|
|
155
|
+
|
|
156
|
+
/// Return whether Python `str.strip()` removes `c`: Rust whitespace or an information separator.
|
|
157
|
+
fn is_python_whitespace(c: char) -> bool {
|
|
158
|
+
c.is_whitespace() || INFO_SEPARATORS.contains(&c)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#[test]
|
|
162
|
+
fn info_separators_are_not_rust_whitespace() {
|
|
163
|
+
// Guards the premise of the parity fix: `str::trim` alone would keep these.
|
|
164
|
+
for sep in INFO_SEPARATORS {
|
|
165
|
+
assert!(!sep.is_whitespace(), "{sep:?} is already Rust whitespace");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#[test]
|
|
170
|
+
fn decode_strips_info_separators_like_python() {
|
|
171
|
+
let clean = iscc_decode(UNIT_32).unwrap();
|
|
172
|
+
for sep in INFO_SEPARATORS {
|
|
173
|
+
let around = format!("{sep}{UNIT_32}{sep}");
|
|
174
|
+
let before_colon = format!("ISCC{sep}:GAAAAAAAAA");
|
|
175
|
+
let after_colon = format!("ISCC:{sep}GAAAAAAAAA");
|
|
176
|
+
for input in [around, before_colon, after_colon] {
|
|
177
|
+
assert_eq!(
|
|
178
|
+
iscc_decode(&input).unwrap(),
|
|
179
|
+
clean,
|
|
180
|
+
"iscc_decode({input:?})"
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[test]
|
|
187
|
+
fn decompose_strips_info_separators_like_python() {
|
|
188
|
+
let clean = iscc_decompose(UNIT_32).unwrap();
|
|
189
|
+
for sep in INFO_SEPARATORS {
|
|
190
|
+
let around = format!("{sep}{UNIT_32}{sep}");
|
|
191
|
+
let before_colon = format!("ISCC{sep}:GAAAAAAAAA");
|
|
192
|
+
let after_colon = format!("ISCC:{sep}GAAAAAAAAA");
|
|
193
|
+
for input in [around, before_colon, after_colon] {
|
|
194
|
+
assert_eq!(
|
|
195
|
+
iscc_decompose(&input).unwrap(),
|
|
196
|
+
clean,
|
|
197
|
+
"iscc_decompose({input:?})"
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#[test]
|
|
204
|
+
fn decode_strips_every_python_whitespace_char() {
|
|
205
|
+
// Sweep all scalar values: every char Python strips is stripped here too.
|
|
206
|
+
let clean = iscc_decode(UNIT_32).unwrap();
|
|
207
|
+
for c in (char::MIN..=char::MAX).filter(|&c| is_python_whitespace(c)) {
|
|
208
|
+
let input = format!("{c}{UNIT_32}{c}");
|
|
209
|
+
assert_eq!(
|
|
210
|
+
iscc_decode(&input).unwrap(),
|
|
211
|
+
clean,
|
|
212
|
+
"iscc_decode({input:?})"
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
#[test]
|
|
218
|
+
fn decode_keeps_zero_width_space() {
|
|
219
|
+
// U+200B is not whitespace in Python or Rust, so it must not be stripped.
|
|
220
|
+
assert!(!is_python_whitespace('\u{200b}'));
|
|
221
|
+
let result = iscc_decode(&format!("\u{200b}{UNIT_32}"));
|
|
222
|
+
assert!(
|
|
223
|
+
matches!(result, Err(IsccError::InvalidInput(_))),
|
|
224
|
+
"U+200B must not be stripped, got {result:?}"
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
#[test]
|
|
229
|
+
fn encode_seq_strips_info_separators() {
|
|
230
|
+
let clean = encode_seq(&[UNIT_32]).unwrap();
|
|
231
|
+
for sep in INFO_SEPARATORS {
|
|
232
|
+
let unit = format!("{sep}{UNIT_32}{sep}");
|
|
233
|
+
assert_eq!(encode_seq(&[unit.as_str()]).unwrap(), clean, "{unit:?}");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
#[test]
|
|
238
|
+
fn encode_seq_rejects_separator_only_unit_as_empty() {
|
|
239
|
+
for sep in INFO_SEPARATORS {
|
|
240
|
+
let unit = sep.to_string();
|
|
241
|
+
let err = encode_seq(&[unit.as_str()]).unwrap_err();
|
|
242
|
+
assert!(
|
|
243
|
+
err.to_string().contains("must not be empty"),
|
|
244
|
+
"encode_seq([{unit:?}]) gave {err}"
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|