blob: 773230f2f1f358570524ffd231ba8a6b767fdeda [file] [log] [blame]
Elliott Hughes774c7f52012-10-01 13:11:03 -07001/*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
Elliott Hughes33697a02016-01-26 13:04:57 -080018
Yabin Cui9df70402014-11-05 18:01:01 -080019#include "BionicDeathTest.h"
Calin Juravlefe317a32014-02-21 15:11:03 +000020#include "TemporaryFile.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080021#include "utils.h"
Elliott Hughes774c7f52012-10-01 13:11:03 -070022
Elliott Hughesb16b7222013-02-04 13:18:00 -080023#include <errno.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080024#include <libgen.h>
25#include <limits.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080026#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080027#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070028#include <stdlib.h>
Calin Juravlefe317a32014-02-21 15:11:03 +000029#include <fcntl.h>
Elliott Hughes40488562014-03-12 13:50:38 -070030#include <sys/types.h>
31#include <sys/wait.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070032
Elliott Hughes274afe82014-11-06 12:40:08 -080033// The random number generator tests all set the seed, get four values, reset the seed and check
34// that they get the first two values repeated, and then reset the seed and check two more values
35// to rule out the possibility that we're just going round a cycle of four values.
36// TODO: factor this out.
37
Elliott Hughes774c7f52012-10-01 13:11:03 -070038TEST(stdlib, drand48) {
39 srand48(0x01020304);
40 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
41 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
42 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
43 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080044 srand48(0x01020304);
45 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
46 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
47 srand48(0x01020304);
48 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
49 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
50}
51
52TEST(stdlib, erand48) {
53 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
54 unsigned short xsubi[3];
55 memcpy(xsubi, seed, sizeof(seed));
56 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
57 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
58 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
59 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
60 memcpy(xsubi, seed, sizeof(seed));
61 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
62 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
63 memcpy(xsubi, seed, sizeof(seed));
64 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
65 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
66}
67
68TEST(stdlib, lcong48) {
69 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
70 lcong48(p);
71 EXPECT_EQ(1531389981, lrand48());
72 EXPECT_EQ(1598801533, lrand48());
73 EXPECT_EQ(2080534853, lrand48());
74 EXPECT_EQ(1102488897, lrand48());
75 lcong48(p);
76 EXPECT_EQ(1531389981, lrand48());
77 EXPECT_EQ(1598801533, lrand48());
78 lcong48(p);
79 EXPECT_EQ(1531389981, lrand48());
80 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -070081}
82
Elliott Hughesa0beeea2014-06-12 11:48:04 -070083TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070084 srand48(0x01020304);
85 EXPECT_EQ(1409163720, lrand48());
86 EXPECT_EQ(397769746, lrand48());
87 EXPECT_EQ(902267124, lrand48());
88 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080089 srand48(0x01020304);
90 EXPECT_EQ(1409163720, lrand48());
91 EXPECT_EQ(397769746, lrand48());
92 srand48(0x01020304);
93 EXPECT_EQ(1409163720, lrand48());
94 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -070095}
Elliott Hughes774c7f52012-10-01 13:11:03 -070096
Elliott Hughesa0beeea2014-06-12 11:48:04 -070097TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070098 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -070099 EXPECT_EQ(55436735, random());
100 EXPECT_EQ(1399865117, random());
101 EXPECT_EQ(2032643283, random());
102 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800103 srandom(0x01020304);
104 EXPECT_EQ(55436735, random());
105 EXPECT_EQ(1399865117, random());
106 srandom(0x01020304);
107 EXPECT_EQ(55436735, random());
108 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700109}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700110
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700111TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700112 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700113 EXPECT_EQ(55436735, rand());
114 EXPECT_EQ(1399865117, rand());
115 EXPECT_EQ(2032643283, rand());
116 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800117 srand(0x01020304);
118 EXPECT_EQ(55436735, rand());
119 EXPECT_EQ(1399865117, rand());
120 srand(0x01020304);
121 EXPECT_EQ(55436735, rand());
122 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700123}
124
125TEST(stdlib, mrand48) {
126 srand48(0x01020304);
127 EXPECT_EQ(-1476639856, mrand48());
128 EXPECT_EQ(795539493, mrand48());
129 EXPECT_EQ(1804534249, mrand48());
130 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800131 srand48(0x01020304);
132 EXPECT_EQ(-1476639856, mrand48());
133 EXPECT_EQ(795539493, mrand48());
134 srand48(0x01020304);
135 EXPECT_EQ(-1476639856, mrand48());
136 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700137}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800138
139TEST(stdlib, posix_memalign) {
140 void* p;
141
142 ASSERT_EQ(0, posix_memalign(&p, 512, 128));
143 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
144 free(p);
145
146 // Can't align to a non-power of 2.
147 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
148}
Elliott Hughesf0777842013-03-01 16:59:46 -0800149
150TEST(stdlib, realpath__NULL_filename) {
151 errno = 0;
152 char* p = realpath(NULL, NULL);
153 ASSERT_TRUE(p == NULL);
154 ASSERT_EQ(EINVAL, errno);
155}
156
157TEST(stdlib, realpath__empty_filename) {
158 errno = 0;
159 char* p = realpath("", NULL);
160 ASSERT_TRUE(p == NULL);
161 ASSERT_EQ(ENOENT, errno);
162}
163
164TEST(stdlib, realpath__ENOENT) {
165 errno = 0;
166 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
167 ASSERT_TRUE(p == NULL);
168 ASSERT_EQ(ENOENT, errno);
169}
170
Elliott Hughes31e072f2014-09-30 16:15:42 -0700171TEST(stdlib, realpath__component_after_non_directory) {
172 errno = 0;
173 char* p = realpath("/dev/null/.", NULL);
174 ASSERT_TRUE(p == NULL);
175 ASSERT_EQ(ENOTDIR, errno);
176
177 errno = 0;
178 p = realpath("/dev/null/..", NULL);
179 ASSERT_TRUE(p == NULL);
180 ASSERT_EQ(ENOTDIR, errno);
181}
182
Elliott Hughesf0777842013-03-01 16:59:46 -0800183TEST(stdlib, realpath) {
184 // Get the name of this executable.
185 char executable_path[PATH_MAX];
186 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
187 ASSERT_NE(rc, -1);
188 executable_path[rc] = '\0';
189
190 char buf[PATH_MAX + 1];
191 char* p = realpath("/proc/self/exe", buf);
192 ASSERT_STREQ(executable_path, p);
193
194 p = realpath("/proc/self/exe", NULL);
195 ASSERT_STREQ(executable_path, p);
196 free(p);
197}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700198
199TEST(stdlib, qsort) {
200 struct s {
201 char name[16];
202 static int comparator(const void* lhs, const void* rhs) {
203 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
204 }
205 };
206 s entries[3];
207 strcpy(entries[0].name, "charlie");
208 strcpy(entries[1].name, "bravo");
209 strcpy(entries[2].name, "alpha");
210
211 qsort(entries, 3, sizeof(s), s::comparator);
212 ASSERT_STREQ("alpha", entries[0].name);
213 ASSERT_STREQ("bravo", entries[1].name);
214 ASSERT_STREQ("charlie", entries[2].name);
215
216 qsort(entries, 3, sizeof(s), s::comparator);
217 ASSERT_STREQ("alpha", entries[0].name);
218 ASSERT_STREQ("bravo", entries[1].name);
219 ASSERT_STREQ("charlie", entries[2].name);
220}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800221
222static void* TestBug57421_child(void* arg) {
223 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
224 pthread_join(main_thread, NULL);
225 char* value = getenv("ENVIRONMENT_VARIABLE");
226 if (value == NULL) {
227 setenv("ENVIRONMENT_VARIABLE", "value", 1);
228 }
229 return NULL;
230}
231
232static void TestBug57421_main() {
233 pthread_t t;
234 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
235 pthread_exit(NULL);
236}
237
238// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
239// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800240
241class stdlib_DeathTest : public BionicDeathTest {};
242
243TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800244 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800245 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
246}
Calin Juravlefe317a32014-02-21 15:11:03 +0000247
Elliott Hughes31165ed2014-09-23 17:34:29 -0700248TEST(stdlib, mkostemp64) {
249 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
250 int flags = fcntl(tf.fd, F_GETFD);
251 ASSERT_TRUE(flags != -1);
252 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
253}
254
255TEST(stdlib, mkostemp) {
256 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
257 int flags = fcntl(tf.fd, F_GETFD);
258 ASSERT_TRUE(flags != -1);
259 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
260}
261
262TEST(stdlib, mkstemp64) {
263 TemporaryFile tf(mkstemp64);
264 struct stat64 sb;
265 ASSERT_EQ(0, fstat64(tf.fd, &sb));
266 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
267}
268
Calin Juravlefe317a32014-02-21 15:11:03 +0000269TEST(stdlib, mkstemp) {
270 TemporaryFile tf;
271 struct stat sb;
272 ASSERT_EQ(0, fstat(tf.fd, &sb));
273}
274
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700275TEST(stdlib, system) {
276 int status;
277
278 status = system("exit 0");
279 ASSERT_TRUE(WIFEXITED(status));
280 ASSERT_EQ(0, WEXITSTATUS(status));
281
282 status = system("exit 1");
283 ASSERT_TRUE(WIFEXITED(status));
284 ASSERT_EQ(1, WEXITSTATUS(status));
285}
Elliott Hughes5a817382014-03-12 16:12:57 -0700286
287TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700288 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700289}
290
291TEST(stdlib, strtod) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700292 ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700293}
294
295TEST(stdlib, strtof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700296 ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700297}
298
299TEST(stdlib, strtold) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700300 ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700301}
Elliott Hughes9f525642014-04-08 17:14:01 -0700302
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700303TEST(stdlib, strtof_2206701) {
304 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL));
305 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL));
306}
307
308TEST(stdlib, strtod_largest_subnormal) {
309 // This value has been known to cause javac and java to infinite loop.
310 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
311 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL));
312 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL));
313 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL));
314 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL));
315 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL));
316 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL));
317 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL));
318}
319
Dan Albertb8425c52014-04-29 17:49:06 -0700320TEST(stdlib, quick_exit) {
321 pid_t pid = fork();
322 ASSERT_NE(-1, pid) << strerror(errno);
323
324 if (pid == 0) {
325 quick_exit(99);
326 }
327
Elliott Hughes33697a02016-01-26 13:04:57 -0800328 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700329}
330
331static int quick_exit_status = 0;
332
333static void quick_exit_1(void) {
334 ASSERT_EQ(quick_exit_status, 0);
335 quick_exit_status = 1;
336}
337
338static void quick_exit_2(void) {
339 ASSERT_EQ(quick_exit_status, 1);
340}
341
342static void not_run(void) {
343 FAIL();
344}
345
346TEST(stdlib, at_quick_exit) {
347 pid_t pid = fork();
348 ASSERT_NE(-1, pid) << strerror(errno);
349
350 if (pid == 0) {
351 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
352 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
353 atexit(not_run);
354 quick_exit(99);
355 }
356
Elliott Hughes33697a02016-01-26 13:04:57 -0800357 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700358}
359
Elliott Hughes9f525642014-04-08 17:14:01 -0700360TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800361 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700362 ASSERT_NE(-1, pid) << strerror(errno);
363
364 if (pid == 0) {
365 _Exit(99);
366 }
367
Elliott Hughes33697a02016-01-26 13:04:57 -0800368 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700369}
Elliott Hughes49167062014-07-25 17:24:00 -0700370
371TEST(stdlib, pty_smoke) {
372 // getpt returns a pty with O_RDWR|O_NOCTTY.
373 int fd = getpt();
374 ASSERT_NE(-1, fd);
375
376 // grantpt is a no-op.
377 ASSERT_EQ(0, grantpt(fd));
378
379 // ptsname_r should start "/dev/pts/".
380 char name_r[128];
381 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
382 name_r[9] = 0;
383 ASSERT_STREQ("/dev/pts/", name_r);
384
385 close(fd);
386}
387
388TEST(stdlib, posix_openpt) {
389 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
390 ASSERT_NE(-1, fd);
391 close(fd);
392}
393
394TEST(stdlib, ptsname_r_ENOTTY) {
395 errno = 0;
396 char buf[128];
397 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
398 ASSERT_EQ(ENOTTY, errno);
399}
400
401TEST(stdlib, ptsname_r_EINVAL) {
402 int fd = getpt();
403 ASSERT_NE(-1, fd);
404 errno = 0;
405 char* buf = NULL;
406 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
407 ASSERT_EQ(EINVAL, errno);
408 close(fd);
409}
410
411TEST(stdlib, ptsname_r_ERANGE) {
412 int fd = getpt();
413 ASSERT_NE(-1, fd);
414 errno = 0;
415 char buf[1];
416 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
417 ASSERT_EQ(ERANGE, errno);
418 close(fd);
419}
420
421TEST(stdlib, ttyname_r) {
422 int fd = getpt();
423 ASSERT_NE(-1, fd);
424
425 // ttyname_r returns "/dev/ptmx" for a pty.
426 char name_r[128];
427 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
428 ASSERT_STREQ("/dev/ptmx", name_r);
429
430 close(fd);
431}
432
433TEST(stdlib, ttyname_r_ENOTTY) {
434 int fd = open("/dev/null", O_WRONLY);
435 errno = 0;
436 char buf[128];
437 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
438 ASSERT_EQ(ENOTTY, errno);
439 close(fd);
440}
441
442TEST(stdlib, ttyname_r_EINVAL) {
443 int fd = getpt();
444 ASSERT_NE(-1, fd);
445 errno = 0;
446 char* buf = NULL;
447 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
448 ASSERT_EQ(EINVAL, errno);
449 close(fd);
450}
451
452TEST(stdlib, ttyname_r_ERANGE) {
453 int fd = getpt();
454 ASSERT_NE(-1, fd);
455 errno = 0;
456 char buf[1];
457 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
458 ASSERT_EQ(ERANGE, errno);
459 close(fd);
460}
461
462TEST(stdlib, unlockpt_ENOTTY) {
463 int fd = open("/dev/null", O_WRONLY);
464 errno = 0;
465 ASSERT_EQ(-1, unlockpt(fd));
466 ASSERT_EQ(ENOTTY, errno);
467 close(fd);
468}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700469
470TEST(stdlib, strtol_EINVAL) {
471 errno = 0;
472 strtol("123", NULL, -1);
473 ASSERT_EQ(EINVAL, errno);
474 errno = 0;
475 strtol("123", NULL, 1);
476 ASSERT_EQ(EINVAL, errno);
477 errno = 0;
478 strtol("123", NULL, 37);
479 ASSERT_EQ(EINVAL, errno);
480}
481
482TEST(stdlib, strtoll_EINVAL) {
483 errno = 0;
484 strtoll("123", NULL, -1);
485 ASSERT_EQ(EINVAL, errno);
486 errno = 0;
487 strtoll("123", NULL, 1);
488 ASSERT_EQ(EINVAL, errno);
489 errno = 0;
490 strtoll("123", NULL, 37);
491 ASSERT_EQ(EINVAL, errno);
492}
493
494TEST(stdlib, strtoul_EINVAL) {
495 errno = 0;
496 strtoul("123", NULL, -1);
497 ASSERT_EQ(EINVAL, errno);
498 errno = 0;
499 strtoul("123", NULL, 1);
500 ASSERT_EQ(EINVAL, errno);
501 errno = 0;
502 strtoul("123", NULL, 37);
503 ASSERT_EQ(EINVAL, errno);
504}
505
506TEST(stdlib, strtoull_EINVAL) {
507 errno = 0;
508 strtoull("123", NULL, -1);
509 ASSERT_EQ(EINVAL, errno);
510 errno = 0;
511 strtoull("123", NULL, 1);
512 ASSERT_EQ(EINVAL, errno);
513 errno = 0;
514 strtoull("123", NULL, 37);
515 ASSERT_EQ(EINVAL, errno);
516}
Elliott Hughesdf143f82016-04-04 17:34:04 -0700517
518TEST(stdlib, getsubopt) {
519 char* const tokens[] = {
520 const_cast<char*>("a"),
521 const_cast<char*>("b"),
522 const_cast<char*>("foo"),
523 nullptr
524 };
525 std::string input = "a,b,foo=bar,a,unknown";
526 char* subopts = &input[0];
527 char* value = nullptr;
528
529 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
530 ASSERT_EQ(nullptr, value);
531 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
532 ASSERT_EQ(nullptr, value);
533 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
534 ASSERT_STREQ("bar", value);
535 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
536 ASSERT_EQ(nullptr, value);
537
538 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
539}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700540
541TEST(stdlib, mblen) {
542 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
543 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
544 EXPECT_EQ(0, mblen(nullptr, 1));
545
546 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
547
548 // 1-byte UTF-8.
549 EXPECT_EQ(1, mblen("abcdef", 6));
550 // 2-byte UTF-8.
551 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
552 // 3-byte UTF-8.
553 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
554 // 4-byte UTF-8.
555 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
556
557 // Illegal over-long sequence.
558 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
559
560 // "mblen() shall ... return 0 (if s points to the null byte)".
561 EXPECT_EQ(0, mblen("", 1));
562}