aprender-core 0.29.2

Next-generation machine learning library in pure Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use super::*;

// ========================================================================
// Section 6: Conversion Report Tests (P061-P070)
// ========================================================================

#[test]
fn p061_conversion_lossless() {
    let report = ConversionReport {
        path: ConversionPath::direct(FormatType::Gguf, FormatType::Apr),
        source_inspection: InspectionReport {
            format: FormatType::Gguf,
            file_size: 1000,
            metadata: BTreeMap::new(),
            tensors: vec![],
            total_params: 100,
            quantization: None,
            architecture: None,
        },
        target_inspection: InspectionReport {
            format: FormatType::Apr,
            file_size: 1000,
            metadata: BTreeMap::new(),
            tensors: vec![],
            total_params: 100,
            quantization: None,
            architecture: None,
        },
        warnings: vec![],
        duration_ms: 100,
        modified_tensors: vec![],
        dropped_tensors: vec![],
    };
    assert!(report.is_lossless());
    assert!(report.tensor_counts_match());
}

#[test]
fn p062_conversion_with_dropped_tensors() {
    let report = ConversionReport {
        path: ConversionPath::direct(FormatType::Gguf, FormatType::Apr),
        source_inspection: InspectionReport {
            format: FormatType::Gguf,
            file_size: 1000,
            metadata: BTreeMap::new(),
            tensors: vec![
                TensorInfo {
                    name: "layer.0".to_string(),
                    dtype: "F32".to_string(),
                    shape: vec![100, 100],
                    size_bytes: 40000,
                    stats: None,
                },
                TensorInfo {
                    name: "layer.1".to_string(),
                    dtype: "F32".to_string(),
                    shape: vec![100, 100],
                    size_bytes: 40000,
                    stats: None,
                },
            ],
            total_params: 20000,
            quantization: None,
            architecture: None,
        },
        target_inspection: InspectionReport {
            format: FormatType::Apr,
            file_size: 800,
            metadata: BTreeMap::new(),
            tensors: vec![TensorInfo {
                name: "layer.0".to_string(),
                dtype: "F32".to_string(),
                shape: vec![100, 100],
                size_bytes: 40000,
                stats: None,
            }],
            total_params: 10000,
            quantization: None,
            architecture: None,
        },
        warnings: vec!["Tensor dropped".to_string()],
        duration_ms: 100,
        modified_tensors: vec![],
        dropped_tensors: vec!["layer.1".to_string()],
    };
    assert!(!report.is_lossless());
    assert!(!report.tensor_counts_match());
}

#[test]
fn p063_conversion_warnings() {
    let report = ConversionReport {
        path: ConversionPath::direct(FormatType::Gguf, FormatType::Apr),
        source_inspection: InspectionReport {
            format: FormatType::Gguf,
            file_size: 1000,
            metadata: BTreeMap::new(),
            tensors: vec![],
            total_params: 0,
            quantization: None,
            architecture: None,
        },
        target_inspection: InspectionReport {
            format: FormatType::Apr,
            file_size: 1000,
            metadata: BTreeMap::new(),
            tensors: vec![],
            total_params: 0,
            quantization: None,
            architecture: None,
        },
        warnings: vec!["Warning 1".to_string(), "Warning 2".to_string()],
        duration_ms: 50,
        modified_tensors: vec![],
        dropped_tensors: vec![],
    };
    assert_eq!(report.warnings.len(), 2);
    assert!(report.is_lossless());
}

// ========================================================================
// Section 7: TensorInfo Tests (P071-P080)
// ========================================================================

#[test]
fn p071_tensor_info_creation() {
    let info = TensorInfo {
        name: "model.embed".to_string(),
        dtype: "F32".to_string(),
        shape: vec![32000, 4096],
        size_bytes: 32000 * 4096 * 4,
        stats: None,
    };
    assert_eq!(info.name, "model.embed");
    assert_eq!(info.dtype, "F32");
}

#[test]
fn p072_tensor_info_with_stats() {
    let stats = TensorStats {
        min: -1.0,
        max: 1.0,
        mean: 0.0,
        std: 0.5,
    };
    let info = TensorInfo {
        name: "layer.weight".to_string(),
        dtype: "F16".to_string(),
        shape: vec![1024, 1024],
        size_bytes: 1024 * 1024 * 2,
        stats: Some(stats),
    };
    assert!(info.stats.is_some());
    let s = info.stats.unwrap();
    assert_eq!(s.min, -1.0);
    assert_eq!(s.max, 1.0);
}

