1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-08-10 11:25:23 +00:00

Attempts to bring a little more consistency to my use of Swift in test code.

This commit is contained in:
Thomas Harte
2020-10-17 22:42:54 -04:00
parent c3187fdbe1
commit 69509f6502
12 changed files with 58 additions and 49 deletions

View File

@@ -14,38 +14,38 @@ class MOS6502InterruptTests: XCTestCase {
override func setUp() { override func setUp() {
super.setUp() super.setUp()
// create a machine full of NOPs // Create a machine full of NOPs.
machine = CSTestMachine6502(processor: .processor6502) machine = CSTestMachine6502(processor: .processor6502)
for c in 0...65535 { for c in 0...65535 {
machine.setValue(0xea, forAddress: UInt32(c)) machine.setValue(0xea, forAddress: UInt32(c))
} }
// set the IRQ vector to be 0x1234 // Set the IRQ vector to 0x1234.
machine.setValue(0x34, forAddress: 0xfffe) machine.setValue(0x34, forAddress: 0xfffe)
machine.setValue(0x12, forAddress: 0xffff) machine.setValue(0x12, forAddress: 0xffff)
// add a CLI // Add a CLI.
machine.setValue(0x58, forAddress: 0x4000) machine.setValue(0x58, forAddress: 0x4000)
// pick things off at 0x4000 // Begin at 0x4000.
machine.setValue(0x4000, for: CSTestMachine6502Register.programCounter) machine.setValue(0x4000, for: .programCounter)
} }
func testIRQLine() { func testIRQLine() {
// run for six cycles; check that no interrupt has occurred // Run for six cycles; check that no interrupt has occurred.
machine.runForNumber(ofCycles: 6) machine.runForNumber(ofCycles: 6)
XCTAssert(machine.value(for: .programCounter) == 0x4003, "No interrupt should have occurred with line low") XCTAssertEqual(machine.value(for: .programCounter), 0x4003, "No interrupt should have occurred with line low")
// enable the interrupt line, check that it was too late // enable the interrupt line, check that it was too late
machine.irqLine = true machine.irqLine = true
machine.runForNumber(ofCycles: 2) machine.runForNumber(ofCycles: 2)
XCTAssert(machine.value(for: .programCounter) == 0x4004, "No interrupt should have occurred from interrupt raised between instructions") XCTAssertEqual(machine.value(for: .programCounter), 0x4004, "No interrupt should have occurred from interrupt raised between instructions")
// run for a further 7 cycles, confirm that the IRQ vector was jumped to // run for a further 7 cycles, confirm that the IRQ vector was jumped to
machine.runForNumber(ofCycles: 6) machine.runForNumber(ofCycles: 6)
XCTAssert(machine.value(for: .programCounter) != 0x1234, "Interrupt routine should not yet have begun") XCTAssertNotEqual(machine.value(for: .programCounter), 0x1234, "Interrupt routine should not yet have begun")
machine.runForNumber(ofCycles: 1) machine.runForNumber(ofCycles: 1)
XCTAssert(machine.value(for: .programCounter) == 0x1234, "Interrupt routine should just have begun") XCTAssertEqual(machine.value(for: .programCounter), 0x1234, "Interrupt routine should just have begun")
} }
func testIFlagSet() { func testIFlagSet() {
@@ -53,8 +53,8 @@ class MOS6502InterruptTests: XCTestCase {
machine.irqLine = true machine.irqLine = true
machine.runForNumber(ofCycles: 11) machine.runForNumber(ofCycles: 11)
XCTAssert(machine.value(for: .programCounter) == 0x1234, "Interrupt routine should just have begun") XCTAssertEqual(machine.value(for: .programCounter), 0x1234, "Interrupt routine should just have begun")
XCTAssert(machine.value(for: .flags) & 0x04 == 0x04, "Interrupt status flag should be set") XCTAssertEqual(machine.value(for: .flags) & 0x04, 0x04, "Interrupt status flag should be set")
} }
func testCLISEIFlagClear() { func testCLISEIFlagClear() {
@@ -64,12 +64,13 @@ class MOS6502InterruptTests: XCTestCase {
// run for four cycles; the CLI and SEI should have been performed // run for four cycles; the CLI and SEI should have been performed
machine.runForNumber(ofCycles: 4) machine.runForNumber(ofCycles: 4)
XCTAssert(machine.value(for: .programCounter) == 0x4002, "CLI/SEI pair should have been performed in their entirety") XCTAssertEqual(machine.value(for: .programCounter), 0x4002, "CLI/SEI pair should have been performed in their entirety")
// run for seven more cycles // run for seven more cycles
machine.runForNumber(ofCycles: 7) machine.runForNumber(ofCycles: 7)
// interrupt should have taken place despite SEI // interrupt should have taken place despite SEI
XCTAssert(machine.value(for: .programCounter) == 0x1234, "Interrupt routine should just have begun") XCTAssertEqual(machine.value(for: .programCounter), 0x1234, "Interrupt routine should just have begun")
} }
} }

View File

@@ -22,7 +22,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x18, // [2] CLC 0x18, // [2] CLC
0x2a, // [2] ROL A 0x2a, // [2] ROL A
] ]
self.runTest(code, expectedRunLength: 10) runTest(code, expectedRunLength: 10)
} }
func testLDA() { func testLDA() {
@@ -40,7 +40,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0xb1, 0x00, // [5] LDA ($00), y (no wrap) 0xb1, 0x00, // [5] LDA ($00), y (no wrap)
0xb1, 0x02, // [6] LDA ($01), y (wrap) 0xb1, 0x02, // [6] LDA ($01), y (wrap)
] ]
self.runTest(code, expectedRunLength: 48) runTest(code, expectedRunLength: 48)
} }
func testBIT() { func testBIT() {
@@ -48,7 +48,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x24, 0x2a, // [3] BIT $2a 0x24, 0x2a, // [3] BIT $2a
0x2c, 0x2a, 0x2b, // [4] BIT $2b2a 0x2c, 0x2a, 0x2b, // [4] BIT $2b2a
] ]
self.runTest(code, expectedRunLength: 7) runTest(code, expectedRunLength: 7)
} }
func testSTA() { func testSTA() {
@@ -64,7 +64,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x91, 0x00, // [6] STA ($00), y (no wrap) 0x91, 0x00, // [6] STA ($00), y (no wrap)
0x91, 0x02, // [6] STA ($01), y (wrap) 0x91, 0x02, // [6] STA ($01), y (wrap)
] ]
self.runTest(code, expectedRunLength: 49) runTest(code, expectedRunLength: 49)
} }
func testINC() { func testINC() {
@@ -75,7 +75,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0xfe, 0x00, 0x00, // [7] INC $0000, x (no wrap) 0xfe, 0x00, 0x00, // [7] INC $0000, x (no wrap)
0xfe, 0x02, 0x00, // [7] INC $0002, x (wrap) 0xfe, 0x02, 0x00, // [7] INC $0002, x (wrap)
] ]
self.runTest(code, expectedRunLength: 31) runTest(code, expectedRunLength: 31)
} }
func testJSR() { func testJSR() {
@@ -85,7 +85,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x60, // [6] RTS 0x60, // [6] RTS
] ]
machine.addTrapAddress(0x0203) machine.addTrapAddress(0x0203)
self.runTest(code, expectedRunLength: 12) runTest(code, expectedRunLength: 12)
} }
func testJMP() { func testJMP() {
@@ -94,7 +94,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4c, 0x0b, 0x02, // [3] JMP 020b 0x4c, 0x0b, 0x02, // [3] JMP 020b
] ]
self.runTest(code, expectedRunLength: 8) runTest(code, expectedRunLength: 8)
} }
func testPHAPLA() { func testPHAPLA() {
@@ -103,7 +103,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x48, // [3] PHA 0x48, // [3] PHA
0x68, // [4] PLA 0x68, // [4] PLA
] ]
self.runTest(code, expectedRunLength: 10) runTest(code, expectedRunLength: 10)
} }
func testBCS() { func testBCS() {
@@ -130,7 +130,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
] ]
self.runTest(code, expectedRunLength: 14) runTest(code, expectedRunLength: 14)
} }
func testSnippet1() { func testSnippet1() {
@@ -138,7 +138,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x8d, 0x08, 0x00, // [4] STA $0008 0x8d, 0x08, 0x00, // [4] STA $0008
0xc6, 0xb4, // [5] DEC $B4 0xc6, 0xb4, // [5] DEC $B4
] ]
self.runTest(code, expectedRunLength: 9) runTest(code, expectedRunLength: 9)
} }
func testSnippet2() { func testSnippet2() {
@@ -146,7 +146,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x16, 0x16, // [6] ASL $16, x 0x16, 0x16, // [6] ASL $16, x
0x46, 0x46, // [5] LSR $46 0x46, 0x46, // [5] LSR $46
] ]
self.runTest(code, expectedRunLength: 11) runTest(code, expectedRunLength: 11)
} }
func testSnippet3() { func testSnippet3() {
@@ -174,7 +174,7 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0x60, // [6] RTS 0x60, // [6] RTS
] ]
machine.addTrapAddress(0x0203) machine.addTrapAddress(0x0203)
self.runTest(code, expectedRunLength: 66) runTest(code, expectedRunLength: 66)
} }
func testNOP() { func testNOP() {
@@ -194,10 +194,10 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
0xe2, 0x00, // [2] NOP # 0xe2, 0x00, // [2] NOP #
0xf4, 0x00, // [4] NOP zpg, x 0xf4, 0x00, // [4] NOP zpg, x
] ]
self.runTest(code, expectedRunLength: 43) runTest(code, expectedRunLength: 43)
} }
func runTest(_ code: [UInt8], expectedRunLength: UInt32) { private func runTest(_ code: [UInt8], expectedRunLength: UInt32) {
machine.trapHandler = self machine.trapHandler = self
let immediateCode = Data(code) let immediateCode = Data(code)
@@ -213,17 +213,17 @@ class MOS6502TimingTests: XCTestCase, CSTestMachineTrapHandler {
machine.setValue(0xff, for: CSTestMachine6502Register.X) machine.setValue(0xff, for: CSTestMachine6502Register.X)
machine.setValue(0xfe, for: CSTestMachine6502Register.Y) machine.setValue(0xfe, for: CSTestMachine6502Register.Y)
self.endTime = 0 endTime = 0
while self.endTime == 0 { while endTime == 0 {
machine.runForNumber(ofCycles: 10) machine.runForNumber(ofCycles: 10)
} }
XCTAssert(self.endTime == expectedRunLength, "Took \(self.endTime) cycles to perform rather than \(expectedRunLength)") XCTAssertEqual(endTime, expectedRunLength, "Took \(endTime) cycles to perform rather than \(expectedRunLength)")
} }
func testMachine(_ testMachine: CSTestMachine, didTrapAtAddress address: UInt16) { func testMachine(_ testMachine: CSTestMachine, didTrapAtAddress address: UInt16) {
if self.endTime == 0 { if endTime == 0 {
self.endTime = (machine.timestamp / 2) - 1 endTime = (machine.timestamp / 2) - 1
} }
} }
} }

