Created
September 2, 2016 13:19
-
-
Save anonymous/46eb8bd56683d34ab6c51b30eaf9dd93 to your computer and use it in GitHub Desktop.
TryCF Gist
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
<cfscript> | |
function f(size, factor=1) { | |
var unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; | |
if (factor < unit.len() && size >= 1024) { | |
size = size/1024; | |
return f(size, ++factor); | |
} | |
return int(size) & " " & unit[factor]; | |
} | |
function assert(actual,expected) { | |
var passed = actual == expected; | |
writeOutput(passed & " | expected: #expected# got #actual#<br>"); | |
} | |
assert(f(1), '1 B'); | |
assert(f(1024), '1 KB'); | |
assert(f(1048576), '1 MB'); | |
assert(f(1234567), '1 MB'); | |
assert(f(9999999999), '9 GB' ); | |
assert(f(1099511627776), '1 TB' ); | |
assert(f(1125899906842624), '1 PB' ); | |
assert(f(1152921504606846976), '1024 PB'); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment