Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Add function to generate random bool #24

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading