This repository was archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcode_test.go
More file actions
133 lines (107 loc) · 2.48 KB
/
opcode_test.go
File metadata and controls
133 lines (107 loc) · 2.48 KB
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
package intgode
import "testing"
func TestOpcodeNames(t *testing.T) {
testCases := []struct {
name string
want opcodeName
got opcodeName
}{
{"Add", 1, add},
{"Multiply", 2, multiply},
{"Input", 3, input},
{"Output", 4, output},
{"JumpIfTrue", 5, jumpIfTrue},
{"JumpIfFalse", 6, jumpIfFalse},
{"LessThan", 7, lessThan},
{"Equals", 8, equals},
{"RelativeBaseOffset", 9, relativeBaseOffset},
{"Halt", 99, halt},
}
for _, tC := range testCases {
if tC.want != tC.got {
t.Errorf("Invalid opcode number for %s. Want %d got %d", tC.name, tC.want, tC.got)
}
}
}
func TestAddInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{1, 0, 0, 0, 99})
go program.Exec()
<-program.Output()
if program.Memory()[0] != 2 {
t.FailNow()
}
}
func TestMultiplyInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{2, 0, 0, 0, 99})
go program.Exec()
<-program.Output()
if program.Memory()[0] != 4 {
t.FailNow()
}
}
func TestInputInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{3, 0, 99})
go program.Exec()
<-program.Output()
program.Input() <- 5
<-program.Output()
if program.Memory()[0] != 5 {
t.FailNow()
}
}
func TestOutputInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{4, 0, 99})
go program.Exec()
out := <-program.Output()
if out[0] != 4 {
t.FailNow()
}
}
func TestJumpIfTrueInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{5, 0, 0, 4, 0, 99})
go program.Exec()
<-program.Output()
if program.Memory()[0] != 5 {
t.FailNow()
}
}
func TestJumpIfFalseInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{6, 2, 0, 4, 0, 99, 1, 0, 0, 0, 99})
go program.Exec()
<-program.Output()
if program.Memory()[0] != 12 {
t.FailNow()
}
}
func TestLessThanInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{7, 0, 4, 0, 99})
go program.Exec()
<-program.Output()
if program.Memory()[0] != 1 {
t.FailNow()
}
}
func TestEqualsInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{8, 1, 1, 0, 99})
go program.Exec()
<-program.Output()
if program.Memory()[0] != 1 {
t.FailNow()
}
}
func TestRelativeBaseOffsetInstruction(t *testing.T) {
program := NewIntcodeProgram([]int{9, 2, 99})
go program.Exec()
<-program.Output()
if program.RelativeBase() != 99 {
t.FailNow()
}
}
func TestHalt(t *testing.T) {
program := NewIntcodeProgram([]int{99, 1, 0, 0, 0})
go program.Exec()
<-program.Output()
if !program.Halted() || program.Memory()[0] != 99 {
t.FailNow()
}
}