-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_monitors.py
255 lines (194 loc) · 8.71 KB
/
test_monitors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""Test the monitors module."""
import pytest
from names import Names
from network import Network
from devices import Devices
from monitors import Monitors
@pytest.fixture
def new_monitors():
"""Return a Monitors class instance with monitors set on three outputs."""
new_names = Names()
new_devices = Devices(new_names)
new_network = Network(new_names, new_devices)
new_monitors = Monitors(new_names, new_devices, new_network)
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = new_names.lookup(["Sw1", "Sw2", "Or1",
"I1", "I2"])
# Add 2 switches and an OR gate
new_devices.make_device(SW1_ID, new_devices.SWITCH, 0)
new_devices.make_device(SW2_ID, new_devices.SWITCH, 0)
new_devices.make_device(OR1_ID, new_devices.OR, 2)
# Make connections
new_network.make_connection(SW1_ID, None, OR1_ID, I1)
new_network.make_connection(SW2_ID, None, OR1_ID, I2)
# Set monitors
new_monitors.make_monitor(SW1_ID, None)
new_monitors.make_monitor(SW2_ID, None)
new_monitors.make_monitor(OR1_ID, None)
return new_monitors
def test_make_monitor(new_monitors):
"""Test if make_monitor correctly updates the monitors dictionary."""
names = new_monitors.names
[SW1_ID, SW2_ID, OR1_ID] = names.lookup(["Sw1", "Sw2", "Or1"])
assert new_monitors.monitors_dictionary == {(SW1_ID, None): [],
(SW2_ID, None): [],
(OR1_ID, None): []}
def test_make_monitor_gives_errors(new_monitors):
"""Test if make_monitor returns the correct errors."""
names = new_monitors.names
network = new_monitors.network
devices = new_monitors.devices
[SW1_ID, SW3_ID, OR1_ID, I1, SWITCH_ID] = names.lookup(["Sw1", "Sw3",
"Or1", "I1",
"SWITCH"])
assert new_monitors.make_monitor(OR1_ID, I1) == new_monitors.NOT_OUTPUT
assert new_monitors.make_monitor(SW1_ID,
None) == new_monitors.MONITOR_PRESENT
# I1 is not a device_id in the network
assert new_monitors.make_monitor(I1,
None) == network.DEVICE_ABSENT
# Make a new switch device
devices.make_device(SW3_ID, SWITCH_ID, 0)
assert new_monitors.make_monitor(SW3_ID, None) == new_monitors.NO_ERROR
def test_remove_monitor(new_monitors):
"""Test if remove_monitor correctly updates the monitors dictionary."""
names = new_monitors.names
[SW1_ID, SW2_ID, OR1_ID] = names.lookup(["Sw1", "Sw2", "Or1"])
new_monitors.remove_monitor(SW1_ID, None)
assert new_monitors.monitors_dictionary == {(SW2_ID, None): [],
(OR1_ID, None): []}
def test_get_signal_names(new_monitors):
"""Test if get_signal_names returns the correct signal name lists."""
names = new_monitors.names
devices = new_monitors.devices
[D_ID] = names.lookup(["D1"])
# Create a D-type device
devices.make_device(D_ID, devices.D_TYPE)
assert new_monitors.get_signal_names() == [["Sw1", "Sw2", "Or1"],
["D1.Q", "D1.QBAR"]]
def test_record_signals(new_monitors):
"""Test if record_signals records the correct signals."""
names = new_monitors.names
devices = new_monitors.devices
network = new_monitors.network
[SW1_ID, SW2_ID, OR1_ID] = names.lookup(["Sw1", "Sw2", "Or1"])
HIGH = devices.HIGH
LOW = devices.LOW
# Both switches are currently LOW
network.execute_network()
new_monitors.record_signals()
# Set Sw1 to HIGH
devices.set_switch(SW1_ID, HIGH)
network.execute_network()
new_monitors.record_signals()
# Set Sw2 to HIGH
devices.set_switch(SW2_ID, HIGH)
network.execute_network()
new_monitors.record_signals()
assert new_monitors.monitors_dictionary == {
(SW1_ID, None): [LOW, HIGH, HIGH],
(SW2_ID, None): [LOW, LOW, HIGH],
(OR1_ID, None): [LOW, HIGH, HIGH]}
def test_get_margin(new_monitors):
"""Test if get_margin returns the length of the longest monitor name."""
names = new_monitors.names
devices = new_monitors.devices
[D_ID, DTYPE_ID, QBAR_ID, Q_ID] = names.lookup(["Dtype1", "DTYPE",
"QBAR", "Q"])
# Create a D-type device and set monitors on its outputs
devices.make_device(D_ID, DTYPE_ID)
new_monitors.make_monitor(D_ID, QBAR_ID)
new_monitors.make_monitor(D_ID, Q_ID)
# Longest name should be Dtype1.QBAR
assert new_monitors.get_margin() == 11
def test_reset_monitors(new_monitors):
"""Test if reset_monitors clears the signal lists of all the monitors."""
names = new_monitors.names
devices = new_monitors.devices
[SW1_ID, SW2_ID, OR1_ID] = names.lookup(["Sw1", "Sw2", "Or1"])
LOW = devices.LOW
new_monitors.record_signals()
new_monitors.record_signals()
assert new_monitors.monitors_dictionary == {(SW1_ID, None): [LOW, LOW],
(SW2_ID, None): [LOW, LOW],
(OR1_ID, None): [LOW, LOW]}
new_monitors.reset_monitors()
assert new_monitors.monitors_dictionary == {(SW1_ID, None): [],
(SW2_ID, None): [],
(OR1_ID, None): []}
def test_display_signals_console(capsys, new_monitors):
"""Test if signal traces are displayed correctly on the console."""
names = new_monitors.names
devices = new_monitors.devices
network = new_monitors.network
[SW1_ID, CLOCK_ID, CL_ID] = names.lookup(["Sw1", "CLOCK", "Clock1"])
HIGH = devices.HIGH
# Make a clock and set a monitor on its output
devices.make_device(CL_ID, CLOCK_ID, 2)
new_monitors.make_monitor(CL_ID, None)
# Both switches are currently LOW
for _ in range(10):
network.execute_network()
new_monitors.record_signals()
# Set Sw1 to HIGH
devices.set_switch(SW1_ID, HIGH)
for _ in range(10):
network.execute_network()
new_monitors.record_signals()
new_monitors.display_signals_console()
# Get std_output
out, _ = capsys.readouterr()
traces = out.split("\n")
assert len(traces) == 5
assert "Sw1 : __________----------" in traces
assert "Sw2 : ____________________" in traces
assert "Or1 : __________----------" in traces
# Clock could be anywhere in its cycle, but its half period is 2
assert ("Clock1: __--__--__--__--__--" in traces or
"Clock1: _--__--__--__--__--_" in traces or
"Clock1: --__--__--__--__--__" in traces or
"Clock1: -__--__--__--__--__-" in traces)
assert "" in traces # additional empty line at the end
def test_display_signals_gui(new_monitors):
"""Test if signal trace data is correct for GUI display."""
names = new_monitors.names
devices = new_monitors.devices
network = new_monitors.network
[SW1_ID, CLOCK_ID, CL_ID] = names.lookup(["Sw1", "CLOCK", "Clock1"])
HIGH = devices.HIGH
# Make a clock and set a monitor on its output
devices.make_device(CL_ID, CLOCK_ID, 2)
new_monitors.make_monitor(CL_ID, None)
# Both switches are currently LOW
for _ in range(10):
network.execute_network()
new_monitors.record_signals()
# Set Sw1 to HIGH
devices.set_switch(SW1_ID, HIGH)
for _ in range(10):
network.execute_network()
new_monitors.record_signals()
# Set arbitrary high/low axis positions
high, low = 25, 5
# Get signal data for 40 points
signalData = new_monitors.display_signals_gui(high, low)
assert list(signalData.keys()) == [
'Sw1', 'Sw2', 'Or1', 'Clock1'
]
# Check all x coordinates are the same for every device
for device in signalData.keys():
assert signalData[device][1] == [
0, 20, 20, 40, 40, 60, 60, 80, 80, 100, 100,
120, 120, 140, 140, 160, 160, 180, 180, 200,
200, 220, 220, 240, 240, 260, 260, 280, 280,
300, 300, 320, 320, 340, 340, 360, 360, 380,
380, 400
]
# Assert y coordinates are all correct
assert signalData['Sw1'][2] == [low]*20 + [high]*20
assert signalData['Sw2'][2] == [low]*40
assert signalData['Or1'][2] == [low]*20 + [high]*20
# Clock could be anywhere in cycle, but half period is 2
assert (signalData['Clock1'][2] == [[low]*4 + [high]*4] * 5 or
[[low]*2 + [high]*4 + [low]*2] * 5 or
[[high]*4 + [low]*4] * 5 or
[[high]*2 + [low]*4 + [high]*2] * 5)