tensorlogic-ir 0.1.0

Intermediate representation (IR) and AST types for TensorLogic
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
//! EinsumGraph validation and sanitization utilities.
//!
//! Checks graph integrity: missing inputs, duplicate outputs, cycles,
//! unreachable nodes. Provides sanitization to fix common issues.
//!
//! This module provides a complementary validation layer on top of
//! the `crate::graph::validation` module, using a unified issue-based reporting
//! model with severity levels (Error, Warning, Info).

use crate::EinsumGraph;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};

/// Severity of a validation issue.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IssueSeverity {
    /// Critical problem that makes the graph invalid.
    Error,
    /// Non-critical issue that may indicate a problem.
    Warning,
    /// Informational note about the graph structure.
    Info,
}

impl std::fmt::Display for IssueSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IssueSeverity::Error => write!(f, "ERROR"),
            IssueSeverity::Warning => write!(f, "WARNING"),
            IssueSeverity::Info => write!(f, "INFO"),
        }
    }
}

/// A single validation issue found in a graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationIssue {
    /// How severe this issue is.
    pub severity: IssueSeverity,
    /// Machine-readable issue code (e.g., "empty-graph", "duplicate-output").
    pub code: String,
    /// Human-readable description of the issue.
    pub message: String,
    /// Index of the node related to this issue, if applicable.
    pub node_index: Option<usize>,
}

impl std::fmt::Display for ValidationIssue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(idx) = self.node_index {
            write!(
                f,
                "[{}] {} (node {}): {}",
                self.severity, self.code, idx, self.message
            )
        } else {
            write!(f, "[{}] {}: {}", self.severity, self.code, self.message)
        }
    }
}

/// Result of graph validation containing all discovered issues.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ValidationResult {
    /// All issues found during validation.
    pub issues: Vec<ValidationIssue>,
}

impl ValidationResult {
    /// Returns `true` if the graph has no error-level issues.
    pub fn is_valid(&self) -> bool {
        !self
            .issues
            .iter()
            .any(|i| i.severity == IssueSeverity::Error)
    }

    /// Count of error-level issues.
    pub fn error_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Error)
            .count()
    }

    /// Count of warning-level issues.
    pub fn warning_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Warning)
            .count()
    }

    /// Count of info-level issues.
    pub fn info_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Info)
            .count()
    }

    /// Human-readable summary string.
    pub fn summary(&self) -> String {
        format!(
            "{} errors, {} warnings",
            self.error_count(),
            self.warning_count()
        )
    }

    /// Filter issues by severity.
    pub fn issues_by_severity(&self, severity: IssueSeverity) -> Vec<&ValidationIssue> {
        self.issues
            .iter()
            .filter(|i| i.severity == severity)
            .collect()
    }

    /// Filter issues by code prefix.
    pub fn issues_by_code(&self, code: &str) -> Vec<&ValidationIssue> {
        self.issues.iter().filter(|i| i.code == code).collect()
    }
}

impl std::fmt::Display for ValidationResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Validation: {}", self.summary())?;
        for issue in &self.issues {
            writeln!(f, "  {}", issue)?;
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Validation entry point
// ---------------------------------------------------------------------------

/// Validate an `EinsumGraph` for structural integrity.
///
/// Runs all structural checks and returns a unified [`ValidationResult`].
///
/// # Checks performed
///
/// 1. **Empty graph** (warning) -- graph has zero nodes
/// 2. **Duplicate graph outputs** (warning) -- same tensor index listed twice
/// 3. **Node input references** (error) -- node inputs reference non-existent tensors
/// 4. **Node output references** (error) -- node outputs reference non-existent tensors
/// 5. **Unreachable nodes** (warning) -- nodes whose outputs are never consumed
/// 6. **Output references** (error) -- graph outputs reference non-existent tensors
/// 7. **Outputs without producer** (error) -- output tensors not produced by any node or input
/// 8. **Cycle detection** (error) -- data-flow cycles in the graph
/// 9. **Empty node outputs** (error) -- nodes that produce no tensors
/// 10. **Duplicate tensor names** (info) -- two tensors share the same name
pub fn validate_einsum_graph(graph: &EinsumGraph) -> ValidationResult {
    let mut result = ValidationResult::default();

    check_empty_graph(graph, &mut result);
    check_duplicate_outputs(graph, &mut result);
    check_node_input_refs(graph, &mut result);
    check_node_output_refs(graph, &mut result);
    check_unreachable_nodes(graph, &mut result);
    check_output_refs(graph, &mut result);
    check_outputs_have_producers(graph, &mut result);
    check_cycles(graph, &mut result);
    check_empty_node_outputs(graph, &mut result);
    check_duplicate_tensor_names(graph, &mut result);

    result
}

// ---------------------------------------------------------------------------
// Individual checks
// ---------------------------------------------------------------------------

fn check_empty_graph(graph: &EinsumGraph, result: &mut ValidationResult) {
    if graph.nodes.is_empty() {
        result.issues.push(ValidationIssue {
            severity: IssueSeverity::Warning,
            code: "empty-graph".to_string(),
            message: "Graph has no nodes".to_string(),
            node_index: None,
        });
    }
}

fn check_duplicate_outputs(graph: &EinsumGraph, result: &mut ValidationResult) {
    let mut seen = HashSet::new();
    for &output in &graph.outputs {
        if !seen.insert(output) {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Warning,
                code: "duplicate-output".to_string(),
                message: format!("Duplicate output tensor index: {}", output),
                node_index: None,
            });
        }
    }
}

