psh-fractional/tester
Mia T. Rain 0207f421f4
-- subtraction:
---- had issues due to an edge case before:
------ eg: 49518.81665 - 22399.4358
------ produces: 47278.87307
------ correct:  27119.38085
---- this happened when ${#1} > ${#2}
---- if $2 contains decimals
-- tester:
---- now exits `1` if answer is "false"
-- division
---- debugging on going
2022-04-22 14:37:13 -04:00

67 lines
1.8 KiB
Bash
Executable file

#!/bin/sh
set -e
# tests all functions 3 times using numbers generated using $RPATH
# psh-prng is used here with my testing
# math is comparred to bc
fd() {
m="$1"; n=1; while [ "$m" ]; do
next="${m%?}"
[ "$n" -eq "${#1}" ] && {
echo "${m%next}"
break
}
: $((n+=1)); m="$next"
done
}
ran() {
ran="$(${RPATH})"
ran="${ran#0.}"
until [ "${#ran}" -le 5 ]; do
ran="${ran%?}"
done
echo "${ran}"
}
case "$1" in
"") types="addition/add subtraction/sub multiplication/mul division/div" ;;
*) types="$1"
esac
IFS=" "; for i in ${types}; do
for n in 1 2; do
ran="$(fd $(ran))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do
ran="$(fd $(ran))"; : $((ran-=1))
done
case $ran in
0) ran=""; tn="$(ran).$(ran) $(ran)";;
1) ran=""; tn="$(ran) $(ran).$(ran)"
ran="$(fd $(ran))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do
ran="$(fd $(ran))"; : $((ran-=1))
done
case $ran in
1) ran=""; tn="${tn% *}.$(ran) ${tn#* }"
esac
esac
# use random number to determine if number will have decimal place
case $i in
*addition/add) op="+";;
*subtraction/sub) op="-";;
*multiplication/mul) op="*";;
*division/div) op="/"
esac
aws="$(echo ${tn% *}${op}${tn#* } | bc -l)"; case "$(fd $aws)" in
'.') aws="0${aws}"
esac
printf ' %s\n%s %s\n_____\nbc: %s\n' "${tn% *}" "$op" "${tn#* }" "$aws"
oaws="$aws"; aws="${aws%.*}${aws#*.}"
[ "$aws" -ge 9223372036854775807 ] 2>/dev/null && {
printf 'psh: too large...\n\n'
} || printf 'psh: %s\n' "$(${i} ${tn% *} ${tn#* })"
[ "$oaws" = "$(${i} ${tn% *} ${tn#* })" ] && {
status=0
} || status=1
case "$status" in
0) printf 'Correct!\n\n' ;;
1) printf 'False..\n\n' && exit 1
esac
done
done