forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-uint128.c
More file actions
88 lines (69 loc) · 1.97 KB
/
test-uint128.c
File metadata and controls
88 lines (69 loc) · 1.97 KB
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
// 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;
const char *exp = test->exp;
if (!setlocale(LC_NUMERIC, test->locale))
return;
if (test->locale) {
/* For locale tests, adapt to what the system provides.
* musl libc may not support thousands_sep for all locales. */
struct lconv *lc = localeconv();
const char *sep = lc->thousands_sep;
if (!sep || !*sep) {
/* No separator available, skip test */
fprintf(stderr, "WARNING: thousands_sep is empty for "
"this system's %s locale! Skipping test...\n",
test->locale);
return;
}
str = uint128_t_to_l10n_string(test->val);
} else {
str = uint128_t_to_string(test->val);
}
check_str(test->val, 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;
}