From 4672f718198e15b9e96d0d41f1436c795779b9bf Mon Sep 17 00:00:00 2001 From: Kevin <Kevin> Date: Fri, 24 Apr 2020 16:34:52 +0200 Subject: [PATCH] changed some tests and desc. of replace operations --- b_asic/signal_flow_graph.py | 11 ++++++----- test/test_sfg.py | 32 +++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/b_asic/signal_flow_graph.py b/b_asic/signal_flow_graph.py index 7643af2d..7cd3972a 100644 --- a/b_asic/signal_flow_graph.py +++ b/b_asic/signal_flow_graph.py @@ -406,14 +406,15 @@ class SFG(AbstractOperation): return self() def replace_operations(self, inputs: Sequence[Input], outputs: Sequence[Output], operation: Operation): - """Replace one or more operations in the sfg with a operation that have same number of inputs and outputs. - Then return a new deepcopy of the sfg. + """Replace multiple operations in the sfg with a operation of equivalent functionallity with the same number of inputs and outputs. + Then return a new deepcopy of the sfg with the replaced operations. Arguments: - inputs: The inputs for the operations we are replacing. - outputs: The outputs for the operations we are replacing. - operation: The replacing operation. + inputs: The inputs of the operations we are replacing. + outputs: The outputs of the operations we are replacing. + operation: The operation used for replacement. """ + return self() def _evaluate_source(self, src: OutputPort, results: MutableResultMap, registers: MutableRegisterMap, prefix: str) -> Number: src_prefix = prefix diff --git a/test/test_sfg.py b/test/test_sfg.py index b1e4f18c..e8879ee6 100644 --- a/test/test_sfg.py +++ b/test/test_sfg.py @@ -1,6 +1,6 @@ import pytest -from b_asic import SFG, Signal, Input, Output, Constant, Addition, Multiplication, MAD +from b_asic import SFG, Signal, Input, Output, Constant, Addition, Subtraction, Multiplication, MAD, ConstantMultiplication, Butterfly class TestInit: @@ -256,20 +256,38 @@ class TestReplaceComponents: assert False class TestReplaceOperations: - def test_replace_mul_add_with_MAD(self): + def test_replace_mul_add_with_mad(self): in1 = Input() in2 = Input() in3 = Input() mul1 = in1 * in2 add1 = mul1 + in3 out1 = Output(add1) - sfg = SFG(inputs=[in1, in2, in3], outputs=[out1]) - assert len(sfg.operations) == 6 mad1 = MAD() - sfg.replace_operations([in1, in2, in3], [out1], mad1) - assert len(sfg.operations) == 5 + + assert mad1 in sfg.operations assert {add1, mul1} not in sfg.operations - \ No newline at end of file + + def test_replace_neg_add_with_sub(self): + in1 = Input() + in2 = Input() + neg = ConstantMultiplication(-1, in1) + add1 = neg + in2 + out1 = Output(add1) + sfg = SFG(inputs=[in1, in2], outputs=[out1]) + + sub1 = Subtraction() + sfg.replace_operations([in1, in2], [out1], sub1) + + assert sub1 in sfg.operations + assert {add1, neg} not in sfg.operations + + def test_not_equal_functionallity(self, operation_tree): + sfg = SFG(outputs=[Output(operation_tree)]) + + mul1 = Multiplication() + with pytest.raises(AssertionError): + sfg.replace_operations([sfg.inputs(0), sfg.inputs(1)], [sfg.outputs(0)], mul1) -- GitLab