]>
Commit | Line | Data |
---|---|---|
b58489d2 DR |
1 | #!/bin/bash |
2 | ||
d3726bbd AG |
3 | declare -i testcount=0 fail=0 pass=0 total=15 |
4 | ||
b58489d2 | 5 | # source the library function |
9263cc58 AG |
6 | lib=${1:-${PMTEST_SCRIPTLIB_DIR}human_to_size.sh} |
7 | if [[ -z $lib || ! -f $lib ]]; then | |
8 | echo "Bail out! human_to_size library ($lib) could not be located\n" | |
b58489d2 DR |
9 | exit 1 |
10 | fi | |
9263cc58 | 11 | . "$lib" |
b58489d2 DR |
12 | |
13 | if ! type -t human_to_size >/dev/null; then | |
d3726bbd | 14 | printf 'Bail out! human_to_size function not found\n' |
b58489d2 DR |
15 | exit 1 |
16 | fi | |
17 | ||
18 | parse_hts() { | |
19 | local input=$1 expected=$2 result | |
20 | ||
21 | (( ++testcount )) | |
22 | ||
23 | result=$(human_to_size "$1") | |
24 | if [[ $result = "$expected" ]]; then | |
25 | (( ++pass )) | |
d3726bbd | 26 | printf "ok %d - %s\n" "$testcount" "$input" |
b58489d2 DR |
27 | else |
28 | (( ++fail )) | |
d3726bbd AG |
29 | printf "not ok %d - %s\n" "$testcount" "$input" |
30 | printf '# [TEST %3s]: FAIL\n' "$testcount" | |
31 | printf '# input: %s\n' "$input" | |
32 | printf '# output: %s\n' "$result" | |
33 | printf '# expected: %s\n' "$expected" | |
b58489d2 DR |
34 | fi |
35 | } | |
36 | ||
37 | summarize() { | |
38 | if (( !fail )); then | |
d3726bbd | 39 | printf '# All %s tests successful\n\n' "$testcount" |
b58489d2 DR |
40 | exit 0 |
41 | else | |
d3726bbd | 42 | printf '# %s of %s tests failed\n\n' "$fail" "$testcount" |
b58489d2 DR |
43 | exit 1 |
44 | fi | |
45 | } | |
46 | trap 'summarize' EXIT | |
47 | ||
d3726bbd AG |
48 | printf '# Beginning human_to_size tests\n' |
49 | ||
50 | echo "1..$total" | |
b58489d2 DR |
51 | |
52 | # parse_hts <input> <expected output> | |
53 | ||
54 | parse_hts '1MiB' 1048576 | |
55 | ||
56 | parse_hts '10XiB' '' | |
57 | ||
58 | parse_hts '10 MiB' 10485760 | |
59 | ||
60 | parse_hts '10 XiB' '' | |
61 | ||
62 | parse_hts '.1 TiB' 109951162778 | |
63 | ||
64 | parse_hts ' -3 KiB ' -3072 | |
65 | ||
66 | parse_hts 'foo3KiB' '' | |
67 | ||
68 | parse_hts '3KiBfoo' '' | |
69 | ||
70 | parse_hts '3kib' '' | |
71 | ||
72 | parse_hts '+1KiB' 1024 | |
73 | ||
74 | parse_hts '+1.0 KiB' 1024 | |
75 | ||
76 | parse_hts '1MB' 1000000 | |
77 | ||
78 | parse_hts '1M' 1048576 | |
79 | ||
80 | parse_hts ' 1 G ' 1073741824 | |
81 | ||
82 | parse_hts '1Q' '' |