Chip-8_Go/pkg/chip8/chip8_test.go

150 lines
3.5 KiB
Go
Raw Normal View History

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"
)
func slicesEqual(x, y []uint8) bool {
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) {
prog := []uint8{1, 2, 3, 4}
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 {
cpu.graphics[i] = uint8(i % 255)
}
cpu.clearDisplay()
graphics_array := cpu.GetGraphicsBuffer()
graphics_slice := graphics_array[:]
empty_slice := make([]uint8, len(cpu.graphics))
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 {
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)
2020-10-10 21:29:53 +01:00
}
}