add unit tests for existing methods
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Sid 2020-10-10 22:29:35 +01:00
parent 80124aa3a5
commit 75ab3648a1
2 changed files with 97 additions and 4 deletions

View File

@ -101,7 +101,7 @@ func (cpu *Chip8) skipIfRegistersEqual() {
} }
} }
func (cpu *Chip8) setRegisterToNN() { func (cpu *Chip8) setRegisterTo() {
r := (cpu.opcode >> 8) & 0x0F r := (cpu.opcode >> 8) & 0x0F
cpu.registers[r] = uint8(cpu.opcode & 0xFF) cpu.registers[r] = uint8(cpu.opcode & 0xFF)
} }

View File

@ -1,6 +1,7 @@
package chip8 package chip8
import ( import (
"fmt"
"testing" "testing"
) )
@ -49,8 +50,100 @@ func TestLeaveFunction(t *testing.T) {
} }
} }
func TestPasses(t *testing.T) { func TestGoTo(t *testing.T) {
if 1 != 1 { cpu := Chip8{opcode: 0x3420}
t.Errorf("We should never get this!") 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)
} }
} }