#[test]
fn p073_tensor_info_multidim_shape() {
    let info = TensorInfo {
        name: "conv.weight".to_string(),
        dtype: "F32".to_string(),
        shape: vec![64, 32, 3, 3],
        size_bytes: 64 * 32 * 3 * 3 * 4,
        stats: None,
    };
    assert_eq!(info.shape.len(), 4);
    let total: usize = info.shape.iter().product();
    assert_eq!(total, 64 * 32 * 3 * 3);
}

#[test]
fn p074_tensor_stats_range() {
    let stats = TensorStats {
        min: -2.5,
        max: 2.5,
        mean: 0.01,
        std: 1.0,
    };
    assert!(stats.min < stats.max);
    assert!(stats.mean >= stats.min && stats.mean <= stats.max);
}

// ========================================================================
// Section 8: InspectionReport Tests (P081-P090)
// ========================================================================

#[test]
fn p081_inspection_report_format() {
    let report = InspectionReport {
        format: FormatType::Gguf,
        file_size: 1_000_000,
        metadata: BTreeMap::new(),
        tensors: vec![],
        total_params: 100_000,
        quantization: None,
        architecture: None,
    };
    assert_eq!(report.format, FormatType::Gguf);
    assert_eq!(report.file_size, 1_000_000);
}

#[test]
fn p082_inspection_report_with_metadata() {
    let mut metadata = BTreeMap::new();
    metadata.insert("model_name".to_string(), "test-model".to_string());
    metadata.insert("version".to_string(), "1.0".to_string());

    let report = InspectionReport {
        format: FormatType::Apr,
        file_size: 500_000,
        metadata,
        tensors: vec![],
        total_params: 50_000,
        quantization: None,
        architecture: Some("transformer".to_string()),
    };
    assert_eq!(report.metadata.len(), 2);
    assert!(report.architecture.is_some());
}

#[test]
fn p083_inspection_report_with_quantization() {
    let report = InspectionReport {
        format: FormatType::Gguf,
        file_size: 2_000_000,
        metadata: BTreeMap::new(),
        tensors: vec![],
        total_params: 1_000_000,
        quantization: Some("Q4_K_M".to_string()),
        architecture: Some("llama".to_string()),
    };
    assert_eq!(report.quantization, Some("Q4_K_M".to_string()));
}

#[test]
fn p084_inspection_report_display() {
    let report = InspectionReport {
        format: FormatType::SafeTensors,
        file_size: 100,
        metadata: BTreeMap::new(),
        tensors: vec![],
        total_params: 10,
        quantization: None,
        architecture: None,
    };
    let display = format!("{}", report);
    assert!(display.contains("Rosetta Stone Inspection"));
    assert!(display.contains("SafeTensors"));
}

// ========================================================================
// Section 12: Destructive Tests (Popperian "Crucial Experiments")
// ========================================================================

#[test]
fn p091_pdf_imposter_test() {
    // The PDF Imposter: Renamed file detection
    // A PDF renamed to .gguf should fail magic detection
    // This test verifies format detection uses magic bytes, not just extension

    // Create a fake "GGUF" file with PDF magic
    let temp_dir = std::env::temp_dir();
    let fake_gguf = temp_dir.join("fake.gguf");

    // PDF magic: %PDF-1.
    std::fs::write(&fake_gguf, b"%PDF-1.4\n").expect("Write test file");

    let result = FormatType::from_magic(&fake_gguf);

    // Clean up
    let _ = std::fs::remove_file(&fake_gguf);

    // Should fail - PDF magic doesn't match any known format
    assert!(
        result.is_err(),
        "PDF disguised as GGUF should fail magic detection"
    );
}

#[test]
fn p092_unicode_ghost_test() {
    // The Unicode Ghost: Complex characters in paths
    // Verify UTF-8 paths are handled correctly

    let path = Path::new("模型_テスト_🤖.gguf");
    let format = FormatType::from_extension(path);
    assert!(
        format.is_ok(),
        "Unicode path should parse extension correctly"
    );
}

