Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float.sh must print exactly three digits after the dot #241

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions compose-dev.yaml
@@ -0,0 +1,12 @@
services:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LaithAlebrahim this file seems to not belong to this repo :)

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

24 changes: 23 additions & 1 deletion help/float.sh
Expand Up @@ -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")
28 changes: 17 additions & 11 deletions tests/help/test-float.sh
Expand Up @@ -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"
10 changes: 5 additions & 5 deletions tests/metrics/test-multimetric.sh
Expand Up @@ -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"