forked from darius/logsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testarithmetic.py
57 lines (45 loc) · 1.34 KB
/
testarithmetic.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
"""
Some exhaustive tests.
TODO: check add16 using a BDD
"""
import arithmetic as A
import logsim as S
from tabulate import truth_table, wire_values
def test_half_adder():
exhaustive_test(half_adder, A.half_adder, 2)
def half_adder(a, b):
sum2 = a + b
carry, sum = sum2>>1, sum2&1
return sum, carry
def test_full_adder():
exhaustive_test(full_adder, A.full_adder, 3)
def full_adder(a, b, c):
sum2 = a + b + c
carry, sum = sum2>>1, sum2&1
return sum, carry
def test_inc16():
exhaustive_test(inc16, lambda *in_: A.inc16(in_), 16)
def inc16(*in_):
n = sum((1<<i) for i, bit in enumerate(in_) if bit)
result = n+1
return tuple((result>>i)&1 for i in range(len(in_)))
def exhaustive_test(spec, builder, ninputs):
inputs = S.wires(ninputs)
outputs = builder(*inputs)
for values in truth_table('x'*ninputs):
#print values, 'spec', spec(*values), 'circuit', run(inputs, values, outputs)
assert spec(*values) == run(values, outputs, inputs)
def run(values, outputs, inputs):
sim = S.Sim()
for wire, value in zip(inputs, values):
sim.initialize(wire, value)
sim.ticktock()
return tuple(wire_values(outputs))
## test_half_adder()
## test_full_adder()
# test_inc16()
if __name__ == '__main__':
test_half_adder()
test_full_adder()
test_inc16()
#test_add16()