From ea3a46db16931376c7db60285e17f5e9d090a126 Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Wed, 29 May 2024 15:09:06 +1000 Subject: [PATCH] Tweak test labels. --- parser/instruction.go | 32 ++++++++++++-------------------- parser/instruction_test.go | 35 ++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/parser/instruction.go b/parser/instruction.go index 3d6a681..f32392a 100644 --- a/parser/instruction.go +++ b/parser/instruction.go @@ -23,28 +23,20 @@ const ( opMulVal ) +var instMap = map[byte]Instruction{ + '>': Instruction{opAddDp, 1}, + '<': Instruction{opAddDp, -1}, + '+': Instruction{opAddVal, 1}, + '-': Instruction{opAddVal, -1}, + '.': Instruction{opOut, 1}, + ',': Instruction{opIn, 1}, + '[': Instruction{opJmpZ, 0}, + ']': Instruction{opJmpNz, 0}, +} + // NewInstruction created from a sourcecode byte func NewInstruction(chr byte) Instruction { - switch chr { - case '>': - return Instruction{opAddDp, 1} - case '<': - return Instruction{opAddDp, -1} - case '+': - return Instruction{opAddVal, 1} - case '-': - return Instruction{opAddVal, -1} - case '.': - return Instruction{opOut, 1} - case ',': - return Instruction{opIn, 1} - case '[': - return Instruction{opJmpZ, 0} - case ']': - return Instruction{opJmpNz, 0} - default: - return Instruction{opNoop, 0} - } + return instMap[chr] } // SameOp compares Instructions operator but not operand diff --git a/parser/instruction_test.go b/parser/instruction_test.go index ef32b9c..2865f45 100644 --- a/parser/instruction_test.go +++ b/parser/instruction_test.go @@ -6,21 +6,22 @@ import ( "testing" ) +var opName = map[Opcode]string{ + opNoop: "nop", + opAddDp: "ptr", + opAddVal: "add", + opSetVal: "set", + opOut: "out", + opIn: "inp", + opJmpZ: "jmp", + opJmpNz: "jnz", + opMove: "mov", + opSkip: "skp", + opMulVal: "mul", +} + func (inst Instruction) string() string { - opName := [...]string{ - "nop", - "ptr", - "add", - "set", - "out", - "inp", - "jmp", - "jnz", - "mov", - "skp", - "mul", - } - return fmt.Sprintf("%s: %v", opName[inst.operator], inst.operand) + return fmt.Sprintf("%s:%v", opName[inst.operator], inst.operand) } func TestNewInstruction(t *testing.T) { @@ -75,7 +76,7 @@ func TestIsZeroOp(t *testing.T) { for idx, val := range program { if want[idx] != val.IsZeroOp() { - t.Errorf("testing %v got %v want %v", val, val.IsZeroOp(), want[idx]) + t.Errorf("testing %v got %v want %v", val.string(), val.IsZeroOp(), want[idx]) } } } @@ -95,7 +96,7 @@ func TestSameOp(t *testing.T) { } for row, rval := range opsList { - t.Run(fmt.Sprintf("%d", row), func(t *testing.T) { + t.Run(opName[rval], func(t *testing.T) { for col, cval := range opsList { rinst := Instruction{rval, col} cinst := Instruction{cval, row} @@ -118,7 +119,7 @@ func TestComplement(t *testing.T) { for row, rval := range opsList { for col, cval := range opsList { - t.Run(fmt.Sprintf("%d-%d", row, col), func(t *testing.T) { + t.Run(fmt.Sprintf("%s-%s", opName[rval], opName[cval]), func(t *testing.T) { for operand := range [6]int{} { rinst := Instruction{rval, operand} cinst := Instruction{cval, operand - 6}