Skip to content
Snippets Groups Projects
Commit 8c8db2fe authored by Olle Hansson's avatar Olle Hansson
Browse files

Added two tests and tidied the FromFile code

parent b41601e6
No related branches found
No related tags found
No related merge requests found
...@@ -165,7 +165,7 @@ class ZeroPad(SignalGenerator): ...@@ -165,7 +165,7 @@ class ZeroPad(SignalGenerator):
class FromFile(SignalGenerator): class FromFile(SignalGenerator):
""" """
Signal generator that reads from file and pads a sequence with zeros. Signal generator that reads from file and pads a sequence with zeros.
File should be of type .txt or .csv and contain a single column vector
Parameters Parameters
---------- ----------
path : string path : string
...@@ -173,20 +173,10 @@ class FromFile(SignalGenerator): ...@@ -173,20 +173,10 @@ class FromFile(SignalGenerator):
""" """
def __init__(self, path) -> None: def __init__(self, path) -> None:
try: self._path = path
Path(path).resolve(strict=True) data = np.loadtxt(path, dtype=complex).tolist()
except FileNotFoundError: self._data = data
raise Exception("Selected input file not found.") self._len = len(data)
try:
data = np.loadtxt(path, dtype=complex).tolist()
self._data = data
self._len = len(data)
except ValueError:
raise Exception(
"Selected input file is not of the right format, should be of filetype"
" .txt or .csv and contain single column input vector."
)
def __call__(self, time: int) -> complex: def __call__(self, time: int) -> complex:
if 0 <= time < self._len: if 0 <= time < self._len:
...@@ -194,7 +184,7 @@ class FromFile(SignalGenerator): ...@@ -194,7 +184,7 @@ class FromFile(SignalGenerator):
return 0.0 return 0.0
def __repr__(self) -> str: def __repr__(self) -> str:
return f"FromFile({self._data})" return f"FromFile({self._path!r})"
class Sinusoid(SignalGenerator): class Sinusoid(SignalGenerator):
......
...@@ -148,6 +148,22 @@ def test_help_dialogs(qtbot): ...@@ -148,6 +148,22 @@ def test_help_dialogs(qtbot):
widget.exit_app() widget.exit_app()
def test_simulate(qtbot, datadir):
# Smoke test to open up the "Simulate SFG" and run default simulation
# Should really test all different tests
widget = GUI.MainWindow()
qtbot.addWidget(widget)
widget._load_from_file(datadir.join('twotapfir.py'))
assert 'twotapfir' in widget.sfg_dict
widget.simulate_sfg()
qtbot.wait(100)
widget.dialog.save_properties()
qtbot.wait(100)
widget.dialog.close()
widget.exit_app()
def test_properties_window_smoke_test(qtbot, datadir): def test_properties_window_smoke_test(qtbot, datadir):
# Smoke test to open up the properties window # Smoke test to open up the properties window
# Should really check that the contents are correct and changes works etc # Should really check that the contents are correct and changes works etc
...@@ -159,9 +175,7 @@ def test_properties_window_smoke_test(qtbot, datadir): ...@@ -159,9 +175,7 @@ def test_properties_window_smoke_test(qtbot, datadir):
dragbutton = widget.operationDragDict[op] dragbutton = widget.operationDragDict[op]
dragbutton.show_properties_window() dragbutton.show_properties_window()
assert dragbutton._properties_window.operation == dragbutton assert dragbutton._properties_window.operation == dragbutton
qtbot.mouseClick( qtbot.mouseClick(dragbutton._properties_window.ok, QtCore.Qt.MouseButton.LeftButton)
dragbutton._properties_window.ok, QtCore.Qt.MouseButton.LeftButton
)
widget.exit_app() widget.exit_app()
...@@ -179,9 +193,7 @@ def test_properties_window_change_name(qtbot, datadir): ...@@ -179,9 +193,7 @@ def test_properties_window_change_name(qtbot, datadir):
dragbutton.show_properties_window() dragbutton.show_properties_window()
assert dragbutton._properties_window.edit_name.text() == "cmul2" assert dragbutton._properties_window.edit_name.text() == "cmul2"
dragbutton._properties_window.edit_name.setText("cmul73") dragbutton._properties_window.edit_name.setText("cmul73")
qtbot.mouseClick( qtbot.mouseClick(dragbutton._properties_window.ok, QtCore.Qt.MouseButton.LeftButton)
dragbutton._properties_window.ok, QtCore.Qt.MouseButton.LeftButton
)
dragbutton._properties_window.save_properties() dragbutton._properties_window.save_properties()
assert dragbutton.name == "cmul73" assert dragbutton.name == "cmul73"
assert dragbutton.operation.name == "cmul73" assert dragbutton.operation.name == "cmul73"
......
...@@ -273,12 +273,15 @@ def test_division(): ...@@ -273,12 +273,15 @@ def test_division():
assert isinstance(g, _DivGenerator) assert isinstance(g, _DivGenerator)
def test_fromfile(): def test_fromfile(datadir):
absolute_path = os.path.dirname(__file__) g = FromFile(datadir.join('input.csv'))
relative_path = "../examples/input.csv"
full_path = os.path.join(absolute_path, relative_path)
g = FromFile(full_path)
assert g(-1) == 0.0 assert g(-1) == 0.0
assert g(0) == 0 assert g(0) == 0
assert g(1) == 1 assert g(1) == 1
assert g(2) == 0 assert g(2) == 0
with pytest.raises(FileNotFoundError, match="tset.py not found"):
g = FromFile(datadir.join('tset.py'))
with pytest.raises(ValueError, match="could not convert string"):
g = FromFile(datadir.join('test.py'))
0
1
0
0
0
"""
B-ASIC automatically generated SFG file.
Name: test
Last saved: 2023-02-15 16:45:16.836909.
"""
from b_asic import SFG, Delay, Input, Output, Signal
# Inputs:
in1 = Input(name="")
# Outputs:
out1 = Output(name="")
# Operations:
t1 = Delay(name="")
# Signals:
Signal(source=t1.output(0), destination=out1.input(0))
Signal(source=in1.output(0), destination=t1.input(0))
test = SFG(inputs=[in1], outputs=[out1], name='test')
# SFG Properties:
prop = {'name': test}
positions = {'t1': (114, 38, False), 'in1': (-57, 38, False), 'out1': (304, 38, False)}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment