Skip to content

Commit

Permalink
lib: Add function to generate random bool
Browse files Browse the repository at this point in the history
  • Loading branch information
gliga committed Aug 14, 2024
1 parent f6ffab5 commit 3a6db0e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/util/rand.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/util/rand_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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) || \
Expand Down

0 comments on commit 3a6db0e

Please sign in to comment.