* [LTP] [PATCH v2] Add tst_parse_filesize functionality in LTP test API
@ 2021-12-07 11:46 Andrea Cervesato via ltp
2021-12-07 12:53 ` Cyril Hrubis
0 siblings, 1 reply; 3+ messages in thread
From: Andrea Cervesato via ltp @ 2021-12-07 11:46 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
include/tst_test.h | 1 +
lib/newlib_tests/test_tst_test.c | 123 +++++++++++++++++++++++++++++++
lib/tst_test.c | 41 +++++++++++
3 files changed, 165 insertions(+)
create mode 100644 lib/newlib_tests/test_tst_test.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/test_tst_test.c b/lib/newlib_tests/test_tst_test.c
new file mode 100644
index 000000000..095e9c3fd
--- /dev/null
+++ b/lib/newlib_tests/test_tst_test.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*
+ * Tests for lib/tst_test.c
+ */
+
+#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 == 1)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1);
+ }
+
+ /* 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 == 1024)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1024);
+ }
+
+ 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 == 1048576)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1048576);
+ }
+
+ 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 == 1073741824)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1073741824);
+ }
+
+ /* 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 == 1024)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1024);
+ }
+
+ 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 == 1048576)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1048576);
+ }
+
+ 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 == 1073741824)
+ tst_res(TPASS, "value is %lli", val);
+ else
+ tst_res(TFAIL, "%lli != %lli", val, 1073741824);
+ }
+
+ /* 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("garbage", &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..78b107ed0 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 || (strlen(end) != 0 && strlen(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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [LTP] [PATCH v2] Add tst_parse_filesize functionality in LTP test API
2021-12-07 11:46 [LTP] [PATCH v2] Add tst_parse_filesize functionality in LTP test API Andrea Cervesato via ltp
@ 2021-12-07 12:53 ` Cyril Hrubis
2021-12-07 12:56 ` Cyril Hrubis
0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2021-12-07 12:53 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
> +++ b/lib/newlib_tests/test_tst_test.c
> @@ -0,0 +1,123 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*
> + * Tests for lib/tst_test.c
> + */
Well technically this is a test just for tst_parse_filesize() so I would
name the file test_parse_filesize.c or something like that.
> +#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 == 1)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1);
> + }
> +
> + /* 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 == 1024)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1024);
> + }
> +
> + 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 == 1048576)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1048576);
> + }
> +
> + 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 == 1073741824)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1073741824);
> + }
> +
> + /* 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 == 1024)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1024);
> + }
> +
> + 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 == 1048576)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1048576);
> + }
> +
> + 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 == 1073741824)
> + tst_res(TPASS, "value is %lli", val);
> + else
> + tst_res(TFAIL, "%lli != %lli", val, 1073741824);
> + }
> +
> + /* 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("garbage", &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..78b107ed0 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 || (strlen(end) != 0 && strlen(end) != 1))
No need for the strlen() here. what you do here is a fancy way of doing (!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))
The rest looks good, but we should add a short note about the function
to the doc/c-test-api.txt as well.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [LTP] [PATCH v2] Add tst_parse_filesize functionality in LTP test API
2021-12-07 12:53 ` Cyril Hrubis
@ 2021-12-07 12:56 ` Cyril Hrubis
0 siblings, 0 replies; 3+ messages in thread
From: Cyril Hrubis @ 2021-12-07 12:56 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
> > + if (str == end || (strlen(end) != 0 && strlen(end) != 1))
>
> No need for the strlen() here. what you do here is a fancy way of doing (!end[0] && !end[1]).
And of course it should be (end[0] && end[1]) that means that the string
is not terminated.
Or optionally (strlen(end) > 1) would work too.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-12-07 12:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-07 11:46 [LTP] [PATCH v2] Add tst_parse_filesize functionality in LTP test API Andrea Cervesato via ltp
2021-12-07 12:53 ` Cyril Hrubis
2021-12-07 12:56 ` Cyril Hrubis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.