summaryrefslogtreecommitdiffstats
path: root/unit/test-suffix-si-parse.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-01-31 04:13:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-01-31 04:13:00 +0000
commitdc3f3471f8a00ce0c8fb4cbf2a31e299696f3bbc (patch)
treee97b4f25c511372d73bdd96c389c5f468d99138a /unit/test-suffix-si-parse.c
parentAdding upstream version 2.2.1. (diff)
downloadnvme-cli-365eaf9a975847f0dbb48b5dadc1eed5a5a42e5f.tar.xz
nvme-cli-365eaf9a975847f0dbb48b5dadc1eed5a5a42e5f.zip
Adding upstream version 2.3.upstream/2.3
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.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/unit/test-suffix-si-parse.c b/unit/test-suffix-si-parse.c
new file mode 100644
index 0000000..879518b
--- /dev/null
+++ b/unit/test-suffix-si-parse.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "../util/suffix.h"
+#include "../util/types.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+static int test_rc;
+
+static void check_num(const char *val, int lbas, __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);
+
+ test_rc = 1;
+}
+
+struct tonum_test {
+ const char *val;
+ int lbas;
+ const __u64 exp;
+};
+
+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 },
+};
+
+void tonum_test(struct tonum_test *test)
+{
+ __u64 num;
+ bool suffixed;
+
+ num = suffix_si_parse(test->val, &suffixed);
+
+ if (suffixed)
+ num /= test->lbas;
+
+ check_num(test->val, test->lbas, test->exp, num);
+}
+
+int main(void)
+{
+ unsigned int i;
+
+ test_rc = 0;
+
+ for (i = 0; i < ARRAY_SIZE(tonum_tests); i++)
+ tonum_test(&tonum_tests[i]);
+
+ return test_rc ? EXIT_FAILURE : EXIT_SUCCESS;
+}