From 7359501c4b44efadc76eef5570008dd52f6767fc Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Wed, 7 Aug 2019 13:27:11 -0700 Subject: [PATCH] Added tests for stdlib math functions. Added check for most-negative-number in `abs` subroutine. --- stdlib/math.pvvs | 17 +++++++++++++++++ stdlib_tests/5001_abs.pvvs | 26 ++++++++++++++++++++++++++ stdlib_tests/5002_random.pvvs | 10 ++++++++++ stdlib_tests/vv_test.py | 2 ++ 4 files changed, 55 insertions(+) create mode 100644 stdlib_tests/5001_abs.pvvs create mode 100644 stdlib_tests/5002_random.pvvs diff --git a/stdlib/math.pvvs b/stdlib/math.pvvs index d618d83..3876c6e 100644 --- a/stdlib/math.pvvs +++ b/stdlib/math.pvvs @@ -46,6 +46,17 @@ NTN | RTS @ abs(signed number) <-- TOS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NSSVTSSSTN | Mark: 10001 (absolute value) + +@ Catch -(2^63) as a special case since its absolute value will overflow +@ a twos-complement 64-bit word. Return zero as though the absolute value +@ overflowed to the bottom of the non-negative integers rather than +@ overflowing back to the most negative integer. +SNS | DUP +SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63) +TSST | SUBTRACT +NTSSSSTSSSTSSSSSSTSN | BRZ > 00010001 00000010 + +@ Handle all the other numbers. SNS | DUP NTTSSSTSSSTSSSSSSSSN | BMI > 00010001 00000000 NSNSSSTSSSTSSSSSSSTN | JMP > 00010001 00000001 @@ -55,4 +66,10 @@ TSSN | MULTIPLY NSSVSSSTSSSTSSSSSSSTN | Mark: 00010001 00000001 NTN | RTS +@ Special case: Push 0 and return. +NSSVSSSTSSSTSSSSSSTSN | Mark: 00010001 00000010 +SNN | DROP +SSSSN | PUSH 0 +NTN | RTS + #endif diff --git a/stdlib_tests/5001_abs.pvvs b/stdlib_tests/5001_abs.pvvs new file mode 100644 index 0000000..7898e56 --- /dev/null +++ b/stdlib_tests/5001_abs.pvvs @@ -0,0 +1,26 @@ +@ +1 should remain +1 +SSSTN | PUSH +1 +NSTTSSSTN | JSR > 10001 (abs) +NSTTSSTN | JSR > 1001 (print number from stack) + +@ -1 should become +1 +SSTTN | PUSH -1 +NSTTSSSTN | JSR > 10001 (abs) +NSTTSSTN | JSR > 1001 (print number from stack) + +@ 0 should remain 0 +SSSSN | PUSH 0 +NSTTSSSTN | JSR > 10001 (abs) +NSTTSSTN | JSR > 1001 (print number from stack) + +@ Test the most negative number. +@ It is impossible to return the true absolute value but should return zero. +SSSTN | PUSH +1 +SSSTTTTTTN | PUSH +63 +NSTTSTTSTN | JSR > 101101 (lshift) +NSTTSSSTN | JSR > 10001 (abs) +NSTTSSTN | JSR > 1001 (print number from stack) +NNN | DIE + +#include +#include diff --git a/stdlib_tests/5002_random.pvvs b/stdlib_tests/5002_random.pvvs new file mode 100644 index 0000000..9a869ee --- /dev/null +++ b/stdlib_tests/5002_random.pvvs @@ -0,0 +1,10 @@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ +@ This test intentionally blank. +@ +@ If there comes a day when the tests are extended beyond testing basic +@ functionality, include random in the new tests. +@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +NNN | Die diff --git a/stdlib_tests/vv_test.py b/stdlib_tests/vv_test.py index a121659..d3420f1 100755 --- a/stdlib_tests/vv_test.py +++ b/stdlib_tests/vv_test.py @@ -37,6 +37,8 @@ tests = [ ['3005_or', '', '+0+1+1-1-1'], ['3006_xor', '', '+0+1+0-2-1'], ['4001_strlen', '', '+11'], + ['5001_abs', '', '+1+1+0+0'], + ['5002_random', '', ''], ] for test in tests: -- 2.20.1