Skip to content

Commit 6abdcac

Browse files
committed
Improved test coverage
Signed-off-by: Marvin Hansen <[email protected]>
1 parent 4714286 commit 6abdcac

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

ultragraph/src/types/storage/graph_csm/graph_csm_algo_topological.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ where
134134
fn topological_sort(&self) -> Result<Option<Vec<usize>>, GraphError> {
135135
let num_nodes = self.number_nodes();
136136
if num_nodes == 0 {
137-
return Ok(Some(Vec::new()));
137+
return Ok(None);
138138
}
139139

140140
// 1. Compute in-degrees for all nodes.

ultragraph/tests/types/ultra_graph/graph_algo_centrality_tests.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ fn test_betweenness_centrality_normalized() {
9898
});
9999
}
100100

101+
#[test]
102+
fn test_betweenness_centrality_directed_normalized() {
103+
let mut g = UltraGraphWeighted::<i32, i32>::new();
104+
g.add_node(0).unwrap();
105+
g.add_node(1).unwrap();
106+
g.add_node(2).unwrap();
107+
g.add_node(3).unwrap();
108+
109+
// 0 -> 1 -> 2
110+
// 0 -> 3 -> 2
111+
g.add_edge(0, 1, 1).unwrap();
112+
g.add_edge(1, 2, 1).unwrap();
113+
g.add_edge(0, 3, 1).unwrap();
114+
g.add_edge(3, 2, 1).unwrap();
115+
g.freeze();
116+
117+
// N = 4
118+
// Directed normalization factor: (N-1)(N-2) = (3)(2) = 6
119+
// Unnormalized BC(1) = 0.5, BC(3) = 0.5
120+
// Normalized BC(1) = 0.5 / 6.0 = 0.0833...
121+
// Normalized BC(3) = 0.5 / 6.0 = 0.0833...
122+
let result = g.betweenness_centrality(true, true).unwrap();
123+
let expected = [(0, 0.0), (1, 0.5 / 6.0), (2, 0.0), (3, 0.5 / 6.0)];
124+
result.iter().for_each(|(node, score)| {
125+
let expected_score = expected.iter().find(|(n, _)| n == node).unwrap().1;
126+
assert!(
127+
(score - expected_score).abs() < 1e-9,
128+
"Node {node}: Expected {expected_score}, Got {score}"
129+
);
130+
});
131+
}
132+
101133
#[test]
102134
fn test_pathway_betweenness_centrality_undirected() {
103135
let mut g = UltraGraphWeighted::<i32, i32>::new();

ultragraph/tests/types/ultra_graph/graph_algo_pathfinder_tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ fn test_shortest_path_len_on_static_graph() {
4646
assert_eq!(g.shortest_path_len(3, 0).unwrap(), None);
4747
}
4848

49+
#[test]
50+
fn test_shortest_path_len_is_one() {
51+
let g = utils_tests::get_acyclic_graph();
52+
let res = g.shortest_path_len(0, 0);
53+
assert!(res.is_ok());
54+
let res = res.unwrap();
55+
assert_eq!(res, Some(1));
56+
}
57+
4958
#[test]
5059
fn test_shortest_path_on_dynamic_graph() {
5160
let mut g = UltraGraph::new();
@@ -57,6 +66,18 @@ fn test_shortest_path_on_dynamic_graph() {
5766
));
5867
}
5968

69+
#[test]
70+
fn test_shortest_path_is_one() {
71+
let mut g = UltraGraph::new();
72+
g.add_node(0).unwrap();
73+
g.freeze();
74+
75+
let res = g.shortest_path(0, 0);
76+
assert!(res.is_ok());
77+
let res = res.unwrap();
78+
assert_eq!(res, Some(vec![0]));
79+
}
80+
6081
#[test]
6182
fn test_shortest_path_on_static_graph() {
6283
let g = utils_tests::get_acyclic_graph();

ultragraph/tests/types/ultra_graph/graph_csm_algo_topological_tests.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
44
*/
55
use ultragraph::utils::utils_tests;
6-
use ultragraph::{GraphError, GraphMut, GraphView, TopologicalGraphAlgorithms, UltraGraph};
6+
use ultragraph::{
7+
GraphError, GraphMut, GraphView, TopologicalGraphAlgorithms, UltraGraph, UltraGraphContainer,
8+
};
79

810
#[test]
911
fn test_find_cycle_on_dynamic_graph() {
@@ -12,6 +14,13 @@ fn test_find_cycle_on_dynamic_graph() {
1214
assert!(matches!(g.find_cycle(), Err(GraphError::GraphNotFrozen)));
1315
}
1416

17+
#[test]
18+
fn test_find_cycle_on_empty_graph() {
19+
let mut g: UltraGraphContainer<u64, ()> = UltraGraph::new();
20+
g.freeze();
21+
assert!(matches!(g.find_cycle(), Ok(None)));
22+
}
23+
1524
#[test]
1625
fn test_find_cycle_on_cyclic_graph() {
1726
let g = utils_tests::get_cyclic_graph();
@@ -71,3 +80,10 @@ fn test_topological_sort_on_acyclic_graph() {
7180
let sorted = g.topological_sort().unwrap().unwrap();
7281
assert_eq!(sorted, vec![0, 1, 2, 3]);
7382
}
83+
84+
#[test]
85+
fn test_topological_sort_on_empty_graph() {
86+
let mut g: UltraGraphContainer<u64, ()> = UltraGraph::new();
87+
g.freeze();
88+
assert!(matches!(g.topological_sort(), Ok(None)));
89+
}

0 commit comments

Comments
 (0)