View File

@@ -11,7 +11,7 @@ import Foundation
class MOS6532Tests: XCTestCase { class MOS6532Tests: XCTestCase {
fileprivate func with6532(_ action: (MOS6532Bridge) -> ()) { private func with6532(_ action: (MOS6532Bridge) -> ()) {
let bridge = MOS6532Bridge() let bridge = MOS6532Bridge()
action(bridge) action(bridge)
} }

View File

@@ -542,4 +542,5 @@ class WDC65816AddressingTests: XCTestCase {
XCTAssertEqual(machine.value(for: .A), 0xcdab) XCTAssertEqual(machine.value(for: .A), 0xcdab)
} }
} }

View File

@@ -27,4 +27,5 @@ class AllSuiteATests: XCTestCase {
} }
} }
} }
} }

View File

@@ -40,7 +40,7 @@ class BCDTest: XCTestCase, CSTestMachineTrapHandler {
} }
} }
fileprivate var output: String = "" private var output: String = ""
func testMachine(_ testMachine: CSTestMachine, didTrapAtAddress address: UInt16) { func testMachine(_ testMachine: CSTestMachine, didTrapAtAddress address: UInt16) {
let machine6502 = testMachine as! CSTestMachine6502 let machine6502 = testMachine as! CSTestMachine6502
@@ -48,4 +48,5 @@ class BCDTest: XCTestCase, CSTestMachineTrapHandler {
let character = machine6502.value(for: .A) let character = machine6502.value(for: .A)
output.append(Character(UnicodeScalar(character)!)) output.append(Character(UnicodeScalar(character)!))
} }
} }

