diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-06-19 09:52:45 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-06-19 23:15:19 +0900 |
commit | 049025a4153072912fbd6504860cd84634321056 (patch) | |
tree | d579a6c2579756e20d23a2a865a37e4da62ee2bc | |
parent | b7de125bba74b23cec0e49189302655371d3e982 (diff) | |
download | systemd-049025a4153072912fbd6504860cd84634321056.tar.gz |
test: add tests for format_bytes()
-rw-r--r-- | src/test/meson.build | 4 | ||||
-rw-r--r-- | src/test/test-format-util.c | 38 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/test/meson.build b/src/test/meson.build index ee6cdb6d54..34dd5cbc45 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -216,6 +216,10 @@ tests += [ [], []], + [['src/test/test-format-util.c'], + [], + []], + [['src/test/test-ratelimit.c'], [], []], diff --git a/src/test/test-format-util.c b/src/test/test-format-util.c new file mode 100644 index 0000000000..2986b1bd8d --- /dev/null +++ b/src/test/test-format-util.c @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "format-util.h" +#include "macro.h" +#include "string-util.h" + +static void test_format_bytes_one(size_t val, bool trailing_B, const char *iec_with_p, const char *iec_without_p, + const char *non_iec_with_p, const char *non_iec_without_p) { + char buf[FORMAT_BYTES_MAX]; + + assert_se(streq_ptr(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), iec_with_p)); + assert_se(streq_ptr(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_USE_IEC | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), iec_without_p)); + assert_se(streq_ptr(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_BELOW_POINT | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), non_iec_with_p)); + assert_se(streq_ptr(format_bytes_full(buf, sizeof buf, val, trailing_B ? FORMAT_BYTES_TRAILING_B : 0), non_iec_without_p)); +} + +static void test_format_bytes(void) { + test_format_bytes_one(900, true, "900B", "900B", "900B", "900B"); + test_format_bytes_one(900, false, "900", "900", "900", "900"); + test_format_bytes_one(1023, true, "1023B", "1023B", "1.0K", "1K"); + test_format_bytes_one(1023, false, "1023", "1023", "1.0K", "1K"); + test_format_bytes_one(1024, true, "1.0K", "1K", "1.0K", "1K"); + test_format_bytes_one(1024, false, "1.0K", "1K", "1.0K", "1K"); + test_format_bytes_one(1100, true, "1.0K", "1K", "1.1K", "1K"); + test_format_bytes_one(1500, true, "1.4K", "1K", "1.5K", "1K"); + test_format_bytes_one((size_t) 3*1024*1024, true, "3.0M", "3M", "3.1M", "3M"); + test_format_bytes_one((size_t) 3*1024*1024*1024, true, "3.0G", "3G", "3.2G", "3G"); + test_format_bytes_one((size_t) 3*1024*1024*1024*1024, true, "3.0T", "3T", "3.2T", "3T"); + test_format_bytes_one((size_t) 3*1024*1024*1024*1024*1024, true, "3.0P", "3P", "3.3P", "3P"); + test_format_bytes_one((size_t) 3*1024*1024*1024*1024*1024*1024, true, "3.0E", "3E", "3.4E", "3E"); + test_format_bytes_one(SIZE_MAX, true, NULL, NULL, NULL, NULL); +} + +int main(void) { + test_format_bytes(); + + return 0; +} |