summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorBart Van Assche <bvanassche@acm.org>2020-07-02 15:18:37 -0700
committerBart Van Assche <bvanassche@acm.org>2020-07-02 15:41:35 -0700
commit53916bbd100ef806a7af6ece50c4ee0569bac4c6 (patch)
treeedb2f2e0c0b09552c3f50d38602a4f5842aa7705 /unittests
parentfd99605cc5f16bc8171233ba6a8f0ab47e57c4f9 (diff)
downloadfio-53916bbd100ef806a7af6ece50c4ee0569bac4c6.tar.gz
fio-53916bbd100ef806a7af6ece50c4ee0569bac4c6.tar.bz2
Add a num2str() unit test
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Diffstat (limited to 'unittests')
-rw-r--r--unittests/lib/num2str.c53
-rw-r--r--unittests/unittest.c1
-rw-r--r--unittests/unittest.h1
3 files changed, 55 insertions, 0 deletions
diff --git a/unittests/lib/num2str.c b/unittests/lib/num2str.c
new file mode 100644
index 00000000..931e28a4
--- /dev/null
+++ b/unittests/lib/num2str.c
@@ -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);
+}
diff --git a/unittests/unittest.c b/unittests/unittest.c
index c37e1971..f490b485 100644
--- a/unittests/unittest.c
+++ b/unittests/unittest.c
@@ -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);
diff --git a/unittests/unittest.h b/unittests/unittest.h
index 786c1c97..ecb7d124 100644
--- a/unittests/unittest.h
+++ b/unittests/unittest.h
@@ -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);