Mercurial > gnulib
annotate tests/test-strtoumax.c @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
15553 | 1 /* |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 * Copyright (C) 2011-2019 Free Software Foundation, Inc. |
15553 | 3 * |
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. | |
8 * | |
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. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
19190 | 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
15553 | 16 |
17 #include <config.h> | |
18 | |
19 #include <inttypes.h> | |
20 | |
21 #include "signature.h" | |
22 #ifndef strtoumax | |
23 SIGNATURE_CHECK (strtoumax, uintmax_t, (const char *, char **, int)); | |
24 #endif | |
25 | |
26 #include <errno.h> | |
27 | |
28 #include "macros.h" | |
29 | |
30 int | |
31 main (void) | |
32 { | |
33 /* Subject sequence empty or invalid. */ | |
34 { | |
35 const char input[] = ""; | |
36 char *ptr; | |
37 uintmax_t result; | |
38 errno = 0; | |
39 result = strtoumax (input, &ptr, 10); | |
40 ASSERT (result == 0); | |
41 ASSERT (ptr == input); | |
42 ASSERT (errno == 0 || errno == EINVAL); | |
43 } | |
44 { | |
45 const char input[] = " "; | |
46 char *ptr; | |
47 uintmax_t result; | |
48 errno = 0; | |
49 result = strtoumax (input, &ptr, 10); | |
50 ASSERT (result == 0); | |
51 ASSERT (ptr == input); | |
52 ASSERT (errno == 0 || errno == EINVAL); | |
53 } | |
54 { | |
55 const char input[] = " +"; | |
56 char *ptr; | |
57 uintmax_t result; | |
58 errno = 0; | |
59 result = strtoumax (input, &ptr, 10); | |
60 ASSERT (result == 0); | |
61 ASSERT (ptr == input); | |
62 ASSERT (errno == 0 || errno == EINVAL); | |
63 } | |
64 { | |
65 const char input[] = " -"; | |
66 char *ptr; | |
67 uintmax_t result; | |
68 errno = 0; | |
69 result = strtoumax (input, &ptr, 10); | |
70 ASSERT (result == 0); | |
71 ASSERT (ptr == input); | |
72 ASSERT (errno == 0 || errno == EINVAL); | |
73 } | |
74 | |
75 /* Simple integer values. */ | |
76 { | |
77 const char input[] = "0"; | |
78 char *ptr; | |
79 uintmax_t result; | |
80 errno = 0; | |
81 result = strtoumax (input, &ptr, 10); | |
82 ASSERT (result == 0); | |
83 ASSERT (ptr == input + 1); | |
84 ASSERT (errno == 0); | |
85 } | |
86 { | |
87 const char input[] = "+0"; | |
88 char *ptr; | |
89 uintmax_t result; | |
90 errno = 0; | |
91 result = strtoumax (input, &ptr, 10); | |
92 ASSERT (result == 0); | |
93 ASSERT (ptr == input + 2); | |
94 ASSERT (errno == 0); | |
95 } | |
96 { | |
97 const char input[] = "-0"; | |
98 char *ptr; | |
99 uintmax_t result; | |
100 errno = 0; | |
101 result = strtoumax (input, &ptr, 10); | |
102 ASSERT (result == 0); | |
103 ASSERT (ptr == input + 2); | |
104 ASSERT (errno == 0); | |
105 } | |
106 { | |
107 const char input[] = "23"; | |
108 char *ptr; | |
109 uintmax_t result; | |
110 errno = 0; | |
111 result = strtoumax (input, &ptr, 10); | |
112 ASSERT (result == 23); | |
113 ASSERT (ptr == input + 2); | |
114 ASSERT (errno == 0); | |
115 } | |
116 { | |
117 const char input[] = " 23"; | |
118 char *ptr; | |
119 uintmax_t result; | |
120 errno = 0; | |
121 result = strtoumax (input, &ptr, 10); | |
122 ASSERT (result == 23); | |
123 ASSERT (ptr == input + 3); | |
124 ASSERT (errno == 0); | |
125 } | |
126 { | |
127 const char input[] = "+23"; | |
128 char *ptr; | |
129 uintmax_t result; | |
130 errno = 0; | |
131 result = strtoumax (input, &ptr, 10); | |
132 ASSERT (result == 23); | |
133 ASSERT (ptr == input + 3); | |
134 ASSERT (errno == 0); | |
135 } | |
136 { | |
137 const char input[] = "-23"; | |
138 char *ptr; | |
139 uintmax_t result; | |
140 errno = 0; | |
141 result = strtoumax (input, &ptr, 10); | |
142 ASSERT (result == - (uintmax_t) 23); | |
143 ASSERT (ptr == input + 3); | |
144 ASSERT (errno == 0); | |
145 } | |
146 | |
16231
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
147 /* Large integer values. */ |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
148 { |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
149 const char input[] = "2147483647"; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
150 char *ptr; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
151 uintmax_t result; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
152 errno = 0; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
153 result = strtoumax (input, &ptr, 10); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
154 ASSERT (result == 2147483647); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
155 ASSERT (ptr == input + 10); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
156 ASSERT (errno == 0); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
157 } |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
158 { |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
159 const char input[] = "-2147483648"; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
160 char *ptr; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
161 uintmax_t result; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
162 errno = 0; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
163 result = strtoumax (input, &ptr, 10); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
164 ASSERT (result == - (uintmax_t) 2147483648U); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
165 ASSERT (ptr == input + 11); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
166 ASSERT (errno == 0); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
167 } |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
168 { |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
169 const char input[] = "4294967295"; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
170 char *ptr; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
171 uintmax_t result; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
172 errno = 0; |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
173 result = strtoumax (input, &ptr, 10); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
174 ASSERT (result == 4294967295U); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
175 ASSERT (ptr == input + 10); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
176 ASSERT (errno == 0); |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
177 } |
e8cde289d525
strtoumax tests: Enhance tests.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
178 |
15553 | 179 return 0; |
180 } |