diff --git a/examples/README.md b/examples/README.md index 31bc64cf..dc0c7e5b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -37,6 +37,8 @@ If you are using a release version of the compiler, consider browsing the older * [Life](crossplatform/life.mfk) (C64/C16/Atari/ZX Spectrum) – Conway's game of life +* [Test suite](tests) (C64/ZX Spectrum) – the semi-official test-suite for Millfork + ## Commodore 64 examples ### Graphical examples diff --git a/examples/tests/framework.mfk b/examples/tests/framework.mfk new file mode 100644 index 00000000..c20e1158 --- /dev/null +++ b/examples/tests/framework.mfk @@ -0,0 +1,41 @@ +import stdio +import keyboard + +pointer current_suite_name +byte current_test_number +word failure_count = 0 + +void start_suite(pointer suite_name) { + putstrz("Running "z) + putstrz(suite_name) + new_line() + current_suite_name = suite_name + current_test_number = 0 +} + +void print_failure() { + putstrz("Test failed: "z) + putstrz(current_suite_name) + putstrz(" #"z) + putword(current_test_number) + new_line() + failure_count += 1 +} + +void assert_equal(word expected, word actual) { + if actual != expected { + print_failure() + putstrz("Expected:"z) + putword(expected) + putstrz("Actual:"z) + putword(actual) + new_line() + readkey() + } +} + +void begin_test() { + +} + +void byte \ No newline at end of file diff --git a/examples/tests/main.mfk b/examples/tests/main.mfk new file mode 100644 index 00000000..57f8404e --- /dev/null +++ b/examples/tests/main.mfk @@ -0,0 +1,8 @@ +import test_fibonacci + +void main() { + ensure_mixedcase() + test_fibonacci() + putstrz("Total failures: "z) + putword(failure_count) +} \ No newline at end of file diff --git a/examples/tests/test_fibonacci.mfk b/examples/tests/test_fibonacci.mfk new file mode 100644 index 00000000..5b4c65a6 --- /dev/null +++ b/examples/tests/test_fibonacci.mfk @@ -0,0 +1,18 @@ +import framework + +word fib(byte n) { + stack byte i + i = n + if i < 2 { return 1 } + return fib(i-1) + fib(i-2) +} + +void test_fibonacci() { + start_suite("Fibonacci"z) + + assert_equal(1, fib(0)) + assert_equal(1, fib(1)) + assert_equal(2, fib(2)) + assert_equal(3, fib(3)) + assert_equal(5, fib(4)) +} \ No newline at end of file