fn check_node_input_refs(graph: &EinsumGraph, result: &mut ValidationResult) {
    let num_tensors = graph.tensors.len();
    for (node_idx, node) in graph.nodes.iter().enumerate() {
        for &input_idx in &node.inputs {
            if input_idx >= num_tensors {
                result.issues.push(ValidationIssue {
                    severity: IssueSeverity::Error,
                    code: "invalid-input-ref".to_string(),
                    message: format!(
                        "Node {} input references tensor index {} but only {} tensors exist",
                        node_idx, input_idx, num_tensors
                    ),
                    node_index: Some(node_idx),
                });
            }
        }
    }
}

fn check_node_output_refs(graph: &EinsumGraph, result: &mut ValidationResult) {
    let num_tensors = graph.tensors.len();
    for (node_idx, node) in graph.nodes.iter().enumerate() {
        for &output_idx in &node.outputs {
            if output_idx >= num_tensors {
                result.issues.push(ValidationIssue {
                    severity: IssueSeverity::Error,
                    code: "invalid-output-ref".to_string(),
                    message: format!(
                        "Node {} output references tensor index {} but only {} tensors exist",
                        node_idx, output_idx, num_tensors
                    ),
                    node_index: Some(node_idx),
                });
            }
        }
    }
}

fn check_unreachable_nodes(graph: &EinsumGraph, result: &mut ValidationResult) {
    if graph.nodes.is_empty() {
        return;
    }

    // A node is "reachable" if at least one of its outputs is consumed by
    // another node or is a graph output.
    let output_set: HashSet<usize> = graph.outputs.iter().copied().collect();

    // Collect all tensor indices consumed as inputs by any node.
    let mut consumed_tensors: HashSet<usize> = HashSet::new();
    for node in &graph.nodes {
        for &inp in &node.inputs {
            consumed_tensors.insert(inp);
        }
    }

    for (node_idx, node) in graph.nodes.iter().enumerate() {
        let any_output_used = node
            .outputs
            .iter()
            .any(|o| consumed_tensors.contains(o) || output_set.contains(o));
        if !any_output_used {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Warning,
                code: "unreachable-node".to_string(),
                message: format!(
                    "Node {} outputs are never consumed and not graph outputs",
                    node_idx
                ),
                node_index: Some(node_idx),
            });
        }
    }
}

fn check_output_refs(graph: &EinsumGraph, result: &mut ValidationResult) {
    let num_tensors = graph.tensors.len();
    for &output_idx in &graph.outputs {
        if output_idx >= num_tensors {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Error,
                code: "invalid-graph-output".to_string(),
                message: format!(
                    "Graph output references tensor index {} but only {} tensors exist",
                    output_idx, num_tensors
                ),
                node_index: None,
            });
        }
    }
}

fn check_outputs_have_producers(graph: &EinsumGraph, result: &mut ValidationResult) {
    // Build set of tensors produced by nodes.
    let mut produced: HashSet<usize> = HashSet::new();
    for node in &graph.nodes {
        for &out in &node.outputs {
            produced.insert(out);
        }
    }

    let input_set: HashSet<usize> = graph.inputs.iter().copied().collect();

    for &output_idx in &graph.outputs {
        if output_idx >= graph.tensors.len() {
            continue; // Already reported in check_output_refs
        }
        if !produced.contains(&output_idx) && !input_set.contains(&output_idx) {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Error,
                code: "output-no-producer".to_string(),
                message: format!(
                    "Output tensor {} ('{}') is not produced by any node and is not a graph input",
                    output_idx, graph.tensors[output_idx]
                ),
                node_index: None,
            });
        }
    }
}

