2020-10-10 21:29:53 +01:00
|
|
|
package chip8
|
|
|
|
|
|
|
|
import (
|
2020-10-10 22:29:35 +01:00
|
|
|
"fmt"
|
2020-10-10 21:29:53 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-10-10 22:58:18 +01:00
|
|
|
func slicesEqual(x, y []byte) bool {
|
2020-10-10 21:29:53 +01:00
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, xi := range x {
|
|
|
|
if xi != y[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is kinda shit, there's so much stuff we ain't testing
|
|
|
|
// Maybe fix
|
|
|
|
func TestCreateCPU(t *testing.T) {
|
2020-10-10 22:58:18 +01:00
|
|
|
prog := []byte{1, 2, 3, 4}
|
2020-10-10 21:29:53 +01:00
|
|
|
new_cpu := NewCHIP8(prog)
|
|
|
|
if !slicesEqual(new_cpu.memory[200:204], prog) {
|
|
|
|
t.Errorf("CPU not initalized properly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClearDisplay(t *testing.T) {
|
|
|
|
cpu := Chip8{}
|
|
|
|
for i := range cpu.graphics {
|
2020-10-10 22:58:18 +01:00
|
|
|
cpu.graphics[i] = byte(i % 255)
|
2020-10-10 21:29:53 +01:00
|
|
|
}
|
|
|
|
cpu.clearDisplay()
|
|
|
|
graphics_array := cpu.GetGraphicsBuffer()
|
|
|
|
graphics_slice := graphics_array[:]
|
2020-10-10 22:58:18 +01:00
|
|
|
empty_slice := make([]byte, len(cpu.graphics))
|
2020-10-10 21:29:53 +01:00
|
|
|
if !slicesEqual(graphics_slice, empty_slice) {
|
|
|
|
t.Errorf("Graphics buffer not cleared properly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeaveFunction(t *testing.T) {
|
|
|
|
cpu := Chip8{pc: 50}
|
|
|
|
cpu.stack = append(cpu.stack, 1, 2, 3, 4, 5)
|
|
|
|
cpu.leaveFunction()
|
|
|
|
if cpu.pc != 4 && len(cpu.stack) != 4 {
|
|
|
|
t.Errorf("TestLeaveFunction not in expected state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 22:29:35 +01:00
|
|
|
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 {
|
2020-10-10 22:58:18 +01:00
|
|
|
register, regValue byte
|
2020-10-10 22:29:35 +01:00
|
|
|
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 {
|
2020-10-10 22:58:18 +01:00
|
|
|
register, regValue byte
|
2020-10-10 22:29:35 +01:00
|
|
|
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 {
|
2020-10-10 22:58:18 +01:00
|
|
|
reg1, reg2, val1, val2 byte
|
2020-10-10 22:29:35 +01:00
|
|
|
}{
|
|
|
|
{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()
|
2020-10-10 22:30:16 +01:00
|
|
|
if cpu.registers[8] != 0x24 {
|
|
|
|
t.Errorf("Register 8 is %d wanted %d", cpu.registers[8], 0x24)
|
2020-10-10 21:29:53 +01:00
|
|
|
}
|
|
|
|
}
|