1 /* Test strtol functions with C23 binary integers.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 #include <support/check.h>
29 # include <support/support.h>
32 #define CONCAT_(X, Y) X ## Y
33 #define CONCAT(X, Y) CONCAT_ (X, Y)
34 #define FNX(FN) CONCAT (FNPFX, FN)
37 # define STRCHR wcschr
39 # define STRCHR strchr
42 #define CHECK_RES(ARG, RES, EP, EXPECTED, EXPECTED_EP) \
47 TEST_COMPARE ((RES), EXPECTED); \
48 TEST_VERIFY ((EP) == EXPECTED_EP); \
52 TEST_COMPARE ((RES), 0); \
53 TEST_VERIFY ((EP) == ((ARG)[0] == L_('-') \
61 one_check (const CHAR
*s
, const CHAR
*expected_p
, long int expected_l
,
62 unsigned long int expected_ul
, long long int expected_ll
,
63 unsigned long long int expected_ull
)
65 expected_p
= expected_p
== NULL
? STRCHR (s
, '\0') : expected_p
;
69 unsigned long int ret_ul
;
71 unsigned long long int ret_ull
;
72 ret_l
= FNX (l
) (s
, &ep
, 0);
73 CHECK_RES (s
, ret_l
, ep
, expected_l
, expected_p
);
74 ret_l
= FNX (l
) (s
, &ep
, 2);
75 CHECK_RES (s
, ret_l
, ep
, expected_l
, expected_p
);
76 ret_ul
= FNX (ul
) (s
, &ep
, 0);
77 CHECK_RES (s
, ret_ul
, ep
, expected_ul
, expected_p
);
78 ret_ul
= FNX (ul
) (s
, &ep
, 2);
79 CHECK_RES (s
, ret_ul
, ep
, expected_ul
, expected_p
);
80 ret_ll
= FNX (ll
) (s
, &ep
, 0);
81 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
82 ret_ll
= FNX (ll
) (s
, &ep
, 2);
83 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
84 ret_ull
= FNX (ull
) (s
, &ep
, 0);
85 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
86 ret_ull
= FNX (ull
) (s
, &ep
, 2);
87 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
88 ret_ll
= FNX (imax
) (s
, &ep
, 0);
89 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
90 ret_ll
= FNX (imax
) (s
, &ep
, 2);
91 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
92 ret_ull
= FNX (umax
) (s
, &ep
, 0);
93 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
94 ret_ull
= FNX (umax
) (s
, &ep
, 2);
95 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
97 ret_ll
= FNX (q
) (s
, &ep
, 0);
98 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
99 ret_ll
= FNX (q
) (s
, &ep
, 2);
100 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
101 ret_ull
= FNX (uq
) (s
, &ep
, 0);
102 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
103 ret_ull
= FNX (uq
) (s
, &ep
, 2);
104 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
107 locale_t loc
= xnewlocale (LC_NUMERIC_MASK
, "C", (locale_t
) 0);
108 ret_l
= FNX (l_l
) (s
, &ep
, 0, loc
);
109 CHECK_RES (s
, ret_l
, ep
, expected_l
, expected_p
);
110 ret_l
= FNX (l_l
) (s
, &ep
, 2, loc
);
111 CHECK_RES (s
, ret_l
, ep
, expected_l
, expected_p
);
112 ret_ul
= FNX (ul_l
) (s
, &ep
, 0, loc
);
113 CHECK_RES (s
, ret_ul
, ep
, expected_ul
, expected_p
);
114 ret_ul
= FNX (ul_l
) (s
, &ep
, 2, loc
);
115 CHECK_RES (s
, ret_ul
, ep
, expected_ul
, expected_p
);
116 ret_ll
= FNX (ll_l
) (s
, &ep
, 0, loc
);
117 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
118 ret_ll
= FNX (ll_l
) (s
, &ep
, 2, loc
);
119 CHECK_RES (s
, ret_ll
, ep
, expected_ll
, expected_p
);
120 ret_ull
= FNX (ull_l
) (s
, &ep
, 0, loc
);
121 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
122 ret_ull
= FNX (ull_l
) (s
, &ep
, 2, loc
);
123 CHECK_RES (s
, ret_ull
, ep
, expected_ull
, expected_p
);
131 const CHAR
*input
= L_("0b");
132 one_check (input
, input
+ 1, 0L, 0UL, 0LL, 0ULL);
134 one_check (L_("0b101"), NULL
, 5, 5, 5, 5);
135 one_check (L_("0B101"), NULL
, 5, 5, 5, 5);
136 one_check (L_("-0b11111"), NULL
, -31, -31, -31, -31);
137 one_check (L_("-0B11111"), NULL
, -31, -31, -31, -31);
138 one_check (L_("0b111111111111111111111111111111111"), NULL
,
139 LONG_MAX
>= 0x1ffffffffLL
? (long int) 0x1ffffffffLL
: LONG_MAX
,
140 (ULONG_MAX
>= 0x1ffffffffULL
141 ? (unsigned long int) 0x1ffffffffULL
143 0x1ffffffffLL
, 0x1ffffffffULL
);
144 one_check (L_("0B111111111111111111111111111111111"), NULL
,
145 LONG_MAX
>= 0x1ffffffffLL
? (long int) 0x1ffffffffLL
: LONG_MAX
,
146 (ULONG_MAX
>= 0x1ffffffffULL
147 ? (unsigned long int) 0x1ffffffffULL
149 0x1ffffffffLL
, 0x1ffffffffULL
);
150 one_check (L_("-0b111111111111111111111111111111111"), NULL
,
151 LONG_MIN
<= -0x1ffffffffLL
? (long int) -0x1ffffffffLL
: LONG_MIN
,
152 (ULONG_MAX
>= 0x1ffffffffULL
153 ? (unsigned long int) -0x1ffffffffULL
155 -0x1ffffffffLL
, -0x1ffffffffULL
);
156 one_check (L_("-0B111111111111111111111111111111111"), NULL
,
157 LONG_MIN
<= -0x1ffffffffLL
? (long int) -0x1ffffffffLL
: LONG_MIN
,
158 (ULONG_MAX
>= 0x1ffffffffULL
159 ? (unsigned long int) -0x1ffffffffULL
161 -0x1ffffffffLL
, -0x1ffffffffULL
);
165 #include <support/test-driver.c>