num2str(): Add the E (exa) prefix
[fio.git] / unittests / lib / num2str.c
1 #include <limits.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include "../../compiler/compiler.h"
5 #include "../../lib/num2str.h"
6 #include "../unittest.h"
7
8 struct testcase {
9         uint64_t num;
10         int maxlen;
11         int base;
12         int pow2;
13         enum n2s_unit unit;
14         const char *expected;
15 };
16
17 static const struct testcase testcases[] = {
18         { 1, 1, 1, 0, N2S_NONE, "1" },
19         { UINT64_MAX, 99, 1, 0, N2S_NONE, "18446744073709551615" },
20         { 18446744073709551, 2, 1, 0, N2S_NONE, "18P" },
21         { 18446744073709551, 4, 1, 0, N2S_NONE, "18.4P" },
22         { UINT64_MAX, 2, 1, 0, N2S_NONE, "18E" },
23         { UINT64_MAX, 4, 1, 0, N2S_NONE, "18.4E" },
24 };
25
26 static void test_num2str(void)
27 {
28         const struct testcase *p;
29         char *str;
30         int i;
31
32         for (i = 0; i < ARRAY_SIZE(testcases); ++i) {
33                 p = &testcases[i];
34                 str = num2str(p->num, p->maxlen, p->base, p->pow2, p->unit);
35                 CU_ASSERT_STRING_EQUAL(str, p->expected);
36                 free(str);
37         }
38 }
39
40 static struct fio_unittest_entry tests[] = {
41         {
42                 .name   = "num2str/1",
43                 .fn     = test_num2str,
44         },
45         {
46                 .name   = NULL,
47         },
48 };
49
50 CU_ErrorCode fio_unittest_lib_num2str(void)
51 {
52         return fio_unittest_add_suite("lib/num2str.c", NULL, NULL, tests);
53 }