#[test]
fn p093_infinite_loop_test() {
    // The Infinite Loop: Cycle detection in chains
    let _rosetta = RosettaStone::new();

    // This chain has a cycle: GGUF → APR → GGUF → APR → SafeTensors
    // APR appears twice, which should be detected as a cycle
    let chain = vec![
        FormatType::Gguf,
        FormatType::Apr,
        FormatType::Gguf,
        FormatType::Apr,
        FormatType::SafeTensors,
    ];

    // Note: This test validates the has_cycle() logic is in place
    let path = ConversionPath {
        source: chain[0],
        target: chain[chain.len() - 1],
        intermediates: chain[1..chain.len() - 1].to_vec(),
    };
    assert!(
        path.has_cycle(),
        "Chain with repeated formats should have cycle"
    );
}

#[test]
fn p094_zero_size_file_test() {
    // Zero-size file should fail inspection
    let temp_dir = std::env::temp_dir();
    let empty_file = temp_dir.join("empty.gguf");
    std::fs::write(&empty_file, b"").expect("Write empty file");

    let result = FormatType::from_magic(&empty_file);
    let _ = std::fs::remove_file(&empty_file);

    assert!(result.is_err(), "Empty file should fail magic detection");
}

#[test]
fn p095_truncated_magic_test() {
    // File with only 3 bytes (truncated magic)
    let temp_dir = std::env::temp_dir();
    let short_file = temp_dir.join("short.gguf");
    std::fs::write(&short_file, b"GGU").expect("Write short file");

    let result = FormatType::from_magic(&short_file);
    let _ = std::fs::remove_file(&short_file);

    assert!(result.is_err(), "Truncated magic should fail");
}

#[test]
fn p096_symlink_path_extension() {
    // Paths with multiple dots
    let path = Path::new("model.v1.backup.gguf");
    let format = FormatType::from_extension(path);
    assert!(format.is_ok());
    assert_eq!(format.unwrap(), FormatType::Gguf);
}

#[test]
fn p097_hidden_file_extension() {
    // Hidden file with extension
    let path = Path::new(".hidden_model.safetensors");
    let format = FormatType::from_extension(path);
    assert!(format.is_ok());
    assert_eq!(format.unwrap(), FormatType::SafeTensors);
}

#[test]
fn p098_mixed_case_extension() {
    // Mixed case extension
    let path = Path::new("model.GgUf");
    let format = FormatType::from_extension(path);
    assert!(format.is_ok());
    assert_eq!(format.unwrap(), FormatType::Gguf);
}

#[test]
fn p099_format_hash_trait() {
    // Verify FormatType implements Hash correctly
    use std::collections::HashSet;
    let mut set = HashSet::new();
    set.insert(FormatType::Gguf);
    set.insert(FormatType::Apr);
    set.insert(FormatType::Gguf); // Duplicate
    assert_eq!(set.len(), 2);
}

#[test]
fn p100_format_eq_trait() {
    // Verify FormatType equality
    assert_eq!(FormatType::Gguf, FormatType::Gguf);
    assert_ne!(FormatType::Gguf, FormatType::Apr);
    assert_ne!(FormatType::Apr, FormatType::SafeTensors);
}

// ========================================================================
// Section 14: Additional Edge Cases (P101-P110)
// ========================================================================

#[test]
fn p101_conversion_path_clone() {
    let path = ConversionPath::direct(FormatType::Gguf, FormatType::Apr);
    let path2 = path.clone();
    assert_eq!(path, path2);
}

#[test]
fn p102_conversion_path_debug() {
    let path = ConversionPath::direct(FormatType::Gguf, FormatType::Apr);
    let debug = format!("{:?}", path);
    assert!(debug.contains("ConversionPath"));
    assert!(debug.contains("Gguf"));
}

#[test]
fn p103_tensor_info_clone() {
    let info = TensorInfo {
        name: "test".to_string(),
        dtype: "F32".to_string(),
        shape: vec![10, 20],
        size_bytes: 800,
        stats: None,
    };
    let info2 = info.clone();
    assert_eq!(info.name, info2.name);
    assert_eq!(info.shape, info2.shape);
}

#[test]
fn p104_tensor_stats_copy() {
    let stats = TensorStats {
        min: 0.0,
        max: 1.0,
        mean: 0.5,
        std: 0.25,
    };
    let stats2 = stats; // Copy
    assert_eq!(stats.mean, stats2.mean);
}

#[test]
fn p105_verification_report_clone() {
    let report = VerificationReport::passing();
    let report2 = report.clone();
    assert_eq!(report.is_equivalent, report2.is_equivalent);
}

#[test]
fn p106_options_debug() {
    let opts = ConversionOptions::default();
    let debug = format!("{:?}", opts);
    assert!(debug.contains("ConversionOptions"));
}