summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDave Reisner <dreisner@archlinux.org>2012-04-25 22:27:19 -0400
committerDan McGee <dan@archlinux.org>2012-06-25 23:39:12 -0500
commitb58489d29a1898a4bff1b5da6ca4bfb14003dbfe (patch)
tree5c42f704cc671136ef43972cfdee1274e620e65f /test
parente183522e3168c4a31103b3c7910fa8d29333fb5a (diff)
downloadpacman-b58489d29a1898a4bff1b5da6ca4bfb14003dbfe.tar.xz
scripts/library: add human_to_size
This is a bash wrapper around an awk function that parses human readable sizes and returns their representative values in bytes, as a string. A small test harness is added to validate the functionality. Signed-off-by: Dave Reisner <dreisner@archlinux.org>
Diffstat (limited to 'test')
-rw-r--r--test/scripts/Makefile.am3
-rw-r--r--test/scripts/human_to_size_test.sh75
2 files changed, 77 insertions, 1 deletions
diff --git a/test/scripts/Makefile.am b/test/scripts/Makefile.am
index b949e880..d525d894 100644
--- a/test/scripts/Makefile.am
+++ b/test/scripts/Makefile.am
@@ -1,5 +1,6 @@
check_SCRIPTS = \
- parseopts_test.sh
+ parseopts_test.sh \
+ human_to_size_test.sh
noinst_SCRIPTS = $(check_SCRIPTS)
diff --git a/test/scripts/human_to_size_test.sh b/test/scripts/human_to_size_test.sh
new file mode 100644
index 00000000..1d747946
--- /dev/null
+++ b/test/scripts/human_to_size_test.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+# source the library function
+if [[ -z $1 || ! -f $1 ]]; then
+ printf "error: path to human_to_size library not provided or does not exist\n"
+ exit 1
+fi
+. "$1"
+
+if ! type -t human_to_size >/dev/null; then
+ printf '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 ))
+ else
+ (( ++fail ))
+ 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' "$testcount"
+ exit 0
+ else
+ printf '%s of %s tests failed\n' "$fail" "$testcount"
+ exit 1
+ fi
+}
+trap 'summarize' EXIT
+
+printf 'Beginning human_to_size tests\n'
+
+# 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' ''