-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_network.py
462 lines (351 loc) · 15.8 KB
/
test_network.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""Test the network module."""
from this import d
import pytest
from names import Names
from devices import Devices
from network import Network
@pytest.fixture
def new_network():
"""Return a new instance of the Network class."""
new_names = Names()
new_devices = Devices(new_names)
return Network(new_names, new_devices)
@pytest.fixture
def network_with_devices():
"""Return a Network class instance with three devices in the network."""
new_names = Names()
new_devices = Devices(new_names)
new_network = Network(new_names, new_devices)
[SW1_ID, SW2_ID, OR1_ID] = new_names.lookup(["Sw1", "Sw2", "Or1"])
# Add devices
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)
return new_network
@pytest.fixture
def network_with_connections():
"""Return a Network class instance with a connection in the network."""
new_names = Names()
new_devices = Devices(new_names)
new_network = Network(new_names, new_devices)
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = new_names.lookup(
["Sw1", "Sw2", "Or1", "I1", "I2"]
)
# Add devices
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)
return new_network
def test_get_connected_output(network_with_devices):
"""Test if the output connected to a given input port is correct."""
network = network_with_devices
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = names.lookup(["Sw1", "Sw2", "Or1", "I1",
"I2"])
# Inputs are unconnected, get_connected_output should return None
assert network.get_connected_output(OR1_ID, I1) is None
assert network.get_connected_output(OR1_ID, I2) is None
# Make connections
network.make_connection(SW1_ID, None, OR1_ID, I1)
network.make_connection(SW2_ID, None, OR1_ID, I2)
assert network.get_connected_output(OR1_ID, I1) == (SW1_ID, None)
assert network.get_connected_output(OR1_ID, I2) == (SW2_ID, None)
# Not a valid port for Sw1, get_connected_output should return None
assert network.get_connected_output(SW1_ID, I2) is None
def test_get_input_signal(network_with_devices):
"""Test if the signal at a given input port is correct"""
network = network_with_devices
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = names.lookup(["Sw1", "Sw2", "Or1", "I1",
"I2"])
# Inputs are unconnected, get_input_signal should return None
assert network.get_input_signal(OR1_ID, I1) is None
assert network.get_input_signal(OR1_ID, I2) is None
# Make connections
network.make_connection(SW1_ID, None, OR1_ID, I1)
network.make_connection(SW2_ID, None, OR1_ID, I2)
# Set Sw2 output to HIGH
switch2 = devices.get_device(SW2_ID)
switch2.outputs[None] = devices.HIGH
assert network.get_input_signal(OR1_ID, I1) == devices.LOW
assert network.get_input_signal(OR1_ID, I2) == devices.HIGH
def test_get_output_signal(network_with_devices):
"""Test if the signal level at the given output is correct."""
network = network_with_devices
devices = network.devices
names = devices.names
[OR1_ID] = names.lookup(["Or1"])
assert network.get_output_signal(OR1_ID, None) == devices.LOW
# Set Or1 output to HIGH
or1 = devices.get_device(OR1_ID)
or1.outputs[None] = devices.HIGH
assert network.get_output_signal(OR1_ID, None) == devices.HIGH
def test_check_network(network_with_devices):
"""Test if the signal at a given input port is correct."""
network = network_with_devices
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = names.lookup(["Sw1", "Sw2", "Or1", "I1",
"I2"])
# Inputs are unconnected, check_network() should return False
assert not network.check_network()
# Make connections
network.make_connection(SW1_ID, None, OR1_ID, I1)
network.make_connection(SW2_ID, None, OR1_ID, I2)
# Inputs are now connected, check_network() should return True
assert network.check_network()
def test_make_connection(network_with_devices):
"""Test if the make_connection function correctly connects devices."""
network = network_with_devices
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = names.lookup(["Sw1", "Sw2", "Or1", "I1",
"I2"])
or1 = devices.get_device(OR1_ID)
# or1 inputs are initially unconnected
assert or1.inputs == {I1: None,
I2: None}
# Make connections
network.make_connection(SW1_ID, None, OR1_ID, I1)
network.make_connection(SW2_ID, None, OR1_ID, I2)
# or1 inputs should now be connected
assert or1.inputs == {I1: (SW1_ID, None),
I2: (SW2_ID, None)}
def test_delete_connection(network_with_connections):
"""Test if delete_connection removes connections."""
network = network_with_connections
devices = network.devices
names = devices.names
OR1_ID = names.query("Or1")
SW1_ID = names.query("Sw1")
SW2_ID = names.query("Sw2")
I1 = names.query("I1")
I2 = names.query("I2")
or1 = devices.get_device(OR1_ID)
# or1 inputs should initially be connected
assert or1.inputs == {I1: (SW1_ID, None),
I2: (SW2_ID, None)}
network.delete_connection(OR1_ID, I1)
network.delete_connection(OR1_ID, I2)
# or1 inputs are now unconnected
assert or1.inputs == {I1: None,
I2: None}
@pytest.mark.parametrize("function_args, error", [
# I1 is not a valid device id
("(I1, I1, OR1_ID, I2)", "network.DEVICE_ABSENT"),
("(OR1_ID, I2, OR1_ID, I2)", "network.INPUT_TO_INPUT"),
("(SW1_ID, None, OR1_ID, None)", "network.OUTPUT_TO_OUTPUT"),
# Switch device does not have port I1, so give PORT_ABSENT_ERROR
("(SW1_ID, I1, OR1_ID, I2)", "network.PORT_ABSENT"),
# Output first
("(SW2_ID, None, OR1_ID, I2)", "network.NO_ERROR"),
# Input first
("(OR1_ID, I2, SW2_ID, None)", "network.NO_ERROR"),
# Note: Or1.I1 will have been connected earlier in the function
("(SW1_ID, None, OR1_ID, I1)", "network.INPUT_CONNECTED"),
])
def test_make_connection_gives_error(network_with_devices,
function_args, error):
"""Test if the make_connection function returns the correct errors."""
network = network_with_devices
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = names.lookup(["Sw1", "Sw2", "Or1", "I1",
"I2"])
# Connect Or1.I1 to Sw1
network.make_connection(SW1_ID, None, OR1_ID, I1)
# left_expression is of the form: network.make_connection(...)
left_expression = eval("".join(["network.make_connection", function_args]))
right_expression = eval(error)
assert left_expression == right_expression
@pytest.mark.parametrize("function_args, error", [
# I1 is not a valid device id
("(I1, I2)", "network.DEVICE_ABSENT"),
# Switch device does not have port I1
("(SW1_ID, I1)", "network.PORT_ABSENT"),
# input not yet connected
("(OR1_ID, I1)", "network.CONNECTION_ABSENT"),
])
def test_delete_connection_gives_error(
network_with_connections,
function_args,
error
):
"""Test if the delete_connection returns the correct errors."""
network = network_with_connections
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, OR1_ID, I1, I2] = names.lookup(["Sw1", "Sw2", "Or1", "I1",
"I2"])
# Delete the connection to Or1.I1
network.delete_connection(OR1_ID, I1)
# left_expression is of the form: network.make_connection(...)
left_expression = eval(
"".join([
"network.delete_connection",
function_args
])
)
right_expression = eval(error)
assert left_expression == right_expression
def test_execute_not(new_network):
"""Test if execute_network returns the correct output for NOT gates."""
network = new_network
devices = network.devices
names = devices.names
[SW1_ID, NOT1_ID, I1] = names.lookup(
["Sw1", "Not1", "I1"])
# Make devices
devices.make_device(NOT1_ID, devices.NOT)
devices.make_device(SW1_ID, devices.SWITCH, 0) # Switch set to LOW
# Make connections
network.make_connection(SW1_ID, None, NOT1_ID, I1)
network.execute_network()
assert network.get_output_signal(NOT1_ID, None) == devices.HIGH
# Set Sw1 to HIGH
devices.set_switch(SW1_ID, devices.HIGH)
network.execute_network()
assert network.get_output_signal(NOT1_ID, None) == devices.LOW
def test_execute_xor(new_network):
"""Test if execute_network returns the correct output for XOR gates."""
network = new_network
devices = network.devices
names = devices.names
[SW1_ID, SW2_ID, XOR1_ID, I1, I2] = names.lookup(
["Sw1", "Sw2", "Xor1", "I1", "I2"])
# Make devices
devices.make_device(XOR1_ID, devices.XOR)
devices.make_device(SW1_ID, devices.SWITCH, 0)
devices.make_device(SW2_ID, devices.SWITCH, 0)
# Make connections
network.make_connection(SW1_ID, None, XOR1_ID, I1)
network.make_connection(SW2_ID, None, XOR1_ID, I2)
network.execute_network()
assert new_network.get_output_signal(XOR1_ID, None) == devices.LOW
# Set Sw1 to HIGH
devices.set_switch(SW1_ID, devices.HIGH)
network.execute_network()
assert network.get_output_signal(XOR1_ID, None) == devices.HIGH
# Set Sw2 to HIGH
devices.set_switch(SW2_ID, devices.HIGH)
network.execute_network()
assert network.get_output_signal(XOR1_ID, None) == devices.LOW
@pytest.mark.parametrize("gate_id, switch_outputs, gate_output, gate_kind", [
("AND1_ID", ["LOW", "HIGH", "LOW"], "LOW", "devices.AND"),
("AND1_ID", ["HIGH", "HIGH", "HIGH"], "HIGH", "devices.AND"),
("NAND1_ID", ["HIGH", "HIGH", "HIGH"], "LOW", "devices.NAND"),
("NAND1_ID", ["HIGH", "HIGH", "LOW"], "HIGH", "devices.NAND"),
("OR1_ID", ["LOW", "LOW", "LOW"], "LOW", "devices.OR"),
("OR1_ID", ["LOW", "HIGH", "HIGH"], "HIGH", "devices.OR"),
("NOR1_ID", ["HIGH", "LOW", "HIGH"], "LOW", "devices.NOR"),
("NOR1_ID", ["LOW", "LOW", "LOW"], "HIGH", "devices.NOR"),
])
def test_execute_non_xor_gates(new_network, gate_id, switch_outputs,
gate_output, gate_kind):
"""Test if execute_network returns the correct output for non-XOR gates."""
network = new_network
devices = network.devices
names = devices.names
[AND1_ID, OR1_ID, NAND1_ID, NOR1_ID, SW1_ID, SW2_ID, SW3_ID, I1, I2,
I3] = names.lookup(["And1", "Or1", "Nand1", "Nor1", "Sw1", "Sw2", "Sw3",
"I1", "I2", "I3"])
LOW = devices.LOW
HIGH = devices.HIGH
# Make devices
gate_id = eval(gate_id)
gate_kind = eval(gate_kind)
devices.make_device(gate_id, gate_kind, 3)
devices.make_device(SW1_ID, devices.SWITCH, 0)
devices.make_device(SW2_ID, devices.SWITCH, 0)
devices.make_device(SW3_ID, devices.SWITCH, 0)
# Make connections
network.make_connection(SW1_ID, None, gate_id, I1)
network.make_connection(SW2_ID, None, gate_id, I2)
network.make_connection(SW3_ID, None, gate_id, I3)
# Set switches
switches = [SW1_ID, SW2_ID, SW3_ID]
for i, switch_output in enumerate(switch_outputs):
devices.set_switch(switches[i], eval(switch_output))
network.execute_network()
assert network.get_output_signal(gate_id, None) == eval(gate_output)
def test_execute_non_gates(new_network):
"""Test if execute_network returns the correct output for non-gate devices.
Tests switches, D-types and clocks.
"""
network = new_network
devices = network.devices
names = devices.names
LOW = devices.LOW
HIGH = devices.HIGH
# Make different devices
[SW1_ID, SW2_ID, SW3_ID, CL_ID, D_ID] = names.lookup(["Sw1", "Sw2", "Sw3",
"Clock1", "D1"])
devices.make_device(SW1_ID, devices.SWITCH, 1)
devices.make_device(SW2_ID, devices.SWITCH, 0)
devices.make_device(SW3_ID, devices.SWITCH, 0)
devices.make_device(CL_ID, devices.CLOCK, 1)
devices.make_device(D_ID, devices.D_TYPE)
# Make connections
network.make_connection(SW1_ID, None, D_ID, devices.DATA_ID)
network.make_connection(CL_ID, None, D_ID, devices.CLK_ID)
network.make_connection(SW2_ID, None, D_ID, devices.SET_ID)
network.make_connection(SW3_ID, None, D_ID, devices.CLEAR_ID)
# Get device outputs, the expression is in a string here so that it
# can be re-evaluated again after executing devices
sw1_output = "network.get_output_signal(SW1_ID, None)"
sw2_output = "network.get_output_signal(SW2_ID, None)"
sw3_output = "network.get_output_signal(SW3_ID, None)"
clock_output = "network.get_output_signal(CL_ID, None)"
dtype_Q = "network.get_output_signal(D_ID, devices.Q_ID)"
dtype_QBAR = "network.get_output_signal(D_ID, devices.QBAR_ID)"
# Execute devices until the clock is LOW at the start of its
# period
clock_device = devices.get_device(CL_ID)
network.execute_network()
while clock_device.clock_counter != 1 or eval(clock_output) != LOW:
network.execute_network()
# The clock is not rising yet, Q could be (randomly) HIGH or LOW
assert [eval(sw1_output), eval(sw2_output), eval(sw3_output),
eval(clock_output)] == [HIGH, LOW, LOW, LOW]
assert eval(dtype_Q) in [HIGH, LOW]
assert eval(dtype_QBAR) == network.invert_signal(eval(dtype_Q))
network.execute_network() # the clock has risen
# While sw1(DATA) is high, Q has now changed to HIGH
assert [eval(sw1_output), eval(sw2_output), eval(sw3_output),
eval(clock_output), eval(dtype_Q), eval(dtype_QBAR)] == [
HIGH, LOW, LOW, HIGH, HIGH, LOW]
devices.set_switch(SW1_ID, LOW) # Sw1 is connected to DATA
devices.set_switch(SW2_ID, HIGH) # Sw2 is connected to SET
network.execute_network() # the clock is not rising yet
network.execute_network() # the clock has risen
# Even if sw1(DATA) is LOW, and the clock is rising,
# sw2(SET) is HIGH, so Q is HIGH
assert [eval(sw1_output), eval(sw2_output), eval(sw3_output),
eval(clock_output), eval(dtype_Q), eval(dtype_QBAR)] == [
LOW, HIGH, LOW, HIGH, HIGH, LOW]
devices.set_switch(SW1_ID, HIGH) # Sw1 is connected to DATA
devices.set_switch(SW2_ID, LOW) # Sw2 is connected to SET
devices.set_switch(SW3_ID, HIGH) # Sw3 is connected to CLEAR
network.execute_network() # the clock is not rising yet
network.execute_network() # the clock has risen
# Even if sw1(DATA) is HIGH, and the clock is rising,
# sw3(CLEAR) is HIGH, so Q is LOW
assert [eval(sw1_output), eval(sw2_output), eval(sw3_output),
eval(clock_output), eval(dtype_Q), eval(dtype_QBAR)] == [
HIGH, LOW, HIGH, HIGH, LOW, HIGH]
def test_oscillating_network(new_network):
"""Test if the execute_network returns False for oscillating networks."""
network = new_network
devices = network.devices
names = devices.names
[NOR1, I1] = names.lookup(["Nor1", "I1"])
# Make NOR gate
devices.make_device(NOR1, devices.NOR, 1)
# Connect the NOR gate to itself
network.make_connection(NOR1, None, NOR1, I1)
assert not network.execute_network()