From 6a162f57a8e501ee5318e6ffef08d5c89c6a254f Mon Sep 17 00:00:00 2001 From: Kevin <Kevin> Date: Mon, 20 Apr 2020 12:33:00 +0200 Subject: [PATCH] Added test cases for printing and simulating the filter used in the first lab for TSTE87. --- test/fixtures/signal_flow_graph.py | 8 +- test/system_test.py | 119 ----------------------------- test/test_print_sfg.py | 9 ++- test/test_simulation.py | 8 +- 4 files changed, 19 insertions(+), 125 deletions(-) delete mode 100644 test/system_test.py diff --git a/test/fixtures/signal_flow_graph.py b/test/fixtures/signal_flow_graph.py index 456a35be..7a8c4a73 100644 --- a/test/fixtures/signal_flow_graph.py +++ b/test/fixtures/signal_flow_graph.py @@ -73,16 +73,16 @@ def sfg_accumulator(): @pytest.fixture def simple_filter(): """A valid SFG that is used as a filter in the first lab for TSTE87. - +------<const1-----+ + +----<constmul1----+ | | | | in1>------add1>------reg>------+------out1> """ in1 = Input() reg = Register() - const1 = ConstantMultiplication(1) - add1 = in1 + const1 + constmul1 = ConstantMultiplication(0.5) + add1 = in1 + constmul1 reg.input(0).connect(add1) - const1.input(0).connect(reg) + constmul1.input(0).connect(reg) out1 = Output(reg) return SFG(inputs=[in1], outputs=[out1]) diff --git a/test/system_test.py b/test/system_test.py deleted file mode 100644 index 3db7536b..00000000 --- a/test/system_test.py +++ /dev/null @@ -1,119 +0,0 @@ -import pytest -import random as rand -import numpy as np -import math -from b_asic import ConstantMultiplication, Input, Output, SFG, Constant, Simulation - -rand.seed(1) - -def test_print_simple_filter(simple_filter): - assert True - -def test_large_connected_constant_multiplication(): - """inp1>----->constmul----->constmul-----> .... ----->constmul----->out1 - """ - my_randoms = [rand.randrange(1,100,1) for _ in range(100)] - - inp1 = Input() - constmul = ConstantMultiplication(my_randoms.pop()) - constmul.input(0).connect(inp1) - - for r in my_randoms: - c = ConstantMultiplication(r, constmul) - constmul = c - - out1 = Output(constmul) - - # Input, Output, signal and operation components - assert len(list(inp1.traverse())) == 203 - - sfg = SFG(inputs=[inp1], outputs=[out1]) - - assert len(list(sfg.operations)) == 100 - -def test_large_connected_constant(): - """inp1>----->const----->const-----> .... ----->const----->out1 - """ - my_randoms = [rand.randrange(1,100,1) for _ in range(100)] - - inp1 = Input() - const = Constant(my_randoms.pop()) - const.input(0).connect(inp1) - - for r in my_randoms: - c = Constant(r) - c.input(0).connect(const) - const = c - - out1 = Output(const) - - # Input, Output, signal and operation components - assert len(list(inp1.traverse())) == 203 - - sfg = SFG(inputs=[inp1], outputs=[out1]) - - assert len(list(sfg.operations)) == 100 - -def test_large_operation_tree(): - """A large addition tree with 100 inputs. - inp1>----+ - | - +----add1>----+ - | | - inp2>----+ | - +-----add51> ... - inp3>----+ | - | | - +----add2>----+ - | - inp4>----+ - - .... - - inp99>---+ - | - +----add50> ... - | - inp100>--+ - """ - num_inputs = 100 - - inputs = [Input() for _ in range(num_inputs)] - operands = inputs - - num_operations = num_inputs - while len(operands) > 1: - additions = [operand1 + operand2 for operand1, operand2 in zip(operands[0::2], operands[1::2])] - operands = additions - num_operations += len(operands) - - output = Output(operands[0], 'out1') - sfg = SFG(inputs=inputs, outputs=[output]) - - assert len(sfg.operations) == num_operations + 1 # am i calculating the number of operations correctly? - - random_inputs = np.array([rand.randrange(1,num_inputs,1) for _ in range(num_inputs)]) - - assert sfg.evaluate_outputs(random_inputs)[0] > sum(random_inputs) # this property should always hold? - - simulate = Simulation(sfg, random_inputs) - output = simulate.run_for(len(random_inputs)) - - assert simulate.results[num_inputs]['out1'] > sum(random_inputs) - -def test_looping_sfg(): - """A valid looping SFG. - +------<constmul2------+ - | | - | | - in1>------add1>----constmul1>------+------out1> - """ - in1 = Input() - constmul1 = ConstantMultiplication(1) - constmul2 = ConstantMultiplication(1) - add1 = in1 + constmul2 - constmul2.input(0).connect(constmul1) - out1 = Output(constmul1) - - return SFG(inputs=[in1], outputs=[out1]) - \ No newline at end of file diff --git a/test/test_print_sfg.py b/test/test_print_sfg.py index 49b0950d..e2e253aa 100644 --- a/test/test_print_sfg.py +++ b/test/test_print_sfg.py @@ -43,4 +43,11 @@ class TestPrintSfg: assert sfg.__str__() == ("id: add1, name: ADD1, input: [s3, s1], output: [s2]\nid: c1, name: CONST, value: 3, input: [], output: [s3]\nid: in1, name: INP1, input: [], output: [s1]\nid: out1, name: OUT1, input: [s2], output: []\n") - \ No newline at end of file + def test_print_simple_filter(self, simple_filter): + assert simple_filter.__str__() == \ + 'id: add1, name: , input: [s1, s3], output: [s4]\n' + \ + 'id: in1, name: , input: [], output: [s1]\n' + \ + 'id: cmul1, name: , input: [s5], output: [s3]\n' + \ + 'id: reg1, name: , input: [s4], output: [s5, s2]\n' + \ + 'id: out1, name: , input: [s2], output: []\n' + \ No newline at end of file diff --git a/test/test_simulation.py b/test/test_simulation.py index faa1f75e..cb0fa79d 100644 --- a/test/test_simulation.py +++ b/test/test_simulation.py @@ -132,4 +132,10 @@ class TestSimulation: assert output2[0] == 3 assert output3[0] == 28 assert output4[0] == 0 - assert output5[0] == 7 \ No newline at end of file + assert output5[0] == 7 + + def test_simulate_simple_filter(self, simple_filter): + input0 = np.array([1, 2, 3, 4, 5]) + simulation = Simulation(simple_filter, [input0], save_results=True) + output0 = [simulation.run()[0] for _ in range(len(input0))] + assert output0 == [0, 1.0, 2.5, 4.25, 6.125] -- GitLab