summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-08-09 00:51:46 +0100
committerSam James <sam@gentoo.org>2024-08-11 11:11:01 +0100
commit39a108d58143886c78c76fa1d9000210102624f7 (patch)
treea3c329d0bdc6f14927a40e70c24d055283e7a5c0 /test-functions
parentRename quote_args_bash() to _quote_args_bash() (diff)
downloadgentoo-functions-39a108d58143886c78c76fa1d9000210102624f7.tar.gz
gentoo-functions-39a108d58143886c78c76fa1d9000210102624f7.tar.bz2
gentoo-functions-39a108d58143886c78c76fa1d9000210102624f7.zip
Handle integer overflow as a special case in _should_throttle()
At the point that the genfun_time variable overflows, guarantee that the should_throttle() function behaves as if no throttling should occur rather than proceed to perform arithmetic based on the result of deducting genfun_last_time from genfun_time. Further, guarantee that the should_throttle() function behaves as if no throttling should occur upon the very first occasion that it is called, provided that the call to update_time() succeeds. Finally, add a test case. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
Diffstat (limited to 'test-functions')
-rwxr-xr-xtest-functions47
1 files changed, 47 insertions, 0 deletions
diff --git a/test-functions b/test-functions
index 1f54208..34ffc41 100755
--- a/test-functions
+++ b/test-functions
@@ -1069,6 +1069,52 @@ test_update_time() {
iterate_tests 3 "$@"
}
+test_should_throttle() {
+ local bits max_int
+
+ genfun_time=
+ bits=30
+ while [ "${bits}" -lt 128 ]; do
+ # Dash is buggy and fails to handle $(( 1 << ++bits )).
+ bits=$(( bits + 1 ))
+ case $(( max_int = 1 << bits )) in
+ -*)
+ max_int=$(( max_int - 1 ))
+ genfun_time=$(( max_int - 4 ))
+ break
+ esac
+ done
+
+ if [ ! "${genfun_time}" ]; then
+ bailout "Failed to calculate the maximum possible integer value"
+ fi
+
+ # For the first test, genfun_last_time is not yet known. Therefore, the
+ # return value should always be 1. For the fifth test, integer overflow
+ # is expected to occur. Again, the return value should always be 1.
+ set -- \
+ ge 1 "${max_int}" \
+ eq 0 2 \
+ ge 1 1 \
+ ge 1 0 \
+ ge 1 2 \
+ eq 0 2 \
+ ge 1 1 \
+ ge 1 0
+
+ _update_time() {
+ true
+ }
+
+ callback() {
+ shift
+ test_description="_should_throttle $1 (${genfun_time}, $(( genfun_time += 1 )))"
+ _should_throttle "$1"
+ }
+
+ iterate_tests 3 "$@"
+}
+
iterate_tests() {
local code i j passed slice_width total
@@ -1152,6 +1198,7 @@ else
test_assign || rc=1
test_deref || rc=1
test_update_time || rc=1
+ test_should_throttle || rc=1
fi
cleanup_tmpdir