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