fn check_cycles(graph: &EinsumGraph, result: &mut ValidationResult) {
    if graph.nodes.is_empty() {
        return;
    }

    // Build adjacency: node -> set of successor nodes (via tensor flow).
    let num_nodes = graph.nodes.len();
    let mut adj: Vec<Vec<usize>> = vec![Vec::new(); num_nodes];

    // Map tensor index -> producing node index.
    let mut tensor_producer: HashMap<usize, usize> = HashMap::new();
    for (nidx, node) in graph.nodes.iter().enumerate() {
        for &out in &node.outputs {
            tensor_producer.insert(out, nidx);
        }
    }

    // For each node, find successor nodes: nodes that consume tensors this node produces.
    for (nidx, node) in graph.nodes.iter().enumerate() {
        for &out in &node.outputs {
            for (other_idx, other_node) in graph.nodes.iter().enumerate() {
                if other_idx != nidx && other_node.inputs.contains(&out) {
                    adj[nidx].push(other_idx);
                }
            }
        }
    }

    // Standard DFS cycle detection.
    let mut visited = vec![0u8; num_nodes]; // 0=unvisited, 1=in-stack, 2=done

    for start in 0..num_nodes {
        if visited[start] == 0 && dfs_has_cycle(start, &adj, &mut visited) {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Error,
                code: "cycle-detected".to_string(),
                message: format!("Cyclic dependency detected involving node {}", start),
                node_index: Some(start),
            });
        }
    }
}

fn dfs_has_cycle(node: usize, adj: &[Vec<usize>], visited: &mut [u8]) -> bool {
    visited[node] = 1; // in-stack
    for &next in &adj[node] {
        if visited[next] == 1 {
            return true;
        }
        if visited[next] == 0 && dfs_has_cycle(next, adj, visited) {
            return true;
        }
    }
    visited[node] = 2; // done
    false
}

fn check_empty_node_outputs(graph: &EinsumGraph, result: &mut ValidationResult) {
    for (node_idx, node) in graph.nodes.iter().enumerate() {
        if node.outputs.is_empty() {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Error,
                code: "node-no-outputs".to_string(),
                message: format!("Node {} produces no outputs", node_idx),
                node_index: Some(node_idx),
            });
        }
    }
}

fn check_duplicate_tensor_names(graph: &EinsumGraph, result: &mut ValidationResult) {
    let mut name_indices: HashMap<&str, Vec<usize>> = HashMap::new();
    for (idx, name) in graph.tensors.iter().enumerate() {
        name_indices.entry(name.as_str()).or_default().push(idx);
    }
    for (name, indices) in &name_indices {
        if indices.len() > 1 {
            result.issues.push(ValidationIssue {
                severity: IssueSeverity::Info,
                code: "duplicate-tensor-name".to_string(),
                message: format!("Tensor name '{}' is used by indices {:?}", name, indices),
                node_index: None,
            });
        }
    }
}

// ---------------------------------------------------------------------------
// Graph statistics
// ---------------------------------------------------------------------------

/// Graph statistics computed during validation.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GraphSanitizationStats {
    /// Number of nodes in the graph.
    pub node_count: usize,
    /// Number of graph-level output tensor indices.
    pub output_count: usize,
    /// Number of tensors in the graph.
    pub tensor_count: usize,
    /// Whether the graph contains cycles.
    pub has_cycles: bool,
    /// Number of unreachable nodes.
    pub unreachable_count: usize,
    /// Maximum depth of the data-flow DAG (0 for empty graphs).
    pub max_depth: usize,
}

/// Compute statistics about an `EinsumGraph`.
pub fn compute_graph_stats(graph: &EinsumGraph) -> GraphSanitizationStats {
    let validation = validate_einsum_graph(graph);

    let unreachable_count = validation
        .issues
        .iter()
        .filter(|i| i.code == "unreachable-node")
        .count();

    let has_cycles = validation.issues.iter().any(|i| i.code == "cycle-detected");

    let max_depth = compute_max_depth(graph);

    GraphSanitizationStats {
        node_count: graph.nodes.len(),
        output_count: graph.outputs.len(),
        tensor_count: graph.tensors.len(),
        has_cycles,
        unreachable_count,
        max_depth,
    }
}

