|
10 | 10 | // License for the specific language governing permissions and limitations |
11 | 11 | // under the License. |
12 | 12 |
|
| 13 | +use foldhash::fast::RandomState; |
13 | 14 | use foldhash::HashSet; |
14 | 15 | use hashbrown::HashMap; |
| 16 | +use indexmap::IndexSet; |
15 | 17 | use petgraph::data::Build; |
| 18 | +use petgraph::prelude::*; |
16 | 19 | use petgraph::visit::{ |
17 | 20 | Data, EdgeCount, EdgeRef, GraphBase, IntoEdgeReferences, IntoNodeIdentifiers, |
18 | 21 | }; |
19 | 22 | use rustworkx_core::err::ContractError; |
| 23 | +use rustworkx_core::graph_ext::contraction::can_contract; |
20 | 24 | use rustworkx_core::graph_ext::*; |
21 | 25 | use std::convert::Infallible; |
22 | 26 | use std::fmt::Debug; |
@@ -401,3 +405,36 @@ where |
401 | 405 | assert_eq!(dag.node_count(), 1); |
402 | 406 | assert_eq!(dag.edge_count(), 0); |
403 | 407 | } |
| 408 | + |
| 409 | +#[test] |
| 410 | +fn test_can_contract_without_cycle_true() { |
| 411 | + // a -> b -> c (contract b and c, should be allowed) |
| 412 | + let mut graph = StableDiGraph::<&str, ()>::default(); |
| 413 | + let a = graph.add_node("a"); |
| 414 | + let b = graph.add_node("b"); |
| 415 | + let c = graph.add_node("c"); |
| 416 | + graph.add_edge(a, b, ()); |
| 417 | + graph.add_edge(b, c, ()); |
| 418 | + let mut nodes: IndexSet<_, RandomState> = IndexSet::with_hasher(RandomState::default()); |
| 419 | + nodes.insert(b); |
| 420 | + nodes.insert(c); |
| 421 | + |
| 422 | + assert!(can_contract(&graph, &nodes)); |
| 423 | +} |
| 424 | + |
| 425 | +#[test] |
| 426 | +fn test_can_contract_without_cycle_false() { |
| 427 | + // a -> b -> c, c -> a (contract a and c, would create a cycle) |
| 428 | + let mut graph = StableDiGraph::<&str, ()>::default(); |
| 429 | + let a = graph.add_node("a"); |
| 430 | + let b = graph.add_node("b"); |
| 431 | + let c = graph.add_node("c"); |
| 432 | + graph.add_edge(a, b, ()); |
| 433 | + graph.add_edge(b, c, ()); |
| 434 | + graph.add_edge(c, a, ()); |
| 435 | + let mut nodes: IndexSet<_, RandomState> = IndexSet::with_hasher(RandomState::default()); |
| 436 | + nodes.insert(a); |
| 437 | + nodes.insert(c); |
| 438 | + |
| 439 | + assert!(!can_contract(&graph, &nodes)); |
| 440 | +} |
0 commit comments