-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadder_test.go
138 lines (128 loc) · 5.6 KB
/
adder_test.go
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
package main
import "testing"
func TestSimpleAdder(t *testing.T) {
tt := []struct {
input1 NodeState
input2 NodeState
expectedOut NodeState
expectedCarry NodeState
}{
{input1: Off, input2: Off, expectedOut: Off, expectedCarry: Off},
{input1: On, input2: Off, expectedOut: On, expectedCarry: Off},
{input1: Off, input2: On, expectedOut: On, expectedCarry: Off},
{input1: On, input2: On, expectedOut: Off, expectedCarry: On},
}
for _, tc := range tt {
components := []Component{}
input1 := NewNode("Input1")
input2 := NewNode("Input2")
inputComponents := []Component{
NewInput(input1, tc.input1),
NewInput(input2, tc.input2),
}
components = append(components, inputComponents...)
adderOut, adderCarry, adder := NewSimpleAdder(input1, input2)
components = append(components, adder)
c := NewCircuit(components, 4, false)
if err := c.Simulate(); err != nil {
t.Errorf(err.Error())
}
if adderOut.State != tc.expectedOut || adderCarry.State != tc.expectedCarry {
t.Errorf("Inputs<input1: %s, input2: %s> generated output state <out: %s, carry: %s> instead of <out: %s, carry: %s>",
tc.input1, tc.input2, adderOut.State, adderCarry.State, tc.expectedOut, tc.expectedCarry)
}
}
}
func TestFullAdder(t *testing.T) {
tt := []struct {
input1 NodeState
input2 NodeState
carryIn NodeState
expectedOut NodeState
expectedCarry NodeState
}{
{input1: Off, input2: Off, carryIn: Off, expectedOut: Off, expectedCarry: Off},
{input1: On, input2: Off, carryIn: Off, expectedOut: On, expectedCarry: Off},
{input1: Off, input2: On, carryIn: Off, expectedOut: On, expectedCarry: Off},
{input1: On, input2: On, carryIn: Off, expectedOut: Off, expectedCarry: On},
{input1: Off, input2: Off, carryIn: On, expectedOut: On, expectedCarry: Off},
{input1: On, input2: Off, carryIn: On, expectedOut: Off, expectedCarry: On},
{input1: Off, input2: On, carryIn: On, expectedOut: Off, expectedCarry: On},
{input1: On, input2: On, carryIn: On, expectedOut: On, expectedCarry: On},
}
for _, tc := range tt {
components := []Component{}
input1 := NewNode("Input1")
input2 := NewNode("Input2")
carryIn := NewNode("CarryIn")
inputComponents := []Component{
NewInput(input1, tc.input1),
NewInput(input2, tc.input2),
NewInput(carryIn, tc.carryIn),
}
components = append(components, inputComponents...)
adderOut, adderCarry, adder := NewFullAdder(input1, input2, carryIn)
components = append(components, adder)
c := NewCircuit(components, 4, false)
if err := c.Simulate(); err != nil {
t.Errorf(err.Error())
}
if adderOut.State != tc.expectedOut || adderCarry.State != tc.expectedCarry {
t.Errorf("Inputs<input1: %s, input2: %s> generated output state <out: %s, carry: %s> instead of <out: %s, carry: %s>",
tc.input1, tc.input2, adderOut.State, adderCarry.State, tc.expectedOut, tc.expectedCarry)
}
}
}
func TestAdderSubtractor(t *testing.T) {
tt := []struct {
input1 NodeState
input2 NodeState
carryIn NodeState
operation NodeState
expectedOut NodeState
expectedCarry NodeState
}{
// addition
{input1: Off, input2: Off, carryIn: Off, operation: Off, expectedOut: Off, expectedCarry: Off},
{input1: On, input2: Off, carryIn: Off, operation: Off, expectedOut: On, expectedCarry: Off},
{input1: Off, input2: On, carryIn: Off, operation: Off, expectedOut: On, expectedCarry: Off},
{input1: On, input2: On, carryIn: Off, operation: Off, expectedOut: Off, expectedCarry: On},
{input1: Off, input2: Off, carryIn: On, operation: Off, expectedOut: On, expectedCarry: Off},
{input1: On, input2: Off, carryIn: On, operation: Off, expectedOut: Off, expectedCarry: On},
{input1: Off, input2: On, carryIn: On, operation: Off, expectedOut: Off, expectedCarry: On},
{input1: On, input2: On, carryIn: On, operation: Off, expectedOut: On, expectedCarry: On},
// subtraction
{input1: Off, input2: Off, carryIn: Off, operation: On, expectedOut: On, expectedCarry: Off},
{input1: On, input2: Off, carryIn: Off, operation: On, expectedOut: Off, expectedCarry: On},
{input1: Off, input2: On, carryIn: Off, operation: On, expectedOut: Off, expectedCarry: Off},
{input1: On, input2: On, carryIn: Off, operation: On, expectedOut: On, expectedCarry: Off},
{input1: Off, input2: Off, carryIn: On, operation: On, expectedOut: Off, expectedCarry: On},
{input1: On, input2: Off, carryIn: On, operation: On, expectedOut: On, expectedCarry: On},
{input1: Off, input2: On, carryIn: On, operation: On, expectedOut: On, expectedCarry: Off},
{input1: On, input2: On, carryIn: On, operation: On, expectedOut: Off, expectedCarry: On},
}
for _, tc := range tt {
components := []Component{}
input1 := NewNode("Input1")
input2 := NewNode("Input2")
carryIn := NewNode("CarryIn")
operation := NewNode("Operation")
inputComponents := []Component{
NewInput(input1, tc.input1),
NewInput(input2, tc.input2),
NewInput(carryIn, tc.carryIn),
NewInput(operation, tc.operation),
}
components = append(components, inputComponents...)
adderOut, adderCarry, adder := NewAdderSubtractor(input1, input2, carryIn, operation)
components = append(components, adder)
c := NewCircuit(components, 4, false)
if err := c.Simulate(); err != nil {
t.Errorf(err.Error())
}
if adderOut.State != tc.expectedOut || adderCarry.State != tc.expectedCarry {
t.Errorf("Inputs<input1: %s, input2: %s, carryIn: %s, operation: %s> generated output state <out: %s, carry: %s> instead of <out: %s, carry: %s>",
tc.input1, tc.input2, carryIn.State, operation.State, adderOut.State, adderCarry.State, tc.expectedOut, tc.expectedCarry)
}
}
}