Skip to content
Snippets Groups Projects
Commit 6a162f57 authored by Kevin's avatar Kevin
Browse files

Added test cases for printing and simulating the filter used in the first lab for TSTE87.

parent ba2e02d6
No related branches found
No related tags found
2 merge requests!25Resolve "System tests iteration 1",!24Resolve "System tests iteration 1"
Pipeline #13432 failed
...@@ -73,16 +73,16 @@ def sfg_accumulator(): ...@@ -73,16 +73,16 @@ def sfg_accumulator():
@pytest.fixture @pytest.fixture
def simple_filter(): def simple_filter():
"""A valid SFG that is used as a filter in the first lab for TSTE87. """A valid SFG that is used as a filter in the first lab for TSTE87.
+------<const1-----+ +----<constmul1----+
| | | |
| | | |
in1>------add1>------reg>------+------out1> in1>------add1>------reg>------+------out1>
""" """
in1 = Input() in1 = Input()
reg = Register() reg = Register()
const1 = ConstantMultiplication(1) constmul1 = ConstantMultiplication(0.5)
add1 = in1 + const1 add1 = in1 + constmul1
reg.input(0).connect(add1) reg.input(0).connect(add1)
const1.input(0).connect(reg) constmul1.input(0).connect(reg)
out1 = Output(reg) out1 = Output(reg)
return SFG(inputs=[in1], outputs=[out1]) return SFG(inputs=[in1], outputs=[out1])
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
...@@ -43,4 +43,11 @@ class TestPrintSfg: ...@@ -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") 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")
def test_print_simple_filter(self, simple_filter):
\ No newline at end of file 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
...@@ -132,4 +132,10 @@ class TestSimulation: ...@@ -132,4 +132,10 @@ class TestSimulation:
assert output2[0] == 3 assert output2[0] == 3
assert output3[0] == 28 assert output3[0] == 28
assert output4[0] == 0 assert output4[0] == 0
assert output5[0] == 7 assert output5[0] == 7
\ No newline at end of file
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]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment