Skip to content

Commit

Permalink
Tweak test labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed May 29, 2024
1 parent 8d2137d commit ea3a46d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
32 changes: 12 additions & 20 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 18 additions & 17 deletions parser/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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])
}
}
}
Expand All @@ -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}
Expand All @@ -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}
Expand Down

0 comments on commit ea3a46d

Please sign in to comment.