bounded_graph 0.3.0

A thin newtype wrapper for `petgraph` to assist in the creation of graphs with restrictions on their edges
Documentation
use crate::{BoundedGraph, BoundedGraphError, SymmetricFixedEdgeCount};

#[test]
fn test_fixed_edge_count_basic() {
    let mut graph = BoundedGraph::<SymmetricFixedEdgeCount<2>, ()>::new();
    let n1 = graph.add_node(SymmetricFixedEdgeCount::empty());
    let n2 = graph.add_node(SymmetricFixedEdgeCount::empty());
    let n3 = graph.add_node(SymmetricFixedEdgeCount::empty());

    // Should be able to add 2 edges from n1
    assert!(graph.add_edge(n1, n2, ()).is_ok());
    assert!(graph.add_edge(n1, n3, ()).is_ok());

    // Third edge should fail
    let n4 = graph.add_node(SymmetricFixedEdgeCount::empty());
    let result = graph.add_edge(n1, n4, ());
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        BoundedGraphError::EdgeRejected {
            source_rejected: true,
            target_rejected: false,
            ..
        }
    ));
}

#[test]
fn test_fixed_edge_count_with_data() {
    #[derive(Debug, Clone, PartialEq)]
    struct NodeData {
        name: String,
        value: i32,
    }

    let mut graph = BoundedGraph::<SymmetricFixedEdgeCount<3, NodeData>, i32>::new();
    let n1 = graph.add_node(SymmetricFixedEdgeCount::new(NodeData {
        name: "first".to_string(),
        value: 100,
    }));
    let n2 = graph.add_node(SymmetricFixedEdgeCount::new(NodeData {
        name: "second".to_string(),
        value: 200,
    }));

    assert_eq!(graph[n1].data.name, "first");
    assert_eq!(graph[n1].data.value, 100);
    assert_eq!(graph[n2].data.name, "second");

    assert!(graph.add_edge(n1, n2, 42).is_ok());
    assert_eq!(graph.edge_count(), 1);
}

#[test]
fn test_fixed_edge_count_with_data_deref() {
    #[derive(Debug, Clone, PartialEq)]
    struct NodeData {
        name: String,
        value: i32,
    }

    let mut graph = BoundedGraph::<SymmetricFixedEdgeCount<3, NodeData>, i32>::new();
    let n1 = graph.add_node(SymmetricFixedEdgeCount::new(NodeData {
        name: "first".to_string(),
        value: 100,
    }));
    let n2 = graph.add_node(SymmetricFixedEdgeCount::new(NodeData {
        name: "second".to_string(),
        value: 200,
    }));

    // Test Deref - accessing fields directly
    assert_eq!(graph[n1].name, "first");
    assert_eq!(graph[n1].value, 100);
    assert_eq!(graph[n2].name, "second");

    assert!(graph.add_edge(n1, n2, 42).is_ok());
    assert_eq!(graph.edge_count(), 1);
}

#[test]
fn test_fixed_edge_count_default() {
    let node = SymmetricFixedEdgeCount::<5>::default();
    assert_eq!(node.data, ());

    let node_with_int = SymmetricFixedEdgeCount::<3, i32>::default();
    assert_eq!(node_with_int.data, 0);
}

#[test]
fn test_fixed_edge_count_empty() {
    let node = SymmetricFixedEdgeCount::<2>::empty();
    assert_eq!(node.data, ());

    // Can also use with explicit type
    let node2 = SymmetricFixedEdgeCount::<5>::empty();
    assert_eq!(node2.data, ());
}

#[test]
fn test_fixed_edge_count_zero_capacity() {
    let mut graph = BoundedGraph::<SymmetricFixedEdgeCount<0>, ()>::new();
    let n1 = graph.add_node(SymmetricFixedEdgeCount::empty());
    let n2 = graph.add_node(SymmetricFixedEdgeCount::empty());

    // Should not be able to add any edges
    let result = graph.add_edge(n1, n2, ());
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        BoundedGraphError::EdgeRejected {
            source_rejected: true,
            target_rejected: true,
            ..
        }
    ));
}

#[test]
fn test_fixed_edge_count_from_edges() {
    let edges = vec![(0, 1), (1, 2), (2, 0)];
    let graph = BoundedGraph::<SymmetricFixedEdgeCount<5>, ()>::from_edges(&edges);

    assert_eq!(graph.node_count(), 3);
    assert_eq!(graph.edge_count(), 3);
}

#[test]
fn test_deref_mut() {
    #[derive(Debug, Clone, PartialEq)]
    struct NodeData {
        count: i32,
    }

    let mut graph = BoundedGraph::<SymmetricFixedEdgeCount<3, NodeData>, ()>::new();
    let n1 = graph.add_node(SymmetricFixedEdgeCount::new(NodeData { count: 0 }));

    // Test DerefMut - modifying fields directly
    graph[n1].count += 5;
    assert_eq!(graph[n1].count, 5);

    graph[n1].count *= 2;
    assert_eq!(graph[n1].count, 10);
}