summaryrefslogtreecommitdiffstats
path: root/unit
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
parentAdding upstream version 2.2.1. (diff)
downloadnvme-cli-dc3f3471f8a00ce0c8fb4cbf2a31e299696f3bbc.tar.xz
nvme-cli-dc3f3471f8a00ce0c8fb4cbf2a31e299696f3bbc.zip
Adding upstream version 2.3.upstream/2.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'unit')
-rw-r--r--unit/meson.build28
-rw-r--r--unit/test-suffix-si-parse.c64
-rw-r--r--unit/test-uint128-si.c66
-rw-r--r--unit/test-uint128.c62
4 files changed, 220 insertions, 0 deletions
diff --git a/unit/meson.build b/unit/meson.build
new file mode 100644
index 0000000..d4ff925
--- /dev/null
+++ b/unit/meson.build
@@ -0,0 +1,28 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+test_uint128 = executable(
+ 'test-uint128',
+ ['test-uint128.c', '../util/types.c', '../util/suffix.c'],
+ include_directories: [incdir, '..'],
+ dependencies: [libnvme_dep],
+)
+
+test('uint128', test_uint128)
+
+test_suffix_si_parse = executable(
+ 'test-suffix-si-parse',
+ ['test-suffix-si-parse.c', '../util/suffix.c'],
+ include_directories: [incdir, '..'],
+ dependencies: [libnvme_dep],
+)
+
+test('suffix_si_parse', test_suffix_si_parse)
+
+test_uint128_si = executable(
+ 'test-uint128-si',
+ ['test-uint128-si.c', '../util/types.c', '../util/suffix.c'],
+ include_directories: [incdir, '..'],
+ dependencies: [libnvme_dep],
+)
+
+test('uint128-si', test_uint128_si)
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;
+}
diff --git a/unit/test-uint128-si.c b/unit/test-uint128-si.c
new file mode 100644
index 0000000..cc13450
--- /dev/null
+++ b/unit/test-uint128-si.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "../util/types.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+/* create a uint128_t from four uint32_ts. w0 is the most significant value,
+ * w2 the least */
+#define U128(w0, w1, w2, w3) { .words = { w0, w1, w2, w3 } }
+
+static int test_rc;
+
+static void check_str(nvme_uint128_t val, __u32 bytes_per_unit, const char *exp,
+ const char *res)
+{
+ if (!strcmp(res, exp))
+ return;
+
+ printf("ERROR: printing {%08x.%08x.%08x.%08x} (bytes per unit %u), got '%s', expected '%s'\n",
+ val.words[3], val.words[2], val.words[1], val.words[0],
+ bytes_per_unit, res, exp);
+
+ test_rc = 1;
+}
+
+struct tostr_test {
+ nvme_uint128_t val;
+ __u32 bytes_per_unit;
+ const char *exp;
+};
+
+static struct tostr_test tostr_tests[] = {
+ { U128(0, 0, 0, 0), 1, "0.00 B" },
+ { U128(0, 0, 0, 1), 1, "1.00 B" },
+ { U128(0, 0, 0, 10), 1, "10.00 B" },
+ { U128(4, 3, 2, 1), 1, "316.91 RB" },
+ { U128(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff), 1,
+ "340282366.92 QB" },
+ { U128(0, 0, 0, 0xae0dc2), 1000 * 512, "5.84 TB" },
+ { U128(0, 0, 0, 0xf9c546), 1000 * 512, "8.38 TB" },
+ { U128(0, 0, 0, 0x4c2aa594), 1000 * 512, "654.27 TB" },
+ { U128(0, 0, 0, 0x5b013de8), 1000 * 512, "781.73 TB" },
+};
+
+void tostr_test(struct tostr_test *test)
+{
+ char *str;
+ str = uint128_t_to_si_string(test->val, test->bytes_per_unit);
+ check_str(test->val, test->bytes_per_unit, test->exp, str);
+}
+
+int main(void)
+{
+ unsigned int i;
+
+ test_rc = 0;
+
+ for (i = 0; i < ARRAY_SIZE(tostr_tests); i++)
+ tostr_test(&tostr_tests[i]);
+
+ return test_rc ? EXIT_FAILURE : EXIT_SUCCESS;
+}
diff --git a/unit/test-uint128.c b/unit/test-uint128.c
new file mode 100644
index 0000000..6301a38
--- /dev/null
+++ b/unit/test-uint128.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "../util/types.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+/* create a uint128_t from four uint32_ts. w0 is the most significant value,
+ * w2 the least */
+#define U128(w0, w1, w2, w3) { .words = { w0, w1, w2, w3 } }
+
+static int test_rc;
+
+static void check_str(nvme_uint128_t val, const char *exp, const char *res)
+{
+ if (!strcmp(res, exp))
+ return;
+
+ printf("ERROR: printing {%08x.%08x.%08x.%08x}, got '%s', expected '%s'\n",
+ val.words[3], val.words[2], val.words[1], val.words[0],
+ res, exp);
+
+ test_rc = 1;
+}
+
+struct tostr_test {
+ nvme_uint128_t val;
+ const char *exp;
+};
+
+static struct tostr_test tostr_tests[] = {
+ { U128(0, 0, 0, 0), "0" },
+ { U128(0, 0, 0, 1), "1" },
+ { U128(0, 0, 0, 10), "10" },
+ { U128(4, 3, 2, 1), "316912650112397582603894390785" },
+ {
+ U128(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff),
+ "340282366920938463463374607431768211455"
+ },
+};
+
+void tostr_test(struct tostr_test *test)
+{
+ char *str;
+ str = uint128_t_to_string(test->val);
+ check_str(test->val, test->exp, str);
+}
+
+int main(void)
+{
+ unsigned int i;
+
+ test_rc = 0;
+
+ for (i = 0; i < ARRAY_SIZE(tostr_tests); i++)
+ tostr_test(&tostr_tests[i]);
+
+ return test_rc ? EXIT_FAILURE : EXIT_SUCCESS;
+}