diff --git a/test/fixtures/operation_tree.py b/test/fixtures/operation_tree.py index 94a1e42f724fdf7f14dbd13debaccc850fbbf552..e5274f02fc40c534593ee8b6cc3deb43d6df5c6a 100644 --- a/test/fixtures/operation_tree.py +++ b/test/fixtures/operation_tree.py @@ -10,21 +10,43 @@ def operation(): @pytest.fixture def operation_tree(): """Return a addition operation connected with 2 constants. - ---C---+ - +--A - ---C---+ + 2>--+ + | + 2+3=5> + | + 3>--+ """ return Addition(Constant(2), Constant(3)) @pytest.fixture def large_operation_tree(): """Return an addition operation connected with a large operation tree with 2 other additions and 4 constants. - ---C---+ - +--A---+ - ---C---+ | - +---A - ---C---+ | - +--A---+ - ---C---+ + 2>--+ + | + 2+3=5>--+ + | | + 3>--+ | + 5+9=14> + 4>--+ | + | | + 4+5=9>--+ + | + 5>--+ """ return Addition(Addition(Constant(2), Constant(3)), Addition(Constant(4), Constant(5))) + +@pytest.fixture +def operation_graph_with_cycle(): + """Return an invalid addition operation connected with an operation graph containing a cycle. + +---+ + | | + ?+7=?>-------+ + | | + 7>--+ ?+6=?> + | + 6 + """ + c1 = Constant(7) + add1 = Addition(None, c1) + add1.input(0).connect(add1) + return Addition(add1, c1) \ No newline at end of file diff --git a/test/test_sfg.py b/test/test_sfg.py index d3daf2e96db0cd87350b148e0969febb2397a0fd..91f50ea159e50ba1d6e90d09444e9aac0188cc28 100644 --- a/test/test_sfg.py +++ b/test/test_sfg.py @@ -1,3 +1,5 @@ +import pytest + from b_asic import SFG from b_asic.signal import Signal from b_asic.core_operations import Addition, Constant @@ -5,28 +7,29 @@ from b_asic.special_operations import Input, Output class TestConstructor: def test_outputs_construction(self, operation_tree): - outp = Output(operation_tree) - sfg = SFG(outputs=[outp]) + sfg = SFG(outputs=[Output(operation_tree)]) assert len(list(sfg.components)) == 7 assert sfg.input_count == 0 assert sfg.output_count == 1 def test_signals_construction(self, operation_tree): - outs = Signal(source=operation_tree.output(0)) - sfg = SFG(output_signals=[outs]) + sfg = SFG(output_signals=[Signal(source=operation_tree.output(0))]) assert len(list(sfg.components)) == 7 assert sfg.input_count == 0 assert sfg.output_count == 1 - def test_operations_construction(self, operation_tree): - sfg1 = SFG(operations=[operation_tree]) - sfg2 = SFG(operations=[operation_tree.input(1).signals[0].source.operation]) + def test_cycle_construction(self, operation_graph_with_cycle): + with pytest.raises(Exception): + SFG(outputs=[Output(operation_graph_with_cycle)]) + + +class TestEvaluation: + def test_evaluate_output(self, operation_tree): + sfg = SFG(outputs=[Output(operation_tree)]) + assert sfg.evaluate_output(0, [])[0] == 5 - assert len(list(sfg1.components)) == 5 - assert len(list(sfg2.components)) == 5 - assert sfg1.input_count == 0 - assert sfg2.input_count == 0 - assert sfg1.output_count == 0 - assert sfg2.output_count == 0 + def test_evaluate_output_large(self, large_operation_tree): + sfg = SFG(outputs=[Output(large_operation_tree)]) + assert sfg.evaluate_output(0, [])[0] == 14 \ No newline at end of file