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