From 02be941404a023fb3e1047dffe8c1578cbdc798f Mon Sep 17 00:00:00 2001 From: Milos Gligoric Date: Wed, 14 Aug 2024 13:46:18 -0500 Subject: [PATCH] lib: Add function to generate random bool (#24) --- src/util/rand.sh | 11 +++++++++++ src/util/rand_test.sh | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/util/rand.sh b/src/util/rand.sh index 05a37af..b74dbe6 100644 --- a/src/util/rand.sh +++ b/src/util/rand.sh @@ -13,6 +13,17 @@ readonly RAND_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # ---------- # Functions. +function rand_bool() { + # Generate random bool. + local ctx; is_ctx "${1}" && ctx="${1}" && shift + [ $# -ne 0 ] && { ctx_wn $ctx; return $EC; } + shift 0 || { ctx_wn $ctx; return $EC; } + + # No arguments to check. + + echo $(( ${RANDOM} % 2 )) +} + function rand_int() { # Generate random int. local ctx; is_ctx "${1}" && ctx="${1}" && shift diff --git a/src/util/rand_test.sh b/src/util/rand_test.sh index 66f98f4..4bc8ffc 100644 --- a/src/util/rand_test.sh +++ b/src/util/rand_test.sh @@ -15,6 +15,16 @@ readonly RAND_TEST_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # ---------- # Functions. +function test_rand_bool() { + local val + val=$(rand_bool) || \ + assert_fail + + is_bool "${val}" || \ + assert_fail +} +readonly -f test_rand_bool + function test_rand_int() { local val val=$(rand_int) || \