diff options
Diffstat (limited to 'tests/zfs-tests/include/math.shlib')
-rw-r--r-- | tests/zfs-tests/include/math.shlib | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/zfs-tests/include/math.shlib b/tests/zfs-tests/include/math.shlib index ca1cbcb4e..4ed11fb4b 100644 --- a/tests/zfs-tests/include/math.shlib +++ b/tests/zfs-tests/include/math.shlib @@ -41,3 +41,28 @@ function within_percent return 1 } + +# +# Return 0 if the human readable string of the form <value>[suffix] can +# be converted to bytes. Allow suffixes are shown in the table below. +# +function to_bytes +{ + typeset size=$1 + typeset value=$(echo "$size" | grep -o '[0-9]\+') + + case $size in + *PB|*pb|*P|*p) factor='1024^5' ;; + *TB|*tb|*T|*t) factor='1024^4' ;; + *GB|*gb|*G|*g) factor='1024^3' ;; + *MB|*mb|*M|*m) factor='1024^2' ;; + *KB|*kb|*K|*k) factor='1024^1' ;; + *B|*b) factor='1024^0' ;; + *[!0-9.]*) return 1 ;; + *) factor='1024^0' ;; + esac + + echo "$value * ($factor)" | bc + + return 0 +} |