summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-06-02 18:46:43 +0100
committerKerin Millar <kfm@plushkava.net>2024-06-12 08:06:41 +0100
commit5ee035a364bea8d12bc8abfe769014e230a212a6 (patch)
tree0d16021306fa59a378f27386ee31d813aaf6167f /test-functions
parentAdd the warn() function (diff)
downloadgentoo-functions-5ee035a364bea8d12bc8abfe769014e230a212a6.tar.gz
gentoo-functions-5ee035a364bea8d12bc8abfe769014e230a212a6.tar.bz2
gentoo-functions-5ee035a364bea8d12bc8abfe769014e230a212a6.zip
Add the srandom() function
This is based on the behaviour of the special SRANDOM variable in bash. It should be noted that sh is capable of bitwise arithmetic. For instance, it is possible to clamp the number in such a way that it does not exceed the minimum possible value of RAND_MAX (32767). n=$(srandom) && : $(( n >>= 17 )) Signed-off-by: Kerin Millar <kfm@plushkava.net>
Diffstat (limited to 'test-functions')
-rwxr-xr-xtest-functions22
1 files changed, 22 insertions, 0 deletions
diff --git a/test-functions b/test-functions
index 8ff6380..0c4a222 100755
--- a/test-functions
+++ b/test-functions
@@ -397,6 +397,27 @@ test_yesno() {
iterate_tests 3 "$@"
}
+test_srandom() {
+ set -- \
+ eq 0 \
+ eq 0 \
+ eq 0 \
+ eq 0 \
+ eq 0
+
+ row=0
+
+ callback() {
+ number=$(srandom)
+ test_description="srandom ($(( row += 1 ))/5: ${number:-blank})"
+ is_int "${number}" \
+ && test "${number}" -ge 0 \
+ && test "${number}" -le 4294967295
+ }
+
+ iterate_tests 2 "$@"
+}
+
iterate_tests() {
slice_width=$1
shift
@@ -457,6 +478,7 @@ test_is_visible || rc=1
test_yesno || rc=1
test_die || rc=1
test_edo || rc=1
+test_srandom || rc=1
cleanup_tmpdir