]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/zfs-tests/include/math.shlib
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / tests / zfs-tests / include / math.shlib
1 #
2 # This file and its contents are supplied under the terms of the
3 # Common Development and Distribution License ("CDDL"), version 1.0.
4 # You may only use this file in accordance with the terms of version
5 # 1.0 of the CDDL.
6 #
7 # A full copy of the text of the CDDL should have accompanied this
8 # source.  A copy of the CDDL is also available via the Internet at
9 # http://www.illumos.org/license/CDDL.
10 #
11
12 #
13 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
14 #
15
16 #
17 # Return 0 if the percentage difference between $a and $b is $percent or
18 # greater. Return 1 if the percentage is lower or if we would divide by
19 # zero. For use like this:
20 #
21 # Do $action if the calculated percentage is greater or equal to that passed in:
22 #       within_percent A B P && $action
23 # Do $action if the calculated percentage is less than that passed in:
24 #       within_percent A B P || $action
25 #
26 function within_percent
27 {
28         typeset a=$1
29         typeset b=$1
30         typeset percent=$3
31
32         # Set $a or $b to $2 such that a >= b
33         [[ '1' = $(echo "if ($2 > $a) 1 else 0" | bc) ]] && a=$2 || b=$2
34
35         # Prevent division by 0
36         [[ $a =~ [1-9] ]] || return 1
37
38         typeset p=$(echo "scale=2; $b * 100 / $a" | bc)
39         log_note "Comparing $a and $b given $percent% (calculated: $p%)"
40         [[ '1' = $(echo "scale=2; if ($p >= $percent) 1 else 0" | bc) ]] && \
41             return 0
42
43         return 1
44 }
45
46 #
47 # Return 0 if value is within +/-tolerance of target.
48 # Return 1 if value exceeds our tolerance.
49 # For use like this:
50 #
51 # Do $action if value is within the tolerance from target passed in:
52 #       within_tolerance VAL TAR TOL && $action
53 # Do $action if value surpasses the tolerance from target passed in:
54 #       within_tolerance VAL TAR TOL || $action
55 #
56 function within_tolerance #value #target #tolerance
57 {
58         typeset val=$1
59         typeset target=$2
60         typeset tol=$3
61
62         typeset diff=$((abs(val - target)))
63         log_note "Checking if $val is within +/-$tol of $target (diff: $diff)"
64         ((diff <= tol)) && return 0
65
66         return 1
67 }
68
69 #
70 # Return 0 if the human readable string of the form <value>[suffix] can
71 # be converted to bytes.  Allow suffixes are shown in the table below.
72 #
73 function to_bytes
74 {
75         typeset size=$1
76         typeset value=$(echo "$size" | grep -o '[0-9]\+')
77
78         case $size in
79                 *PB|*pb|*P|*p)  factor='1024^5' ;;
80                 *TB|*tb|*T|*t)  factor='1024^4' ;;
81                 *GB|*gb|*G|*g)  factor='1024^3' ;;
82                 *MB|*mb|*M|*m)  factor='1024^2' ;;
83                 *KB|*kb|*K|*k)  factor='1024^1' ;;
84                 *B|*b)          factor='1024^0' ;;
85                 *[!0-9.]*)      return 1 ;;
86                 *)              factor='1024^0' ;;
87         esac
88
89         echo "$value * ($factor)" | bc
90
91         return 0
92 }
93
94 #
95 # Verify $a is equal to $b, otherwise raise an error specifying
96 # the $type of values being compared
97 #
98 function verify_eq # <a> <b> <type>
99 {
100         typeset a=$1
101         typeset b=$2
102         typeset type=$3
103
104         if [[ $a -ne $b ]]; then
105                 log_fail "Compared $type should be equal: $a != $b"
106         fi
107 }
108
109 #
110 # Verify $a is not equal to $b, otherwise raise an error specifying
111 # the $type of values being compared
112 #
113 function verify_ne # <a> <b> <type>
114 {
115         typeset a=$1
116         typeset b=$2
117         typeset type=$3
118
119         if [[ $a -eq $b ]]; then
120                 log_fail "Compared $type should be not equal: $a == $b"
121         fi
122 }
123
124 # A simple function to get a random number between two bounds (inclusive)
125 #
126 # Probably not the most efficient for large ranges, but it's okay.
127 #
128 # Note since we're using $RANDOM, 32767 is the largest number we
129 # can accept as the upper bound.
130 #
131 # $1 lower bound
132 # $2 upper bound
133 function random_int_between
134 {
135         typeset -i min=$1
136         typeset -i max=$2
137         typeset -i rand=0
138
139         while [[ $rand -lt $min ]] ; do
140                 rand=$(( $RANDOM % $max + 1))
141         done
142
143         echo $rand
144 }