From: Andrea Cervesato via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v5] Add tst_parse_filesize functionality in LTP test API
Date: Thu, 9 Dec 2021 10:11:04 +0100 [thread overview]
Message-ID: <20211209091104.4539-1-andrea.cervesato@suse.com> (raw)
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
include/tst_test.h | 1 +
lib/newlib_tests/.gitignore | 1 +
lib/newlib_tests/test_parse_filesize.c | 122 +++++++++++++++++++++++++
lib/tst_test.c | 41 +++++++++
4 files changed, 165 insertions(+)
create mode 100644 lib/newlib_tests/test_parse_filesize.c
diff --git a/include/tst_test.h b/include/tst_test.h
index c06a4729b..450ddf086 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -122,6 +122,7 @@ struct tst_option {
int tst_parse_int(const char *str, int *val, int min, int max);
int tst_parse_long(const char *str, long *val, long min, long max);
int tst_parse_float(const char *str, float *val, float min, float max);
+int tst_parse_filesize(const char *str, long long *val, long long min, long long max);
struct tst_tag {
const char *name;
diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore
index cf467b5a0..6259692b1 100644
--- a/lib/newlib_tests/.gitignore
+++ b/lib/newlib_tests/.gitignore
@@ -47,3 +47,4 @@ tst_fuzzy_sync01
tst_fuzzy_sync02
tst_fuzzy_sync03
test_zero_hugepage
+test_parse_filesize
diff --git a/lib/newlib_tests/test_parse_filesize.c b/lib/newlib_tests/test_parse_filesize.c
new file mode 100644
index 000000000..d60d7dc6a
--- /dev/null
+++ b/lib/newlib_tests/test_parse_filesize.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*
+ * Tests for tst_parse_filesize.
+ */
+
+#include "tst_test.h"
+
+static void do_test(void)
+{
+ long long val = 0;
+ int ret = 0;
+
+ if ((ret = tst_parse_filesize("1", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1LL);
+ }
+
+ /* small letters */
+ if ((ret = tst_parse_filesize("1k", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1024LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1024LL);
+ }
+
+ if ((ret = tst_parse_filesize("1m", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1048576LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1048576LL);
+ }
+
+ if ((ret = tst_parse_filesize("1g", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1073741824LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1073741824LL);
+ }
+
+ /* big letters */
+ if ((ret = tst_parse_filesize("1K", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1024LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1024LL);
+ }
+
+ if ((ret = tst_parse_filesize("1M", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1048576LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1048576LL);
+ }
+
+ if ((ret = tst_parse_filesize("1G", &val, LLONG_MIN, LLONG_MAX))) {
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ if (val == 1073741824LL)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1073741824LL);
+ }
+
+ /* test errors */
+ if ((ret = tst_parse_filesize("k", &val, LLONG_MIN, LLONG_MAX))) {
+ if (ret == EINVAL)
+ tst_res(TPASS, "return code %d (%s)", ret, tst_strerrno(ret));
+ else
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ tst_res(TFAIL, "tst_parse_filesize should have failed");
+ }
+
+ if ((ret = tst_parse_filesize("100", &val, LLONG_MIN, 10))) {
+ if (ret == ERANGE)
+ tst_res(TPASS, "return code %d (%s)", ret, tst_strerrno(ret));
+ else
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ tst_res(TFAIL, "tst_parse_filesize should have failed");
+ }
+
+ if ((ret = tst_parse_filesize("10", &val, 100, LLONG_MAX))) {
+ if (ret == ERANGE)
+ tst_res(TPASS, "return code %d (%s)", ret, tst_strerrno(ret));
+ else
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ tst_res(TFAIL, "tst_parse_filesize should have failed");
+ }
+
+ if ((ret = tst_parse_filesize("10garbage", &val, LLONG_MIN, LLONG_MAX))) {
+ if (ret == EINVAL)
+ tst_res(TPASS, "return code %d (%s)", ret, tst_strerrno(ret));
+ else
+ tst_res(TFAIL, "return code %d (%s)", ret, tst_strerrno(ret));
+ } else {
+ tst_res(TFAIL, "tst_parse_filesize should have failed");
+ }
+}
+
+static struct tst_test test = {
+ .test_all = do_test,
+};
diff --git a/lib/tst_test.c b/lib/tst_test.c
index a79275722..2f28874be 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -679,6 +679,47 @@ int tst_parse_float(const char *str, float *val, float min, float max)
return 0;
}
+int tst_parse_filesize(const char *str, long long *val, long long min, long long max)
+{
+ long long rval;
+ char *end;
+
+ if (!str)
+ return 0;
+
+ errno = 0;
+ rval = strtoll(str, &end, 0);
+
+ if (str == end || (end[0] && end[1]))
+ return EINVAL;
+
+ if (errno)
+ return errno;
+
+ switch (*end) {
+ case 'g':
+ case 'G':
+ rval *= (1024 * 1024 * 1024);
+ break;
+ case 'm':
+ case 'M':
+ rval *= (1024 * 1024);
+ break;
+ case 'k':
+ case 'K':
+ rval *= 1024;
+ break;
+ default:
+ break;
+ }
+
+ if (rval > max || rval < min)
+ return ERANGE;
+
+ *val = rval;
+ return 0;
+}
+
static void print_colored(const char *str)
{
if (tst_color_enabled(STDOUT_FILENO))
--
2.34.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2021-12-09 9:11 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-09 9:11 Andrea Cervesato via ltp [this message]
2021-12-09 19:28 ` [LTP] [PATCH v5] Add tst_parse_filesize functionality in LTP test API Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211209091104.4539-1-andrea.cervesato@suse.com \
--to=ltp@lists.linux.it \
--cc=andrea.cervesato@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox