aprender-core 0.29.3

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
452
453
454
pub(super) use super::{
    numerical, process_edge_case_result, run_edge_case_tests, test_empty_handling,
    test_inf_handling, test_max_size_handling, test_nan_handling, test_zero_handling,
    CategoryScore, EdgeCaseBehavior, EdgeCaseConfig, EdgeCaseResult, Severity,
};
use std::time::{Duration, Instant};

#[test]
fn test_edge_case_config_default() {
    let config = EdgeCaseConfig::default();
    assert!(config.test_nan);
    assert!(config.test_inf);
    assert!(config.test_empty);
    assert!(!config.allow_panic);
}

#[test]
fn test_edge_case_behavior() {
    assert!(EdgeCaseBehavior::GracefulError.is_acceptable());
    assert!(EdgeCaseBehavior::ReturnsDefault.is_acceptable());
    assert!(EdgeCaseBehavior::Normal.is_acceptable());
    assert!(!EdgeCaseBehavior::Panics.is_acceptable());
    assert!(!EdgeCaseBehavior::Hangs.is_acceptable());
}

#[test]
fn test_nan_handling_fn() {
    let predict =
        |input: &[f32]| -> Result<Vec<f32>, String> { Ok(input.iter().map(|x| x * 2.0).collect()) };

    let result = test_nan_handling(predict);
    assert!(result.passed);
    assert!(result.actual.is_acceptable());
}

#[test]
fn test_nan_handling_with_error_fn() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        if input.iter().any(|x| x.is_nan()) {
            return Err("NaN not allowed".to_string());
        }
        Ok(input.to_vec())
    };

    let result = test_nan_handling(predict);
    assert!(result.passed); // Error is acceptable
    assert_eq!(result.actual, EdgeCaseBehavior::GracefulError);
}

#[test]
fn test_inf_handling_fn() {
    let predict =
        |input: &[f32]| -> Result<Vec<f32>, String> { Ok(input.iter().map(|x| x * 2.0).collect()) };

    let result = test_inf_handling(predict);
    assert!(result.passed);
}

#[test]
fn test_empty_handling_fn() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        if input.is_empty() {
            return Err("Empty input".to_string());
        }
        Ok(input.to_vec())
    };

    let result = test_empty_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::GracefulError);
}

#[test]
fn test_zero_handling_fn() {
    let predict =
        |input: &[f32]| -> Result<Vec<f32>, String> { Ok(input.iter().map(|x| x * 2.0).collect()) };

    let result = test_zero_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::Normal);
}

#[test]
fn test_run_edge_case_tests() {
    let config = EdgeCaseConfig::default();
    let (score, issues) = run_edge_case_tests(&config);

    assert_eq!(score.points_possible, 15);
    assert!(score.tests_passed > 0);
    // With mock implementation, most tests should pass
    assert!(score.pass_rate() > 50.0);
    // Should have minimal issues with well-behaved mock
    assert!(issues.len() <= 2);
}

#[test]
fn test_numerical_underflow() {
    let result = numerical::test_underflow();
    assert!(result.passed);
}

#[test]
fn test_numerical_overflow() {
    let result = numerical::test_overflow();
    assert!(result.passed);
}

#[test]
fn test_numerical_precision() {
    let result = numerical::test_precision_loss();
    assert!(result.passed);
}

#[test]
fn test_max_size_handling_works() {
    // Test the function exists and returns correct type
    let start = Instant::now();
    let test_size = 10_000;
    let input: Vec<f32> = vec![1.0; test_size];
    let passed = input.len() <= 1_000_000;

    assert!(passed);
    assert!(start.elapsed().as_millis() < 1000);
}

#[test]
fn test_edge_case_result_creation() {
    let result = EdgeCaseResult {
        name: "Test".to_string(),
        passed: true,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Normal,
        error: None,
        duration: Duration::from_millis(10),
    };

    assert_eq!(result.name, "Test");
    assert!(result.passed);
    assert!(result.error.is_none());
}

// Additional tests for coverage

