Skip to content

Commit

Permalink
Test by using assert_almost_equal!
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoBruzzone <[email protected]>
  • Loading branch information
FedericoBruzzone committed Feb 12, 2025
1 parent 1a72203 commit 718a922
Showing 1 changed file with 114 additions and 108 deletions.
222 changes: 114 additions & 108 deletions rustworkx-core/src/centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,14 @@ mod test_newman_weighted_closeness_centrality {
use super::newman_weighted_closeness_centrality;
use petgraph::visit::EdgeRef;

macro_rules! assert_almost_equal {
($x:expr, $y:expr, $d:expr) => {
if ($x - $y).abs() >= $d {
panic!("{} != {} within delta of {}", $x, $y, $d);
}
};
}

#[test]
fn test_weighted_closeness_graph() {
let g = petgraph::graph::UnGraph::<u32, f64>::from_edges([
Expand Down Expand Up @@ -1318,20 +1326,20 @@ mod test_newman_weighted_closeness_centrality {
(6, 7, 0.25),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [
Some(0.0),
Some(1.0),
Some(0.4),
Some(0.176470),
Some(0.0),
Some(1.0),
Some(0.4),
Some(0.176470),
];

assert_eq!(
[
Some(0.0),
Some(1.0),
Some(0.4),
Some(0.17647058823529413),
Some(0.0),
Some(1.0),
Some(0.4),
Some(0.17647058823529413)
],
*c
);
for i in 0..8 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}

#[test]
Expand All @@ -1345,20 +1353,20 @@ mod test_newman_weighted_closeness_centrality {
(6, 7, 0.25),
]);
let c = newman_weighted_closeness_centrality(&g, true, |x| *x.weight());
let result = [
Some(0.0),
Some(0.14285714),
Some(0.11428571),
Some(0.07563025),
Some(0.0),
Some(0.14285714),
Some(0.11428571),
Some(0.07563025),
];

assert_eq!(
[
Some(0.0),
Some(0.14285714285714285),
Some(0.1142857142857143),
Some(0.07563025210084033),
Some(0.0),
Some(0.14285714285714285),
Some(0.1142857142857143),
Some(0.07563025210084033)
],
*c
);
for i in 0..8 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}

#[test]
Expand All @@ -1373,21 +1381,21 @@ mod test_newman_weighted_closeness_centrality {
(7, 8, 0.125),
]);
let c = newman_weighted_closeness_centrality(&g, true, |x| *x.weight());
let result = [
Some(0.0),
Some(0.125),
Some(0.1),
Some(0.06617647),
Some(0.0),
Some(0.125),
Some(0.1),
Some(0.06617647),
Some(0.04081632),
];

assert_eq!(
[
Some(0.0),
Some(0.125),
Some(0.1),
Some(0.0661764705882353),
Some(0.0),
Some(0.125),
Some(0.1),
Some(0.0661764705882353),
Some(0.04081632653061224)
],
*c
);
for i in 0..9 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}

#[test]
Expand All @@ -1398,16 +1406,16 @@ mod test_newman_weighted_closeness_centrality {
(2, 3, 0.5),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [
Some(0.1842105),
Some(0.2234042),
Some(0.2234042),
Some(0.1721311),
];

assert_eq!(
[
Some(0.1842105263157895),
Some(0.22340425531914893),
Some(0.22340425531914893),
Some(0.17213114754098358)
],
*c
);
for i in 0..4 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}
#[test]
fn test_weighted_closeness_small_digraph() {
Expand All @@ -1417,11 +1425,11 @@ mod test_newman_weighted_closeness_centrality {
(2, 3, 0.5),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [Some(0.0), Some(0.7), Some(0.175), Some(0.172131)];

assert_eq!(
[Some(0.0), Some(0.7), Some(0.175), Some(0.17213114754098358),],
*c
);
for i in 0..4 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}

#[test]
Expand All @@ -1437,21 +1445,21 @@ mod test_newman_weighted_closeness_centrality {
(0, 8, 1.0),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [
Some(0.1),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.10256),
];

assert_eq!(
[
Some(0.1),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.10256410256410256)
],
*c
);
for i in 0..9 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}

#[test]
Expand All @@ -1467,21 +1475,21 @@ mod test_newman_weighted_closeness_centrality {
(0, 8, 1.0),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [
Some(0.112676056),
Some(0.056737588),
Some(0.056737588),
Some(0.056737588),
Some(0.056737588),
Some(0.056737588),
Some(0.056737588),
Some(0.056737588),
Some(0.102564102),
];

assert_eq!(
[
Some(0.11267605633802817),
Some(0.05673758865248227),
Some(0.05673758865248227),
Some(0.05673758865248227),
Some(0.05673758865248227),
Some(0.05673758865248227),
Some(0.05673758865248227),
Some(0.05673758865248227),
Some(0.10256410256410256)
],
*c
);
for i in 0..9 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}

#[test]
Expand All @@ -1497,20 +1505,18 @@ mod test_newman_weighted_closeness_centrality {
(1, 7, 1.0),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [
Some(0.1),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(1.0),
];

assert_eq!(
[
Some(0.1),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(1.0)
],
*c
);
assert_eq!(result, *c);
}

#[test]
Expand All @@ -1526,20 +1532,20 @@ mod test_newman_weighted_closeness_centrality {
(8, 7, 1.0),
]);
let c = newman_weighted_closeness_centrality(&g, false, |x| *x.weight());
let result = [
Some(0.098765),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(1.0),
Some(0.0),
];

assert_eq!(
[
Some(0.09876543209876543),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(0.0),
Some(1.0),
Some(0.0)
],
*c
);
for i in 0..9 {
assert_almost_equal!(result[i].unwrap(), c[i].unwrap(), 1e-4);
}
}
}

0 comments on commit 718a922

Please sign in to comment.