diff options
author | elioat <elioat@tilde.institute> | 2025-01-09 16:57:05 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-01-09 16:57:05 -0500 |
commit | 662c3ceae2a4ffc598a8b99690a0cfdc0b4f47a8 (patch) | |
tree | 22c03fefb86b314341fbea70afaa95d91c7e7bbe /awk/forth/test.forth | |
parent | b9440ce32de013237d61d2cccab229537ba19caa (diff) | |
download | tour-662c3ceae2a4ffc598a8b99690a0cfdc0b4f47a8.tar.gz |
*
Diffstat (limited to 'awk/forth/test.forth')
-rw-r--r-- | awk/forth/test.forth | 201 |
1 files changed, 44 insertions, 157 deletions
diff --git a/awk/forth/test.forth b/awk/forth/test.forth index d746e66..a1f4f50 100644 --- a/awk/forth/test.forth +++ b/awk/forth/test.forth @@ -1,157 +1,44 @@ -( Basic Forth test suite ) - -testing "Basic stack operations" -5 dup . . test 5 test 5 - -testing "Addition" -3 4 + test 7 - -testing "Subtraction" -10 3 - test 7 - -testing "Multiplication" -6 7 * test 42 - -testing "Division" -20 4 / test 5 - -testing "Stack manipulation - rot" -1 2 3 rot test 1 - -testing "Stack manipulation - drop" -1 2 3 drop test 2 - -testing "Stack manipulation - nip" -1 2 3 nip test 3 - -testing "Stack manipulation - tuck" -1 2 tuck test 2 - -testing "Stack manipulation - over" -1 2 over test 1 - -testing "Variables" -variable x -5 x ! -x @ test 5 -10 x ! -x ? ( should print 10 ) - -testing "Negate" -5 negate test -5 - -testing "Absolute value" --7 abs test 7 - -testing "Maximum" -3 8 max test 8 - -testing "Minimum" -3 8 min test 3 - -testing "Modulo" -17 5 mod test 2 - -testing "Equality" -5 5 = test 1 -5 6 = test 0 - -testing "Word definition" -: square dup * ; -5 square test 25 - -testing "Complex word definition" -variable counter -: increment-counter counter @ 1 + counter ! ; -5 counter ! -increment-counter -counter @ test 6 - -testing "Basic conditional - if/then" -: test-if-1 ( n -- n ) dup 5 > if ." Greater than 5" then ; -6 test-if-1 test 6 ( should print "Greater than 5" and leave 6 ) -4 test-if-1 test 4 ( should print nothing and leave 4 ) - -testing "Basic conditional - if/else/then" -: test-if-2 ( n -- n ) dup 5 > if ." Greater" else ." Less=" then ; -6 test-if-2 test 6 ( should print "Greater" and leave 6 ) -4 test-if-2 test 4 ( should print "Less=" and leave 4 ) -5 test-if-2 test 5 ( should print "Less=" and leave 5 ) - -testing "Nested conditionals" -: test-if-3 ( n -- n ) - dup 10 > if - dup 20 > if - ." >20 " - then - ." >10 " - then ; -25 test-if-3 test 25 ( should print ">20 >10 " and leave 25 ) -15 test-if-3 test 15 ( should print ">10 " and leave 15 ) -5 test-if-3 test 5 ( should print nothing and leave 5 ) - -testing "Conditional with stack operations" -: test-if-4 ( n -- n n ) - dup 5 > if - dup - then ; -6 test-if-4 swap test 6 test 6 ( should leave 6 6 ) -4 test-if-4 test 4 ( should leave just 4 ) - -testing "Complex nested conditionals" -: test-if-5 ( n -- n ) - dup 0 < if - ." negative " - else - dup 100 > if - ." big " - else - dup 50 > if - ." medium " - else - ." small " - then - then - then ; --5 test-if-5 test -5 ( should print "negative " ) -150 test-if-5 test 150 ( should print "big " ) -75 test-if-5 test 75 ( should print "medium " ) -25 test-if-5 test 25 ( should print "small " ) - -testing "Conditionals in word definitions" -: abs-test ( n -- |n| ) dup 0 < if negate then ; --5 abs-test test 5 -5 abs-test test 5 - -testing "Complex conditional word" -: max-test ( n1 n2 -- max ) - 2dup > if - drop - else - nip - then ; -5 3 max-test test 5 -3 5 max-test test 5 - -( Try to use if outside of a definition - should error ) -testing "Compile-only words" -5 4 > if 42 then ( should print error about compile-only word ) - -testing "Comparison operators" -5 3 > test 1 ( 5 > 3 is true ) -3 5 > test 0 ( 3 > 5 is false ) -3 5 < test 1 ( 3 < 5 is true ) -5 3 < test 0 ( 5 < 3 is false ) -5 5 < test 0 ( 5 < 5 is false ) -5 5 > test 0 ( 5 > 5 is false ) - -testing "Comparison in conditionals" -: test-compare ( n -- ) -dup 5 > if ." Greater than 5" else -dup 5 < if ." Less than 5" else -." Equal to 5" then then ; -6 test-compare ( should print "Greater than 5" ) -4 test-compare ( should print "Less than 5" ) -5 test-compare ( should print "Equal to 5" ) - -bye \ No newline at end of file +( Basic arithmetic operations ) +2 3 + . ( expect: 5 ) +10 3 - . ( expect: 7 ) +4 5 * . ( expect: 20 ) +20 4 / . ( expect: 5 ) +7 3 mod . ( expect: 1 ) + +( Stack manipulation operations ) +5 dup . . ( expect: 5 5 ) +1 2 swap . . ( expect: 2 1 ) +1 2 over . . . ( expect: 1 2 1 ) +1 2 3 rot . . . ( expect: 2 3 1 ) +1 2 3 4 2 roll . . . . ( expect: 1 3 4 2 ) +5 drop +1 2 nip . ( expect: 2 ) +1 2 tuck . . . ( expect: 2 1 2 ) + +( Comparison operations ) +5 3 > . ( expect: 1 ) +3 5 < . ( expect: 1 ) +4 4 = . ( expect: 1 ) +5 3 < . ( expect: 0 ) +3 5 > . ( expect: 0 ) +4 5 = . ( expect: 0 ) + +( Math operations ) +5 negate . ( expect: -5 ) +-7 abs . ( expect: 7 ) +5 2 max . ( expect: 5 ) +5 2 min . ( expect: 2 ) + +( Complex stack manipulations ) +1 2 3 4 5 \ Put 5 numbers on stack +3 pick . ( expect: 2 ) +2 roll . ( expect: 4 ) +. . . . ( expect: 5 3 1 ) + +( Error handling tests ) +drop drop drop drop drop \ Clear stack +drop ( expect: Error: Stack underflow ) +. ( expect: Error: Stack underflow ) +5 0 / ( expect: Error: Division by zero ) + +bye \ No newline at end of file |