ruvector-solver 2.0.6

Sublinear-time solver for RuVector: O(log n) to O(√n) algorithms for sparse linear systems, PageRank, and spectral methods
Documentation
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! Comprehensive input validation for solver operations.
//!
//! All validation functions run eagerly before any computation begins, ensuring
//! callers receive clear diagnostics instead of mysterious numerical failures or
//! resource exhaustion. Every public function returns [`ValidationError`] on
//! failure, which converts into [`SolverError::InvalidInput`] via `From`.
//!
//! # Limits
//!
//! Hard limits are enforced to prevent denial-of-service through oversized
//! inputs:
//!
//! | Resource      | Limit                  | Constant          |
//! |---------------|------------------------|-------------------|
//! | Nodes (rows)  | 10,000,000             | [`MAX_NODES`]     |
//! | Edges (nnz)   | 100,000,000            | [`MAX_EDGES`]     |
//! | Dimension     | 65,536                 | [`MAX_DIM`]       |
//! | Iterations    | 1,000,000              | [`MAX_ITERATIONS`]|
//! | Request body  | 10 MiB                 | [`MAX_BODY_SIZE`] |

use crate::error::ValidationError;
use crate::types::{CsrMatrix, SolverResult};

// ---------------------------------------------------------------------------
// Resource limits
// ---------------------------------------------------------------------------

/// Maximum number of rows or columns to prevent resource exhaustion.
pub const MAX_NODES: usize = 10_000_000;

/// Maximum number of non-zero entries.
pub const MAX_EDGES: usize = 100_000_000;

/// Maximum vector/matrix dimension for dense operations.
pub const MAX_DIM: usize = 65_536;

/// Maximum solver iterations to prevent runaway computation.
pub const MAX_ITERATIONS: usize = 1_000_000;

/// Maximum request body size in bytes (10 MiB).
pub const MAX_BODY_SIZE: usize = 10 * 1024 * 1024;

// ---------------------------------------------------------------------------
// CSR matrix validation
// ---------------------------------------------------------------------------

