Skip to content

Commit

Permalink
feat: (Multi)Portgraph implement Arbitrary
Browse files Browse the repository at this point in the history
  • Loading branch information
lmondada authored and aborgna-q committed Feb 19, 2024
1 parent 1204c41 commit 8b914ef
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,48 @@ fn graph_from_edges<G: PortMut + LinkMut + Default>(edges: &[Edge]) -> G {
g
}

/// Parameters for random portgraph generation using proptest
#[derive(Debug, Clone)]
pub struct GenPortGraphParams {
/// Maximum number of nodes in the graph.
pub max_n_nodes: usize,
/// Maximum number of incoming and outgoing ports per node.
///
/// The maximum applies to incoming and outgoing ports separately. The total
/// maximum number of ports per node is `2 * max_n_ports`.
pub max_n_ports: usize,
/// Maximum number of edges in the graph.
pub max_n_edges: usize,
}

impl Default for GenPortGraphParams {
fn default() -> Self {
Self {
max_n_nodes: 100,
max_n_ports: 4,
max_n_edges: 200,
}
}
}

impl Arbitrary for PortGraph {
type Parameters = GenPortGraphParams;
type Strategy = BoxedStrategy<Self>;

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
gen_portgraph(args.max_n_nodes, args.max_n_ports, args.max_n_edges).boxed()
}
}

impl Arbitrary for MultiPortGraph {
type Parameters = GenPortGraphParams;
type Strategy = BoxedStrategy<Self>;

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
gen_multiportgraph(args.max_n_nodes, args.max_n_ports, args.max_n_edges).boxed()
}
}

fn ensure_non_empty<G: PortView + PortMut>(g: &mut G) {
if g.node_count() == 0 {
g.add_node(0, 0);
Expand Down

0 comments on commit 8b914ef

Please sign in to comment.