From ca15c83b6de6bd8535a440b0151d9b326387430e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Jos=C3=A9=20de=20Oliveira?= Date: Thu, 28 Dec 2023 16:01:58 -0300 Subject: [PATCH] test: improve unit tests --- core/audio/audio_test.go | 13 ++- core/cpu_test.go | 237 ++++++++++++++++++++++++--------------- 2 files changed, 159 insertions(+), 91 deletions(-) diff --git a/core/audio/audio_test.go b/core/audio/audio_test.go index 0b71f17..f565c4e 100644 --- a/core/audio/audio_test.go +++ b/core/audio/audio_test.go @@ -1,15 +1,26 @@ package audio_test import ( + "os" "testing" "github.com/gaoliveira21/chip8/core/audio" ) -func Test(t *testing.T) { +func TestNewAudioPlayer(t *testing.T) { p, err := audio.NewAudioPlayer() if p == nil { t.Error(err) } } + +func TestNewAudioPlayerWeb(t *testing.T) { + os.Setenv("EXEC_MODE", "web") + + p, _ := audio.NewAudioPlayer() + + if p != nil { + t.Error("Audio player expected to be nil") + } +} diff --git a/core/cpu_test.go b/core/cpu_test.go index 1f33500..3d6049c 100644 --- a/core/cpu_test.go +++ b/core/cpu_test.go @@ -185,7 +185,7 @@ func TestJPWithOffset(t *testing.T) { } } -func TestSKPVxEqualsToNN(t *testing.T) { +func TestSKPVxEqualToNN(t *testing.T) { cpu := NewCpu() cpu.mmu.Write(0x200, 0x35) @@ -233,7 +233,7 @@ func TestSKPVxNotEqualToNN(t *testing.T) { } } -func TestSKPVxEqualsToVy(t *testing.T) { +func TestSKPVxEqualToVy(t *testing.T) { cpu := NewCpu() cpu.mmu.Write(0x200, 0x55) @@ -416,14 +416,21 @@ func TestVxXORVy(t *testing.T) { func TestSUBWithoutCarry(t *testing.T) { cpu := NewCpu() - var vIndex uint8 = 0x1 + cpu.mmu.Write(0x200, 0x81) + cpu.mmu.Write(0x201, 0x25) - cpu.sub(vIndex, 0x02, 0x03) + var vxIndex uint8 = 0x1 + var vyIndex uint8 = 0x2 - expected := 0x02 - 0x03 + cpu.v[vxIndex] = 0x2 + cpu.v[vyIndex] = 0x3 - if cpu.v[vIndex] != byte(expected) { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + expected := 0x2 - 0x3 + + cpu.Clock() + + if cpu.v[vxIndex] != byte(expected) { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vxIndex, cpu.v[vxIndex], expected) } if cpu.v[0xF] != 0x0 { @@ -434,14 +441,21 @@ func TestSUBWithoutCarry(t *testing.T) { func TestSUBWithCarry(t *testing.T) { cpu := NewCpu() - var vIndex uint8 = 0x1 + cpu.mmu.Write(0x200, 0x81) + cpu.mmu.Write(0x201, 0x27) - cpu.sub(vIndex, 0x03, 0x02) + var vxIndex uint8 = 0x1 + var vyIndex uint8 = 0x2 - expected := 0x03 - 0x02 + cpu.v[vxIndex] = 0x2 + cpu.v[vyIndex] = 0x3 - if cpu.v[vIndex] != byte(expected) { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + expected := 0x3 - 0x2 + + cpu.Clock() + + if cpu.v[vxIndex] != byte(expected) { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vxIndex, cpu.v[vxIndex], expected) } if cpu.v[0xF] != 0x1 { @@ -449,179 +463,222 @@ func TestSUBWithCarry(t *testing.T) { } } -func TestLDT(t *testing.T) { +func TestSHRWithoutFlag(t *testing.T) { cpu := NewCpu() - cpu.ldt(0x60) + cpu.mmu.Write(0x200, 0x81) + cpu.mmu.Write(0x201, 0x06) - if cpu.delayTimer != 0x60 { - t.Errorf("cpu.delayTimer = 0x%X; expected 0x60", cpu.delayTimer) + var vIndex uint8 = 0x1 + cpu.v[vIndex] = 0b11111110 + expected := cpu.v[vIndex] >> 1 + + cpu.Clock() + + if cpu.v[vIndex] != expected { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + } + + if cpu.v[0xF] != 0x0 { + t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) } } -func TestLDS(t *testing.T) { +func TestSHRWithFlag(t *testing.T) { cpu := NewCpu() - cpu.lds(0x80) + cpu.mmu.Write(0x200, 0x81) + cpu.mmu.Write(0x201, 0x06) - if cpu.SoundTimer != 0x80 { - t.Errorf("cpu.SoundTimer = 0x%X; expected 0x80", cpu.SoundTimer) + var vIndex uint8 = 0x1 + cpu.v[vIndex] = 0b00000001 + expected := cpu.v[vIndex] >> 1 + + cpu.Clock() + + if cpu.v[vIndex] != expected { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + } + + if cpu.v[0xF] != 0x1 { + t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x1) } } -func TestLDKWithNoKeyPressed(t *testing.T) { +func TestSHLWithoutFlag(t *testing.T) { cpu := NewCpu() - cpu.pc += 2 + + cpu.mmu.Write(0x200, 0x81) + cpu.mmu.Write(0x201, 0x0E) var vIndex uint8 = 0x1 + cpu.v[vIndex] = 0b01111110 + expected := cpu.v[vIndex] << 1 - cpu.ldk(vIndex) + cpu.Clock() - if cpu.v[vIndex] != 0x0 { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], 0x0) + if cpu.v[vIndex] != expected { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) } - if cpu.pc != 0x200 { - t.Errorf("cpu.pc = 0x%X; expected 0x200", cpu.pc) + if cpu.v[0xF] != 0x0 { + t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) } } -func TestLDKWithKeyPressed(t *testing.T) { +func TestSHLWithFlag(t *testing.T) { cpu := NewCpu() - cpu.pc += 2 - var vIndex uint8 = 0x1 - var keyPressed uint8 = 0xF + cpu.mmu.Write(0x200, 0x81) + cpu.mmu.Write(0x201, 0x0E) - cpu.Keys[keyPressed] = 0x1 + var vIndex uint8 = 0x1 + cpu.v[vIndex] = 0b11111110 + expected := cpu.v[vIndex] << 1 - cpu.ldk(vIndex) + cpu.Clock() - if cpu.v[vIndex] != keyPressed { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], keyPressed) + if cpu.v[vIndex] != expected { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) } - if cpu.pc != 0x202 { - t.Errorf("cpu.pc = 0x%X; expected 0x200", cpu.pc) + if cpu.v[0xF] != 0x1 { + t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) } } -func TestADIWithoutOverflow(t *testing.T) { +func TestSKPVxNotEqualToVy(t *testing.T) { cpu := NewCpu() - cpu.adi(0x80) - cpu.adi(0x50) + cpu.mmu.Write(0x200, 0x95) + cpu.mmu.Write(0x201, 0x60) - var expected uint16 = 0x80 + 0x50 + cpu.v[0x5] = 0x70 + cpu.v[0x6] = 0x71 - if cpu.i != expected { - t.Errorf("cpu.i = 0x%X; expected 0x%X", cpu.i, expected) + cpu.Clock() + + if cpu.pc != 0x204 { + t.Errorf("cpu.pc = 0x%X; expected 0x204", cpu.pc) } - if cpu.v[0xF] != 0x0 { - t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) + cpu.mmu.Write(0x204, 0x95) + cpu.mmu.Write(0x205, 0x60) + + cpu.v[0x5] = 0x70 + cpu.v[0x6] = 0x70 + + cpu.Clock() + + if cpu.pc != 0x206 { + t.Errorf("cpu.pc = 0x%X; expected 0x206", cpu.pc) } } -func TestADIWithOverflow(t *testing.T) { +func TestLDI(t *testing.T) { cpu := NewCpu() - cpu.adi(0x0FFF) - cpu.adi(0x01) + cpu.mmu.Write(0x200, 0xAA) + cpu.mmu.Write(0x201, 0xBC) - var expected uint16 = 0x0FFF + 0x01 + var expected uint16 = 0x0ABC + + cpu.Clock() if cpu.i != expected { t.Errorf("cpu.i = 0x%X; expected 0x%X", cpu.i, expected) } - - if cpu.v[0xF] != 0x1 { - t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) - } } -func TestSHRWithoutFlag(t *testing.T) { +func TestLDT(t *testing.T) { cpu := NewCpu() - var vIndex uint8 = 0x1 - cpu.v[vIndex] = 0b11111110 - expected := cpu.v[vIndex] >> 1 - - cpu.shr(vIndex) + cpu.ldt(0x60) - if cpu.v[vIndex] != expected { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + if cpu.delayTimer != 0x60 { + t.Errorf("cpu.delayTimer = 0x%X; expected 0x60", cpu.delayTimer) } +} - if cpu.v[0xF] != 0x0 { - t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) +func TestLDS(t *testing.T) { + cpu := NewCpu() + + cpu.lds(0x80) + + if cpu.SoundTimer != 0x80 { + t.Errorf("cpu.SoundTimer = 0x%X; expected 0x80", cpu.SoundTimer) } } -func TestSHRWithFlag(t *testing.T) { +func TestLDKWithNoKeyPressed(t *testing.T) { cpu := NewCpu() + cpu.pc += 2 var vIndex uint8 = 0x1 - cpu.v[vIndex] = 0b00000001 - expected := cpu.v[vIndex] >> 1 - cpu.shr(vIndex) + cpu.ldk(vIndex) - if cpu.v[vIndex] != expected { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + if cpu.v[vIndex] != 0x0 { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], 0x0) } - if cpu.v[0xF] != 0x1 { - t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x1) + if cpu.pc != 0x200 { + t.Errorf("cpu.pc = 0x%X; expected 0x200", cpu.pc) } } -func TestSHLWithoutFlag(t *testing.T) { +func TestLDKWithKeyPressed(t *testing.T) { cpu := NewCpu() + cpu.pc += 2 var vIndex uint8 = 0x1 - cpu.v[vIndex] = 0b01111110 - expected := cpu.v[vIndex] << 1 + var keyPressed uint8 = 0xF - cpu.shl(vIndex) + cpu.Keys[keyPressed] = 0x1 - if cpu.v[vIndex] != expected { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + cpu.ldk(vIndex) + + if cpu.v[vIndex] != keyPressed { + t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], keyPressed) } - if cpu.v[0xF] != 0x0 { - t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) + if cpu.pc != 0x202 { + t.Errorf("cpu.pc = 0x%X; expected 0x200", cpu.pc) } } -func TestSHLWithFlag(t *testing.T) { +func TestADIWithoutOverflow(t *testing.T) { cpu := NewCpu() - var vIndex uint8 = 0x1 - cpu.v[vIndex] = 0b11111110 - expected := cpu.v[vIndex] << 1 + cpu.adi(0x80) + cpu.adi(0x50) - cpu.shl(vIndex) + var expected uint16 = 0x80 + 0x50 - if cpu.v[vIndex] != expected { - t.Errorf("cpu.v[%d] = 0x%X; expected 0x%X", vIndex, cpu.v[vIndex], expected) + if cpu.i != expected { + t.Errorf("cpu.i = 0x%X; expected 0x%X", cpu.i, expected) } - if cpu.v[0xF] != 0x1 { + if cpu.v[0xF] != 0x0 { t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) } } -func TestLDI(t *testing.T) { +func TestADIWithOverflow(t *testing.T) { cpu := NewCpu() - var expected uint16 = 0x0ABC + cpu.adi(0x0FFF) + cpu.adi(0x01) - cpu.ldi(expected) + var expected uint16 = 0x0FFF + 0x01 if cpu.i != expected { t.Errorf("cpu.i = 0x%X; expected 0x%X", cpu.i, expected) } + + if cpu.v[0xF] != 0x1 { + t.Errorf("cpu.v[0xF] = 0x%X; expected 0x%X", cpu.v[0xF], 0x0) + } } func TestBCD(t *testing.T) {