diff options
Diffstat (limited to '')
-rw-r--r-- | unit/meson.build | 46 | ||||
-rw-r--r-- | unit/test-argconfig-parse.c | 164 | ||||
-rw-r--r-- | unit/test-suffix-binary-parse.c | 68 | ||||
-rw-r--r-- | unit/test-suffix-si-parse.c | 83 | ||||
-rw-r--r-- | unit/test-uint128-si.c | 66 | ||||
-rw-r--r-- | unit/test-uint128.c | 74 |
6 files changed, 501 insertions, 0 deletions
diff --git a/unit/meson.build b/unit/meson.build new file mode 100644 index 0000000..7e0e878 --- /dev/null +++ b/unit/meson.build @@ -0,0 +1,46 @@ +# 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_suffix_binary_parse = executable( + 'test-suffix-binary-parse', + ['test-suffix-binary-parse.c', '../util/suffix.c'], + include_directories: [incdir, '..'], + dependencies: [libnvme_dep], +) + +test('suffix_binary_parse', test_suffix_binary_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) + +test_argconfig_parse = executable( + 'test-argconfig-parse', + ['test-argconfig-parse.c', '../util/argconfig.c', '../util/suffix.c'], + include_directories: [incdir, '..'], + dependencies: [libnvme_dep], +) + +test('argconfig_parse', test_argconfig_parse) diff --git a/unit/test-argconfig-parse.c b/unit/test-argconfig-parse.c new file mode 100644 index 0000000..23c8d4f --- /dev/null +++ b/unit/test-argconfig-parse.c @@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <locale.h> + +#include "../util/argconfig.h" +#include "nvme/types.h" + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +static int test_rc; + +union val { + bool flag; + __u64 suffix; + __u32 uint; + int int_val; + __u64 long_val; + double double_val; + __u8 byte; + __u16 shrt; + int incr; + char *string; + char *fmt; + char *file; + char *list; + char *str; +}; + +struct toval_test { + char *arg; + void *val; + union val exp; + int size; + int ret; +}; + +static void check_val(const char *arg, void *exp, void *val, int size) +{ + if ((size && !memcmp(exp, val, size)) || + (!size && !strcmp(*(char **)exp, *(char **)val))) + return; + + switch (size) { + case 0: + printf("ERROR: printing {%s}, got '%s', expected '%s'\n", + arg, *(char **)val, *(char **)exp); + break; + default: + printf("ERROR: printing {%s}, got '%llu', expected '%llu'\n", + arg, *(unsigned long long *)val, *(unsigned long long *)exp); + break; + } + + test_rc = 1; +} + +struct cfg { + bool flag; + __u64 suffix; + __u32 uint; + int int_val; + __u64 long_val; + double double_val; + __u8 byte; + __u16 shrt; + int incr; + char *string; + char *fmt; + char *file; + char *list; + char *str; +}; + +static struct cfg cfg; + +#define VAL_TEST(a, c, v, l, r) \ + { a, &cfg.c, { .c = v }, l ? sizeof(cfg.c) : 0, r } + +static struct toval_test toval_tests[] = { + VAL_TEST("--flag", flag, true, true, 0), + VAL_TEST("--flag=1", flag, false, true, -EINVAL), + VAL_TEST("--suffix=0", suffix, 0, true, 0), + VAL_TEST("--suffix=1", suffix, 1, true, 0), + VAL_TEST("--suffix=1234", suffix, 1234, true, 0), + VAL_TEST("--suffix=4096", suffix, 4096, true, 0), + VAL_TEST("--suffix=1Ki", suffix, 1024, true, 0), + VAL_TEST("--suffix=34Gi", suffix, 36507222016, true, 0), + VAL_TEST("--suffix=34.9Ki", suffix, 0, true, -EINVAL), + VAL_TEST("--suffix=32Gii", suffix, 0, true, -EINVAL), + VAL_TEST("--uint=1", uint, 1, true, 0), + VAL_TEST("--int=1", int_val, 1, true, 0), + VAL_TEST("--long=1", long_val, 1, true, 0), + VAL_TEST("--double=1", double_val, 1, true, 0), + VAL_TEST("--byte=1", byte, 1, true, 0), + VAL_TEST("--byte=256", byte, 0, true, -EINVAL), + VAL_TEST("--shrt=1", shrt, 1, true, 0), + VAL_TEST("--incr", incr, 1, true, 0), + VAL_TEST("--incr=1", incr, 0, true, -EINVAL), + VAL_TEST("--string=string", string, "string", false, 0), + VAL_TEST("--fmt=fmt", fmt, "fmt", false, 0), + VAL_TEST("--file=file", file, "file", false, 0), + VAL_TEST("--list=list", list, "list", false, 0), + VAL_TEST("--str=str", str, "str", false, 0), +}; + +void toval_test(struct toval_test *test) +{ + const char *desc = "Test argconfig parse"; + int ret; + char *argv[] = { "test-argconfig", test->arg }; + + OPT_ARGS(opts) = { + OPT_FLAG("flag",'f', &cfg.flag, "flag"), + OPT_SUFFIX("suffix", 's', &cfg.suffix, "suffix"), + OPT_UINT("uint", 'u', &cfg.uint, "uint"), + OPT_INT("int", 'i', &cfg.int_val, "int"), + OPT_LONG("long", 'l', &cfg.long_val, "long"), + OPT_DOUBLE("double", 'd', &cfg.double_val, "double"), + OPT_BYTE("byte", 'b', &cfg.byte, "byte"), + OPT_SHRT("shrt", 'S', &cfg.shrt, "shrt"), + OPT_INCR("incr", 'I', &cfg.incr, "incr"), + OPT_STRING("string", 't', "STRING", &cfg.string, "string"), + OPT_FMT("fmt", 'F', &cfg.fmt, "fmt"), + OPT_FILE("file", 'L', &cfg.file, "file"), + OPT_LIST("list", 'T', &cfg.list, "list"), + OPT_STR("str", 'r', &cfg.str, "str"), + OPT_END() + }; + + ret = argconfig_parse(2, argv, desc, opts); + if (ret != test->ret) { + printf("ERROR: converting {%s} failed\n", test->arg); + test_rc = 1; + return; + } + if (ret) + return; + + check_val(test->arg, &test->exp, test->val, test->size); +} + +int main(void) +{ + unsigned int i; + FILE *f; + + test_rc = 0; + setlocale(LC_NUMERIC, "C"); + f = freopen("/dev/null", "w", stderr); + if (!f) + printf("ERROR: reopening stderr failed: %s\n", strerror(errno)); + + for (i = 0; i < ARRAY_SIZE(toval_tests); i++) + toval_test(&toval_tests[i]); + + if (f) + fclose(f); + + return test_rc ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/unit/test-suffix-binary-parse.c b/unit/test-suffix-binary-parse.c new file mode 100644 index 0000000..5f6ac4a --- /dev/null +++ b/unit/test-suffix-binary-parse.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.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, __u64 exp, __u64 num) +{ + if (exp == num) + return; + + 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; + const uint64_t exp; + int ret; +}; + +static struct tonum_test tonum_tests[] = { + { "1234", 1234, 0 }, + { "1Ki", 1024, 0}, + { "34Gi", 36507222016, 0 }, + { "34.9Ki", 0, -EINVAL}, + { "32Gii", 0, -EINVAL }, +}; + +void tonum_test(struct tonum_test *test) +{ + char *endptr; + uint64_t num; + int ret; + + ret = suffix_binary_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->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-suffix-si-parse.c b/unit/test-suffix-si-parse.c new file mode 100644 index 0000000..54cff0e --- /dev/null +++ b/unit/test-suffix-si-parse.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <locale.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, __u64 exp, __u64 num) +{ + if (exp == num) + return; + + 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; + const uint64_t exp; + int ret; +}; + +static struct tonum_test tonum_tests[] = { + { "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 }, + { "800G", 800000000000, 0 }, + { "800GG", 0, -EINVAL }, + { "800G800", 0, -EINVAL }, + { "800.0G", 800000000000, 0 }, + { "800.G", 0, -EINVAL }, + { "800.", 0, -EINVAL }, +}; + +void tonum_test(struct tonum_test *test) +{ + 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->exp, num); +} + +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]); + + 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..f8478ef --- /dev/null +++ b/unit/test-uint128.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <locale.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 { + const char *locale; + nvme_uint128_t val; + const char *exp; +}; + +static struct tostr_test tostr_tests[] = { + { NULL, U128(0, 0, 0, 0),"0" }, + { NULL, U128(0, 0, 0, 1), "1" }, + { NULL, U128(0, 0, 0, 10), "10" }, + { NULL, U128(4, 3, 2, 1), "316912650112397582603894390785" }, + { + NULL, + U128(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff), + "340282366920938463463374607431768211455" + }, + { "fr_FR.utf-8", U128(0, 0, 0, 1000), "1\u202f000" }, +}; + +void tostr_test(struct tostr_test *test) +{ + char *str; + + if (!setlocale(LC_NUMERIC, test->locale)) + return; + + if (test->locale) + str = uint128_t_to_l10n_string(test->val); + else + 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; +} |