-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes and adds tests for the dehumanize function
- Loading branch information
Arturo Pie
committed
Oct 3, 2019
1 parent
bd5fbab
commit b5ff3da
Showing
3 changed files
with
50 additions
and
8 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
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,39 @@ | ||
#!/bin/bash | ||
set -o pipefail | ||
|
||
. "$(dirname "$0")"/dehumanize.sh | ||
|
||
test_without_unit(){ | ||
assertEquals 45 $(dehumanize 45) | ||
} | ||
|
||
test_bytes(){ | ||
assertEquals 45 $(dehumanize 45b) | ||
assertEquals 45 $(dehumanize 45B) | ||
} | ||
|
||
test_kilobytes(){ | ||
assertEquals 46080 $(dehumanize 45kb) | ||
assertEquals 46080 $(dehumanize 45KB) | ||
} | ||
|
||
test_megabytes(){ | ||
assertEquals 47185920 $(dehumanize 45mb) | ||
assertEquals 47185920 $(dehumanize 45MB) | ||
} | ||
|
||
test_gigabytes(){ | ||
assertEquals 48318382080 $(dehumanize 45gb) | ||
assertEquals 48318382080 $(dehumanize 45GB) | ||
} | ||
|
||
test_terabytes(){ | ||
assertEquals 49478023249920 $(dehumanize 45tb) | ||
assertEquals 49478023249920 $(dehumanize 45TB) | ||
} | ||
|
||
test_using_decimals(){ | ||
assertEquals 1610612736 $(dehumanize 1.5gb) | ||
} | ||
|
||
. shunit2 |
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,10 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Converts human-readable units like 1.43K and 120.3M to bytes | ||
dehumanize() { | ||
awk '/[0-9][bB]?$/ {printf "%u\n", $1*1} | ||
/[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024*1024)} | ||
/[gG][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} | ||
/[mM][bB]?$/ {printf "%u\n", $1*(1024*1024)} | ||
/[kK][bB]?$/ {printf "%u\n", $1*1024}' <<< "$1" | ||
} |