View File

@@ -10,21 +10,21 @@ import XCTest
class C1540Tests: XCTestCase { class C1540Tests: XCTestCase {
fileprivate func with1540(_ action: (C1540Bridge) -> ()) { private func with1540(_ action: (C1540Bridge) -> ()) {
let bridge = C1540Bridge() let bridge = C1540Bridge()
action(bridge) action(bridge)
} }
fileprivate func transmit(_ c1540: C1540Bridge, value: Int) { private func transmit(_ c1540: C1540Bridge, value: Int) {
var shiftedValue = value var shiftedValue = value
c1540.dataLine = true c1540.dataLine = true
c1540.run(forCycles: 256) c1540.run(forCycles: 256)
XCTAssert(c1540.dataLine == false, "Listener should have taken data line low for start of transmission") XCTAssertFalse(c1540.dataLine, "Listener should have taken data line low for start of transmission")
c1540.clockLine = true c1540.clockLine = true
c1540.run(forCycles: 256) // this isn't time limited on real hardware c1540.run(forCycles: 256) // this isn't time limited on real hardware
XCTAssert(c1540.dataLine == true, "Listener should have let data line go high again") XCTAssertTrue(c1540.dataLine, "Listener should have let data line go high again")
// set up for byte transfer // set up for byte transfer
c1540.clockLine = false c1540.clockLine = false
@@ -47,7 +47,7 @@ class C1540Tests: XCTestCase {
// check for acknowledgment // check for acknowledgment
c1540.dataLine = true c1540.dataLine = true
c1540.run(forCycles: 1000) c1540.run(forCycles: 1000)
XCTAssert(c1540.dataLine == false, "Listener should have acknowledged byte") XCTAssertFalse(c1540.dataLine, "Listener should have acknowledged byte")
} }
// MARK: EOI // MARK: EOI
@@ -64,10 +64,11 @@ class C1540Tests: XCTestCase {
// proceed 1 ms and check that the 1540 pulled the data line low // proceed 1 ms and check that the 1540 pulled the data line low
$0.run(forCycles: 1000) $0.run(forCycles: 1000)
XCTAssert($0.dataLine == false, "Listener should have taken data line low") XCTAssertFalse($0.dataLine, "Listener should have taken data line low")
// transmit LISTEN #8 // transmit LISTEN #8
self.transmit($0, value: 0x28) transmit($0, value: 0x28)
} }
} }
} }

