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.
@@ -0,0 +1,303 @@
1
+ //! Integration tests for the public algorithm primitive API.
2
+ //!
3
+ //! Verifies that `sliding_window`, `alg_simhash`, `alg_minhash_256`,
4
+ //! `alg_cdc_chunks`, and `soft_hash_video_v0` are accessible from the crate
5
+ //! root and produce correct results for a variety of inputs.
6
+
7
+ // ---- sliding_window tests ----
8
+
9
+ #[test]
10
+ fn test_sliding_window_basic_ngrams() {
11
+ // "hello" with width 3 → ["hel", "ell", "llo"]
12
+ let result = iscc_lib::sliding_window("hello", 3).unwrap();
13
+ assert_eq!(
14
+ result,
15
+ vec!["hel".to_string(), "ell".to_string(), "llo".to_string()]
16
+ );
17
+ }
18
+
19
+ #[test]
20
+ fn test_sliding_window_input_shorter_than_width() {
21
+ // Input shorter than width returns a single element with the full input
22
+ let result = iscc_lib::sliding_window("ab", 5).unwrap();
23
+ assert_eq!(result, vec!["ab".to_string()]);
24
+ }
25
+
26
+ #[test]
27
+ fn test_sliding_window_unicode_cjk() {
28
+ // CJK characters are single Unicode code points, width 2
29
+ let result = iscc_lib::sliding_window("你好世界", 2).unwrap();
30
+ assert_eq!(
31
+ result,
32
+ vec!["你好".to_string(), "好世".to_string(), "世界".to_string()]
33
+ );
34
+ }
35
+
36
+ #[test]
37
+ fn test_sliding_window_width_2_minimum() {
38
+ let result = iscc_lib::sliding_window("abcde", 2).unwrap();
39
+ assert_eq!(
40
+ result,
41
+ vec![
42
+ "ab".to_string(),
43
+ "bc".to_string(),
44
+ "cd".to_string(),
45
+ "de".to_string()
46
+ ]
47
+ );
48
+ }
49
+
50
+ #[test]
51
+ fn test_sliding_window_empty_input() {
52
+ // Empty input returns a single empty string element
53
+ let result = iscc_lib::sliding_window("", 3).unwrap();
54
+ assert_eq!(result, vec!["".to_string()]);
55
+ }
56
+
57
+ #[test]
58
+ fn test_sliding_window_width_too_small_returns_error() {
59
+ // width < 2 should return Err, not panic
60
+ let result = iscc_lib::sliding_window("test", 1);
61
+ assert!(result.is_err(), "expected error for width < 2");
62
+ let msg = result.unwrap_err().to_string();
63
+ assert!(
64
+ msg.contains("width must be 2"),
65
+ "error should mention width constraint, got: {msg}"
66
+ );
67
+ }
68
+
69
+ // ---- alg_simhash tests ----
70
+
71
+ #[test]
72
+ fn test_alg_simhash_empty_input() {
73
+ // Empty input is rejected
74
+ let empty: Vec<Vec<u8>> = vec![];
75
+ let result = iscc_lib::alg_simhash(&empty);
76
+ assert!(result.is_err(), "expected error for empty input");
77
+ let msg = result.unwrap_err().to_string();
78
+ assert!(
79
+ msg.contains("must not be empty"),
80
+ "error should mention empty list, got: {msg}"
81
+ );
82
+ }
83
+
84
+ #[test]
85
+ fn test_alg_simhash_single_digest_returns_itself() {
86
+ let digest = vec![0xAB, 0xCD, 0xEF, 0x01];
87
+ let result = iscc_lib::alg_simhash(std::slice::from_ref(&digest)).unwrap();
88
+ assert_eq!(result, digest);
89
+ }
90
+
91
+ #[test]
92
+ fn test_alg_simhash_identical_digests() {
93
+ // Multiple identical digests should return the same digest
94
+ let digest = vec![0x42u8; 16];
95
+ let result = iscc_lib::alg_simhash(&[digest.clone(), digest.clone(), digest.clone()]).unwrap();
96
+ assert_eq!(result, digest);
97
+ }
98
+
99
+ #[test]
100
+ fn test_alg_simhash_different_digests_meaningful_hash() {
101
+ // Two opposing digests: simhash with threshold count*2 >= n
102
+ // For n=2, bits present in either digest survive (OR behavior)
103
+ let d1 = vec![0xF0, 0x0F, 0xF0, 0x0F];
104
+ let d2 = vec![0x0F, 0xF0, 0x0F, 0xF0];
105
+ let result = iscc_lib::alg_simhash(&[d1, d2]).unwrap();
106
+ assert_eq!(result.len(), 4);
107
+ // With 2 digests, threshold is count >= 1, so all set bits from
108
+ // both survive → all 0xFF
109
+ assert_eq!(result, vec![0xFF, 0xFF, 0xFF, 0xFF]);
110
+ }
111
+
112
+ #[test]
113
+ fn test_alg_simhash_4byte_digests() {
114
+ // alg_simhash output length matches input digest length
115
+ // 4-byte digests in → 4-byte SimHash out (used by audio code)
116
+ let digests: Vec<[u8; 4]> = vec![[1, 2, 3, 4], [5, 6, 7, 8]];
117
+ let result = iscc_lib::alg_simhash(&digests).unwrap();
118
+ assert_eq!(result.len(), 4);
119
+ }
120
+
121
+ #[test]
122
+ fn test_alg_simhash_mismatched_lengths_returns_error() {
123
+ // Mismatched digest lengths should return Err, not panic
124
+ let result = iscc_lib::alg_simhash(&[vec![1u8, 2], vec![1u8, 2, 3]]);
125
+ assert!(
126
+ result.is_err(),
127
+ "expected error for mismatched digest lengths"
128
+ );
129
+ let msg = result.unwrap_err().to_string();
130
+ assert!(
131
+ msg.contains("equal length"),
132
+ "error should mention equal length, got: {msg}"
133
+ );
134
+ }
135
+
136
+ #[test]
137
+ fn test_alg_simhash_mismatched_three_digests() {
138
+ // First and second match, third is different length
139
+ let result = iscc_lib::alg_simhash(&[vec![0u8; 4], vec![0u8; 4], vec![0u8; 8]]);
140
+ assert!(result.is_err());
141
+ }
142
+
143
+ // ---- alg_minhash_256 tests ----
144
+
145
+ #[test]
146
+ fn test_alg_minhash_256_empty_features() {
147
+ // Empty features have no minimum, so the call is rejected
148
+ let err = iscc_lib::alg_minhash_256(&[]).unwrap_err();
149
+ assert!(matches!(err, iscc_lib::IsccError::InvalidInput(_)));
150
+ assert!(err.to_string().contains("features must not be empty"));
151
+ }
152
+
153
+ #[test]
154
+ fn test_alg_minhash_256_single_feature() {
155
+ let result = iscc_lib::alg_minhash_256(&[42]).unwrap();
156
+ assert_eq!(result.len(), 32);
157
+ }
158
+
159
+ #[test]
160
+ fn test_alg_minhash_256_deterministic() {
161
+ // Same input always produces same output
162
+ let features = vec![100, 200, 300, 400, 500];
163
+ let result1 = iscc_lib::alg_minhash_256(&features).unwrap();
164
+ let result2 = iscc_lib::alg_minhash_256(&features).unwrap();
165
+ assert_eq!(result1, result2);
166
+ }
167
+
168
+ #[test]
169
+ fn test_alg_minhash_256_different_features_different_digests() {
170
+ let result_a = iscc_lib::alg_minhash_256(&[1, 2, 3]).unwrap();
171
+ let result_b = iscc_lib::alg_minhash_256(&[100, 200, 300]).unwrap();
172
+ assert_ne!(result_a, result_b);
173
+ }
174
+
175
+ // ---- alg_cdc_chunks tests ----
176
+
177
+ #[test]
178
+ fn test_alg_cdc_chunks_basic_chunking() {
179
+ // Known data of sufficient size produces multiple chunks
180
+ let data: Vec<u8> = (0..=255).cycle().take(8192).collect();
181
+ let chunks = iscc_lib::alg_cdc_chunks(&data, false, 1024).unwrap();
182
+ assert!(
183
+ chunks.len() > 1,
184
+ "expected multiple chunks, got {}",
185
+ chunks.len()
186
+ );
187
+ }
188
+
189
+ #[test]
190
+ fn test_alg_cdc_chunks_empty_input() {
191
+ // Empty input returns a single empty chunk
192
+ let chunks = iscc_lib::alg_cdc_chunks(b"", false, 1024).unwrap();
193
+ assert_eq!(chunks.len(), 1);
194
+ assert_eq!(chunks[0].len(), 0);
195
+ }
196
+
197
+ #[test]
198
+ fn test_alg_cdc_chunks_reassembly() {
199
+ // Concatenating chunks must reproduce the original data
200
+ let data: Vec<u8> = (0..=255).cycle().take(4096).collect();
201
+ let chunks = iscc_lib::alg_cdc_chunks(&data, false, 1024).unwrap();
202
+ let reassembled: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
203
+ assert_eq!(reassembled, data);
204
+ }
205
+
206
+ #[test]
207
+ fn test_alg_cdc_chunks_utf32_alignment() {
208
+ // With utf32=true, all chunk boundaries must be 4-byte aligned
209
+ let data: Vec<u8> = (0..=255).cycle().take(8192).collect();
210
+ let chunks = iscc_lib::alg_cdc_chunks(&data, true, 1024).unwrap();
211
+ let mut offset = 0;
212
+ for chunk in &chunks {
213
+ offset += chunk.len();
214
+ assert_eq!(
215
+ offset % 4,
216
+ 0,
217
+ "chunk boundary at offset {offset} is not 4-byte aligned"
218
+ );
219
+ }
220
+ }
221
+
222
+ #[test]
223
+ fn test_alg_cdc_chunks_smaller_avg_more_chunks() {
224
+ // Smaller avg_chunk_size produces more chunks for the same data
225
+ let data: Vec<u8> = (0..=255).cycle().take(16384).collect();
226
+ let chunks_large = iscc_lib::alg_cdc_chunks(&data, false, 2048).unwrap();
227
+ let chunks_small = iscc_lib::alg_cdc_chunks(&data, false, 512).unwrap();
228
+ assert!(
229
+ chunks_small.len() > chunks_large.len(),
230
+ "smaller avg_chunk_size ({}) should produce more chunks than larger ({})",
231
+ chunks_small.len(),
232
+ chunks_large.len()
233
+ );
234
+ }
235
+
236
+ // ---- soft_hash_video_v0 tests ----
237
+
238
+ #[test]
239
+ fn test_soft_hash_video_v0_basic_64bit() {
240
+ // Single all-zero 380-element frame with bits=64 → 8-byte digest
241
+ let frame = vec![0i32; 380];
242
+ let result = iscc_lib::soft_hash_video_v0(&[frame], 64).unwrap();
243
+ assert_eq!(result.len(), 8, "bits=64 should produce 8-byte digest");
244
+ }
245
+
246
+ #[test]
247
+ fn test_soft_hash_video_v0_256bit() {
248
+ // Single frame with bits=256 → 32-byte digest
249
+ let frame: Vec<i32> = (0..380).collect();
250
+ let result = iscc_lib::soft_hash_video_v0(&[frame], 256).unwrap();
251
+ assert_eq!(result.len(), 32, "bits=256 should produce 32-byte digest");
252
+ }
253
+
254
+ #[test]
255
+ fn test_soft_hash_video_v0_deduplication() {
256
+ // Duplicate frame signatures should not change the result
257
+ let frame: Vec<i32> = (0..380).map(|i| i % 3).collect();
258
+ let single = iscc_lib::soft_hash_video_v0(std::slice::from_ref(&frame), 64).unwrap();
259
+ let doubled = iscc_lib::soft_hash_video_v0(&[frame.clone(), frame], 64).unwrap();
260
+ assert_eq!(single, doubled, "duplicates should be deduplicated");
261
+ }
262
+
263
+ #[test]
264
+ fn test_soft_hash_video_v0_empty_input_error() {
265
+ // Empty frame_sigs slice returns InvalidInput error
266
+ let empty: &[Vec<i32>] = &[];
267
+ let err = iscc_lib::soft_hash_video_v0(empty, 64).unwrap_err();
268
+ assert!(
269
+ err.to_string().contains("must not be empty"),
270
+ "expected 'must not be empty' error, got: {err}"
271
+ );
272
+ }
273
+
274
+ #[test]
275
+ fn test_soft_hash_video_v0_consistency_with_gen_video_code() {
276
+ // soft_hash_video_v0 digest should match the body of the gen_video_code_v0 output
277
+ // Using conformance vector test_0000: single all-zero 380-element frame, bits=64
278
+ let frame = vec![0i32; 380];
279
+ let digest = iscc_lib::soft_hash_video_v0(std::slice::from_ref(&frame), 64).unwrap();
280
+ let code_result = iscc_lib::gen_video_code_v0(std::slice::from_ref(&frame), 64).unwrap();
281
+ // Strip "ISCC:" prefix and decode base32 to get raw bytes
282
+ let component = code_result.iscc.strip_prefix("ISCC:").unwrap();
283
+ let raw = iscc_lib::codec::decode_base32(component).unwrap();
284
+ // decode_header returns (mtype, stype, version, bits, body)
285
+ let (_mt, _st, _v, _bits, body) = iscc_lib::codec::decode_header(&raw).unwrap();
286
+ assert_eq!(
287
+ digest, body,
288
+ "soft_hash_video_v0 digest should match gen_video_code_v0 body"
289
+ );
290
+ }
291
+
292
+ // ---- Import path verification ----
293
+
294
+ #[test]
295
+ fn test_flat_crate_root_imports() {
296
+ // Verify all 5 algorithm primitives are callable via iscc_lib::<fn>
297
+ let _ = iscc_lib::sliding_window("test", 2).unwrap();
298
+ let _ = iscc_lib::alg_simhash(&[vec![0u8; 32]]).unwrap();
299
+ let _ = iscc_lib::alg_minhash_256(&[1]).unwrap();
300
+ let _ = iscc_lib::alg_cdc_chunks(b"", false, 1024).unwrap();
301
+ let frame = vec![0i32; 380];
302
+ let _ = iscc_lib::soft_hash_video_v0(&[frame], 64);
303
+ }