Browse Source

test(division): add condition to work with one argument

pull/1749/head
eslopfer 2 years ago
parent
commit
08c8ce153b
  1. 6
      sh/tests/division_test.sh
  2. 28
      sh/tests/student/division.sh

6
sh/tests/division_test.sh

@ -8,8 +8,14 @@ IFS='
script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
challenge() {
if [ $# -eq 1 ]
then
submitted=$(bash "$script_dirS"/student/division.sh $1)
expected=$(bash "$script_dirS"/solutions/division.sh $1)
else
submitted=$(bash "$script_dirS"/student/division.sh $1 $2)
expected=$(bash "$script_dirS"/solutions/division.sh $1 $2)
fi
diff <(echo "$submitted") <(echo "$expected")
}

28
sh/tests/student/division.sh

@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Check if two arguments were provided
if [ $# -ne 2 ]
then
echo "Error: two numbers must be provided"
exit 1
fi
# Check if the arguments are numeric
if ! [[ $1 =~ ^-?[0-9]*\.?[0-9]+$ ]] || ! [[ $2 =~ ^-?[0-9]*\.?[0-9]+$ ]]
then
echo "Error: both arguments must be numeric"
exit 1
fi
# Check if the second argument is not 0
if [ $(echo "$2 == 0" | bc) -eq 1 ]
then
echo "Error: division by zero is not allowed"
exit 1
fi
# Divide the first argument by the second using bc
result=$(echo "$1 / $2" | bc )
# Output the result
echo $result
Loading…
Cancel
Save