From 49c916fea147a33c8cb8794edfe2b582022f9159 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Mon, 6 Apr 2020 13:27:39 +0200 Subject: [PATCH] Improve the test suite --- .gitignore | 1 + examples/tests/README.md | 38 +++++++++++++++++++++++++++++ examples/tests/framework.mfk | 42 ++++++++++++++++++++++++++------- examples/tests/main.mfk | 6 ++++- examples/tests/test_encconv.mfk | 15 ++++++++++++ examples/tests/test_pstring.mfk | 8 ++++++- examples/tests/test_string.mfk | 6 +++-- 7 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 examples/tests/README.md create mode 100644 examples/tests/test_encconv.mfk diff --git a/.gitignore b/.gitignore index 2b414c93..2450533c 100644 --- a/.gitignore +++ b/.gitignore @@ -54,6 +54,7 @@ HELLOCPC FIZZBUZZ __hw_bbcmicro __hw_cpc464 +tests-cpc-* #heap dumps java_pid* diff --git a/examples/tests/README.md b/examples/tests/README.md new file mode 100644 index 00000000..a2754c4e --- /dev/null +++ b/examples/tests/README.md @@ -0,0 +1,38 @@ +# The test suite + +This is the semi-official test suite for Millfork standard libraries. + +## Compiling + +Compile the `main.mfk` file and run the resulting program. +You are advised to try various different optimization options. + + millfork -t main.mfk + +Supported platforms: + +* Commodore 64, 128 and Plus/4, loadable program (`c64`, `c128`, `plus4`) + +* ZX Spectrum (`zxspectrum`) + +* NEC PC-88, bootable floppy (`pc88`) + +* MSX, cartridge (`msx_crt`) + +* Atari computers, loadable programs (`a8`) + +* Amstrad CPC, loadable programs (`cpc464`) + +Compiling with the `-D PRINT_SUCCESSES` will cause the suite to print all tests, including successful ones. +Otherwise, only failed tests will be printed. + +On each failed the following message will be printed: + + [FAIL] # + +To continue, press any key (on MSX, press RETURN). + +At the end of a successful run, the test suite should print + + Total failures: 0 + diff --git a/examples/tests/framework.mfk b/examples/tests/framework.mfk index cd4ad2a9..5bd82f64 100644 --- a/examples/tests/framework.mfk +++ b/examples/tests/framework.mfk @@ -1,10 +1,24 @@ import stdio -import keyboard pointer current_suite_name byte current_test_number word failure_count = 0 +#if ZX_SPECTRUM || CBM || NEC_PC88 || ATARI_8 || AMSTRAD_CPC + +import keyboard +alias wait_after_failure = readkey + +#elseif MSX + +alias wait_after_failure = readline + +#else + +macro void wait_after_failure() { while true {} } + +#endif + void start_suite(pointer suite_name) { putstrz("Running "z) putstrz(suite_name) @@ -14,7 +28,7 @@ void start_suite(pointer suite_name) { } void print_failure() { - putstrz("Test failed: "z) + putstrz("[FAIL]: "z) putstrz(current_suite_name) putstrz(" #"z) putword(current_test_number) @@ -22,6 +36,14 @@ void print_failure() { failure_count += 1 } +void print_success() { + putstrz("[ OK ]: "z) + putstrz(current_suite_name) + putstrz(" #"z) + putword(current_test_number) + new_line() +} + void assert_equal(word expected, word actual) { current_test_number += 1 if actual != expected { @@ -31,7 +53,11 @@ void assert_equal(word expected, word actual) { putstrz(" Actual: "z) putword(actual) new_line() - readkey() + wait_after_failure() +#if PRINT_SUCCESSES + } else { + print_success() +#endif } } @@ -40,11 +66,9 @@ void assert_true(bool result) { if not(result) { print_failure() readkey() +#if PRINT_SUCCESSES + } else { + print_success() +#endif } } - -void begin_test() { - -} - -void byte \ No newline at end of file diff --git a/examples/tests/main.mfk b/examples/tests/main.mfk index bd52493b..7ff7e3af 100644 --- a/examples/tests/main.mfk +++ b/examples/tests/main.mfk @@ -1,15 +1,19 @@ import test_fibonacci import test_pstring import test_string +import test_encconv void main() { ensure_mixedcase() + // readkey() test_fibonacci() test_string() test_pstring() + test_encconv() new_line() putstrz("Total failures: "z) putword(failure_count) -} \ No newline at end of file + while true {} // don't exit +} diff --git a/examples/tests/test_encconv.mfk b/examples/tests/test_encconv.mfk new file mode 100644 index 00000000..236847fc --- /dev/null +++ b/examples/tests/test_encconv.mfk @@ -0,0 +1,15 @@ +import framework +import encconv +import scrstring +import string + +void test_encconv() { + array buffer[256] + start_suite("encconv"z) + + strzcopy(buffer, "test"z) + strz_to_screencode(buffer) + assert_equal(0, scrstrzcmp("test"scrz, buffer)) + strz_from_screencode(buffer) + assert_equal(0, strzcmp("test"z, buffer)) +} diff --git a/examples/tests/test_pstring.mfk b/examples/tests/test_pstring.mfk index 8b52e9c9..989b37c8 100644 --- a/examples/tests/test_pstring.mfk +++ b/examples/tests/test_pstring.mfk @@ -4,6 +4,7 @@ import framework void test_pstring() { array buffer[256] + // #1-5 start_suite("pstring"z) assert_equal(0, pstrcmp("a"p, "a"p)) assert_equal(-1, pstrcmp("a"p, "b"p)) @@ -11,6 +12,7 @@ void test_pstring() { assert_equal(1, pstrcmp("b"p, "a"p)) assert_equal(1, pstrcmp("ab"p, "a"p)) + // #6-10 pstrcopy(buffer, "test"p) assert_equal(4, pstrlen(buffer)) assert_equal(0, pstrcmp("test"p, buffer)) @@ -19,5 +21,9 @@ void test_pstring() { assert_equal(0, pstrcmp("testhello"p, buffer)) assert_equal(1234, pstr2word("1234"p)) + // #11 + pstrcopy(buffer, "test****test"p) + pstrpaste(buffer+5, "test"p) + assert_equal(0, pstrcmp("testtesttest"p, buffer)) -} \ No newline at end of file +} diff --git a/examples/tests/test_string.mfk b/examples/tests/test_string.mfk index 27881a7e..59c90afe 100644 --- a/examples/tests/test_string.mfk +++ b/examples/tests/test_string.mfk @@ -19,5 +19,7 @@ void test_string() { assert_equal(0, strzcmp("testhello"z, buffer)) assert_equal(1234, strz2word("1234"z)) - -} \ No newline at end of file + strzcopy(buffer, "test****test"z) + strzpaste(buffer+4, "test"z) + assert_equal(0, strzcmp("testtesttest"z, buffer)) +}