Add a num2str() unit test
authorBart Van Assche <bvanassche@acm.org>
Thu, 2 Jul 2020 22:18:37 +0000 (15:18 -0700)
committerBart Van Assche <bvanassche@acm.org>
Thu, 2 Jul 2020 22:41:35 +0000 (15:41 -0700)
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Makefile
unittests/lib/num2str.c [new file with mode: 0644]
unittests/unittest.c
unittests/unittest.h

index 7eb5e899df994ee822ac1043737404e18c79026a..99e966359ff532cd825fa4d4a2a20caa8f3797d4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -337,12 +337,14 @@ PROGS += $(T_PROGS)
 ifdef CONFIG_HAVE_CUNIT
 UT_OBJS = unittests/unittest.o
 UT_OBJS += unittests/lib/memalign.o
+UT_OBJS += unittests/lib/num2str.o
 UT_OBJS += unittests/lib/strntol.o
 UT_OBJS += unittests/oslib/strlcat.o
 UT_OBJS += unittests/oslib/strndup.o
 UT_OBJS += unittests/oslib/strcasestr.o
 UT_OBJS += unittests/oslib/strsep.o
 UT_TARGET_OBJS = lib/memalign.o
+UT_TARGET_OBJS += lib/num2str.o
 UT_TARGET_OBJS += lib/strntol.o
 UT_TARGET_OBJS += oslib/strlcat.o
 UT_TARGET_OBJS += oslib/strndup.o
diff --git a/unittests/lib/num2str.c b/unittests/lib/num2str.c
new file mode 100644 (file)
index 0000000..931e28a
--- /dev/null
@@ -0,0 +1,53 @@
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include "../../compiler/compiler.h"
+#include "../../lib/num2str.h"
+#include "../unittest.h"
+
+struct testcase {
+       uint64_t num;
+       int maxlen;
+       int base;
+       int pow2;
+       enum n2s_unit unit;
+       const char *expected;
+};
+
+static const struct testcase testcases[] = {
+       { 1, 1, 1, 0, N2S_NONE, "1" },
+       { UINT64_MAX, 99, 1, 0, N2S_NONE, "18446744073709551615" },
+       { 18446744073709551, 2, 1, 0, N2S_NONE, "18P" },
+       { 18446744073709551, 4, 1, 0, N2S_NONE, "18.4P" },
+       { UINT64_MAX, 2, 1, 0, N2S_NONE, "18" },
+       { UINT64_MAX, 4, 1, 0, N2S_NONE, "18.4" },
+};
+
+static void test_num2str(void)
+{
+       const struct testcase *p;
+       char *str;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(testcases); ++i) {
+               p = &testcases[i];
+               str = num2str(p->num, p->maxlen, p->base, p->pow2, p->unit);
+               CU_ASSERT_STRING_EQUAL(str, p->expected);
+               free(str);
+       }
+}
+
+static struct fio_unittest_entry tests[] = {
+       {
+               .name   = "num2str/1",
+               .fn     = test_num2str,
+       },
+       {
+               .name   = NULL,
+       },
+};
+
+CU_ErrorCode fio_unittest_lib_num2str(void)
+{
+       return fio_unittest_add_suite("lib/num2str.c", NULL, NULL, tests);
+}
index c37e1971a0518544c2cdc136b5e276f387b16d4e..f490b4852b2f0a0a5e7ebbb62be33f2039573fcc 100644 (file)
@@ -48,6 +48,7 @@ int main(void)
        }
 
        fio_unittest_register(fio_unittest_lib_memalign);
+       fio_unittest_register(fio_unittest_lib_num2str);
        fio_unittest_register(fio_unittest_lib_strntol);
        fio_unittest_register(fio_unittest_oslib_strlcat);
        fio_unittest_register(fio_unittest_oslib_strndup);
index 786c1c97ec8f149b8b43c4cd7b6dbe017966230c..ecb7d12415209464da4a6d13b488233108eb25d6 100644 (file)
@@ -15,6 +15,7 @@ CU_ErrorCode fio_unittest_add_suite(const char*, CU_InitializeFunc,
        CU_CleanupFunc, struct fio_unittest_entry*);
 
 CU_ErrorCode fio_unittest_lib_memalign(void);
+CU_ErrorCode fio_unittest_lib_num2str(void);
 CU_ErrorCode fio_unittest_lib_strntol(void);
 CU_ErrorCode fio_unittest_oslib_strlcat(void);
 CU_ErrorCode fio_unittest_oslib_strndup(void);