Skip to content

Commit

Permalink
Added new tests to show reuse after using clear, clear_edges, updated…
Browse files Browse the repository at this point in the history
… docs
  • Loading branch information
abhamra committed Oct 5, 2023
1 parent a6e5ae5 commit 292f8ff
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
---
prelude: >
This is a commit to add the clear and clear_edges functions from StableGraph
to PyGraph and PyDAG.
features:
- |
Added a new function, :func:`clear` that clears all nodes and edges
from the :class:`rustworkx.PyGraph` or :class:`rustworkx.PyDiGraph` object and
sets the `node_removed` flag to `true`.
from a :class:`rustworkx.PyGraph` or :class:`rustworkx.PyDiGraph`
- |
Added a new function, :func:`clear_edges` that clears all edges from the
:class:`rustworkx.PyGraph` or :class:`rustworkx.PyDiGraph` object. Note
that all nodes remain unchanged.
Added a new function, :func:`clear_edges` that clears all edges for
:class:`rustworkx.PyGraph` or :class:`rustworkx.PyDiGraph` without
modifying nodes
29 changes: 28 additions & 1 deletion tests/rustworkx_tests/digraph/test_clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ def test_clear(self):
self.assertEqual(dag.num_edges(), 0)
self.assertEqual(dag.nodes(), [])
self.assertEqual(dag.edges(), [])


def test_clear_reuse(self):
dag = rustworkx.PyDAG()
node_a = dag.add_node("a")
node_b = dag.add_child(node_a, "b", {"a": 1})
node_c = dag.add_child(node_a, "c", {"a": 2})
dag.clear()
node_a = dag.add_node("a")
node_b = dag.add_child(node_a, "b", {"a": 1})
node_c = dag.add_child(node_a, "c", {"a": 2})
self.assertEqual(dag.num_nodes(), 3)
self.assertEqual(dag.num_edges(), 2)
self.assertEqual(dag.nodes(), ["a", "b", "c"])
self.assertEqual(dag.edges(), [{"a": 1}, {"a": 2}])

def test_clear_edges(self):
dag = rustworkx.PyDAG()
Expand All @@ -39,3 +52,17 @@ def test_clear_edges(self):
self.assertEqual(dag.nodes(), ["a", "b", "c"])
self.assertEqual(dag.edges(), [])

def test_clear_edges_reuse(self):
dag = rustworkx.PyDAG()
node_a = dag.add_node("a")
node_b = dag.add_child(node_a, "b", {"a": 1})
node_c = dag.add_child(node_a, "c", {"a": 2})
dag.clear_edges()
dag.add_edge(node_a, node_b, {"a": 1})
dag.add_edge(node_a, node_c, {"a": 2})
self.assertEqual(dag.num_nodes(), 3)
self.assertEqual(dag.num_edges(), 2)
self.assertEqual(dag.nodes(), ["a", "b", "c"])
self.assertEqual(dag.edges(), [{"a": 1}, {"a": 2}])


34 changes: 34 additions & 0 deletions tests/rustworkx_tests/graph/test_clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ def test_clear(self):
self.assertEqual(graph.nodes(), [])
self.assertEqual(graph.edges(), [])

def test_clear_reuse(self):
graph = rustworkx.PyGraph()
node_a = graph.add_node("a")
node_b = graph.add_node("b")
graph.add_edge(node_a, node_b, {"a": 1})
node_c = graph.add_node("c")
graph.add_edge(node_a, node_c, {"a": 2})
graph.clear()
node_a = graph.add_node("a")
node_b = graph.add_node("b")
graph.add_edge(node_a, node_b, {"a": 1})
node_c = graph.add_node("c")
graph.add_edge(node_a, node_c, {"a": 2})
self.assertEqual(graph.num_nodes(), 3)
self.assertEqual(graph.num_edges(), 2)
self.assertEqual(graph.nodes(), ["a", "b", "c"])
self.assertEqual(graph.edges(), [{"a": 1}, {"a": 2}])


def test_clear_edges(self):
graph = rustworkx.PyGraph()
node_a = graph.add_node("a")
Expand All @@ -41,3 +60,18 @@ def test_clear_edges(self):
self.assertEqual(graph.edges(), [])
self.assertEqual(graph.num_nodes(), 3)
self.assertEqual(graph.nodes(), ["a", "b", "c"])

def test_clear_edges_reuse(self):
graph = rustworkx.PyGraph()
node_a = graph.add_node("a")
node_b = graph.add_node("b")
graph.add_edge(node_a, node_b, {"e1", 1})
node_c = graph.add_node("c")
graph.add_edge(node_a, node_c, {"e2", 2})
graph.clear_edges()
graph.add_edge(node_a, node_b, {"e1", 1})
graph.add_edge(node_a, node_c, {"e2", 2})
self.assertEqual(graph.num_nodes(), 3)
self.assertEqual(graph.num_edges(), 2)
self.assertEqual(graph.nodes(), ["a", "b", "c"])
self.assertEqual(graph.edges(), [{"e1", 1}, {"e2", 2}])

0 comments on commit 292f8ff

Please sign in to comment.