File: num2str.c

package info (click to toggle)
fio 3.41-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,012 kB
  • sloc: ansic: 82,290; python: 9,862; sh: 6,067; makefile: 813; yacc: 204; lex: 184
file content (88 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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, "18E" },
	{ UINT64_MAX, 4, 1, 0, N2S_NONE, "18.4E" },
};

static void test_num2str(void)
{
	const struct testcase *p;
	char *str;
	int i;

	for (i = 0; i < FIO_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);
	}
}

struct bytes2str_testcase {
        uint64_t bytes;
        const char *expected;
};

static const struct bytes2str_testcase bytes2str_testcases[] = {
        { 0, "0.00 B" },
        { 512, "512.00 B" },
        { 1024, "1.00 KiB" },
        { 1536, "1.50 KiB" },
        { 1048576, "1.00 MiB" },
        { 1073741824ULL, "1.00 GiB" },
        { 1099511627776ULL, "1.00 TiB" },
        { 1125899906842624ULL, "1.00 PiB" },
        { 1152921504606846976ULL, "1.00 EiB" },
};

static void test_bytes2str_simple(void)
{
        char buf[64];
        int i;

        for (i = 0; i < FIO_ARRAY_SIZE(bytes2str_testcases); ++i) {
                const struct bytes2str_testcase *tc = &bytes2str_testcases[i];
                const char *result = bytes2str_simple(buf, sizeof(buf), tc->bytes);

                CU_ASSERT_PTR_EQUAL(result, buf);
                CU_ASSERT_STRING_EQUAL(result, tc->expected);
        }
}

static struct fio_unittest_entry tests[] = {
	{
		.name	= "num2str/1",
		.fn	= test_num2str,
	},
	{
                .name   = "bytes2str_simple/1",
                .fn     = test_bytes2str_simple,
        },
	{
		.name	= NULL,
	},
};

CU_ErrorCode fio_unittest_lib_num2str(void)
{
	return fio_unittest_add_suite("lib/num2str.c", NULL, NULL, tests);
}