From e3c885085d1603afece4e0d870d2ca775286c71f Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Mon, 30 Dec 2019 20:08:08 -0500 Subject: [PATCH] Consolidate tests Also, removed leftover in runbin.c (what did it do there??) that could result in tests falsely passing. --- tools/emul/runbin/runbin.c | 1 - tools/tests/unit/common.asm | 31 ++++ tools/tests/unit/test_lib_parse.asm | 220 +++++++++++++++++++++------- tools/tests/unit/test_parse_z.asm | 175 ---------------------- 4 files changed, 196 insertions(+), 231 deletions(-) delete mode 100644 tools/tests/unit/test_parse_z.asm diff --git a/tools/emul/runbin/runbin.c b/tools/emul/runbin/runbin.c index 2c5456c..5501d7a 100644 --- a/tools/emul/runbin/runbin.c +++ b/tools/emul/runbin/runbin.c @@ -28,7 +28,6 @@ int main() return 1; } emul_loop(); - if (m->cpu.R1.wr.HL) return m->cpu.R1.br.A; } diff --git a/tools/tests/unit/common.asm b/tools/tests/unit/common.asm index 9d3c3a8..6d48a3d 100644 --- a/tools/tests/unit/common.asm +++ b/tools/tests/unit/common.asm @@ -37,6 +37,37 @@ assertNZ: .msg: .db "Z set", CR, LF, 0 +assertC: + ret c + ld hl, .msg + call printstr + jp fail +.msg: + .db "C not set", CR, LF, 0 + +assertNC: + ret nc + ld hl, .msg + call printstr + jp fail +.msg: + .db "C set", CR, LF, 0 + +; Assert that A == B +assertEQB: + cp b + ret z + call printHex + call printcrlf + ld a, b + call printHex + call printcrlf + ld hl, .msg + call printstr + jp fail +.msg: + .db "A != B", CR, LF, 0 + ; Assert that HL == DE assertEQW: ld a, h diff --git a/tools/tests/unit/test_lib_parse.asm b/tools/tests/unit/test_lib_parse.asm index 788c01e..6cd4991 100644 --- a/tools/tests/unit/test_lib_parse.asm +++ b/tools/tests/unit/test_lib_parse.asm @@ -1,85 +1,195 @@ jp test +.inc "ascii.h" .inc "core.asm" +.equ STDIO_RAMSTART RAMSTART +.inc "stdio.asm" +.inc "common.asm" +.inc "lib/ari.asm" .inc "lib/util.asm" +.inc "lib/fmt.asm" .inc "lib/parse.asm" -zasmGetPC: - ret - -testNum: .db 1 - test: ld sp, 0xffff call testParseHex call testParseHexadecimal + call testParseDecimal + call testParseLiteral ; success xor a halt testParseHex: - ld a, '8' - call parseHex - jp c, fail - cp 8 - jp nz, fail - call nexttest + ld hl, .allGood + ld ix, .testGood + call testList + ld hl, .allBad + ld ix, .testBad + jp testList - ld a, 'e' +.testGood: + ld a, (hl) call parseHex - jp c, fail - cp 0xe - jp nz, fail - call nexttest + call assertNC + inc hl + ld b, (hl) + jp assertEQB - ld a, 'x' +.testBad: + ld a, (hl) call parseHex - jp nc, fail - call nexttest - ret + jp assertC + +.g1: + .db '8', 8 +.g2: + .db 'e', 0xe + +.allGood: + .dw .g1, .g2, 0 + +.b1: + .db 'x' + +.allBad: + .dw .b1, 0 testParseHexadecimal: - ld hl, .s99 + ld hl, .allGood + ld ix, .testGood + jp testList + +.testGood: + ld c, (hl) + inc hl + ld b, (hl) + inc hl call parseHexadecimal - jp nz, fail - ld a, e - cp 0x99 - jp nz, fail - call nexttest + call assertZ + ld l, c + ld h, b + jp assertEQW - ld hl, .saB - call parseHexadecimal - jp nz, fail - ld a, e - cp 0xab - jp nz, fail - call nexttest +.g1: + .dw 0x99 + .db "99", 0 +.g2: + .dw 0xab + .db "aB", 0 +; The string "Foo" will not cause a failure. We will parse up to "o" and then +; stop. +.g3: + .dw 0xf + .db "Foo", 0 - ; The string "Foo" will not cause a failure. We will parse up to "o" - ; and then stop. - ld hl, .sFoo - call parseHexadecimal - jp nz, fail - ld a, e - cp 0xf - call nexttest - ret +.allGood: + .dw .g1, .g2, .g3, 0 -.sFoo: .db "Foo", 0 -.saB: .db "aB", 0 -.s99: .db "99", 0 +testParseDecimal: + ld hl, .allGood + ld ix, .testGood + call testList + ld hl, .allBad + ld ix, .testBad + jp testList -nexttest: - ld a, (testNum) - inc a - ld (testNum), a - ret +.testGood: + ld c, (hl) + inc hl + ld b, (hl) + inc hl + call parseDecimalC + call assertZ + ld l, c + ld h, b + jp assertEQW -fail: - ld a, (testNum) - halt +.testBad: + call parseDecimalC + jp assertNZ -; used as RAM -sandbox: +.g1: + .dw 99 + .db "99", 0 +.g2: + .dw 65535 + .db "65535", 0 +; Space is also accepted as a number "ender" +.g3: + .dw 42 + .db "42 x", 0 +; Tab too +.g4: + .dw 42 + .db "42", 0x09, 'x', 0 +; A simple "0" works too! +.g5: + .dw 0 + .db '0', 0 + +.allGood: + .dw .g1, .g2, .g3, .g4, .g5, 0 + +; null string is invalid +.b1: + .db 0 +; too big, 5 chars +.b2: + .db "65536", 0 +.b3: + .db "99999", 0 +.b4: +; too big, 6 chars with rightmost chars being within bound + .db "111111", 0 + +.allBad: + .dw .b1, .b2, .b3, .b4, 0 + +testParseLiteral: + ld hl, .allGood + ld ix, .testGood + call testList + ld hl, .allBad + ld ix, .testBad + jp testList + +.testGood: + ld c, (hl) + inc hl + ld b, (hl) + inc hl + call parseLiteral + call assertZ + ld l, c + ld h, b + jp assertEQW + +.testBad: + call parseLiteral + jp assertNZ + +.g1: + .dw 99 + .db "99", 0 +.g2: + .dw 0x100 + .db "0x100", 0 +.g3: + .dw 0b0101 + .db "0b0101", 0 +.g4: + .dw 0b01010101 + .db "0b01010101", 0 + +.allGood: + .dw .g1, .g2, .g3, .g4, 0 + +.b1: + .db "Foo", 0 +.allBad: + .dw .b1, 0 + +RAMSTART: diff --git a/tools/tests/unit/test_parse_z.asm b/tools/tests/unit/test_parse_z.asm deleted file mode 100644 index f933808..0000000 --- a/tools/tests/unit/test_parse_z.asm +++ /dev/null @@ -1,175 +0,0 @@ -jp test - -.inc "core.asm" -.inc "str.asm" -.inc "lib/util.asm" -.inc "zasm/util.asm" -.inc "lib/parse.asm" - -; mocks. aren't used in tests -zasmGetPC: -zasmIsFirstPass: -symSelect: -symFindVal: - jp fail - -testNum: .db 1 - -s99: .db "99", 0 -s0x99: .db "0x99", 0 -s0x100: .db "0x100", 0 -s0b0101: .db "0b0101", 0 -s0b01010101: .db "0b01010101", 0 -sFoo: .db "Foo", 0 - -test: - ld sp, 0xffff - - call testLiteral - call testDecimal - - ; success - xor a - halt - -testLiteral: - ld hl, s99 - call parseLiteral - jp nz, fail - ld a, d - or a - jp nz, fail - ld a, e - cp 99 - jp nz, fail - call nexttest - - ld hl, s0x100 - call parseLiteral - jp nz, fail - ld a, d - cp 1 - jp nz, fail - ld a, e - or a - jp nz, fail - call nexttest - - ld hl, sFoo - call parseLiteral - jp z, fail - call nexttest - - ld hl, s0b0101 - call parseLiteral - jp nz, fail - ld a, d - or a - jp nz, fail - ld a, e - cp 0b0101 - jp nz, fail - call nexttest - - ld hl, s0b01010101 - call parseLiteral - jp nz, fail - ld a, d - or a - jp nz, fail - ld a, e - cp 0b01010101 - jp nz, fail - call nexttest - -.equ FOO 0x42 -.equ BAR @+1 - ld a, BAR - cp 0x43 - jp nz, fail - call nexttest - ret - -testDecimal: - -; test valid cases. We loop through tblDecimalValid for our cases - ld b, 5 - ld hl, .valid - -.loop1: - push hl ; --> lvl 1 - ; put expected number in IX - ld e, (hl) - inc hl - ld d, (hl) - inc hl - push de \ pop ix - call parseDecimalC ; --> DE - jp nz, fail - push ix \ pop hl ; push expected number in HL - ld a, h - cp d - jp nz, fail - ld a, l - cp e - jp nz, fail - pop hl ; <-- lvl 1 - ld de, 8 ; row size - add hl, de - djnz .loop1 - call nexttest - -; test invalid cases. We loop through tblDecimalInvalid for our cases - ld b, 4 - ld hl, .invalid - -.loop2: - push hl - call parseDecimalC - pop hl - jp z, fail - ld de, 7 ; row size - add hl, de - djnz .loop2 - call nexttest - ret - -; 2b int, 6b str, null-padded -.valid: - .dw 99 - .db "99", 0, 0, 0, 0 - .dw 65535 - .db "65535", 0 - ; Space is also accepted as a number "ender" - .dw 42 - .db "42 x", 0, 0 - ; Tab too - .dw 42 - .db "42", 0x09, 'x', 0, 0 - ; A simple "0" works too! - .dw 0 - .db '0', 0, 0, 0, 0, 0 - - -; 7b strings, null-padded -.invalid: - ; null string is invalid - .db 0, 0, 0, 0, 0, 0, 0 - ; too big, 5 chars - .db "65536", 0, 0 - .db "99999", 0, 0 - ; too big, 6 chars with rightmost chars being within bound - .db "111111", 0 - - -nexttest: - ld a, (testNum) - inc a - ld (testNum), a - ret - -fail: - ld a, (testNum) - halt - -