1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <inttypes.h>
18
19 #include <errno.h>
20 #include <gtest/gtest.h>
21 #include <stdio.h>
22
23 #define PRINTF_TYPED(FMT_SUFFIX, TYPE_SUFFIX) \
24 do { \
25 char buf[512]; \
26 memset(buf, 0, sizeof(buf)); \
27 snprintf(buf, sizeof(buf), "%" PRId##FMT_SUFFIX, int##TYPE_SUFFIX(123)); \
28 EXPECT_STREQ("123", buf); \
29 memset(buf, 0, sizeof(buf)); \
30 snprintf(buf, sizeof(buf), "%" PRIi##FMT_SUFFIX, int##TYPE_SUFFIX(123)); \
31 EXPECT_STREQ("123", buf); \
32 memset(buf, 0, sizeof(buf)); \
33 snprintf(buf, sizeof(buf), "%" PRIo##FMT_SUFFIX, int##TYPE_SUFFIX(123)); \
34 EXPECT_STREQ("173", buf); \
35 memset(buf, 0, sizeof(buf)); \
36 snprintf(buf, sizeof(buf), "%" PRIu##FMT_SUFFIX, uint##TYPE_SUFFIX(123)); \
37 EXPECT_STREQ("123", buf); \
38 memset(buf, 0, sizeof(buf)); \
39 snprintf(buf, sizeof(buf), "%" PRIx##FMT_SUFFIX, uint##TYPE_SUFFIX(123)); \
40 EXPECT_STREQ("7b", buf); \
41 memset(buf, 0, sizeof(buf)); \
42 snprintf(buf, sizeof(buf), "%" PRIX##FMT_SUFFIX, uint##TYPE_SUFFIX(123)); \
43 EXPECT_STREQ("7B", buf); \
44 } while (false) \
45
46 #define PRINTF_SIZED(WIDTH) \
47 PRINTF_TYPED(WIDTH, WIDTH##_t); \
48 PRINTF_TYPED(FAST##WIDTH, _fast##WIDTH##_t); \
49 PRINTF_TYPED(LEAST##WIDTH, _least##WIDTH##_t) \
50
51
52 #define SCANF_TYPED(FMT_SUFFIX, TYPE_SUFFIX) \
53 do { \
54 int##TYPE_SUFFIX dst_int##TYPE_SUFFIX = 0; \
55 uint##TYPE_SUFFIX dst_uint##TYPE_SUFFIX = 0u; \
56 \
57 sscanf("123", "%" SCNd##FMT_SUFFIX, &dst_int##TYPE_SUFFIX); \
58 EXPECT_EQ(123, dst_int##TYPE_SUFFIX); \
59 dst_int##TYPE_SUFFIX = 0; \
60 sscanf("123", "%" SCNi##FMT_SUFFIX, &dst_int##TYPE_SUFFIX); \
61 EXPECT_EQ(123, dst_int##TYPE_SUFFIX); \
62 dst_int##TYPE_SUFFIX = 0; \
63 sscanf("173", "%" SCNo##FMT_SUFFIX, &dst_int##TYPE_SUFFIX); \
64 EXPECT_EQ(123, dst_int##TYPE_SUFFIX); \
65 dst_int##TYPE_SUFFIX = 0; \
66 sscanf("123", "%" SCNu##FMT_SUFFIX, &dst_uint##TYPE_SUFFIX); \
67 EXPECT_EQ(123u, dst_uint##TYPE_SUFFIX); \
68 dst_uint##TYPE_SUFFIX = 0; \
69 sscanf("7B", "%" SCNx##FMT_SUFFIX, &dst_uint##TYPE_SUFFIX); \
70 EXPECT_EQ(123u, dst_uint##TYPE_SUFFIX); \
71 dst_uint##TYPE_SUFFIX = 0; \
72 } while (false) \
73
74 #define SCANF_SIZED(SIZE) \
75 SCANF_TYPED(SIZE, SIZE##_t); \
76 SCANF_TYPED(FAST##SIZE, _fast##SIZE##_t); \
77 SCANF_TYPED(LEAST##SIZE, _least##SIZE##_t) \
78
79
TEST(inttypes,printf_macros)80 TEST(inttypes, printf_macros) {
81 PRINTF_SIZED(8);
82 PRINTF_SIZED(16);
83 PRINTF_SIZED(32);
84 PRINTF_SIZED(64);
85
86 PRINTF_TYPED(MAX, max_t);
87 PRINTF_TYPED(PTR, ptr_t);
88 }
89
TEST(inttypes,scanf_macros)90 TEST(inttypes, scanf_macros) {
91 SCANF_SIZED(8);
92 SCANF_SIZED(16);
93 SCANF_SIZED(32);
94 SCANF_SIZED(64);
95
96 SCANF_TYPED(MAX, max_t);
97 SCANF_TYPED(PTR, ptr_t);
98 }
99
TEST(inttypes,wcstoimax)100 TEST(inttypes, wcstoimax) {
101 wchar_t* end = nullptr;
102 EXPECT_EQ(123, wcstoimax(L" +123x", &end, 10));
103 EXPECT_EQ(L'x', *end);
104 }
105
TEST(inttypes,wcstoumax)106 TEST(inttypes, wcstoumax) {
107 wchar_t* end = nullptr;
108 EXPECT_EQ(123U, wcstoumax(L" +123x", &end, 10));
109 EXPECT_EQ(L'x', *end);
110 }
111
TEST(inttypes,strtoimax_dec)112 TEST(inttypes, strtoimax_dec) {
113 char* p;
114 EXPECT_EQ(-18737357, strtoimax("-18737357foobar12", &p, 10));
115 EXPECT_STREQ("foobar12", p);
116 }
117
TEST(inttypes,strtoimax_hex)118 TEST(inttypes, strtoimax_hex) {
119 char* p;
120 EXPECT_EQ(-0x18737357f, strtoimax("-18737357foobar12", &p, 16));
121 EXPECT_STREQ("oobar12", p);
122 }
123
TEST(inttypes,strtoimax_EINVAL)124 TEST(inttypes, strtoimax_EINVAL) {
125 errno = 0;
126 strtoimax("123", nullptr, -1);
127 ASSERT_EQ(EINVAL, errno);
128 errno = 0;
129 strtoimax("123", nullptr, 1);
130 ASSERT_EQ(EINVAL, errno);
131 errno = 0;
132 strtoimax("123", nullptr, 37);
133 ASSERT_EQ(EINVAL, errno);
134 }
135
TEST(inttypes,strtoumax_dec)136 TEST(inttypes, strtoumax_dec) {
137 char* p;
138 EXPECT_EQ(18737357U, strtoumax("18737357foobar12", &p, 10));
139 EXPECT_STREQ("foobar12", p);
140 }
141
TEST(inttypes,strtoumax_hex)142 TEST(inttypes, strtoumax_hex) {
143 char* p;
144 EXPECT_EQ(0x18737357fU, strtoumax("18737357foobar12", &p, 16));
145 EXPECT_STREQ("oobar12", p);
146 }
147
TEST(inttypes,strtoumax_negative)148 TEST(inttypes, strtoumax_negative) {
149 char* p;
150 EXPECT_EQ(UINTMAX_MAX - 18737357 + 1, strtoumax("-18737357foobar12", &p, 10));
151 EXPECT_STREQ("foobar12", p);
152 }
153
TEST(inttypes,strtoumax_EINVAL)154 TEST(inttypes, strtoumax_EINVAL) {
155 errno = 0;
156 strtoumax("123", nullptr, -1);
157 ASSERT_EQ(EINVAL, errno);
158 errno = 0;
159 strtoumax("123", nullptr, 1);
160 ASSERT_EQ(EINVAL, errno);
161 errno = 0;
162 strtoumax("123", nullptr, 37);
163 ASSERT_EQ(EINVAL, errno);
164 }
165
TEST(inttypes,wcstoimax_EINVAL)166 TEST(inttypes, wcstoimax_EINVAL) {
167 errno = 0;
168 wcstoimax(L"123", nullptr, -1);
169 ASSERT_EQ(EINVAL, errno);
170 errno = 0;
171 wcstoimax(L"123", nullptr, 1);
172 ASSERT_EQ(EINVAL, errno);
173 errno = 0;
174 wcstoimax(L"123", nullptr, 37);
175 ASSERT_EQ(EINVAL, errno);
176 }
177
TEST(inttypes,wcstoumax_EINVAL)178 TEST(inttypes, wcstoumax_EINVAL) {
179 errno = 0;
180 wcstoumax(L"123", nullptr, -1);
181 ASSERT_EQ(EINVAL, errno);
182 errno = 0;
183 wcstoumax(L"123", nullptr, 1);
184 ASSERT_EQ(EINVAL, errno);
185 errno = 0;
186 wcstoumax(L"123", nullptr, 37);
187 ASSERT_EQ(EINVAL, errno);
188 }
189
TEST(inttypes,div)190 TEST(inttypes, div) {
191 div_t r;
192
193 r = div(5, 3);
194 EXPECT_EQ(1, r.quot);
195 EXPECT_EQ(2, r.rem);
196
197 r = div(5, -3);
198 EXPECT_EQ(-1, r.quot);
199 EXPECT_EQ(2, r.rem);
200
201 r = div(-5, 3);
202 EXPECT_EQ(-1, r.quot);
203 EXPECT_EQ(-2, r.rem);
204
205 r = div(-5, -3);
206 EXPECT_EQ(1, r.quot);
207 EXPECT_EQ(-2, r.rem);
208 }
209
TEST(inttypes,ldiv)210 TEST(inttypes, ldiv) {
211 ldiv_t r;
212
213 r = ldiv(5, 3);
214 EXPECT_EQ(1, r.quot);
215 EXPECT_EQ(2, r.rem);
216
217 r = ldiv(5, -3);
218 EXPECT_EQ(-1, r.quot);
219 EXPECT_EQ(2, r.rem);
220
221 r = ldiv(-5, 3);
222 EXPECT_EQ(-1, r.quot);
223 EXPECT_EQ(-2, r.rem);
224
225 r = ldiv(-5, -3);
226 EXPECT_EQ(1, r.quot);
227 EXPECT_EQ(-2, r.rem);
228 }
229
TEST(inttypes,lldiv)230 TEST(inttypes, lldiv) {
231 lldiv_t r;
232
233 r = lldiv(5, 3);
234 EXPECT_EQ(1, r.quot);
235 EXPECT_EQ(2, r.rem);
236
237 r = lldiv(5, -3);
238 EXPECT_EQ(-1, r.quot);
239 EXPECT_EQ(2, r.rem);
240
241 r = lldiv(-5, 3);
242 EXPECT_EQ(-1, r.quot);
243 EXPECT_EQ(-2, r.rem);
244
245 r = lldiv(-5, -3);
246 EXPECT_EQ(1, r.quot);
247 EXPECT_EQ(-2, r.rem);
248 }
249
TEST(inttypes,imaxdiv)250 TEST(inttypes, imaxdiv) {
251 imaxdiv_t r;
252
253 r = imaxdiv(5, 3);
254 EXPECT_EQ(1, r.quot);
255 EXPECT_EQ(2, r.rem);
256
257 r = imaxdiv(5, -3);
258 EXPECT_EQ(-1, r.quot);
259 EXPECT_EQ(2, r.rem);
260
261 r = imaxdiv(-5, 3);
262 EXPECT_EQ(-1, r.quot);
263 EXPECT_EQ(-2, r.rem);
264
265 r = imaxdiv(-5, -3);
266 EXPECT_EQ(1, r.quot);
267 EXPECT_EQ(-2, r.rem);
268 }
269
TEST(inttypes,imaxabs)270 TEST(inttypes, imaxabs) {
271 EXPECT_EQ(672423489, imaxabs(672423489));
272 EXPECT_EQ(672423489, imaxabs(-672423489));
273 EXPECT_EQ(INTMAX_MAX, imaxabs(-INTMAX_MAX));
274 EXPECT_EQ(INTMAX_MAX, imaxabs(INTMAX_MAX));
275 }
276