diff --git a/help/float.sh b/help/float.sh index b3d94446..79bab62d 100755 --- a/help/float.sh +++ b/help/float.sh @@ -20,14 +20,36 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +#!/bin/bash + +# Function to check if a string is a valid float number +is_float() { + local re="^-?[0-9]*\.?[0-9]+$" + [[ $1 =~ $re ]] +} + set -e set -o pipefail num=$(cat) +# Check if the input is empty or consists of only spaces +if [[ -z "${num}" || "${num}" =~ ^[[:space:]]+$ ]]; then + echo "0.000" # Return default value for empty input or input with spaces + exit +fi + if [ "${num}" == 'NaN' ]; then printf '%s' "${num}" exit fi +# Check if the input is a valid float number +if ! is_float "$num"; then + echo "0.000" + exit 1 +fi + +num_truncated=$(echo "$num * 1000 / 1" | bc) -(LC_NUMERIC=C printf '%.8f' "${num}" 2>/dev/null || echo 0) | sed -e 's/0\+$//' | sed -e 's/\.$//' +# Divide by 1000 to get back to the correct scale and format to three decimal places +(LC_NUMERIC=C printf '%.3f' $(echo "$num_truncated / 1000" | bc -l) 2>/dev/null || echo "0.000") diff --git a/tests/help/test-float.sh b/tests/help/test-float.sh index 88a69f1a..5d539a3a 100755 --- a/tests/help/test-float.sh +++ b/tests/help/test-float.sh @@ -25,32 +25,38 @@ set -o pipefail stdout=$2 -num=$(echo '.42' | "${LOCAL}/help/float.sh") -test "${num}" = '0.42' -echo "${num}" >> "${stdout}" +test "$(echo '.42' | "${LOCAL}/help/float.sh")" = '0.420' echo "👍🏻 Corrected floating point number" -test "$(echo '254.42' | "${LOCAL}/help/float.sh")" = '254.42' +test "$(echo '254.42' | "${LOCAL}/help/float.sh")" = '254.420' echo "👍🏻 Corrected longer floating point number" -test "$(echo '256' | "${LOCAL}/help/float.sh")" = '256' +test "$(echo '256' | "${LOCAL}/help/float.sh")" = '256.000' echo "👍🏻 Corrected integer number" -test "$(echo '09' | "${LOCAL}/help/float.sh")" = '9' +test "$(echo '09' | "${LOCAL}/help/float.sh")" = '9.000' echo "👍🏻 Corrected integer number with leading zero" -test "$(echo '' | "${LOCAL}/help/float.sh")" = '0' +test "$(echo '' | "${LOCAL}/help/float.sh")" = '0.000' echo "👍🏻 Corrected integer number with empty text" -test "$(echo ' ' | "${LOCAL}/help/float.sh")" = '0' +test "$(echo ' ' | "${LOCAL}/help/float.sh")" = '0.000' echo "👍🏻 Corrected integer number with spaces" -test "$(echo 'Blank' | "${LOCAL}/help/float.sh")" = '0' +test "$(echo 'Blank' | "${LOCAL}/help/float.sh")" = '0.000' echo "👍🏻 Corrected integer number with text input" test "$(echo 'NaN' | "${LOCAL}/help/float.sh")" = 'NaN' echo "👍🏻 Corrected integer number with NaN" -test "$(echo '.000000099' | "${LOCAL}/help/float.sh")" = '0.0000001' -echo "👍🏻 Corrected small precision number" +test "$(echo '.000000099' | "${LOCAL}/help/float.sh")" = '0.000' +echo "👍🏻 Printed decimal number with 3 digits" +test "$(echo '254' | "${LOCAL}/help/float.sh")" = '254.000' +echo "👍🏻 Printed decimal number with 3 digits" + +test "$(echo '0.3' | "${LOCAL}/help/float.sh")" = '0.300' +echo "👍🏻 Printed decimal number with 3 digits" + +test "$(echo '0.00023' | "${LOCAL}/help/float.sh")" = '0.000' +echo "👍🏻 Printed decimal number with 3 digits" diff --git a/tests/metrics/test-multimetric.sh b/tests/metrics/test-multimetric.sh index f57832b3..7cf134c3 100755 --- a/tests/metrics/test-multimetric.sh +++ b/tests/metrics/test-multimetric.sh @@ -49,10 +49,10 @@ EOT "${LOCAL}/metrics/multimetric.sh" "${java}" "${temp}/stdout" cat "${TARGET}/temp/multimetric.json" cat "${temp}/stdout" - grep "HSD 6 " "${temp}/stdout" - grep "HSE 1133.21789508 " "${temp}/stdout" - grep "HSV 188.86964918 " "${temp}/stdout" - grep "MIdx 100 " "${temp}/stdout" - grep "FOut 0 " "${temp}/stdout" + grep "hsd 6.000 " "${temp}/stdout" + grep "hse 1133.217 " "${temp}/stdout" + grep "hsv 188.869 " "${temp}/stdout" + grep "midx 100.000 " "${temp}/stdout" + grep "fout 0.000 " "${temp}/stdout" } > "${stdout}" 2>&1 echo "👍🏻 Correctly counted a few metrics"