( 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