From 589790fcda60c5f8af6e21c39b8389bbb5214974 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 4 Dec 2019 14:16:41 -0600 Subject: [PATCH] Update intcode processor tests --- 2019/intcode-processor/processor.go | 11 ++++---- 2019/intcode-processor/processor_test.go | 32 +++++++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/2019/intcode-processor/processor.go b/2019/intcode-processor/processor.go index 7428f2f..3bff2b4 100644 --- a/2019/intcode-processor/processor.go +++ b/2019/intcode-processor/processor.go @@ -32,6 +32,10 @@ func NewProgram(prog []int) *Program { return p } +func (p *Program) GetCode() []int { + return p.code +} + func (p *Program) State() int { return p.state } @@ -43,11 +47,8 @@ func (p *Program) Error() error { func (p *Program) Run() int { for { p.state = p.Step() - switch p.state { - case RET_ERR: - return RET_ERR - case RET_DONE: - return RET_DONE + if p.state != RET_OK { + return p.state } } } diff --git a/2019/intcode-processor/processor_test.go b/2019/intcode-processor/processor_test.go index ff8a49d..547b3d2 100644 --- a/2019/intcode-processor/processor_test.go +++ b/2019/intcode-processor/processor_test.go @@ -1,14 +1,34 @@ package intcodeprocessor import ( - "fmt" "testing" ) -func TestNewProgram(t *testing.T) { - want := fmt.Sprintf("%v", []int{1}) - p := NewProgram([]int{1}) - if got := p.CodeString(); got != want { - t.Errorf("Program.CodeString() = %q, want %q", got, want) +func TestNewAddProgram(t *testing.T) { + strt := []int{1, 0, 0, 0, 99} + want := []int{2, 0, 0, 0, 99} + p := NewProgram(strt) + 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 +}