/// Validate the structural integrity of a CSR matrix.
///
/// Performs the following checks in order:
///
/// 1. `rows` and `cols` are within [`MAX_NODES`].
/// 2. `nnz` (number of non-zeros) is within [`MAX_EDGES`].
/// 3. `row_ptr` length equals `rows + 1`.
/// 4. `row_ptr` is monotonically non-decreasing.
/// 5. `row_ptr[0] == 0` and `row_ptr[rows] == nnz`.
/// 6. `col_indices` length equals `values` length.
/// 7. All column indices are less than `cols`.
/// 8. No `NaN` or `Inf` values in `values`.
/// 9. Column indices are sorted within each row (emits a [`tracing::warn`] if
///    not, but does not error).
///
/// # Errors
///
/// Returns [`ValidationError`] describing the first violation found.
///
/// # Examples
///
/// ```
/// use ruvector_solver::types::CsrMatrix;
/// use ruvector_solver::validation::validate_csr_matrix;
///
/// let m = CsrMatrix::<f32>::from_coo(2, 2, vec![(0, 0, 1.0), (1, 1, 2.0)]);
/// assert!(validate_csr_matrix(&m).is_ok());
/// ```
pub fn validate_csr_matrix(matrix: &CsrMatrix<f32>) -> Result<(), ValidationError> {
    // 1. Dimension bounds
    if matrix.rows > MAX_NODES || matrix.cols > MAX_NODES {
        return Err(ValidationError::MatrixTooLarge {
            rows: matrix.rows,
            cols: matrix.cols,
            max_dim: MAX_NODES,
        });
    }

    // 2. NNZ bounds
    let nnz = matrix.values.len();
    if nnz > MAX_EDGES {
        return Err(ValidationError::DimensionMismatch(format!(
            "nnz {} exceeds maximum allowed {}",
            nnz, MAX_EDGES,
        )));
    }

    // 3. row_ptr length
    let expected_row_ptr_len = matrix.rows + 1;
    if matrix.row_ptr.len() != expected_row_ptr_len {
        return Err(ValidationError::DimensionMismatch(format!(
            "row_ptr length {} does not equal rows + 1 = {}",
            matrix.row_ptr.len(),
            expected_row_ptr_len,
        )));
    }

    // 4. row_ptr monotonicity
    for i in 1..matrix.row_ptr.len() {
        if matrix.row_ptr[i] < matrix.row_ptr[i - 1] {
            return Err(ValidationError::NonMonotonicRowPtrs { position: i });
        }
    }

    // 5. row_ptr boundary values
    if matrix.row_ptr[0] != 0 {
        return Err(ValidationError::DimensionMismatch(format!(
            "row_ptr[0] = {} (expected 0)",
            matrix.row_ptr[0],
        )));
    }
    let expected_nnz = matrix.row_ptr[matrix.rows];
    if expected_nnz != nnz {
        return Err(ValidationError::DimensionMismatch(format!(
            "values length {} does not match row_ptr[rows] = {}",
            nnz, expected_nnz,
        )));
    }

    // 6. col_indices length must match values length
    if matrix.col_indices.len() != nnz {
        return Err(ValidationError::DimensionMismatch(format!(
            "col_indices length {} does not match values length {}",
            matrix.col_indices.len(),
            nnz,
        )));
    }

    // 7. Column index bounds + 9. Sorted check (warn only) + 8. Finiteness
    for row in 0..matrix.rows {
        let start = matrix.row_ptr[row];
        let end = matrix.row_ptr[row + 1];

        let mut prev_col: Option<usize> = None;
        for idx in start..end {
            let col = matrix.col_indices[idx];
            if col >= matrix.cols {
                return Err(ValidationError::IndexOutOfBounds {
                    index: col as u32,
                    row,
                    cols: matrix.cols,
                });
            }

            let val = matrix.values[idx];
            if !val.is_finite() {
                return Err(ValidationError::NonFiniteValue(format!(
                    "matrix[{}, {}] = {}",
                    row, col, val,
                )));
            }

            // Check sorted order within row (warn, not error)
            if let Some(pc) = prev_col {
                if col < pc {
                    tracing::warn!(
                        row = row,
                        "column indices not sorted within row (col {} follows {}); \
                         performance may be degraded",
                        col,
                        pc,
                    );
                }
            }
            prev_col = Some(col);
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// RHS vector validation
// ---------------------------------------------------------------------------

/// Validate a right-hand-side vector for a linear solve.
///
/// Checks:
///
/// 1. `rhs.len() == expected_len` (dimension must match the matrix).
/// 2. No `NaN` or `Inf` entries.
/// 3. If all entries are zero, emits a [`tracing::warn`] (a zero RHS is
///    technically valid but often indicates a bug).
///
/// # Errors
///
/// Returns [`ValidationError`] on dimension mismatch or non-finite values.
pub fn validate_rhs(rhs: &[f32], expected_len: usize) -> Result<(), ValidationError> {
    // 1. Length check
    if rhs.len() != expected_len {
        return Err(ValidationError::DimensionMismatch(format!(
            "rhs length {} does not match expected {}",
            rhs.len(),
            expected_len,
        )));
    }

    // 2. Finite check + 3. All-zeros check
    let mut all_zero = true;
    for (i, &v) in rhs.iter().enumerate() {
        if !v.is_finite() {
            return Err(ValidationError::NonFiniteValue(format!(
                "rhs[{}] = {}",
                i, v,
            )));
        }
        if v != 0.0 {
            all_zero = false;
        }
    }

    if all_zero && !rhs.is_empty() {
        tracing::warn!("rhs vector is all zeros; solution will be trivially zero");
    }

    Ok(())
}

/// Validate the right-hand side vector `b` for compatibility with a matrix.
///
/// This is an alias for [`validate_rhs`] that preserves backward compatibility
/// with the original API name.
pub fn validate_rhs_vector(rhs: &[f32], expected_len: usize) -> Result<(), ValidationError> {
    validate_rhs(rhs, expected_len)
}

// ---------------------------------------------------------------------------
// Solver parameter validation
// ---------------------------------------------------------------------------

/// Validate solver convergence parameters.
///
/// # Rules
///
/// - `tolerance` must be in the range `(0.0, 1.0]` and be finite.
/// - `max_iterations` must be in `[1, MAX_ITERATIONS]`.
///
/// # Errors
///
/// Returns [`ValidationError::ParameterOutOfRange`] if either parameter is
/// outside its valid range.
pub fn validate_params(tolerance: f64, max_iterations: usize) -> Result<(), ValidationError> {
    if !tolerance.is_finite() || tolerance <= 0.0 || tolerance > 1.0 {
        return Err(ValidationError::ParameterOutOfRange {
            name: "tolerance".into(),
            value: format!("{tolerance:.2e}"),
            expected: "(0.0, 1.0]".into(),
        });
    }

    if max_iterations == 0 || max_iterations > MAX_ITERATIONS {
        return Err(ValidationError::ParameterOutOfRange {
            name: "max_iterations".into(),
            value: max_iterations.to_string(),
            expected: format!("[1, {}]", MAX_ITERATIONS),
        });
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Combined solver input validation
// ---------------------------------------------------------------------------

/// Validate the complete solver input (matrix + rhs + parameters).
///
/// This is a convenience function that calls [`validate_csr_matrix`],
/// [`validate_rhs`], and validates tolerance in sequence. It also checks
/// that the matrix is square, which is required by all iterative solvers.
///
/// # Errors
///
/// Returns [`ValidationError`] on the first failing check.
pub fn validate_solver_input(
    matrix: &CsrMatrix<f32>,
    rhs: &[f32],
    tolerance: f64,
) -> Result<(), ValidationError> {
    validate_csr_matrix(matrix)?;
    validate_rhs(rhs, matrix.rows)?;

    // Square matrix required for iterative solvers.
    if matrix.rows != matrix.cols {
        return Err(ValidationError::DimensionMismatch(format!(
            "solver requires a square matrix but got {}x{}",
            matrix.rows, matrix.cols,
        )));
    }

    // Tolerance bounds.
    if !tolerance.is_finite() || tolerance <= 0.0 {
        return Err(ValidationError::ParameterOutOfRange {
            name: "tolerance".into(),
            value: tolerance.to_string(),
            expected: "finite positive value".into(),
        });
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Output validation (post-solve)
// ---------------------------------------------------------------------------

/// Validate a solver result after computation completes.
///
/// This catches silent numerical corruption that may have occurred during
/// iteration:
///
/// 1. No `NaN` or `Inf` in the solution vector.
/// 2. The residual norm is finite.
/// 3. At least one iteration was performed.
///
/// # Errors
///
/// Returns [`ValidationError`] if the output is corrupted.
pub fn validate_output(result: &SolverResult) -> Result<(), ValidationError> {
    // 1. Solution vector finiteness
    for (i, &v) in result.solution.iter().enumerate() {
        if !v.is_finite() {
            return Err(ValidationError::NonFiniteValue(format!(
                "solution[{}] = {}",
                i, v,
            )));
        }
    }

    // 2. Residual finiteness
    if !result.residual_norm.is_finite() {
        return Err(ValidationError::NonFiniteValue(format!(
            "residual_norm = {}",
            result.residual_norm,
        )));
    }

    // 3. Iteration count
    if result.iterations == 0 {
        return Err(ValidationError::ParameterOutOfRange {
            name: "iterations".into(),
            value: "0".into(),
            expected: ">= 1".into(),
        });
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Body size validation (for API / deserialization boundaries)
// ---------------------------------------------------------------------------

/// Validate that a request body does not exceed [`MAX_BODY_SIZE`].
///
/// Call this at the deserialization boundary before parsing untrusted input.
///
/// # Errors
///
/// Returns [`ValidationError::ParameterOutOfRange`] if `size > MAX_BODY_SIZE`.
pub fn validate_body_size(size: usize) -> Result<(), ValidationError> {
    if size > MAX_BODY_SIZE {
        return Err(ValidationError::ParameterOutOfRange {
            name: "body_size".into(),
            value: format!("{} bytes", size),
            expected: format!("<= {} bytes (10 MiB)", MAX_BODY_SIZE),
        });
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{Algorithm, ConvergenceInfo, CsrMatrix, SolverResult};
    use std::time::Duration;

    fn make_identity(n: usize) -> CsrMatrix<f32> {
        let mut row_ptr = vec![0usize; n + 1];
        let mut col_indices = Vec::with_capacity(n);
        let mut values = Vec::with_capacity(n);
        for i in 0..n {
            row_ptr[i + 1] = i + 1;
            col_indices.push(i);
            values.push(1.0);
        }
        CsrMatrix {
            values,
            col_indices,
            row_ptr,
            rows: n,
            cols: n,
        }
    }

    // -- validate_csr_matrix ------------------------------------------------

    #[test]
    fn valid_identity() {
        let mat = make_identity(4);
        assert!(validate_csr_matrix(&mat).is_ok());
    }

    #[test]
    fn valid_empty_matrix() {
        let m = CsrMatrix {
            row_ptr: vec![0],
            col_indices: vec![],
            values: vec![],
            rows: 0,
            cols: 0,
        };
        assert!(validate_csr_matrix(&m).is_ok());
    }

    #[test]
    fn valid_from_coo() {
        let m = CsrMatrix::<f32>::from_coo(
            3,
            3,
            vec![
                (0, 0, 2.0),
                (0, 1, -0.5),
                (1, 0, -0.5),
                (1, 1, 2.0),
                (1, 2, -0.5),
                (2, 1, -0.5),
                (2, 2, 2.0),
            ],
        );
        assert!(validate_csr_matrix(&m).is_ok());
    }

    #[test]
    fn rejects_too_large_matrix() {
        let m = CsrMatrix {
            row_ptr: vec![0, 0],
            col_indices: vec![],
            values: vec![],
            rows: MAX_NODES + 1,
            cols: 1,
        };
        assert!(matches!(
            validate_csr_matrix(&m),
            Err(ValidationError::MatrixTooLarge { .. })
        ));
    }

    #[test]
    fn rejects_wrong_row_ptr_length() {
        let m = CsrMatrix {
            row_ptr: vec![0, 1],
            col_indices: vec![0],
            values: vec![1.0],
            rows: 3,
            cols: 3,
        };
        assert!(matches!(
            validate_csr_matrix(&m),
            Err(ValidationError::DimensionMismatch(_))
        ));
    }

    #[test]
    fn non_monotonic_row_ptr() {
        let mut mat = make_identity(4);
        mat.row_ptr[2] = 0; // break monotonicity
        let err = validate_csr_matrix(&mat).unwrap_err();
        assert!(matches!(err, ValidationError::NonMonotonicRowPtrs { .. }));
    }

    #[test]
    fn rejects_row_ptr_not_starting_at_zero() {
        let m = CsrMatrix {
            row_ptr: vec![1, 2],
            col_indices: vec![0],
            values: vec![1.0],
            rows: 1,
            cols: 1,
        };
        match validate_csr_matrix(&m) {
            Err(ValidationError::DimensionMismatch(msg)) => {
                assert!(msg.contains("row_ptr[0]"), "msg: {msg}");
            }
            other => panic!("expected DimensionMismatch for row_ptr[0], got {other:?}"),
        }
    }

    #[test]
    fn col_index_out_of_bounds() {
        let mut mat = make_identity(4);
        mat.col_indices[1] = 99;
        let err = validate_csr_matrix(&mat).unwrap_err();
        assert!(matches!(err, ValidationError::IndexOutOfBounds { .. }));
    }

    #[test]
    fn nan_value_rejected() {
        let mut mat = make_identity(4);
        mat.values[0] = f32::NAN;
        let err = validate_csr_matrix(&mat).unwrap_err();
        assert!(matches!(err, ValidationError::NonFiniteValue(_)));
    }

    #[test]
    fn inf_value_rejected() {
        let mut mat = make_identity(4);
        mat.values[0] = f32::INFINITY;
        let err = validate_csr_matrix(&mat).unwrap_err();
        assert!(matches!(err, ValidationError::NonFiniteValue(_)));
    }

    // -- validate_rhs -------------------------------------------------------

    #[test]
    fn valid_rhs() {
        assert!(validate_rhs(&[1.0, 2.0, 3.0], 3).is_ok());
    }

    #[test]
    fn rhs_dimension_mismatch() {
        let err = validate_rhs(&[1.0, 2.0], 3).unwrap_err();
        assert!(matches!(err, ValidationError::DimensionMismatch(_)));
    }

    #[test]
    fn rhs_nan_rejected() {
        let err = validate_rhs(&[1.0, f32::NAN, 3.0], 3).unwrap_err();
        assert!(matches!(err, ValidationError::NonFiniteValue(_)));
    }

    #[test]
    fn rhs_inf_rejected() {
        let err = validate_rhs(&[1.0, f32::NEG_INFINITY, 3.0], 3).unwrap_err();
        assert!(matches!(err, ValidationError::NonFiniteValue(_)));
    }

    #[test]
    fn warns_on_all_zero_rhs() {
        // Should succeed but emit a warning (cannot assert warning in unit test,
        // but at least verify it does not error).
        assert!(validate_rhs(&[0.0, 0.0, 0.0], 3).is_ok());
    }

    // -- validate_rhs_vector (backward compat alias) ------------------------

    #[test]
    fn rhs_vector_alias_works() {
        assert!(validate_rhs_vector(&[1.0, 2.0], 2).is_ok());
        assert!(validate_rhs_vector(&[1.0, 2.0], 3).is_err());
    }

    // -- validate_params ----------------------------------------------------

    #[test]
    fn valid_params() {
        assert!(validate_params(1e-8, 500).is_ok());
        assert!(validate_params(1.0, 1).is_ok());
    }

    #[test]
    fn rejects_zero_tolerance() {
        match validate_params(0.0, 100) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "tolerance");
            }
            other => panic!("expected ParameterOutOfRange for tolerance, got {other:?}"),
        }
    }

    #[test]
    fn rejects_negative_tolerance() {
        match validate_params(-1e-6, 100) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "tolerance");
            }
            other => panic!("expected ParameterOutOfRange for tolerance, got {other:?}"),
        }
    }

    #[test]
    fn rejects_tolerance_above_one() {
        match validate_params(1.5, 100) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "tolerance");
            }
            other => panic!("expected ParameterOutOfRange for tolerance, got {other:?}"),
        }
    }

    #[test]
    fn rejects_nan_tolerance() {
        match validate_params(f64::NAN, 100) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "tolerance");
            }
            other => panic!("expected ParameterOutOfRange for tolerance, got {other:?}"),
        }
    }

    #[test]
    fn rejects_zero_iterations() {
        match validate_params(1e-6, 0) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "max_iterations");
            }
            other => panic!("expected ParameterOutOfRange for max_iterations, got {other:?}"),
        }
    }

    #[test]
    fn rejects_excessive_iterations() {
        match validate_params(1e-6, MAX_ITERATIONS + 1) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "max_iterations");
            }
            other => panic!("expected ParameterOutOfRange for max_iterations, got {other:?}"),
        }
    }

    // -- validate_solver_input (combined) -----------------------------------

    #[test]
    fn full_input_validation() {
        let mat = make_identity(3);
        let rhs = vec![1.0f32, 2.0, 3.0];
        assert!(validate_solver_input(&mat, &rhs, 1e-6).is_ok());
    }

    #[test]
    fn non_square_rejected() {
        let mat = CsrMatrix {
            values: vec![],
            col_indices: vec![],
            row_ptr: vec![0, 0, 0],
            rows: 2,
            cols: 3,
        };
        let rhs = vec![1.0f32, 2.0];
        let err = validate_solver_input(&mat, &rhs, 1e-6).unwrap_err();
        assert!(matches!(err, ValidationError::DimensionMismatch(_)));
    }

    #[test]
    fn invalid_tolerance_rejected() {
        let mat = make_identity(2);
        let rhs = vec![1.0f32, 2.0];
        assert!(validate_solver_input(&mat, &rhs, -1.0).is_err());
        assert!(validate_solver_input(&mat, &rhs, 0.0).is_err());
        assert!(validate_solver_input(&mat, &rhs, f64::NAN).is_err());
    }

    // -- validate_output ----------------------------------------------------

    #[test]
    fn valid_output() {
        let result = SolverResult {
            solution: vec![1.0, 2.0, 3.0],
            iterations: 10,
            residual_norm: 1e-8,
            wall_time: Duration::from_millis(5),
            convergence_history: vec![ConvergenceInfo {
                iteration: 0,
                residual_norm: 1.0,
            }],
            algorithm: Algorithm::Neumann,
        };
        assert!(validate_output(&result).is_ok());
    }

    #[test]
    fn rejects_nan_in_solution() {
        let result = SolverResult {
            solution: vec![1.0, f32::NAN, 3.0],
            iterations: 1,
            residual_norm: 1e-8,
            wall_time: Duration::from_millis(1),
            convergence_history: vec![],
            algorithm: Algorithm::Neumann,
        };
        match validate_output(&result) {
            Err(ValidationError::NonFiniteValue(ref msg)) => {
                assert!(msg.contains("solution"), "msg: {msg}");
            }
            other => panic!("expected NonFiniteValue for solution, got {other:?}"),
        }
    }

    #[test]
    fn rejects_inf_in_solution() {
        let result = SolverResult {
            solution: vec![f32::INFINITY],
            iterations: 1,
            residual_norm: 1e-8,
            wall_time: Duration::from_millis(1),
            convergence_history: vec![],
            algorithm: Algorithm::Neumann,
        };
        match validate_output(&result) {
            Err(ValidationError::NonFiniteValue(ref msg)) => {
                assert!(msg.contains("solution"), "msg: {msg}");
            }
            other => panic!("expected NonFiniteValue for solution, got {other:?}"),
        }
    }

    #[test]
    fn rejects_nan_residual() {
        let result = SolverResult {
            solution: vec![1.0],
            iterations: 1,
            residual_norm: f64::NAN,
            wall_time: Duration::from_millis(1),
            convergence_history: vec![],
            algorithm: Algorithm::Neumann,
        };
        match validate_output(&result) {
            Err(ValidationError::NonFiniteValue(ref msg)) => {
                assert!(msg.contains("residual"), "msg: {msg}");
            }
            other => panic!("expected NonFiniteValue for residual, got {other:?}"),
        }
    }

    #[test]
    fn rejects_inf_residual() {
        let result = SolverResult {
            solution: vec![1.0],
            iterations: 1,
            residual_norm: f64::INFINITY,
            wall_time: Duration::from_millis(1),
            convergence_history: vec![],
            algorithm: Algorithm::Neumann,
        };
        assert!(matches!(
            validate_output(&result),
            Err(ValidationError::NonFiniteValue(_))
        ));
    }

    #[test]
    fn rejects_zero_iterations_in_output() {
        let result = SolverResult {
            solution: vec![1.0],
            iterations: 0,
            residual_norm: 1e-8,
            wall_time: Duration::from_millis(1),
            convergence_history: vec![],
            algorithm: Algorithm::Neumann,
        };
        match validate_output(&result) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "iterations");
            }
            other => panic!("expected ParameterOutOfRange, got {other:?}"),
        }
    }

    // -- validate_body_size -------------------------------------------------

    #[test]
    fn valid_body_size() {
        assert!(validate_body_size(1024).is_ok());
        assert!(validate_body_size(MAX_BODY_SIZE).is_ok());
    }

    #[test]
    fn rejects_oversized_body() {
        match validate_body_size(MAX_BODY_SIZE + 1) {
            Err(ValidationError::ParameterOutOfRange { ref name, .. }) => {
                assert_eq!(name, "body_size");
            }
            other => panic!("expected ParameterOutOfRange, got {other:?}"),
        }
    }
}