diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-04-07 12:43:34 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-04-07 12:43:34 +0000 |
commit | 8e1dcd44fcad7826e5f8fa6c5caa6c23989518d9 (patch) | |
tree | 8a5e1e9d02b2562d57d57ef88e26d4778ebeeab3 /unit/test-suffix-si-parse.c | |
parent | Releasing debian version 2.4+really2.3-2. (diff) | |
download | nvme-cli-8e1dcd44fcad7826e5f8fa6c5caa6c23989518d9.tar.xz nvme-cli-8e1dcd44fcad7826e5f8fa6c5caa6c23989518d9.zip |
Merging upstream version 2.4+really2.4.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'unit/test-suffix-si-parse.c')
-rw-r--r-- | unit/test-suffix-si-parse.c | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/unit/test-suffix-si-parse.c b/unit/test-suffix-si-parse.c index 879518b..bc92455 100644 --- a/unit/test-suffix-si-parse.c +++ b/unit/test-suffix-si-parse.c @@ -3,6 +3,8 @@ #include <string.h> #include <stdio.h> #include <stdlib.h> +#include <errno.h> +#include <locale.h> #include "../util/suffix.h" #include "../util/types.h" @@ -11,44 +13,54 @@ static int test_rc; -static void check_num(const char *val, int lbas, __u64 exp, __u64 num) +static void check_num(const char *val, __u64 exp, __u64 num) { if (exp == num) return; - printf("ERROR: printing {%s} (lbas %d), got '%llu', expected '%llu'\n", - val, lbas, (unsigned long long)num, (unsigned long long)exp); + printf("ERROR: printing {%s}, got '%llu', expected '%llu'\n", + val, (unsigned long long)num, (unsigned long long)exp); test_rc = 1; } struct tonum_test { const char *val; - int lbas; - const __u64 exp; + const uint64_t exp; + int ret; }; static struct tonum_test tonum_tests[] = { - { "11995709440", 512, 11995709440 }, - { "1199570940", 512, 1199570940 }, - { "6.14T", 512, 11992187500 }, - { "6.14T", 520, 11807692307 }, - { "6.14T", 4096, 1499023437 }, - { "6.14", 512, 0 }, - { "6.14#", 512, 0 }, + { "11995709440", 11995709440, 0 }, + { "1199570940", 1199570940, 0}, + { "234.567M", 234567000, 0 }, + { "1.2k", 1200, 0 }, + { "6.14T", 6140000000000, 0 }, + { "123.4567k", 123456, 0 }, + { "12345.6789101112M", 12345678910, 0}, + { "6.14", 6, 0 }, + { "6.14#", 0, -EINVAL }, + { "2,33", 0, -EINVAL }, + { "3..3", 0, -EINVAL }, + { "123.12MM", 0, -EINVAL }, }; void tonum_test(struct tonum_test *test) { - __u64 num; - bool suffixed; - - num = suffix_si_parse(test->val, &suffixed); - - if (suffixed) - num /= test->lbas; + char *endptr; + uint64_t num; + int ret; + + ret = suffix_si_parse(test->val, &endptr, &num); + if (ret != test->ret) { + printf("ERROR: converting {%s} failed\n", test->val); + test_rc = 1; + return; + } + if (ret) + return; - check_num(test->val, test->lbas, test->exp, num); + check_num(test->val, test->exp, num); } int main(void) @@ -56,6 +68,7 @@ int main(void) unsigned int i; test_rc = 0; + setlocale(LC_NUMERIC, "C"); for (i = 0; i < ARRAY_SIZE(tonum_tests); i++) tonum_test(&tonum_tests[i]); |