Skip to content

Commit

Permalink
util: Functions for converting binary to decimal and back and alphabe…
Browse files Browse the repository at this point in the history
…t print function (#20)
  • Loading branch information
gliga authored May 6, 2024
1 parent 1b4454f commit ad11606
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/util/binary.sh
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
}
48 changes: 48 additions & 0 deletions src/util/binary_test.sh
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
23 changes: 23 additions & 0 deletions src/util/char.sh
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}
}
2 changes: 2 additions & 0 deletions src/util/p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ readonly UTIL_PACKAGE=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${UTIL_PACKAGE}/math.sh
. ${UTIL_PACKAGE}/rand.sh
. ${UTIL_PACKAGE}/strings.sh
. ${UTIL_PACKAGE}/char.sh
. ${UTIL_PACKAGE}/regexp.sh
. ${UTIL_PACKAGE}/flags.sh
. ${UTIL_PACKAGE}/complex.sh
. ${UTIL_PACKAGE}/user.sh
. ${UTIL_PACKAGE}/binary.sh

0 comments on commit ad11606

Please sign in to comment.