View File

@@ -11,7 +11,7 @@ import XCTest
class KlausDormannTests: XCTestCase { class KlausDormannTests: XCTestCase {
fileprivate func runTest(resource: String, processor: CSTestMachine6502Processor) -> UInt16 { private func runTest(resource: String, processor: CSTestMachine6502Processor) -> UInt16 {
if let filename = Bundle(for: type(of: self)).path(forResource: resource, ofType: "bin") { if let filename = Bundle(for: type(of: self)).path(forResource: resource, ofType: "bin") {
if let functionalTest = try? Data(contentsOf: URL(fileURLWithPath: filename)) { if let functionalTest = try? Data(contentsOf: URL(fileURLWithPath: filename)) {
let machine = CSTestMachine6502(processor: processor) let machine = CSTestMachine6502(processor: processor)
@@ -39,7 +39,7 @@ class KlausDormannTests: XCTestCase {
return 0 return 0
} }
func runTest6502(processor: CSTestMachine6502Processor) { private func runTest6502(processor: CSTestMachine6502Processor) {
func errorForTrapAddress(_ address: UInt16) -> String? { func errorForTrapAddress(_ address: UInt16) -> String? {
switch address { switch address {
case 0x3399: return nil // success! case 0x3399: return nil // success!
@@ -70,7 +70,7 @@ class KlausDormannTests: XCTestCase {
XCTAssert(error == nil, "Failed with error \(error!)") XCTAssert(error == nil, "Failed with error \(error!)")
} }
func runTest65C02(processor: CSTestMachine6502Processor) { private func runTest65C02(processor: CSTestMachine6502Processor) {
func errorForTrapAddress(_ address: UInt16) -> String? { func errorForTrapAddress(_ address: UInt16) -> String? {
switch address { switch address {
case 0x24f1: return nil // success! case 0x24f1: return nil // success!

View File

@@ -11,8 +11,8 @@ import Foundation
class PatrikRakTests: XCTestCase, CSTestMachineTrapHandler { class PatrikRakTests: XCTestCase, CSTestMachineTrapHandler {
fileprivate var done = false private var done = false
fileprivate var output = "" private var output = ""
private func runTest(_ name: String) { private func runTest(_ name: String) {
if let filename = Bundle(for: type(of: self)).path(forResource: name, ofType: "tap") { if let filename = Bundle(for: type(of: self)).path(forResource: name, ofType: "tap") {
@@ -124,4 +124,5 @@ class PatrikRakTests: XCTestCase, CSTestMachineTrapHandler {
break break
} }
} }
} }

View File

@@ -1247,4 +1247,5 @@ class Z80MachineCycleTests: XCTestCase {
] ]
) )
} }
} }

View File

@@ -9,6 +9,7 @@
import XCTest import XCTest
class Z80MemptrTester: XCTestCase { class Z80MemptrTester: XCTestCase {
let machine = CSTestMachineZ80() let machine = CSTestMachineZ80()
private func test(program : [UInt8], initialValue : UInt16) -> UInt16 { private func test(program : [UInt8], initialValue : UInt16) -> UInt16 {

View File

@@ -11,8 +11,8 @@ import Foundation
class ZexallTests: XCTestCase, CSTestMachineTrapHandler { class ZexallTests: XCTestCase, CSTestMachineTrapHandler {
fileprivate var done = false private var done = false
fileprivate var output = "" private var output = ""
private func runTest(_ name: String) { private func runTest(_ name: String) {
if let filename = Bundle(for: type(of: self)).path(forResource: name, ofType: "com") { if let filename = Bundle(for: type(of: self)).path(forResource: name, ofType: "com") {
@@ -177,4 +177,5 @@ class ZexallTests: XCTestCase, CSTestMachineTrapHandler {
break break
} }
} }
} }