-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: Functions for converting binary to decimal and back and alphabe…
…t print function (#20)
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
# | ||
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE | ||
# | ||
# Util functions to manipulate binary numbers. | ||
|
||
if [ -n "${BINARY_MOD:-}" ]; then return 0; fi | ||
readonly BINARY_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
. ${BINARY_MOD}/../lang/p.sh | ||
|
||
|
||
# ---------- | ||
# Functions. | ||
|
||
function binary_d2b() { | ||
# From decimal to binary. | ||
local ctx; is_ctx "${1}" && ctx="${1}" && shift | ||
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; } | ||
local -r n="${1}" | ||
shift 1 || { ctx_wn $ctx; return $EC; } | ||
|
||
# TODO: check for errors. | ||
echo "obase=2; ${n}" | bc | ||
} | ||
|
||
function binary_b2d() { | ||
# From binary to decimal. | ||
local ctx; is_ctx "${1}" && ctx="${1}" && shift | ||
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; } | ||
local -r n="${1}" | ||
shift 1 || { ctx_wn $ctx; return $EC; } | ||
|
||
# TODO: check for errors. | ||
echo "ibase=2;obase=A; ${n}" | bc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/bash | ||
# | ||
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE | ||
# | ||
# Unit tests for the binary functions. | ||
|
||
if [ -n "${BINARY_TEST_MOD:-}" ]; then return 0; fi | ||
readonly BINARY_TEST_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
. ${BINARY_TEST_MOD}/binary.sh | ||
. ${BINARY_TEST_MOD}/../testing/bunit.sh | ||
|
||
|
||
# ---------- | ||
# Functions. | ||
|
||
function test_binary_d2b() { | ||
local res | ||
|
||
res=$(binary_d2b "3") | ||
[ "${res}" = "11" ] || \ | ||
assert_fail | ||
|
||
res=$(binary_d2b "33") | ||
[ "${res}" = "100001" ] || \ | ||
assert_fail | ||
|
||
return 0 | ||
} | ||
readonly -f test_binary_d2b | ||
|
||
function test_binary_b2d() { | ||
local res | ||
|
||
res=$(binary_b2d "011") | ||
[ "${res}" = "3" ] || \ | ||
assert_fail | ||
|
||
res=$(binary_b2d "100001") | ||
[ "${res}" = "33" ] || \ | ||
assert_fail | ||
|
||
#binary_b2d "100001a" && \ | ||
#assert_fail | ||
|
||
return 0 | ||
} | ||
readonly -f test_binary_b2d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
# | ||
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE | ||
# | ||
# Util char functions. | ||
|
||
if [ -n "${CHAR_MOD:-}" ]; then return 0; fi | ||
readonly CHAR_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
. ${CHAR_MOD}/../lang/p.sh | ||
|
||
|
||
# ---------- | ||
# Functions. | ||
|
||
function char_alphabet() { | ||
# Print alphabet on a single line. | ||
local ctx; is_ctx "${1}" && ctx="${1}" && shift | ||
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; } | ||
shift 0 || { ctx_wn $ctx; return $EC; } | ||
|
||
echo {a..z} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters