summaryrefslogtreecommitdiff
path: root/test/scripts/human_to_size_test.sh
blob: dfa01f72d31883ea94d5fbb52fb7661ed1bc2956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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
	printf "Bail out! human_to_size library (%s) could not be located\n" "${lib}"
	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' ''