Update intcode processor tests

This commit is contained in:
Brian Buller 2019-12-04 14:16:41 -06:00
parent bbfcc6022d
commit 589790fcda
2 changed files with 32 additions and 11 deletions

View File

@ -32,6 +32,10 @@ func NewProgram(prog []int) *Program {
return p return p
} }
func (p *Program) GetCode() []int {
return p.code
}
func (p *Program) State() int { func (p *Program) State() int {
return p.state return p.state
} }
@ -43,11 +47,8 @@ func (p *Program) Error() error {
func (p *Program) Run() int { func (p *Program) Run() int {
for { for {
p.state = p.Step() p.state = p.Step()
switch p.state { if p.state != RET_OK {
case RET_ERR: return p.state
return RET_ERR
case RET_DONE:
return RET_DONE
} }
} }
} }

View File

@ -1,14 +1,34 @@
package intcodeprocessor package intcodeprocessor
import ( import (
"fmt"
"testing" "testing"
) )
func TestNewProgram(t *testing.T) { func TestNewAddProgram(t *testing.T) {
want := fmt.Sprintf("%v", []int{1}) strt := []int{1, 0, 0, 0, 99}
p := NewProgram([]int{1}) want := []int{2, 0, 0, 0, 99}
if got := p.CodeString(); got != want { p := NewProgram(strt)
t.Errorf("Program.CodeString() = %q, want %q", got, want) p.Run()
if !intSliceEquals(p.GetCode(), want) {
t.Errorf("Program.GetCode() = %q, want %q", p.GetCode(), want)
} }
} }
func TestNewMultProgram(t *testing.T) {
strt := []int{2, 3, 0, 3, 99}
want := []int{2, 3, 0, 6, 99}
p := NewProgram(strt)
p.Run()
if !intSliceEquals(p.GetCode(), want) {
t.Errorf("Program.GetCode() = %q, want %q", p.GetCode(), want)
}
}
func intSliceEquals(s1, s2 []int) bool {
for k := range s1 {
if s1[k] != s2[k] {
return false
}
}
return true
}