/// Compute the maximum depth of the data-flow DAG using BFS from inputs.
fn compute_max_depth(graph: &EinsumGraph) -> usize {
    if graph.nodes.is_empty() {
        return 0;
    }

    // Map tensor -> producing node.
    let mut tensor_producer: HashMap<usize, usize> = HashMap::new();
    for (nidx, node) in graph.nodes.iter().enumerate() {
        for &out in &node.outputs {
            tensor_producer.insert(out, nidx);
        }
    }

    // Build adjacency: node -> successor nodes.
    let num_nodes = graph.nodes.len();
    let mut adj: Vec<Vec<usize>> = vec![Vec::new(); num_nodes];
    let mut in_degree: Vec<usize> = vec![0; num_nodes];

    for (nidx, node) in graph.nodes.iter().enumerate() {
        for &out in &node.outputs {
            for (other_idx, other_node) in graph.nodes.iter().enumerate() {
                if other_idx != nidx && other_node.inputs.contains(&out) {
                    adj[nidx].push(other_idx);
                    in_degree[other_idx] += 1;
                }
            }
        }
    }

    // BFS topological order to compute depth.
    let mut depth = vec![0usize; num_nodes];
    let mut queue: VecDeque<usize> = VecDeque::new();

    for (i, &deg) in in_degree.iter().enumerate() {
        if deg == 0 {
            queue.push_back(i);
        }
    }

    let mut max_d = 0usize;
    while let Some(n) = queue.pop_front() {
        for &next in &adj[n] {
            let new_depth = depth[n] + 1;
            if new_depth > depth[next] {
                depth[next] = new_depth;
            }
            in_degree[next] -= 1;
            if in_degree[next] == 0 {
                queue.push_back(next);
            }
        }
        if depth[n] > max_d {
            max_d = depth[n];
        }
    }

    max_d
}

// ---------------------------------------------------------------------------
// Sanitization
// ---------------------------------------------------------------------------

