111 lines
3.4 KiB
Rust
111 lines
3.4 KiB
Rust
use approx::assert_relative_eq;
|
|
use rtx_geom::{Edge, EdgeId, GeomError, Graph, Node, NodeId};
|
|
use rtx_tensor::{Device, Tensor};
|
|
|
|
#[test]
|
|
fn test_empty_graph_creation() {
|
|
let graph = Graph::new();
|
|
assert_eq!(graph.node_count(), 0);
|
|
assert_eq!(graph.edge_count(), 0);
|
|
assert!(graph.nodes().is_empty());
|
|
assert!(graph.edges().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn test_add_node_with_features() {
|
|
let mut graph = Graph::new();
|
|
let device = Device::cpu();
|
|
let features = Tensor::zeros([3], &device).unwrap(); // 3-dimensional node features
|
|
let node_id = graph.add_node(Node::new(features.clone()));
|
|
|
|
assert_eq!(graph.node_count(), 1);
|
|
assert_eq!(graph.edge_count(), 0);
|
|
|
|
let node = graph.node(node_id).unwrap();
|
|
assert_eq!(node.features().shape(), features.shape());
|
|
}
|
|
|
|
#[test]
|
|
fn test_add_edge_between_nodes() {
|
|
let mut graph = Graph::new();
|
|
|
|
// Add two nodes
|
|
let device = Device::cpu();
|
|
let features1 = Tensor::zeros([3], &device).unwrap();
|
|
let features2 = Tensor::ones([3], &device).unwrap();
|
|
let node1 = graph.add_node(Node::new(features1));
|
|
let node2 = graph.add_node(Node::new(features2));
|
|
|
|
// Add edge between them
|
|
let edge_weight = Tensor::from_data(vec![0.5], [1], &device).unwrap();
|
|
let edge_id = graph
|
|
.add_edge(node1, node2, Edge::new(edge_weight.clone()))
|
|
.unwrap();
|
|
|
|
assert_eq!(graph.node_count(), 2);
|
|
assert_eq!(graph.edge_count(), 1);
|
|
|
|
let edge = graph.edge(edge_id).unwrap();
|
|
assert_relative_eq!(edge.weight().to_cpu().unwrap()[0], 0.5, epsilon = 1e-6);
|
|
|
|
// Test connectivity
|
|
let neighbors = graph.neighbors(node1);
|
|
assert_eq!(neighbors.len(), 1);
|
|
assert_eq!(neighbors[0], node2);
|
|
}
|
|
|
|
#[test]
|
|
fn test_graph_adjacency_matrix() {
|
|
let mut graph = Graph::new();
|
|
|
|
// Create a small graph: 0 -> 1 -> 2
|
|
let device = Device::cpu();
|
|
let node0 = graph.add_node(Node::new(Tensor::zeros([2], &device).unwrap()));
|
|
let node1 = graph.add_node(Node::new(Tensor::zeros([2], &device).unwrap()));
|
|
let node2 = graph.add_node(Node::new(Tensor::zeros([2], &device).unwrap()));
|
|
|
|
graph
|
|
.add_edge(
|
|
node0,
|
|
node1,
|
|
Edge::new(Tensor::from_data(vec![1.0], [1], &device).unwrap()),
|
|
)
|
|
.unwrap();
|
|
graph
|
|
.add_edge(
|
|
node1,
|
|
node2,
|
|
Edge::new(Tensor::from_data(vec![2.0], [1], &device).unwrap()),
|
|
)
|
|
.unwrap();
|
|
|
|
let adj_matrix = graph.adjacency_matrix();
|
|
assert_eq!(adj_matrix.shape().dims(), &[3, 3]);
|
|
|
|
// Check specific connections
|
|
let adj_data = adj_matrix.to_cpu().unwrap();
|
|
assert_relative_eq!(adj_data[0 * 3 + 1], 1.0, epsilon = 1e-6); // 0 -> 1
|
|
assert_relative_eq!(adj_data[1 * 3 + 2], 2.0, epsilon = 1e-6); // 1 -> 2
|
|
assert_relative_eq!(adj_data[0 * 3 + 2], 0.0, epsilon = 1e-6); // no direct 0 -> 2
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_edge_creation() {
|
|
let mut graph = Graph::new();
|
|
let device = Device::cpu();
|
|
let node1 = graph.add_node(Node::new(Tensor::zeros([2], &device).unwrap()));
|
|
|
|
// Try to create edge to non-existent node
|
|
let fake_node = NodeId::from_raw(999);
|
|
let result = graph.add_edge(
|
|
node1,
|
|
fake_node,
|
|
Edge::new(Tensor::from_data(vec![1.0], [1], &device).unwrap()),
|
|
);
|
|
|
|
match result {
|
|
Err(GeomError::InvalidNodeId(_)) => {} // Expected
|
|
_ => panic!("Expected InvalidNodeId error"),
|
|
}
|
|
}
|