Added tests for stdlib math functions.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Wed, 7 Aug 2019 20:27:11 +0000 (13:27 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Wed, 7 Aug 2019 20:27:11 +0000 (13:27 -0700)
Added check for most-negative-number in `abs` subroutine.

stdlib/math.pvvs
stdlib_tests/5001_abs.pvvs [new file with mode: 0644]
stdlib_tests/5002_random.pvvs [new file with mode: 0644]
stdlib_tests/vv_test.py

index d618d83..3876c6e 100644 (file)
@@ -46,6 +46,17 @@ NTN                     | RTS
 @   abs(signed number)  <-- TOS
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NSSVTSSSTN              | Mark: 10001 (absolute value)
 @   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
 SNS                     | DUP
 NTTSSSTSSSTSSSSSSSSN    | BMI > 00010001 00000000
 NSNSSSTSSSTSSSSSSSTN    | JMP > 00010001 00000001
@@ -55,4 +66,10 @@ TSSN                    | MULTIPLY
 NSSVSSSTSSSTSSSSSSSTN   | Mark: 00010001 00000001
 NTN                     | RTS
 
 NSSVSSSTSSSTSSSSSSSTN   | Mark: 00010001 00000001
 NTN                     | RTS
 
+@ Special case: Push 0 and return.
+NSSVSSSTSSSTSSSSSSTSN   | Mark: 00010001 00000010
+SNN                     | DROP
+SSSSN                   | PUSH 0
+NTN                     | RTS
+
 #endif
 #endif
diff --git a/stdlib_tests/5001_abs.pvvs b/stdlib_tests/5001_abs.pvvs
new file mode 100644 (file)
index 0000000..7898e56
--- /dev/null
@@ -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 <logic.pvvs>
+#include <stdio.pvvs>
diff --git a/stdlib_tests/5002_random.pvvs b/stdlib_tests/5002_random.pvvs
new file mode 100644 (file)
index 0000000..9a869ee
--- /dev/null
@@ -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
index a121659..d3420f1 100755 (executable)
@@ -37,6 +37,8 @@ tests = [
         ['3005_or', '', '+0+1+1-1-1'],
         ['3006_xor', '', '+0+1+0-2-1'],
         ['4001_strlen', '', '+11'],
         ['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:
         ] 
 
 for test in tests: