pebblisp/src/tests.sh

102 lines
3.0 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
PASSES=0
FAILS=0
VALGRIND=0
if [ "$1" == "-val" ]; then
VALGRIND=1
fi
pass() {
echo "$1 test PASSED"
((PASSES++))
}
fail() {
echo "$1 test FAILED"
((FAILS++))
}
check() {
local output
if (($VALGRIND == 1)); then
local output=$(valgrind -q --leak-check=full --track-origins=yes ./pebblisp "$2")
else
local output=$(./pebblisp "$2")
fi
if [ "$output" == "$3" ]; then
pass $1
else
fail $1
echo " Expected '$3' but received '$output'"
fi
}
check "PlainRet" "10" "10"
check "StrRetrn" "\"hey\"" "hey"
echo ""
check "Addition" "(+ 1093 102852)" "103945"
check "Multiply" "(*1000 10000)" "10000000"
check "Division" "(/ 6418425 65)" "98745"
echo ""
check "ChainAdd" "(+ 3917 17304 1293844 400 100000)" "1415465"
check "ChainMul" "(* 8263 23 123)" "23376027"
check "ChainDiv" "(/ 1493856 741 96 7)" "3"
echo ""
check "ListAddi" "(+ 5 (1 2 3 (+ 10 10)))" "( 6 7 8 25 )"
check "ListModu" "(% 5 (1 2 3 (* 11 11)))" "( 1 2 3 1 )"
check "ListsMul" "(* (10 20 30) (1 20 300))" "( 10 400 9000 )"
echo ""
check "GratrThn" "(> 23847123 19375933)" "T"
check "LessThan" "(< 23847123 19375933)" "F"
check "Equality" "(= 987654321 987654321 )" "T"
echo ""
check "IfReturn" "(if (T) 123456789 987654321)" "123456789"
check "IfRetTwo" "(if F 123456789 987654321)" "987654321"
check "EmptyCnd" "(if () T F)" "F"
# check "EtyLstLt" "(if (()) T F)" "T"
echo ""
check "RegLists" "(1 2 3 4 5)" "( 1 2 3 4 5 )"
check "MultiTypeList" "(10 20 \"rascals\")" "( 10 20 rascals )"
check "EmptyLst" "()" "( )"
check "EmptLst2" "( )" "( )"
echo ""
check "DenseSpc" "(+1093 102852)" "103945"
check "WideSpac" "( + 1093 102852 )" "103945"
check "VWidwSpc" " ( + 1093 102852 ) " "103945"
echo ""
check "DemoFunc" "(spent 68)" "Tip: \$13.60"
echo ""
check "MultStmt" "(def yee (fn (a) (* 10 a))) (yee 5)" "50"
check "DefinMap" "(def yee (fn (a) (* 10 a))) (map yee (5 10 2 (+ 12 0)))" "( 50 100 20 120 )"
check "FbnciSeq" "(def fib (fn (a) \
(if (< a 2) \
a \
(+ (fib (- a 1)) (fib (- a 2))) \
))) \
(fib 20)" "6765"
echo ""
check "ExplicitCat" "(cat \"Big\" \" Kitty\")" "Big Kitty"
check "CatNums" "(cat \"There are \" (+ 2 3) \" kitties\")" "There are 5 kitties"
check "ImplicitCat" "(+ \"There are \" (* 5 4) \" bonks\")" "There are 20 bonks"
# Not recommended
check "CatAssocLeft" "(+ 10 20 \" rascals\")" "30 rascals"
echo ""
check "EvalElems" "((* 10 10) 7)" "( 100 7 )"
check "Duplicate" "(def dupe (fn (a) (a a a))) (dupe (*10 10))" "( 100 100 100 )"
echo ""
check "Squaring" "(sq 9876)" "97535376"
check "Cubing" "(cube 81)" "531441"
check "Exponent" "(exp 9 9)" "387420489"
echo ""
if [ "$FAILS" -ne "0" ]; then
echo "$FAILS Tests Failed"
else
echo "$FAILS Tests Failed"
fi
echo "$PASSES Tests Passed"
echo ""