aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-03-26 17:36:33 -0500
committerRob Landley <rob@landley.net>2018-03-26 17:36:33 -0500
commit16c4357f0d0df0ce4f91d424cc57ce571ccf4e91 (patch)
tree46aad6b838a96c243465af0cd6c70d7ac37a70dd
parent48ac4c6c4fb7d25040cadd3c046578dd7530da00 (diff)
downloadtoybox-16c4357f0d0df0ce4f91d424cc57ce571ccf4e91.tar.gz
Rename demo_human_readable.c->demo_number.c and have it do atolx() too.
-rwxr-xr-xtests/demo_number.test27
-rwxr-xr-xtests/test_human_readable.test19
-rw-r--r--toys/example/demo_human_readable.c23
-rw-r--r--toys/example/demo_number.c33
4 files changed, 60 insertions, 42 deletions
diff --git a/tests/demo_number.test b/tests/demo_number.test
new file mode 100755
index 00000000..0d86476f
--- /dev/null
+++ b/tests/demo_number.test
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+testcmd "l 1024" "-h 123456789" "118M\n" "" ""
+testcmd "l 1000" "-d 123456789" "123M\n" "" ""
+testcmd "s 1024" "-h 5675" "5.5K\n" "" ""
+testcmd "s 1000" "-d 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.
+testcmd "edge case" "-h 267350" "261K\n" "" ""
+
+testcmd "-b" "-b 123" "123B\n" "" ""
+testcmd "-b" "-b 123456789" "118M\n" "" ""
+testcmd "-s" "-s 123456789" "118 M\n" "" ""
+testcmd "-bs" "-bs 123456789" "118 M\n" "" ""
+
+testcmd "units" "-b 1c 1b 1k 1kd 1m 1md 1g 1gd 1t 1td 1e 1ed" \
+ "1B\n512B\n1.0K\n0.9K\n1.0M\n977K\n1.0G\n954M\n1.0T\n931G\n1.0E\n888P\n" \
+ "" ""
+
+testcmd "decimal units" "-d 1c 1b 1k 1kd 1m 1md 1g 1gd 1t 1td 1e 1ed" \
+ "1\n512\n1.0k\n1.0k\n1.0M\n1.0M\n1.0G\n1.0G\n1.1T\n1.0T\n1.1E\n1.0E\n" \
+ "" ""
diff --git a/tests/test_human_readable.test b/tests/test_human_readable.test
deleted file mode 100755
index 0f1bfb7a..00000000
--- a/tests/test_human_readable.test
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/bin/bash
-
-[ -f testing.sh ] && . testing.sh
-
-#testing "name" "command" "result" "infile" "stdin"
-
-testing "l 1024" "test_human_readable 123456789" "118M\n" "" ""
-testing "l 1000" "test_human_readable -i 123456789" "123M\n" "" ""
-testing "s 1024" "test_human_readable 5675" "5.5K\n" "" ""
-testing "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 "test_human_readable" "test_human_readable 267350" "261K\n" "" ""
-
-testing "-b" "test_human_readable -b 123" "123B\n" "" ""
-testing "-b" "test_human_readable -b 123456789" "118M\n" "" ""
-testing "-s" "test_human_readable -s 123456789" "118 M\n" "" ""
-testing "-bs" "test_human_readable -bs 123456789" "118 M\n" "" ""
diff --git a/toys/example/demo_human_readable.c b/toys/example/demo_human_readable.c
deleted file mode 100644
index 9fff2626..00000000
--- a/toys/example/demo_human_readable.c
+++ /dev/null
@@ -1,23 +0,0 @@
-/* test_human_readable.c - Expose lib/lib.c human_readable() for testing.
- *
- * Copyright 2015 Rob Landley <rob@landley.net>
-
-USE_TEST_HUMAN_READABLE(NEWTOY(test_human_readable, "<1>1ibs", TOYFLAG_BIN))
-
-config TEST_HUMAN_READABLE
- bool "test_human_readable"
- default n
- help
- usage: test_human_readable [-sbi] NUMBER
-*/
-
-#define FOR_test_human_readable
-#include "toys.h"
-
-void test_human_readable_main(void)
-{
- char *c;
-
- human_readable(toybuf, strtoll(*toys.optargs, &c, 0), toys.optflags);
- printf("%s\n", toybuf);
-}
diff --git a/toys/example/demo_number.c b/toys/example/demo_number.c
new file mode 100644
index 00000000..4c7b7327
--- /dev/null
+++ b/toys/example/demo_number.c
@@ -0,0 +1,33 @@
+/* demo_number.c - Expose atolx() and human_readable() for testing.
+ *
+ * Copyright 2015 Rob Landley <rob@landley.net>
+
+USE_DEMO_NUMBER(NEWTOY(demo_number, "hdbs", TOYFLAG_BIN))
+
+config DEMO_NUMBER
+ bool "demo_number"
+ default n
+ help
+ usage: demo_number [-hsbi] NUMBER...
+
+ -b Use "B" for single byte units (HR_B)
+ -d Decimal units
+ -h human readable
+ -s Space between number and units (HR_SPACE)
+*/
+
+#include "toys.h"
+
+void demo_number_main(void)
+{
+ char **arg;
+
+ for (arg = toys.optargs; *arg; arg++) {
+ long long ll = atolx(*arg);
+
+ if (toys.optflags) {
+ human_readable(toybuf, ll, toys.optflags);
+ xputs(toybuf);
+ } else printf("%lld\n", ll);
+ }
+}