#[test]
fn test_edge_case_behavior_description() {
    assert_eq!(
        EdgeCaseBehavior::GracefulError.description(),
        "Returns graceful error"
    );
    assert_eq!(
        EdgeCaseBehavior::ReturnsDefault.description(),
        "Returns NaN/default"
    );
    assert_eq!(
        EdgeCaseBehavior::Panics.description(),
        "Panics (UNACCEPTABLE)"
    );
    assert_eq!(
        EdgeCaseBehavior::Hangs.description(),
        "Hangs/loops (UNACCEPTABLE)"
    );
    assert_eq!(EdgeCaseBehavior::Normal.description(), "Normal execution");
}

#[test]
fn test_edge_case_config_custom() {
    let config = EdgeCaseConfig {
        test_nan: false,
        test_inf: false,
        test_empty: false,
        test_zero: false,
        test_max_size: false,
        max_input_size: 500,
        allow_panic: true,
    };

    assert!(!config.test_nan);
    assert!(config.allow_panic);
    assert_eq!(config.max_input_size, 500);
}

#[test]
fn test_run_edge_case_tests_disabled_all() {
    let config = EdgeCaseConfig {
        test_nan: false,
        test_inf: false,
        test_empty: false,
        test_zero: false,
        test_max_size: false,
        max_input_size: 1000,
        allow_panic: false,
    };

    let (score, issues) = run_edge_case_tests(&config);
    // No tests run means no passes and no failures
    assert_eq!(score.tests_passed + score.tests_failed, 0);
    assert!(issues.is_empty());
}

#[test]
fn test_run_edge_case_tests_allow_panic() {
    let config = EdgeCaseConfig {
        test_nan: true,
        test_inf: false,
        test_empty: false,
        test_zero: false,
        test_max_size: false,
        max_input_size: 1000,
        allow_panic: true,
    };

    let (score, issues) = run_edge_case_tests(&config);
    assert!(score.tests_passed + score.tests_failed > 0);
    // With allow_panic, no critical issues added
    assert!(issues
        .iter()
        .all(|i| i.severity != super::Severity::Critical));
}

#[test]
fn test_inf_handling_with_error() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        if input.iter().any(|x| x.is_infinite()) {
            return Err("Infinity not allowed".to_string());
        }
        Ok(input.to_vec())
    };

    let result = test_inf_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::GracefulError);
    assert!(result.error.is_some());
}

#[test]
fn test_zero_handling_with_error() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        if input.iter().all(|x| *x == 0.0) {
            return Err("Zero vector not allowed".to_string());
        }
        Ok(input.to_vec())
    };

    let result = test_zero_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::GracefulError);
}

#[test]
fn test_empty_handling_success() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        Ok(input.to_vec()) // Accepts empty
    };

    let result = test_empty_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::Normal);
}

#[test]
fn test_nan_handling_returns_non_nan() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        // Replace NaN with 0.0
        Ok(input
            .iter()
            .map(|x| if x.is_nan() { 0.0 } else { *x })
            .collect())
    };

    let result = test_nan_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::Normal);
}

#[test]
fn test_inf_handling_returns_finite() {
    let predict = |input: &[f32]| -> Result<Vec<f32>, String> {
        // Replace Inf with max finite value
        Ok(input
            .iter()
            .map(|x| {
                if x.is_infinite() {
                    f32::MAX * x.signum()
                } else {
                    *x
                }
            })
            .collect())
    };

    let result = test_inf_handling(predict);
    assert!(result.passed);
    assert_eq!(result.actual, EdgeCaseBehavior::Normal);
}

#[test]
fn test_edge_case_result_with_error() {
    let result = EdgeCaseResult {
        name: "Error test".to_string(),
        passed: false,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Panics,
        error: Some("Something went wrong".to_string()),
        duration: Duration::from_millis(5),
    };

    assert!(!result.passed);
    assert!(result.error.is_some());
    assert!(!result.actual.is_acceptable());
}

