-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* max recursion test * solve one recursion issue * small performance increase * fix RecursionError
- Loading branch information
Showing
3 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import contextlib | ||
import sys | ||
|
||
import pytest | ||
|
||
import znflow | ||
|
||
|
||
class AddOne(znflow.Node): | ||
def __init__(self, x): | ||
super().__init__() | ||
self.x = x | ||
|
||
def run(self): | ||
self.x += 1 | ||
|
||
|
||
@contextlib.contextmanager | ||
def setrecursionlimit(limit: int): | ||
"""Set the recursion limit for the duration of the context manager.""" | ||
_limit = sys.getrecursionlimit() | ||
try: | ||
sys.setrecursionlimit(limit) | ||
yield | ||
finally: | ||
sys.setrecursionlimit(_limit) | ||
|
||
|
||
@pytest.mark.parametrize("depth", [1, 10, 100, 1000]) | ||
def test_AddOneLoop(depth): | ||
with setrecursionlimit(100): | ||
with znflow.DiGraph() as graph: | ||
start = AddOne(0) | ||
for _ in range(depth): | ||
start = AddOne(start.x) | ||
|
||
graph.run() | ||
assert len(graph.nodes) == depth + 1 | ||
assert start.x == depth + 1 | ||
|
||
|
||
@pytest.mark.parametrize("depth", [1, 10, 100, 1000]) | ||
def test_AddOneLoopNoGraph(depth): | ||
with setrecursionlimit(100): | ||
start = AddOne(0) | ||
start.run() | ||
for _ in range(depth): | ||
start = AddOne(start.x) | ||
start.run() | ||
|
||
assert start.x == depth + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters