posix: Reformat Makefile.
[glibc.git] / stdlib / tst-strtol-binary-main.c
blobece310029820a1830c0e5b4611bde657b54a9ae4
1 /* Test strtol functions with C2X binary integers.
2 Copyright (C) 2022-2023 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/>. */
19 #include <inttypes.h>
20 #include <limits.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <wchar.h>
26 #include <support/check.h>
27 #if TEST_LOCALE
28 # include <support/support.h>
29 #endif
31 #define CONCAT_(X, Y) X ## Y
32 #define CONCAT(X, Y) CONCAT_ (X, Y)
33 #define FNX(FN) CONCAT (FNPFX, FN)
35 #define CHECK_RES(ARG, RES, EP, EXPECTED) \
36 do \
37 { \
38 if (TEST_C2X) \
39 { \
40 TEST_COMPARE ((RES), EXPECTED); \
41 TEST_COMPARE (*(EP), 0); \
42 } \
43 else \
44 { \
45 TEST_COMPARE ((RES), 0); \
46 TEST_VERIFY ((EP) == ((ARG)[0] == L_('-') \
47 ? (ARG) + 2 \
48 : (ARG) + 1)); \
49 } \
50 } \
51 while (0)
53 static void
54 one_check (const CHAR *s, long int expected_l, unsigned long int expected_ul,
55 long long int expected_ll, unsigned long long int expected_ull)
57 CHAR *ep;
58 long int ret_l;
59 unsigned long int ret_ul;
60 long long int ret_ll;
61 unsigned long long int ret_ull;
62 ret_l = FNX (l) (s, &ep, 0);
63 CHECK_RES (s, ret_l, ep, expected_l);
64 ret_l = FNX (l) (s, &ep, 2);
65 CHECK_RES (s, ret_l, ep, expected_l);
66 ret_ul = FNX (ul) (s, &ep, 0);
67 CHECK_RES (s, ret_ul, ep, expected_ul);
68 ret_ul = FNX (ul) (s, &ep, 2);
69 CHECK_RES (s, ret_ul, ep, expected_ul);
70 ret_ll = FNX (ll) (s, &ep, 0);
71 CHECK_RES (s, ret_ll, ep, expected_ll);
72 ret_ll = FNX (ll) (s, &ep, 2);
73 CHECK_RES (s, ret_ll, ep, expected_ll);
74 ret_ull = FNX (ull) (s, &ep, 0);
75 CHECK_RES (s, ret_ull, ep, expected_ull);
76 ret_ull = FNX (ull) (s, &ep, 2);
77 CHECK_RES (s, ret_ull, ep, expected_ull);
78 ret_ll = FNX (imax) (s, &ep, 0);
79 CHECK_RES (s, ret_ll, ep, expected_ll);
80 ret_ll = FNX (imax) (s, &ep, 2);
81 CHECK_RES (s, ret_ll, ep, expected_ll);
82 ret_ull = FNX (umax) (s, &ep, 0);
83 CHECK_RES (s, ret_ull, ep, expected_ull);
84 ret_ull = FNX (umax) (s, &ep, 2);
85 CHECK_RES (s, ret_ull, ep, expected_ull);
86 #if TEST_Q
87 ret_ll = FNX (q) (s, &ep, 0);
88 CHECK_RES (s, ret_ll, ep, expected_ll);
89 ret_ll = FNX (q) (s, &ep, 2);
90 CHECK_RES (s, ret_ll, ep, expected_ll);
91 ret_ull = FNX (uq) (s, &ep, 0);
92 CHECK_RES (s, ret_ull, ep, expected_ull);
93 ret_ull = FNX (uq) (s, &ep, 2);
94 CHECK_RES (s, ret_ull, ep, expected_ull);
95 #endif
96 #if TEST_LOCALE
97 locale_t loc = xnewlocale (LC_NUMERIC_MASK, "C", (locale_t) 0);
98 ret_l = FNX (l_l) (s, &ep, 0, loc);
99 CHECK_RES (s, ret_l, ep, expected_l);
100 ret_l = FNX (l_l) (s, &ep, 2, loc);
101 CHECK_RES (s, ret_l, ep, expected_l);
102 ret_ul = FNX (ul_l) (s, &ep, 0, loc);
103 CHECK_RES (s, ret_ul, ep, expected_ul);
104 ret_ul = FNX (ul_l) (s, &ep, 2, loc);
105 CHECK_RES (s, ret_ul, ep, expected_ul);
106 ret_ll = FNX (ll_l) (s, &ep, 0, loc);
107 CHECK_RES (s, ret_ll, ep, expected_ll);
108 ret_ll = FNX (ll_l) (s, &ep, 2, loc);
109 CHECK_RES (s, ret_ll, ep, expected_ll);
110 ret_ull = FNX (ull_l) (s, &ep, 0, loc);
111 CHECK_RES (s, ret_ull, ep, expected_ull);
112 ret_ull = FNX (ull_l) (s, &ep, 2, loc);
113 CHECK_RES (s, ret_ull, ep, expected_ull);
114 #endif
117 static int
118 do_test (void)
120 one_check (L_("0b101"), 5, 5, 5, 5);
121 one_check (L_("0B101"), 5, 5, 5, 5);
122 one_check (L_("-0b11111"), -31, -31, -31, -31);
123 one_check (L_("-0B11111"), -31, -31, -31, -31);
124 one_check (L_("0b111111111111111111111111111111111"),
125 LONG_MAX >= 0x1ffffffffLL ? (long int) 0x1ffffffffLL : LONG_MAX,
126 (ULONG_MAX >= 0x1ffffffffULL
127 ? (unsigned long int) 0x1ffffffffULL
128 : ULONG_MAX),
129 0x1ffffffffLL, 0x1ffffffffULL);
130 one_check (L_("0B111111111111111111111111111111111"),
131 LONG_MAX >= 0x1ffffffffLL ? (long int) 0x1ffffffffLL : LONG_MAX,
132 (ULONG_MAX >= 0x1ffffffffULL
133 ? (unsigned long int) 0x1ffffffffULL
134 : ULONG_MAX),
135 0x1ffffffffLL, 0x1ffffffffULL);
136 one_check (L_("-0b111111111111111111111111111111111"),
137 LONG_MIN <= -0x1ffffffffLL ? (long int) -0x1ffffffffLL : LONG_MIN,
138 (ULONG_MAX >= 0x1ffffffffULL
139 ? (unsigned long int) -0x1ffffffffULL
140 : ULONG_MAX),
141 -0x1ffffffffLL, -0x1ffffffffULL);
142 one_check (L_("-0B111111111111111111111111111111111"),
143 LONG_MIN <= -0x1ffffffffLL ? (long int) -0x1ffffffffLL : LONG_MIN,
144 (ULONG_MAX >= 0x1ffffffffULL
145 ? (unsigned long int) -0x1ffffffffULL
146 : ULONG_MAX),
147 -0x1ffffffffLL, -0x1ffffffffULL);
148 return 0;
151 #include <support/test-driver.c>