Fix human_readable HR_SI.
The sense of HR_SI got flipped (because the tests were all backwards).
With this, we get the same results as coreutils in the directories
I've tried, with the exception of cases where we give "better" results
by rounding to nearest rather than towards infinity.
Also extend the tests a little.
Maybe we should rename HR_SI to HR_1000, since "SI" is clearly
confusing? (coreutils uses --si to mean this, but I suspect it goes
largely unused.)
diff --git a/lib/lib.c b/lib/lib.c
index 27305ec..f5ed52f 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -870,7 +870,7 @@ void names_to_pid(char **names, int
(*callback)(pid_t pid, char *name))
int human_readable(char *buf, unsigned long long num, int style)
{
unsigned long long snap = 0;
- int len, unit, divisor = (style&HR_SI) ? 1024 : 1000;
+ int len, unit, divisor = (style&HR_SI) ? 1000 : 1024;
// Divide rounding up until we have 3 or fewer digits. Since the part we
// print is decimal, the test is 999 even when we divide by 1024.
diff --git a/tests/test_human_readable.test b/tests/test_human_readable.test
index a66b4fd..c34d2df 100755
--- a/tests/test_human_readable.test
+++ b/tests/test_human_readable.test
@@ -4,7 +4,15 @@
#testing "name" "command" "result" "infile" "stdin"
-testing "human_readable" "test_human_readable 123456789" "123M\n" "" ""
-testing "human_readable -b" "test_human_readable -b 123456789" "123MB\n" "" ""
-testing "human_readable -s" "test_human_readable -s 123456789" "123 M\n" "" ""
-testing "human_readable -i" "test_human_readable -i 5675" "5.5k\n" "" ""
+testing "human_readable l 1024" "test_human_readable 123456789" "118M\n" "" ""
+testing "human_readable l 1000" "test_human_readable -i 123456789"
"123M\n" "" ""
+testing "human_readable s 1024" "test_human_readable 5675" "5.5K\n" "" ""
+testing "human_readable s 1000" "test_human_readable -i 5675" "5.6k\n" "" ""
+
+# An example input where we give a better result than coreutils.
+# 267350/1024=261.08. We say 261K and coreutils says 262K.
+testing "human_readable" "test_human_readable 267350" "261K\n" "" ""
+
+testing "human_readable -b" "test_human_readable -b 123456789" "118MB\n" "" ""
+testing "human_readable -s" "test_human_readable -s 123456789" "118 M\n" "" ""
+testing "human_readable -bs" "test_human_readable -bs 123456789" "118
MB\n" "" ""
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net