#![deny(unsafe_code, nonstandard_style)]
#![forbid(rust_2018_idioms)]
use hypergraph::Hypergraph;
#[test]
fn integration() {
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
struct Vertex<'a> {
name: &'a str,
}
impl<'a> Vertex<'a> {
pub fn new(name: &'a str) -> Self {
Vertex { name }
}
}
let mut graph = Hypergraph::<Vertex<'_>, &str>::new();
let vertex_a = Vertex::new("a");
let vertex_b = Vertex::new("b");
let vertex_c = Vertex::new("c");
let vertex_d = Vertex::new("d");
let vertex_e = Vertex::new("e");
assert_eq!(graph.add_vertex(vertex_a), 0);
assert_eq!(graph.add_vertex(vertex_b), 1);
assert_eq!(graph.add_vertex(vertex_c), 2);
assert_eq!(graph.add_vertex(vertex_d), 3);
assert_eq!(graph.add_vertex(vertex_e), 4);
assert_eq!(graph.add_vertex(vertex_e), 4);
assert_eq!(graph.add_hyperedge(&[0, 1, 1, 3], "foo"), Some([0, 0])); assert_eq!(graph.add_hyperedge(&[0, 1, 1, 3], "foo_"), Some([0, 1]));
assert_eq!(graph.add_hyperedge(&[4, 0, 3, 2], "bar"), Some([1, 0]));
assert_eq!(graph.add_hyperedge(&[3], "woot"), Some([2, 0])); assert_eq!(graph.add_hyperedge(&[3], "woot"), Some([2, 0])); assert_eq!(graph.add_hyperedge(&[3], "leet"), Some([2, 1])); assert_eq!(graph.add_hyperedge(&[9], "nope"), None);
assert_eq!(graph.count_vertices(), 5);
assert_eq!(graph.count_hyperedges(), 5);
assert_eq!(graph.get_vertex_weight(0), Some(&vertex_a));
assert_eq!(graph.get_vertex_weight(4), Some(&vertex_e));
assert_eq!(graph.get_vertex_weight(5), None); assert_eq!(graph.get_hyperedge_weight([0, 0]), Some(&"foo"));
assert_eq!(graph.get_hyperedge_weight([2, 1]), Some(&"leet"));
assert_eq!(graph.get_hyperedge_weight([3, 0]), None);
assert_eq!(graph.get_hyperedge_vertices(0), Some(vec![0, 1, 1, 3]));
assert_eq!(graph.get_hyperedge_vertices(3), None);
assert_eq!(
graph.get_vertex_hyperedges(0),
Some(vec![vec![0, 1, 1, 3], vec![4, 0, 3, 2]])
);
assert_eq!(graph.get_vertex_hyperedges(1), Some(vec![vec![0, 1, 1, 3]]));
assert_eq!(graph.get_vertex_hyperedges(2), Some(vec![vec![4, 0, 3, 2]]));
assert_eq!(
graph.get_vertex_hyperedges(3),
Some(vec![vec![0, 1, 1, 3], vec![4, 0, 3, 2], vec![3]])
);
assert_eq!(graph.get_vertex_hyperedges(4), Some(vec![vec![4, 0, 3, 2]]));
assert_eq!(graph.get_hyperedges_intersections(&[0, 1]), vec![0, 3]);
assert_eq!(graph.get_hyperedges_intersections(&[0, 1, 2]), vec![3]);
assert_eq!(graph.get_hyperedges_intersections(&[0]), vec![0, 1, 3]);
assert_eq!(
graph.get_hyperedges_intersections(&[3]), vec![]
);
assert_eq!(graph.get_hyperedges_connections(1, 1), vec![0]);
assert_eq!(graph.get_hyperedges_connections(3, 2), vec![1]);
assert_eq!(graph.get_hyperedges_connections(3, 0), vec![]);
assert_eq!(graph.get_vertex_connections(0), vec![1, 3]);
assert_eq!(graph.get_vertex_connections(1), vec![1, 3]);
assert_eq!(graph.get_vertex_connections(2), vec![]);
assert_eq!(graph.get_vertex_connections(3), vec![2]);
assert_eq!(
graph.get_dijkstra_connections(4, 2),
Some(vec![4, 0, 3, 2,])
);
assert_eq!(graph.get_dijkstra_connections(0, 3), Some(vec![0, 3,]));
assert_eq!(graph.get_dijkstra_connections(0, 4), None);
assert_eq!(graph.get_dijkstra_connections(1, 1), Some(vec![1,]));
assert_eq!(graph.get_dijkstra_connections(3, 3), Some(vec![3,]));
let vertex_a = Vertex::new("brand new heavies");
assert!(graph.update_vertex_weight(0, vertex_a));
assert_eq!(graph.get_vertex_weight(0), Some(&vertex_a));
assert!(graph.update_hyperedge_weight([0, 0], "yup"));
assert_eq!(graph.get_hyperedge_weight([0, 0]), Some(&"yup"));
assert_eq!(
graph.count_vertices(),
5,
"Number of vertices should remain the same."
);
assert_eq!(
graph.count_hyperedges(),
5,
"Number of hyperedges should remain the same."
);
assert!(graph.update_hyperedge_vertices(0, &[0, 4]));
assert_eq!(graph.get_hyperedge_vertices(0), Some(vec![0, 4]));
assert_eq!(
graph.get_vertex_hyperedges(0),
Some(vec![vec![0, 4], vec![4, 0, 3, 2]])
);
assert_eq!(graph.get_vertex_hyperedges(1), Some(vec![]));
assert_eq!(graph.get_vertex_hyperedges(2), Some(vec![vec![4, 0, 3, 2]]));
assert_eq!(
graph.get_vertex_hyperedges(3),
Some(vec![vec![4, 0, 3, 2], vec![3]])
);
assert_eq!(
graph.get_vertex_hyperedges(4),
Some(vec![vec![4, 0, 3, 2], vec![0, 4]])
);
assert!(graph.remove_vertex(4));
assert_eq!(graph.get_hyperedge_vertices(0), Some(vec![0])); assert_eq!(graph.get_hyperedge_vertices(1), Some(vec![0, 3, 2])); assert_eq!(graph.get_vertex_weight(4), None);
assert_eq!(graph.count_vertices(), 4);
assert_eq!(graph.get_vertex_hyperedges(2), Some(vec![vec![0, 3, 2]]));
assert!(graph.remove_vertex(0));
assert_eq!(graph.get_hyperedge_vertices(0), Some(vec![])); assert_eq!(graph.get_hyperedge_vertices(1), Some(vec![0, 2])); assert_eq!(graph.get_hyperedge_vertices(2), Some(vec![0])); assert_eq!(graph.get_vertex_weight(3), None); assert_eq!(graph.get_vertex_weight(0), Some(&vertex_d));
assert_eq!(graph.add_hyperedge(&[0], "last"), Some([2, 2])); assert!(graph.remove_hyperedge([2, 2]));
assert_eq!(graph.get_hyperedge_weight([2, 0]), Some(&"woot")); assert_eq!(graph.get_hyperedge_weight([2, 1]), Some(&"leet")); assert_eq!(graph.get_hyperedge_weight([2, 2]), None);
assert!(graph.remove_hyperedge([2, 0]));
assert_eq!(graph.get_hyperedge_weight([2, 0]), Some(&"leet")); assert_eq!(graph.get_hyperedge_weight([2, 1]), None);
assert_eq!(graph.get_hyperedge_vertices(2), Some(vec![0]));
assert_eq!(graph.get_vertex_hyperedges(0), Some(vec![vec![0]]));
assert!(graph.remove_hyperedge([2, 0]));
assert_eq!(graph.get_hyperedge_weight([2, 0]), None); assert_eq!(graph.get_vertex_hyperedges(0), Some(vec![]));
graph.render_to_graphviz_dot();
}