1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-08-13 17:03:46 -04:00
pacman/test/scripts/human_to_size_test.sh
Andrew Gregory 9263cc5874 provide default values for test scripts
Our test scripts currently require that the first argument be the
library or binary to be tested.  This makes integrating them with
automake which doesn't have a mechanism for passing specific arguments
to individual tests.  Instead, provide a default built from paths in the
environment which can be provided to all test scripts by automake.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
2013-08-21 11:00:18 +10:00

83 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
declare -i testcount=0 fail=0 pass=0 total=15
# source the library function
lib=${1:-${PMTEST_SCRIPTLIB_DIR}human_to_size.sh}
if [[ -z $lib || ! -f $lib ]]; then
echo "Bail out! human_to_size library ($lib) could not be located\n"
exit 1
fi
. "$lib"
if ! type -t human_to_size >/dev/null; then
printf 'Bail out! human_to_size function not found\n'
exit 1
fi
parse_hts() {
local input=$1 expected=$2 result
(( ++testcount ))
result=$(human_to_size "$1")
if [[ $result = "$expected" ]]; then
(( ++pass ))
printf "ok %d - %s\n" "$testcount" "$input"
else
(( ++fail ))
printf "not ok %d - %s\n" "$testcount" "$input"
printf '# [TEST %3s]: FAIL\n' "$testcount"
printf '# input: %s\n' "$input"
printf '# output: %s\n' "$result"
printf '# expected: %s\n' "$expected"
fi
}
summarize() {
if (( !fail )); then
printf '# All %s tests successful\n\n' "$testcount"
exit 0
else
printf '# %s of %s tests failed\n\n' "$fail" "$testcount"
exit 1
fi
}
trap 'summarize' EXIT
printf '# Beginning human_to_size tests\n'
echo "1..$total"
# parse_hts <input> <expected output>
parse_hts '1MiB' 1048576
parse_hts '10XiB' ''
parse_hts '10 MiB' 10485760
parse_hts '10 XiB' ''
parse_hts '.1 TiB' 109951162778
parse_hts ' -3 KiB ' -3072
parse_hts 'foo3KiB' ''
parse_hts '3KiBfoo' ''
parse_hts '3kib' ''
parse_hts '+1KiB' 1024
parse_hts '+1.0 KiB' 1024
parse_hts '1MB' 1000000
parse_hts '1M' 1048576
parse_hts ' 1 G ' 1073741824
parse_hts '1Q' ''