#[test]
fn test_edge_case_config_clone() {
    let config = EdgeCaseConfig::default();
    let cloned = config.clone();
    assert_eq!(config.test_nan, cloned.test_nan);
    assert_eq!(config.max_input_size, cloned.max_input_size);
}

#[test]
fn test_edge_case_result_clone() {
    let result = EdgeCaseResult {
        name: "Test".to_string(),
        passed: true,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Normal,
        error: None,
        duration: Duration::from_millis(10),
    };

    let cloned = result.clone();
    assert_eq!(result.name, cloned.name);
    assert_eq!(result.passed, cloned.passed);
}

#[test]
fn test_edge_case_config_debug() {
    let config = EdgeCaseConfig::default();
    let debug = format!("{config:?}");
    assert!(debug.contains("EdgeCaseConfig"));
}

#[test]
fn test_edge_case_result_debug() {
    let result = EdgeCaseResult {
        name: "Test".to_string(),
        passed: true,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Normal,
        error: None,
        duration: Duration::from_millis(10),
    };

    let debug = format!("{result:?}");
    assert!(debug.contains("EdgeCaseResult"));
}

#[test]
fn test_process_edge_case_result_failed_no_error() {
    // Test process_edge_case_result with failed test and no error message
    use super::{process_edge_case_result, CategoryScore};

    let result = EdgeCaseResult {
        name: "Failing test".to_string(),
        passed: false,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Panics, // Unacceptable behavior
        error: None,                      // No error message - will use description
        duration: Duration::from_millis(5),
    };

    let mut score = CategoryScore::new(10);
    process_edge_case_result(&result, &mut score);

    assert_eq!(score.tests_failed, 1);
    assert_eq!(score.tests_passed, 0);
}

#[test]
fn test_process_edge_case_result_passed_but_unacceptable() {
    // Test when passed=true but actual behavior is unacceptable
    use super::{process_edge_case_result, CategoryScore};

    let result = EdgeCaseResult {
        name: "Passed but hangs".to_string(),
        passed: true,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Hangs, // Unacceptable
        error: Some("Timeout detected".to_string()),
        duration: Duration::from_millis(5000),
    };

    let mut score = CategoryScore::new(10);
    process_edge_case_result(&result, &mut score);

    // Should fail because actual behavior is unacceptable
    assert_eq!(score.tests_failed, 1);
}

#[test]
fn test_process_edge_case_result_passed_acceptable() {
    use super::{process_edge_case_result, CategoryScore};

    let result = EdgeCaseResult {
        name: "Good test".to_string(),
        passed: true,
        expected: EdgeCaseBehavior::Normal,
        actual: EdgeCaseBehavior::Normal,
        error: None,
        duration: Duration::from_millis(1),
    };

    let mut score = CategoryScore::new(10);
    process_edge_case_result(&result, &mut score);

    assert_eq!(score.tests_passed, 1);
    assert_eq!(score.tests_failed, 0);
}

#[test]
fn test_max_size_handling_exceeds_limit() {
    // Test max_size_handling when size exceeds the cap (handled internally)
    use super::test_max_size_handling;

    // When max_size is smaller than actual allocation cap
    let result = test_max_size_handling(100);
    assert!(result.passed); // Always passes because min(100, 10_000) = 100

    // Test with very small max_size
    let result_small = test_max_size_handling(1);
    assert!(result_small.passed);
    assert_eq!(result_small.actual, EdgeCaseBehavior::Normal);
}

#[test]
fn test_run_edge_case_tests_only_nan() {
    let config = EdgeCaseConfig {
        test_nan: true,
        test_inf: false,
        test_empty: false,
        test_zero: false,
        test_max_size: false,
        max_input_size: 1000,
        allow_panic: false,
    };

    let (score, _issues) = run_edge_case_tests(&config);
    assert_eq!(score.tests_passed + score.tests_failed, 1);
}

#[path = "robustness_tests_edge_cases.rs"]
mod robustness_tests_edge_cases;
#[path = "robustness_tests_hangs.rs"]
mod robustness_tests_hangs;