From 75ab3648a1041c2a6faca579d602227f9fe0bbf2 Mon Sep 17 00:00:00 2001 From: "S.D" Date: Sat, 10 Oct 2020 22:29:35 +0100 Subject: [PATCH] add unit tests for existing methods --- pkg/chip8/chip8.go | 2 +- pkg/chip8/chip8_test.go | 99 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/pkg/chip8/chip8.go b/pkg/chip8/chip8.go index cf9b546..59b6982 100644 --- a/pkg/chip8/chip8.go +++ b/pkg/chip8/chip8.go @@ -101,7 +101,7 @@ func (cpu *Chip8) skipIfRegistersEqual() { } } -func (cpu *Chip8) setRegisterToNN() { +func (cpu *Chip8) setRegisterTo() { r := (cpu.opcode >> 8) & 0x0F cpu.registers[r] = uint8(cpu.opcode & 0xFF) } diff --git a/pkg/chip8/chip8_test.go b/pkg/chip8/chip8_test.go index ea377aa..38b98ed 100644 --- a/pkg/chip8/chip8_test.go +++ b/pkg/chip8/chip8_test.go @@ -1,6 +1,7 @@ package chip8 import ( + "fmt" "testing" ) @@ -49,8 +50,100 @@ func TestLeaveFunction(t *testing.T) { } } -func TestPasses(t *testing.T) { - if 1 != 1 { - t.Errorf("We should never get this!") +func TestGoTo(t *testing.T) { + cpu := Chip8{opcode: 0x3420} + cpu.goTo() + if cpu.pc != 0x420 { + t.Errorf("Test GoTo not working as expected") + } +} + +func TestCallSubroutine(t *testing.T) { + cpu := Chip8{opcode: 0x3420, pc: 43} + cpu.callSubroutine() + if (cpu.pc != 420) && (cpu.stack[0] != 43) && (len(cpu.stack) != 1) { + t.Errorf("CallSubroutine not working as expected") + } +} + +func TestSkipIfRegisterEqual(t *testing.T) { + var tests = []struct { + register, regValue uint8 + compValue, want uint16 + }{ + {0, 2, 4, 0}, + {0, 2, 2, 2}, + {15, 2, 2, 2}, + {15, 1, 12, 0}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("Reg:%d,RegVal:%d,Comp:%d", tt.register, tt.regValue, tt.compValue) + t.Run(testname, func(t *testing.T) { + cpu := Chip8{opcode: tt.compValue + uint16(tt.register)<<8} + cpu.registers[tt.register] = tt.regValue + cpu.skipIfRegisterEqual() + if tt.want != cpu.pc { + t.Errorf("PC is %d, Wanted %d", cpu.pc, tt.want) + } + }) + } +} + +func TestSkipIfRegisterNotEqual(t *testing.T) { + var tests = []struct { + register, regValue uint8 + compValue, want uint16 + }{ + {0, 2, 4, 2}, + {0, 2, 2, 0}, + {15, 2, 2, 0}, + {15, 1, 12, 2}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("Reg:%d,RegVal:%d,Comp:%d", tt.register, tt.regValue, tt.compValue) + t.Run(testname, func(t *testing.T) { + cpu := Chip8{opcode: tt.compValue + uint16(tt.register)<<8} + cpu.registers[tt.register] = tt.regValue + cpu.skipIfRegisterNotEqual() + if tt.want != cpu.pc { + t.Errorf("PC is %d, Wanted %d", cpu.pc, tt.want) + } + }) + } +} + +func TestSkipIfRegistersEqual(t *testing.T) { + var tests = []struct { + reg1, reg2, val1, val2 uint8 + }{ + {0, 2, 4, 2}, + {0, 2, 2, 2}, + {15, 1, 2, 0}, + {15, 4, 12, 12}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("Reg1:%d,Reg2:%d,val1:%d,val2:%d", tt.reg1, tt.reg2, tt.val1, tt.val2) + t.Run(testname, func(t *testing.T) { + cpu := Chip8{opcode: uint16(tt.reg1)<<8 + uint16(tt.reg2)<<4} + cpu.registers[tt.reg1] = tt.val1 + cpu.registers[tt.reg2] = tt.val2 + cpu.skipIfRegistersEqual() + if (2 != cpu.pc) && (tt.val1 == tt.val2) { + t.Errorf("PC is %d, Wanted %d", cpu.pc, 2) + } else if (0 != cpu.pc) && (tt.val1 != tt.val2) { + t.Errorf("PC is %d, Wanted %d", cpu.pc, 2) + } + }) + } +} + +func TestSetRegisterTo(t *testing.T) { + cpu := Chip8{opcode: 0x0824} + cpu.setRegisterTo() + if cpu.registers[8] != 24 { + t.Errorf("Register 8 is %d wanted %d", cpu.registers[8], 24) } }