-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu_test.go
156 lines (150 loc) · 4.5 KB
/
alu_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"math/bits"
"math/rand"
"testing"
"time"
)
func TestHalfAdder(t *testing.T) {
for _, tt := range []struct{
a uint16
b uint16
sum uint16
carry uint16
}{
{a:0, b:0, carry:0, sum:0},
{a:0, b:1, carry:0, sum:1},
{a:1, b:0, carry:0, sum:1},
{a:1, b:1, carry:1, sum:0},
}{
gotCarry, gotSum := HalfAdder(toBit(tt.a), toBit(tt.b))
if gotCarry != toBit(tt.carry) {
t.Errorf("gotCarry %t but want %t", gotCarry, toBit(tt.carry))
}
if gotSum != toBit(tt.sum) {
t.Errorf("gotSum %t but want %t", gotSum, toBit(tt.sum))
}
}
}
func TestFullAdder(t *testing.T) {
for _, tt := range []struct{
a uint16
b uint16
c uint16
sum uint16
carry uint16
}{
{a:0, b:0, c:0, carry:0, sum:0},
{a:0, b:0, c:1, carry:0, sum:1},
{a:0, b:1, c:0, carry:0, sum:1},
{a:0, b:1, c:1, carry:1, sum:0},
{a:1, b:0, c:0, carry:0, sum:1},
{a:1, b:0, c:1, carry:1, sum:0},
{a:1, b:1, c:0, carry:1, sum:0},
{a:1, b:1, c:1, carry:1, sum:1},
}{
gotCarry, gotSum := FullAdder(toBit(tt.a), toBit(tt.b), toBit(tt.c))
if gotCarry != toBit(tt.carry) {
t.Errorf("gotCarry %t but want %t", gotCarry, toBit(tt.carry))
}
if gotSum != toBit(tt.sum) {
t.Errorf("gotSum %t but want %t", gotSum, toBit(tt.sum))
}
}
}
// fuzz testing with ten random inputs
func TestAdd16(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for i:=0; i<10; i++ {
a, b := uint16(rand.Intn(65536)), uint16(rand.Intn(65536))
out := Add16(toBit16(a), toBit16(b))
got := fromBit16(out)
want := a + b
if got != want {
t.Errorf("expected %d but got %d", want, got)
}
}
}
func TestAlu(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for _, tt := range []struct{
c [6]uint16 // controls: zx, nx, zy, ny, f, no
wantFunc func(a, b uint16) int16
}{
{
c: [6]uint16{1,0,1,0,1,0},
wantFunc: func(a, b uint16) int16 { return 0 },
},
{
c: [6]uint16{1,1,1,1,1,1},
wantFunc: func(a, b uint16) int16 { return 1 },
},
{
c: [6]uint16{1,1,1,0,1,0},
wantFunc: func(a, b uint16) int16 { return -1 },
},
{
c: [6]uint16{0,0,1,1,0,0},
wantFunc: func(a, b uint16) int16 { return int16(a) },
},
{
c: [6]uint16{1,1,0,0,0,0},
wantFunc: func(a, b uint16) int16 { return int16(b) },
},
{
c: [6]uint16{0,0,1,1,1,1},
wantFunc: func(a, b uint16) int16 { return int16(-a) },
},
{
c: [6]uint16{1,1,0,0,1,1},
wantFunc: func(a, b uint16) int16 { return int16(-b) },
},
{
c: [6]uint16{0,0,0,0,1,0},
wantFunc: func(a, b uint16) int16 { return int16(a + b) },
},
{
c: [6]uint16{0,0,0,0,0,0},
wantFunc: func(a, b uint16) int16 { return int16(a & b) },
},
{
c: [6]uint16{0,1,0,1,0,1},
wantFunc: func(a, b uint16) int16 { return int16(a | b) },
},
}{
for i:=0; i<10; i++ {
a, b := uint16(rand.Intn(65536)), uint16(rand.Intn(65536))
zx := toBit(tt.c[0])
nx := toBit(tt.c[1])
zy := toBit(tt.c[2])
ny := toBit(tt.c[3])
f := toBit(tt.c[4])
no := toBit(tt.c[5])
out, zr, ng := Alu(toBit16(a), toBit16(b), zx, nx, zy, ny, f, no)
got := fromBit16Signed(out)
want := tt.wantFunc(a, b)
if got != want {
t.Errorf("expected %d but got %d", want, got)
}
if zr != (got==0) {
t.Errorf("expected zr to match out: %t %d", zr, got)
}
if ng != (got<0) {
t.Errorf("expected ng to match out: %t %d", ng, got)
}
}
}
}
func TestShift(t *testing.T) {
for i:=0; i<10; i++ {
in := uint16(rand.Intn(65536))
n := uint16(rand.Intn(16))
n16 := toBit16(n)
n4 := [4]bit{n16[12], n16[13], n16[14], n16[15]}
got := fromBit16(Shift(toBit16(in), n4))
want := bits.RotateLeft16(in, int(n))
if got != want {
t.Errorf("got %d but want %d", got, want)
}
}
}