From 3fcc8ba58f99a96d48d4a5a701095ed48ce809ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ivar=20H=C3=A4rnqvist?= <ivarhar@outlook.com>
Date: Wed, 8 Apr 2020 16:32:54 +0200
Subject: [PATCH] update sfg tests

---
 test/fixtures/operation_tree.py | 42 +++++++++++++++++++++++++--------
 test/test_sfg.py                | 29 +++++++++++++----------
 2 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/test/fixtures/operation_tree.py b/test/fixtures/operation_tree.py
index 94a1e42f..e5274f02 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 d3daf2e9..91f50ea1 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
-- 
GitLab