Skip to content

Commit

Permalink
add mock CPU classes for abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
taniarascia committed Jul 16, 2019
1 parent 34e2ded commit 211eb31
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 12 deletions.
8 changes: 3 additions & 5 deletions classes/interfaces/MockCpuInterface.js
Expand Up @@ -22,14 +22,11 @@ class MockCpuInterface extends CpuInterface {
}

mapKey() {
if (this.resolveKey) {
this.resolveKey(5)
this.resolveKey = null
}
this.resolveKey(5)
}

clearDisplay() {
console.log('Screen is cleared')
return 'Screen is cleared'
}

renderDisplay() {
Expand Down Expand Up @@ -77,4 +74,5 @@ class MockCpuInterface extends CpuInterface {

module.exports = {
MockCpuInterface,
CpuInterface,
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -35,7 +35,7 @@
"data/*.js",
"classes/*.js",
"classes/interfaces/*.js",
"classes/*.js",
"!data/keyMap.js",
"!/node_modules/",
"!classes/interfaces/DOMCpuInterface.js",
"!classes/interfaces/TerminalCpuInterface.js"
Expand Down
149 changes: 143 additions & 6 deletions tests/cpu.test.js
@@ -1,9 +1,9 @@
describe('CPU tests', async () => {
const { CPU } = require('../classes/CPU')
const { MockCpuInterface } = require('../classes/interfaces/MockCpuInterface')
const cpuInterface = new MockCpuInterface()
const cpu = new CPU(cpuInterface)
const { CPU } = require('../classes/CPU')
const { MockCpuInterface, CpuInterface } = require('../classes/interfaces/MockCpuInterface')
const cpuInterface = new MockCpuInterface()
const cpu = new CPU(cpuInterface)

describe('CPU tests', async () => {
test('CPU should not execute after halting', async () => {
cpu.load({ data: 0x0000 })
cpu.halted = true
Expand Down Expand Up @@ -41,6 +41,22 @@ describe('CPU tests', async () => {
)
})

test('Tick should disable sound if sound is enabled and sound timer is zero', async () => {
cpu.load({ data: [0x00e0] })
await cpu.step()
cpu.soundEnabled = true
await cpu.tick()

expect(cpu.soundEnabled).toBe(false)
})

test('3: CLS (00e0) - Program should clear the display', async () => {
cpu.load({ data: [0x00e0] })
const mockClearDisplay = cpu.interface.clearDisplay()

expect(mockClearDisplay).toBe('Screen is cleared')
})

test('3: RET (00ee) - Program counter should be set to stack pointer, then decrement stack pointer', async () => {
cpu.load({ data: [0x00ee] })
cpu.SP = 0x2
Expand Down Expand Up @@ -426,12 +442,30 @@ describe('CPU tests', async () => {
})

test('28: LD_VX_N (Fx0A) - Register x should be set to the value of keypress', async () => {
cpu.load({ data: [0xfb0a] })
cpu.load({ data: [0xfb0a, 0xfa07] })
await cpu.step()

expect(cpu.registers[0xb]).toBe(5)
})

test('28: LD_VX_N (Fx0A) - Error should be thrown if CPU is halted during waitKey', async () => {
cpu.load({ data: [0xfb0a] })
let error
try {
if (!cpu.halted) {
cpu.halt()
await cpu.step()
}
} catch (e) {
error = e
}
expect(error).toEqual(
new Error(
'A problem has been detected and Chip-8 has been shut down to prevent damage to your computer.'
)
)
})

test('29: LD_DT_VX (Fx15) - Delay timer should be set to the value of register x', async () => {
// todo tick
cpu.load({ data: [0xfb15] })
Expand Down Expand Up @@ -584,3 +618,106 @@ describe('CPU tests', async () => {
expect(error).toEqual(new Error('Illegal instruction.'))
})
})

describe('CPU abstract class', () => {
class ChildCpuInterface extends CpuInterface {
constructor() {
super()
}
}

test('CPU interface should throw an error if it is instantiated directly', () => {
let error
try {
new CpuInterface()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Cannot instantiate abstract class'))
})

test('CPU interface must have clearDisplay method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.clearDisplay()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})

test('CPU interface must have renderDisplay method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.renderDisplay()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})

test('CPU interface must have waitKey method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.waitKey()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})

test('CPU interface must have getKeys method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.getKeys()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})

test('CPU interface must have drawPixel method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.drawPixel()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})

test('CPU interface must have enableSound method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.enableSound()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})

test('CPU interface must have disableSound method', () => {
let error
try {
const childCpuInterface = new ChildCpuInterface()
childCpuInterface.disableSound()
} catch (e) {
error = e
}
// BSOD on halted program
expect(error).toEqual(new Error('Must be implemented on the inherited class.'))
})
})

0 comments on commit 211eb31

Please sign in to comment.