/// Sanitize a graph by fixing common issues.
///
/// Currently performs:
/// - Deduplication of graph output indices.
///
/// The returned graph is independent of the input (cloned).
pub fn sanitize_graph(graph: &EinsumGraph) -> EinsumGraph {
    let mut sanitized = graph.clone();

    // Deduplicate output indices, preserving order.
    let mut seen = HashSet::new();
    sanitized.outputs.retain(|o| seen.insert(*o));

    sanitized
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{EinsumGraph, EinsumNode};

    /// Helper: build a minimal valid graph (input -> relu -> output).
    fn make_valid_graph() -> EinsumGraph {
        let mut g = EinsumGraph::new();
        let t0 = g.add_tensor("input");
        let t1 = g.add_tensor("output");
        g.inputs = vec![t0];
        g.outputs = vec![t1];
        g.add_node(EinsumNode::elem_unary("relu", t0, t1))
            .expect("failed to add node");
        g
    }

    // 1
    #[test]
    fn test_validate_empty_graph() {
        let graph = EinsumGraph::new();
        let result = validate_einsum_graph(&graph);
        assert!(
            result.is_valid(),
            "empty graph should be valid (only warnings)"
        );
        assert!(
            result.issues.iter().any(|i| i.code == "empty-graph"),
            "should have empty-graph warning"
        );
    }

    // 2
    #[test]
    fn test_validate_valid_graph() {
        let graph = make_valid_graph();
        let result = validate_einsum_graph(&graph);
        assert!(
            result.is_valid(),
            "well-formed graph should be valid: {:?}",
            result
        );
    }

    // 3
    #[test]
    fn test_validate_duplicate_outputs() {
        let mut graph = make_valid_graph();
        // outputs is [1]; push duplicate
        graph.outputs.push(graph.outputs[0]);
        let result = validate_einsum_graph(&graph);
        assert!(
            result.issues.iter().any(|i| i.code == "duplicate-output"),
            "should detect duplicate outputs"
        );
    }

    // 4
    #[test]
    fn test_validate_result_summary() {
        let graph = EinsumGraph::new();
        let result = validate_einsum_graph(&graph);
        let summary = result.summary();
        assert!(
            summary.contains("errors") && summary.contains("warnings"),
            "summary should mention errors and warnings: {}",
            summary
        );
    }

    // 5
    #[test]
    fn test_validate_error_count() {
        // Build a graph where output has no producer and is not an input.
        let mut graph = EinsumGraph::new();
        let t0 = graph.add_tensor("a");
        let _t1 = graph.add_tensor("b");
        graph.outputs = vec![t0]; // t0 not produced, not in inputs
        let result = validate_einsum_graph(&graph);
        assert!(
            result.error_count() >= 1,
            "should have at least one error for output without producer"
        );
    }

    // 6
    #[test]
    fn test_validate_warning_count() {
        let graph = EinsumGraph::new();
        let result = validate_einsum_graph(&graph);
        assert!(
            result.warning_count() >= 1,
            "empty graph should have at least one warning"
        );
    }

    // 7
    #[test]
    fn test_graph_stats_node_count() {
        let graph = make_valid_graph();
        let stats = compute_graph_stats(&graph);
        assert_eq!(stats.node_count, 1);
    }

    // 8
    #[test]
    fn test_graph_stats_output_count() {
        let graph = make_valid_graph();
        let stats = compute_graph_stats(&graph);
        assert_eq!(stats.output_count, 1);
    }

    // 9
    #[test]
    fn test_sanitize_dedup_outputs() {
        let mut graph = make_valid_graph();
        graph.outputs.push(graph.outputs[0]);
        assert_eq!(graph.outputs.len(), 2);
        let sanitized = sanitize_graph(&graph);
        assert_eq!(sanitized.outputs.len(), 1, "duplicates should be removed");
    }

    // 10
    #[test]
    fn test_sanitize_preserves_valid() {
        let graph = make_valid_graph();
        let sanitized = sanitize_graph(&graph);
        assert_eq!(sanitized.tensors, graph.tensors);
        assert_eq!(sanitized.nodes, graph.nodes);
        assert_eq!(sanitized.outputs, graph.outputs);
        assert_eq!(sanitized.inputs, graph.inputs);
    }

    // 11
    #[test]
    fn test_issue_severity_eq() {
        assert_eq!(IssueSeverity::Error, IssueSeverity::Error);
        assert_eq!(IssueSeverity::Warning, IssueSeverity::Warning);
        assert_eq!(IssueSeverity::Info, IssueSeverity::Info);
        assert_ne!(IssueSeverity::Error, IssueSeverity::Warning);
    }

    // 12
    #[test]
    fn test_validation_result_default() {
        let result = ValidationResult::default();
        assert!(result.issues.is_empty());
        assert!(result.is_valid());
    }

    // 13
    #[test]
    fn test_validation_result_is_valid_no_errors() {
        let mut result = ValidationResult::default();
        result.issues.push(ValidationIssue {
            severity: IssueSeverity::Warning,
            code: "test".to_string(),
            message: "just a warning".to_string(),
            node_index: None,
        });
        assert!(result.is_valid(), "warnings only => valid");
    }

    // 14
    #[test]
    fn test_validation_result_is_valid_with_errors() {
        let mut result = ValidationResult::default();
        result.issues.push(ValidationIssue {
            severity: IssueSeverity::Error,
            code: "test-error".to_string(),
            message: "an error".to_string(),
            node_index: None,
        });
        assert!(!result.is_valid(), "errors => not valid");
    }

    // 15
    #[test]
    fn test_graph_stats_default() {
        let stats = GraphSanitizationStats::default();
        assert_eq!(stats.node_count, 0);
        assert_eq!(stats.output_count, 0);
        assert_eq!(stats.tensor_count, 0);
        assert!(!stats.has_cycles);
        assert_eq!(stats.unreachable_count, 0);
        assert_eq!(stats.max_depth, 0);
    }

    // 16
    #[test]
    fn test_validate_outputs_reference() {
        // Graph outputs reference a tensor that doesn't exist.
        let mut graph = EinsumGraph::new();
        graph.add_tensor("a");
        graph.outputs = vec![999]; // non-existent
        let result = validate_einsum_graph(&graph);
        assert!(
            result
                .issues
                .iter()
                .any(|i| i.code == "invalid-graph-output"),
            "should detect invalid graph output reference"
        );
        assert!(!result.is_valid());
    }

    // 17
    #[test]
    fn test_sanitize_returns_clone() {
        let graph = make_valid_graph();
        let sanitized = sanitize_graph(&graph);
        // Mutating original should not affect sanitized.
        let mut original = graph;
        original.tensors.push("extra".to_string());
        assert_ne!(original.tensors.len(), sanitized.tensors.len());
    }

    // 18
    #[test]
    fn test_compute_stats_empty() {
        let graph = EinsumGraph::new();
        let stats = compute_graph_stats(&graph);
        assert_eq!(stats.node_count, 0);
        assert_eq!(stats.output_count, 0);
        assert_eq!(stats.tensor_count, 0);
        assert!(!stats.has_cycles);
        assert_eq!(stats.unreachable_count, 0);
        assert_eq!(stats.max_depth, 0);
    }
}