From 08c8ce153b07904b6d7d4bdd53f6d00dd277e731 Mon Sep 17 00:00:00 2001 From: eslopfer Date: Mon, 9 Jan 2023 07:17:42 +0000 Subject: [PATCH] test(division): add condition to work with one argument --- sh/tests/division_test.sh | 6 ++++++ sh/tests/student/division.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 sh/tests/student/division.sh diff --git a/sh/tests/division_test.sh b/sh/tests/division_test.sh index d106227e0..23ba0ab09 100755 --- a/sh/tests/division_test.sh +++ b/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") } diff --git a/sh/tests/student/division.sh b/sh/tests/student/division.sh new file mode 100644 index 000000000..8f5b62ef9 --- /dev/null +++ b/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