Merge remote-tracking branch 'allan/working-split/vcs'
[pacman-ng.git] / test / scripts / human_to_size_test.sh
blobdbf1997aec59e938ccd431f7ac045307d59c56ed
1 #!/bin/bash
3 # source the library function
4 if [[ -z $1 || ! -f $1 ]]; then
5 printf "error: path to human_to_size library not provided or does not exist\n"
6 exit 1
7 fi
8 . "$1"
10 if ! type -t human_to_size >/dev/null; then
11 printf 'human_to_size function not found\n'
12 exit 1
15 parse_hts() {
16 local input=$1 expected=$2 result
18 (( ++testcount ))
20 result=$(human_to_size "$1")
21 if [[ $result = "$expected" ]]; then
22 (( ++pass ))
23 else
24 (( ++fail ))
25 printf '[TEST %3s]: FAIL\n' "$testcount"
26 printf ' input: %s\n' "$input"
27 printf ' output: %s\n' "$result"
28 printf ' expected: %s\n' "$expected"
32 summarize() {
33 if (( !fail )); then
34 printf 'All %s tests successful\n\n' "$testcount"
35 exit 0
36 else
37 printf '%s of %s tests failed\n\n' "$fail" "$testcount"
38 exit 1
41 trap 'summarize' EXIT
43 printf 'Beginning human_to_size tests\n'
45 # parse_hts <input> <expected output>
47 parse_hts '1MiB' 1048576
49 parse_hts '10XiB' ''
51 parse_hts '10 MiB' 10485760
53 parse_hts '10 XiB' ''
55 parse_hts '.1 TiB' 109951162778
57 parse_hts ' -3 KiB ' -3072
59 parse_hts 'foo3KiB' ''
61 parse_hts '3KiBfoo' ''
63 parse_hts '3kib' ''
65 parse_hts '+1KiB' 1024
67 parse_hts '+1.0 KiB' 1024
69 parse_hts '1MB' 1000000
71 parse_hts '1M' 1048576
73 parse_hts ' 1 G ' 1073741824
75 parse_hts '1Q' ''