usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-strtoll.c
blobcef67231b9a77ffdd72d4f59aeb26a4fb7b2474e
1 /*
2 * Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include <stdlib.h>
21 #include "signature.h"
22 #ifndef strtoll
23 SIGNATURE_CHECK (strtoll, long long, (const char *, char **, int));
24 #endif
26 #include <errno.h>
28 #include "macros.h"
30 int
31 main (void)
33 /* Subject sequence empty or invalid. */
35 const char input[] = "";
36 char *ptr;
37 long long result;
38 errno = 0;
39 result = strtoll (input, &ptr, 10);
40 ASSERT (result == 0);
41 ASSERT (ptr == input);
42 ASSERT (errno == 0 || errno == EINVAL);
45 const char input[] = " ";
46 char *ptr;
47 long long result;
48 errno = 0;
49 result = strtoll (input, &ptr, 10);
50 ASSERT (result == 0);
51 ASSERT (ptr == input);
52 ASSERT (errno == 0 || errno == EINVAL);
55 const char input[] = " +";
56 char *ptr;
57 long long result;
58 errno = 0;
59 result = strtoll (input, &ptr, 10);
60 ASSERT (result == 0);
61 ASSERT (ptr == input);
62 ASSERT (errno == 0 || errno == EINVAL);
65 const char input[] = " -";
66 char *ptr;
67 long long result;
68 errno = 0;
69 result = strtoll (input, &ptr, 10);
70 ASSERT (result == 0);
71 ASSERT (ptr == input);
72 ASSERT (errno == 0 || errno == EINVAL);
75 /* Simple integer values. */
77 const char input[] = "0";
78 char *ptr;
79 long long result;
80 errno = 0;
81 result = strtoll (input, &ptr, 10);
82 ASSERT (result == 0);
83 ASSERT (ptr == input + 1);
84 ASSERT (errno == 0);
87 const char input[] = "+0";
88 char *ptr;
89 long long result;
90 errno = 0;
91 result = strtoll (input, &ptr, 10);
92 ASSERT (result == 0);
93 ASSERT (ptr == input + 2);
94 ASSERT (errno == 0);
97 const char input[] = "-0";
98 char *ptr;
99 long long result;
100 errno = 0;
101 result = strtoll (input, &ptr, 10);
102 ASSERT (result == 0);
103 ASSERT (ptr == input + 2);
104 ASSERT (errno == 0);
107 const char input[] = "23";
108 char *ptr;
109 long long result;
110 errno = 0;
111 result = strtoll (input, &ptr, 10);
112 ASSERT (result == 23);
113 ASSERT (ptr == input + 2);
114 ASSERT (errno == 0);
117 const char input[] = " 23";
118 char *ptr;
119 long long result;
120 errno = 0;
121 result = strtoll (input, &ptr, 10);
122 ASSERT (result == 23);
123 ASSERT (ptr == input + 3);
124 ASSERT (errno == 0);
127 const char input[] = "+23";
128 char *ptr;
129 long long result;
130 errno = 0;
131 result = strtoll (input, &ptr, 10);
132 ASSERT (result == 23);
133 ASSERT (ptr == input + 3);
134 ASSERT (errno == 0);
137 const char input[] = "-23";
138 char *ptr;
139 long long result;
140 errno = 0;
141 result = strtoll (input, &ptr, 10);
142 ASSERT (result == -23);
143 ASSERT (ptr == input + 3);
144 ASSERT (errno == 0);
147 /* Large integer values. */
149 const char input[] = "2147483647";
150 char *ptr;
151 long long result;
152 errno = 0;
153 result = strtoll (input, &ptr, 10);
154 ASSERT (result == 2147483647);
155 ASSERT (ptr == input + 10);
156 ASSERT (errno == 0);
159 const char input[] = "-2147483648";
160 char *ptr;
161 long long result;
162 errno = 0;
163 result = strtoll (input, &ptr, 10);
164 ASSERT (result == -2147483647 - 1);
165 ASSERT (ptr == input + 11);
166 ASSERT (errno == 0);
168 if (sizeof (long long) > sizeof (int))
170 const char input[] = "4294967295";
171 char *ptr;
172 long long result;
173 errno = 0;
174 result = strtoll (input, &ptr, 10);
175 ASSERT (result == 65535LL * 65537LL);
176 ASSERT (ptr == input + 10);
177 ASSERT (errno == 0);
180 /* Hexadecimal integer syntax. */
182 const char input[] = "0x2A";
183 char *ptr;
184 long long result;
185 errno = 0;
186 result = strtoll (input, &ptr, 10);
187 ASSERT (result == 0LL);
188 ASSERT (ptr == input + 1);
189 ASSERT (errno == 0);
192 const char input[] = "0x2A";
193 char *ptr;
194 long long result;
195 errno = 0;
196 result = strtoll (input, &ptr, 16);
197 ASSERT (result == 42LL);
198 ASSERT (ptr == input + 4);
199 ASSERT (errno == 0);
202 const char input[] = "0x2A";
203 char *ptr;
204 long long result;
205 errno = 0;
206 result = strtoll (input, &ptr, 0);
207 ASSERT (result == 42LL);
208 ASSERT (ptr == input + 4);
209 ASSERT (errno == 0);
212 const char input[] = "0x";
213 char *ptr;
214 long long result;
215 errno = 0;
216 result = strtoll (input, &ptr, 10);
217 ASSERT (result == 0LL);
218 ASSERT (ptr == input + 1);
219 ASSERT (errno == 0);
222 const char input[] = "0x";
223 char *ptr;
224 long long result;
225 errno = 0;
226 result = strtoll (input, &ptr, 16);
227 ASSERT (result == 0LL);
228 ASSERT (ptr == input + 1);
229 ASSERT (errno == 0);
232 const char input[] = "0x";
233 char *ptr;
234 long long result;
235 errno = 0;
236 result = strtoll (input, &ptr, 0);
237 ASSERT (result == 0LL);
238 ASSERT (ptr == input + 1);
239 ASSERT (errno == 0);
242 /* Binary integer syntax. */
244 const char input[] = "0b111010";
245 char *ptr;
246 long long result;
247 errno = 0;
248 result = strtoll (input, &ptr, 10);
249 ASSERT (result == 0LL);
250 ASSERT (ptr == input + 1);
251 ASSERT (errno == 0);
254 const char input[] = "0b111010";
255 char *ptr;
256 long long result;
257 errno = 0;
258 result = strtoll (input, &ptr, 2);
259 ASSERT (result == 58LL);
260 ASSERT (ptr == input + 8);
261 ASSERT (errno == 0);
264 const char input[] = "0b111010";
265 char *ptr;
266 long long result;
267 errno = 0;
268 result = strtoll (input, &ptr, 0);
269 ASSERT (result == 58LL);
270 ASSERT (ptr == input + 8);
271 ASSERT (errno == 0);
274 const char input[] = "0b";
275 char *ptr;
276 long long result;
277 errno = 0;
278 result = strtoll (input, &ptr, 10);
279 ASSERT (result == 0LL);
280 ASSERT (ptr == input + 1);
281 ASSERT (errno == 0);
284 const char input[] = "0b";
285 char *ptr;
286 long long result;
287 errno = 0;
288 result = strtoll (input, &ptr, 2);
289 ASSERT (result == 0LL);
290 ASSERT (ptr == input + 1);
291 ASSERT (errno == 0);
294 const char input[] = "0b";
295 char *ptr;
296 long long result;
297 errno = 0;
298 result = strtoll (input, &ptr, 0);
299 ASSERT (result == 0LL);
300 ASSERT (ptr == input + 1);
301 ASSERT (errno == 0);
304 return test_exit_status;