Debian Codegraph @

testing/fulltests/unit-tests/T017strtoull_clib.c

raw
1 /* HEADER Testing strtoull(). */
2
3 #ifdef HAVE_STRTOULL
4
5 OK(1, "Skipping strtoull() test because using strtoull() from C library.\n");
6
7 #else
8
9 /*
10 * UINT64_C: C99 macro for the suffix for uint64_t constants.
11 */
12 #ifndef UINT64_C
13 #ifdef _MSC_VER
14 #define UINT64_C(c) c##ui64
15 #else
16 #define UINT64_C(c) c##ULL
17 #endif
18 #endif
19
20 /*
21 * According to the C99 standard, the constant ULLONG_MAX must be defined in
22 * <limits.h>. Define it here for pre-C99 compilers.
23 */
24 #ifndef ULLONG_MAX
25 #define ULLONG_MAX UINT64_C(0xffffffffffffffff)
26 #endif
27
28 #ifndef PRIu64
29 #ifdef _MSC_VER
30 #define PRIu64 "I64u"
31 #else
32 #define PRIu64 "llu"
33 #endif
34 #endif
35
36 struct strtoull_testcase {
37 /*
38 * inputs
39 */
40 const char *nptr;
41 int base;
42 /*
43 * expected outputs
44 */
45 int expected_errno;
46 int expected_end;
47 uint64_t expected_result;
48 };
49
50 static const struct strtoull_testcase test_input[] = {
51 {"0x0", 0, 0, 3, 0},
52 {"1", 0, 0, 1, 1},
53 {"0x1", 0, 0, 3, 1},
54 {" -0666", 0, 0, 7, -0666},
55 {" -0x666", 0, 0, 8, -0x666},
56 {"18446744073709551614", 0, 0, 20, UINT64_C(0xfffffffffffffffe)},
57 {"0xfffffffffffffffe", 0, 0, 18, UINT64_C(0xfffffffffffffffe)},
58 {"18446744073709551615", 0, 0, 20, UINT64_C(0xffffffffffffffff)},
59 {"0xffffffffffffffff", 0, 0, 18, UINT64_C(0xffffffffffffffff)},
60 {"18446744073709551616", 0, ERANGE, 20, UINT64_C(0xffffffffffffffff)},
61 {"0x10000000000000000", 0, ERANGE, 19, UINT64_C(0xffffffffffffffff)},
62 {"ff", 16, 0, 2, 255},
63 {"0xff", 16, 0, 4, 255},
64 {" ", 0, 0, 0, 0},
65 {"0x", 0, 0, 1, 0},
66 {"0x", 8, 0, 1, 0},
67 {"0x", 16, 0, 1, 0},
68 {"zyyy", 0, 0, 0, 0},
69 {"0xfffffffffffffffff", 0, ERANGE, 19, ULLONG_MAX},
70 {"0xfffffffffffffffffz", 0, ERANGE, 19, ULLONG_MAX}
71 };
72
73 unsigned int i;
74
75 for (i = 0; i < sizeof(test_input) / sizeof(test_input[0]); i++) {
76 const struct strtoull_testcase *const p = &test_input[i];
77 char *endptr;
78 uint64_t result;
79
80 errno = 0;
81 result = strtoull(p->nptr, &endptr, p->base);
82 OKF(errno == p->expected_errno,
83 ("test %d (input \"%s\"): expected errno %d, got errno %d",
84 i, p->nptr, p->expected_errno, errno));
85 OKF(result == p->expected_result,
86 ("test %d (input \"%s\"): expected result %" PRIu64
87 ", got result %" PRIu64,
88 i, p->nptr, p->expected_result, result));
89 OKF(endptr - p->nptr == p->expected_end,
90 ("test %d (input \"%s\"): expected end %d, got end %d",
91 i, p->nptr, p->expected_end, (int) (endptr - p->nptr)));
92 }
93
94 #endif