-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bits.py
executable file
·118 lines (109 loc) · 3.46 KB
/
test_bits.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
from combinational_circuit import Bit, nand, compile_program, write_test
from single_bit_gates import mux as bit_mux, dmux as bit_dmux
import sys
"""Tests all single-bit functions (several examples, mux, dmux) by writing test files (input, expected output) and
compiling the individual c programs. Tests can only be called one at a time.
Useage: $ ./test_runner.sh
- loops over each of the tests
- calls ./ex.sh <test_name> which calls test_bits.py <test_name>
- test_bits checks test_name against the defined tests and
- writes tests (test-input, expected-output, test-output (blank))
- prints out the C code for that test
- ex.sh redirects stdo into test_name.c, compiles the c program, tests it with the provided inputs,
and runs a diff of the test-output and the expected-output
- test_runner moves onto the next test and repeats until all tests have been checked"""
def ex1():
a = Bit()
b = Bit()
c = Bit()
x = ~a & (b | c)
y = nand(b, x)
test_values = """\
000 01
001 11
010 10
011 10
100 01
101 01
110 01
111 01"""
inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines())
outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines()))
write_test('ex1', inputs, outputs)
compile_program([x, y])
def ex2():
a = Bit()
b = Bit()
test_values = """\
00 0
01 0
10 0
11 1"""
inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines())
outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines()))
write_test('ex2', inputs, outputs)
compile_program([a & b])
def ex3():
a = Bit()
b = Bit()
c = Bit()
x = (a & b) | (~c & nand(b, a))
y = ~x
test_values = """\
111 10
110 10
101 01
100 10
011 01
010 10
001 01
000 10"""
inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines())
outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines()))
write_test('ex3', inputs, outputs)
compile_program([x, y])
def ex4():
a = Bit()
b = Bit()
x = (a & b) ^ (~a)
y = a ^ x
test_values = """\
00 11
01 11
10 01
11 10"""
inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines())
outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines()))
write_test('ex4', inputs, outputs)
compile_program([x, y])
def mux():
a = Bit()
b = Bit()
s = Bit()
test_values = """\
000 0
001 0
010 0
011 1
100 1
101 0
110 1
111 1"""
inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines())
outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines()))
write_test('mux', inputs, outputs)
bit_mux(a, b, s)
def dmux():
a = Bit()
s = Bit()
test_values = """\
00 00
01 00
10 10
11 01"""
inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines())
outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines()))
write_test('dmux', inputs, outputs)
bit_dmux(a, s)
test_dict = {'ex1': ex1, 'ex2': ex2, 'ex3': ex3, 'ex4': ex4, 'mux': mux, 'dmux': dmux}
test_dict[sys.argv[1]]()