From 0fdcdd59971adb0ac5861992961c4c48accdfbba Mon Sep 17 00:00:00 2001 From: LaithAlebrahim Date: Fri, 22 Mar 2024 00:01:04 +0300 Subject: [PATCH 1/5] float.sh must print exactly three digits after the dot --- help/float.sh | 6 +++++- tests/help/test-float.sh | 28 +++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/help/float.sh b/help/float.sh index b3d94446..786ec840 100755 --- a/help/float.sh +++ b/help/float.sh @@ -30,4 +30,8 @@ if [ "${num}" == 'NaN' ]; then exit fi -(LC_NUMERIC=C printf '%.8f' "${num}" 2>/dev/null || echo 0) | sed -e 's/0\+$//' | sed -e 's/\.$//' +# Multiply by 1000 and use integer division to truncate +num_truncated=$(echo "$num * 1000 / 1" | bc) + +# 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) 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" From 008fd1ec44925fb22fc7e5e2fea446bbb985b16f Mon Sep 17 00:00:00 2001 From: LaithAlebrahim Date: Fri, 22 Mar 2024 03:55:15 +0000 Subject: [PATCH 2/5] Improve code --- compose-dev.yaml | 12 ++++++++++++ help/float.sh | 8 ++++++++ 2 files changed, 20 insertions(+) create mode 100644 compose-dev.yaml diff --git a/compose-dev.yaml b/compose-dev.yaml new file mode 100644 index 00000000..a92f7012 --- /dev/null +++ b/compose-dev.yaml @@ -0,0 +1,12 @@ +services: + app: + entrypoint: + - sleep + - infinity + image: docker/dev-environments-default:stable-1 + init: true + volumes: + - type: bind + source: /var/run/docker.sock + target: /var/run/docker.sock + diff --git a/help/float.sh b/help/float.sh index 786ec840..e4d934e4 100755 --- a/help/float.sh +++ b/help/float.sh @@ -20,11 +20,19 @@ # 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 + set -e set -o pipefail num=$(cat) +# Check if the input is empty or consists of only spaces +if [[ -z "${num// }" ]]; then + echo "0.000" # Return default value for empty input or input with spaces + exit +fi + if [ "${num}" == 'NaN' ]; then printf '%s' "${num}" exit From ebe8071994216cafef0bcaf38e50cd7e73adb3f0 Mon Sep 17 00:00:00 2001 From: LaithAlebrahim Date: Fri, 22 Mar 2024 04:29:17 +0000 Subject: [PATCH 3/5] Improve code --- help/float.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/help/float.sh b/help/float.sh index e4d934e4..79bab62d 100755 --- a/help/float.sh +++ b/help/float.sh @@ -22,13 +22,19 @@ # 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// }" ]]; then +if [[ -z "${num}" || "${num}" =~ ^[[:space:]]+$ ]]; then echo "0.000" # Return default value for empty input or input with spaces exit fi @@ -37,9 +43,13 @@ 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 -# Multiply by 1000 and use integer division to truncate num_truncated=$(echo "$num * 1000 / 1" | bc) # 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) +(LC_NUMERIC=C printf '%.3f' $(echo "$num_truncated / 1000" | bc -l) 2>/dev/null || echo "0.000") From 73f6a3f281d88882bdef0cebc873c9e472495e3c Mon Sep 17 00:00:00 2001 From: LaithAlebrahim Date: Fri, 22 Mar 2024 04:31:07 +0000 Subject: [PATCH 4/5] Fix test-multimetric with new values --- tests/metrics/test-multimetric.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/metrics/test-multimetric.sh b/tests/metrics/test-multimetric.sh index 8103be02..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" From 1a21a5629895f2ad3a0e555919dd95889d91d5a8 Mon Sep 17 00:00:00 2001 From: LaithAlebrahim Date: Fri, 22 Mar 2024 12:55:51 +0000 Subject: [PATCH 5/5] Delete files for dev testing --- compose-dev.yaml | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 compose-dev.yaml diff --git a/compose-dev.yaml b/compose-dev.yaml deleted file mode 100644 index a92f7012..00000000 --- a/compose-dev.yaml +++ /dev/null @@ -1,12 +0,0 @@ -services: - app: - entrypoint: - - sleep - - infinity - image: docker/dev-environments-default:stable-1 - init: true - volumes: - - type: bind - source: /var/run/docker.sock - target: /var/run/docker.sock -