blob: 27495d88fc4538ffe5fb4e2ccfdcf3d9a49aa8d8 [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"
Elliott Hughes7f0849f2016-08-26 16:17:17 -070020#include "math_data_test.h"
Calin Juravlefe317a32014-02-21 15:11:03 +000021#include "TemporaryFile.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080022#include "utils.h"
Elliott Hughes774c7f52012-10-01 13:11:03 -070023
Elliott Hughesb16b7222013-02-04 13:18:00 -080024#include <errno.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070025#include <fcntl.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080026#include <libgen.h>
27#include <limits.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070028#include <math.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080029#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080030#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070031#include <stdlib.h>
Elliott Hughes40488562014-03-12 13:50:38 -070032#include <sys/types.h>
33#include <sys/wait.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070034
Elliott Hughes274afe82014-11-06 12:40:08 -080035// The random number generator tests all set the seed, get four values, reset the seed and check
36// that they get the first two values repeated, and then reset the seed and check two more values
37// to rule out the possibility that we're just going round a cycle of four values.
38// TODO: factor this out.
39
Elliott Hughes774c7f52012-10-01 13:11:03 -070040TEST(stdlib, drand48) {
41 srand48(0x01020304);
42 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
43 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
44 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
45 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080046 srand48(0x01020304);
47 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
48 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
49 srand48(0x01020304);
50 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
51 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
52}
53
54TEST(stdlib, erand48) {
55 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
56 unsigned short xsubi[3];
57 memcpy(xsubi, seed, sizeof(seed));
58 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
59 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
60 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
61 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
62 memcpy(xsubi, seed, sizeof(seed));
63 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
64 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
65 memcpy(xsubi, seed, sizeof(seed));
66 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
67 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
68}
69
70TEST(stdlib, lcong48) {
71 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
72 lcong48(p);
73 EXPECT_EQ(1531389981, lrand48());
74 EXPECT_EQ(1598801533, lrand48());
75 EXPECT_EQ(2080534853, lrand48());
76 EXPECT_EQ(1102488897, lrand48());
77 lcong48(p);
78 EXPECT_EQ(1531389981, lrand48());
79 EXPECT_EQ(1598801533, lrand48());
80 lcong48(p);
81 EXPECT_EQ(1531389981, lrand48());
82 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -070083}
84
Elliott Hughesa0beeea2014-06-12 11:48:04 -070085TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070086 srand48(0x01020304);
87 EXPECT_EQ(1409163720, lrand48());
88 EXPECT_EQ(397769746, lrand48());
89 EXPECT_EQ(902267124, lrand48());
90 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080091 srand48(0x01020304);
92 EXPECT_EQ(1409163720, lrand48());
93 EXPECT_EQ(397769746, lrand48());
94 srand48(0x01020304);
95 EXPECT_EQ(1409163720, lrand48());
96 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -070097}
Elliott Hughes774c7f52012-10-01 13:11:03 -070098
Elliott Hughesa0beeea2014-06-12 11:48:04 -070099TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700100 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700101 EXPECT_EQ(55436735, random());
102 EXPECT_EQ(1399865117, random());
103 EXPECT_EQ(2032643283, random());
104 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800105 srandom(0x01020304);
106 EXPECT_EQ(55436735, random());
107 EXPECT_EQ(1399865117, random());
108 srandom(0x01020304);
109 EXPECT_EQ(55436735, random());
110 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700111}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700112
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700113TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700114 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700115 EXPECT_EQ(55436735, rand());
116 EXPECT_EQ(1399865117, rand());
117 EXPECT_EQ(2032643283, rand());
118 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800119 srand(0x01020304);
120 EXPECT_EQ(55436735, rand());
121 EXPECT_EQ(1399865117, rand());
122 srand(0x01020304);
123 EXPECT_EQ(55436735, rand());
124 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700125}
126
127TEST(stdlib, mrand48) {
128 srand48(0x01020304);
129 EXPECT_EQ(-1476639856, mrand48());
130 EXPECT_EQ(795539493, mrand48());
131 EXPECT_EQ(1804534249, mrand48());
132 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800133 srand48(0x01020304);
134 EXPECT_EQ(-1476639856, mrand48());
135 EXPECT_EQ(795539493, mrand48());
136 srand48(0x01020304);
137 EXPECT_EQ(-1476639856, mrand48());
138 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700139}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800140
Christopher Ferris3a32d952017-06-15 13:30:44 -0700141TEST(stdlib, posix_memalign_sweep) {
142 void* ptr;
Elliott Hughesb16b7222013-02-04 13:18:00 -0800143
Christopher Ferris3a32d952017-06-15 13:30:44 -0700144 // These should all fail.
145 for (size_t align = 0; align < sizeof(long); align++) {
146 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
147 << "Unexpected value at align " << align;
148 }
Elliott Hughesb16b7222013-02-04 13:18:00 -0800149
Christopher Ferris3a32d952017-06-15 13:30:44 -0700150 // Verify powers of 2 up to 2048 allocate, and verify that all other
151 // alignment values between the powers of 2 fail.
152 size_t last_align = sizeof(long);
153 for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
154 // Try all of the non power of 2 values from the last until this value.
155 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
156 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
157 << "Unexpected success at align " << fail_align;
158 }
159 ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
160 << "Unexpected failure at align " << align;
161 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
162 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
163 free(ptr);
164 last_align = align;
165 }
166}
167
168TEST(stdlib, posix_memalign_various_sizes) {
169 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
170 for (auto size : sizes) {
171 void* ptr;
172 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
173 << "posix_memalign failed at size " << size;
174 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
175 << "Pointer not aligned at size " << size << " ptr " << ptr;
176 free(ptr);
177 }
178}
179
180TEST(stdlib, posix_memalign_overflow) {
181 void* ptr;
182 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
Elliott Hughesb16b7222013-02-04 13:18:00 -0800183}
Elliott Hughesf0777842013-03-01 16:59:46 -0800184
185TEST(stdlib, realpath__NULL_filename) {
186 errno = 0;
George Burgess IV95bd4882017-08-14 14:48:55 -0700187 // Work around the compile-time error generated by FORTIFY here.
188 const char* path = NULL;
189 char* p = realpath(path, NULL);
Elliott Hughesf0777842013-03-01 16:59:46 -0800190 ASSERT_TRUE(p == NULL);
191 ASSERT_EQ(EINVAL, errno);
192}
193
194TEST(stdlib, realpath__empty_filename) {
195 errno = 0;
196 char* p = realpath("", NULL);
197 ASSERT_TRUE(p == NULL);
198 ASSERT_EQ(ENOENT, errno);
199}
200
201TEST(stdlib, realpath__ENOENT) {
202 errno = 0;
203 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
204 ASSERT_TRUE(p == NULL);
205 ASSERT_EQ(ENOENT, errno);
206}
207
Elliott Hughes31e072f2014-09-30 16:15:42 -0700208TEST(stdlib, realpath__component_after_non_directory) {
209 errno = 0;
210 char* p = realpath("/dev/null/.", NULL);
211 ASSERT_TRUE(p == NULL);
212 ASSERT_EQ(ENOTDIR, errno);
213
214 errno = 0;
215 p = realpath("/dev/null/..", NULL);
216 ASSERT_TRUE(p == NULL);
217 ASSERT_EQ(ENOTDIR, errno);
218}
219
Elliott Hughesf0777842013-03-01 16:59:46 -0800220TEST(stdlib, realpath) {
221 // Get the name of this executable.
222 char executable_path[PATH_MAX];
223 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
224 ASSERT_NE(rc, -1);
225 executable_path[rc] = '\0';
226
227 char buf[PATH_MAX + 1];
228 char* p = realpath("/proc/self/exe", buf);
229 ASSERT_STREQ(executable_path, p);
230
231 p = realpath("/proc/self/exe", NULL);
232 ASSERT_STREQ(executable_path, p);
233 free(p);
234}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700235
236TEST(stdlib, qsort) {
237 struct s {
238 char name[16];
239 static int comparator(const void* lhs, const void* rhs) {
240 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
241 }
242 };
243 s entries[3];
244 strcpy(entries[0].name, "charlie");
245 strcpy(entries[1].name, "bravo");
246 strcpy(entries[2].name, "alpha");
247
248 qsort(entries, 3, sizeof(s), s::comparator);
249 ASSERT_STREQ("alpha", entries[0].name);
250 ASSERT_STREQ("bravo", entries[1].name);
251 ASSERT_STREQ("charlie", entries[2].name);
252
253 qsort(entries, 3, sizeof(s), s::comparator);
254 ASSERT_STREQ("alpha", entries[0].name);
255 ASSERT_STREQ("bravo", entries[1].name);
256 ASSERT_STREQ("charlie", entries[2].name);
257}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800258
259static void* TestBug57421_child(void* arg) {
260 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
261 pthread_join(main_thread, NULL);
262 char* value = getenv("ENVIRONMENT_VARIABLE");
263 if (value == NULL) {
264 setenv("ENVIRONMENT_VARIABLE", "value", 1);
265 }
266 return NULL;
267}
268
269static void TestBug57421_main() {
270 pthread_t t;
271 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
272 pthread_exit(NULL);
273}
274
275// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
276// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800277
278class stdlib_DeathTest : public BionicDeathTest {};
279
280TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800281 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800282 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
283}
Calin Juravlefe317a32014-02-21 15:11:03 +0000284
Elliott Hughes31165ed2014-09-23 17:34:29 -0700285TEST(stdlib, mkostemp64) {
286 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
287 int flags = fcntl(tf.fd, F_GETFD);
288 ASSERT_TRUE(flags != -1);
289 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
290}
291
292TEST(stdlib, mkostemp) {
293 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
294 int flags = fcntl(tf.fd, F_GETFD);
295 ASSERT_TRUE(flags != -1);
296 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
297}
298
299TEST(stdlib, mkstemp64) {
300 TemporaryFile tf(mkstemp64);
301 struct stat64 sb;
302 ASSERT_EQ(0, fstat64(tf.fd, &sb));
303 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
304}
305
Calin Juravlefe317a32014-02-21 15:11:03 +0000306TEST(stdlib, mkstemp) {
307 TemporaryFile tf;
308 struct stat sb;
309 ASSERT_EQ(0, fstat(tf.fd, &sb));
310}
311
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700312TEST(stdlib, system) {
313 int status;
314
315 status = system("exit 0");
316 ASSERT_TRUE(WIFEXITED(status));
317 ASSERT_EQ(0, WEXITSTATUS(status));
318
319 status = system("exit 1");
320 ASSERT_TRUE(WIFEXITED(status));
321 ASSERT_EQ(1, WEXITSTATUS(status));
322}
Elliott Hughes5a817382014-03-12 16:12:57 -0700323
324TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700325 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700326}
327
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700328template <typename T>
329static void CheckStrToFloat(T fn(const char* s, char** end)) {
330 FpUlpEq<0, T> pred;
331
332 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
333 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
334 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
335
Dan Albertf6346552016-12-02 12:02:03 -0800336 const char* s = " \t\v\f\r\n9.0";
337 char* p;
338 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
339 EXPECT_EQ(s + strlen(s), p);
340
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700341 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
342 EXPECT_TRUE(isnan(fn("nan", nullptr)));
343 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
344
345 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
346 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
347 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
348
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700349 EXPECT_TRUE(isnan(fn("+nanny", &p)));
350 EXPECT_STREQ("ny", p);
351 EXPECT_TRUE(isnan(fn("nanny", &p)));
352 EXPECT_STREQ("ny", p);
353 EXPECT_TRUE(isnan(fn("-nanny", &p)));
354 EXPECT_STREQ("ny", p);
355
356 EXPECT_EQ(0, fn("muppet", &p));
357 EXPECT_STREQ("muppet", p);
358 EXPECT_EQ(0, fn(" muppet", &p));
359 EXPECT_STREQ(" muppet", p);
360
361 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
362 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
363 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
364
365 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
366 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
367 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
368
369 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
370 EXPECT_STREQ("initude", p);
371 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
372 EXPECT_STREQ("initude", p);
373 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
374 EXPECT_STREQ("initude", p);
375
376 // Check case-insensitivity.
377 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
378 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
379}
380
Elliott Hughes5a817382014-03-12 16:12:57 -0700381TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700382 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700383}
384
385TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700386 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700387}
388
389TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700390 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700391}
Elliott Hughes9f525642014-04-08 17:14:01 -0700392
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700393TEST(stdlib, strtof_2206701) {
394 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL));
395 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL));
396}
397
398TEST(stdlib, strtod_largest_subnormal) {
399 // This value has been known to cause javac and java to infinite loop.
400 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
401 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL));
402 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL));
403 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL));
404 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL));
405 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL));
406 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL));
407 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL));
408}
409
Dan Albertb8425c52014-04-29 17:49:06 -0700410TEST(stdlib, quick_exit) {
411 pid_t pid = fork();
412 ASSERT_NE(-1, pid) << strerror(errno);
413
414 if (pid == 0) {
415 quick_exit(99);
416 }
417
Elliott Hughes33697a02016-01-26 13:04:57 -0800418 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700419}
420
421static int quick_exit_status = 0;
422
423static void quick_exit_1(void) {
424 ASSERT_EQ(quick_exit_status, 0);
425 quick_exit_status = 1;
426}
427
428static void quick_exit_2(void) {
429 ASSERT_EQ(quick_exit_status, 1);
430}
431
432static void not_run(void) {
433 FAIL();
434}
435
436TEST(stdlib, at_quick_exit) {
437 pid_t pid = fork();
438 ASSERT_NE(-1, pid) << strerror(errno);
439
440 if (pid == 0) {
441 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
442 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
443 atexit(not_run);
444 quick_exit(99);
445 }
446
Elliott Hughes33697a02016-01-26 13:04:57 -0800447 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700448}
449
Elliott Hughes9f525642014-04-08 17:14:01 -0700450TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800451 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700452 ASSERT_NE(-1, pid) << strerror(errno);
453
454 if (pid == 0) {
455 _Exit(99);
456 }
457
Elliott Hughes33697a02016-01-26 13:04:57 -0800458 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700459}
Elliott Hughes49167062014-07-25 17:24:00 -0700460
461TEST(stdlib, pty_smoke) {
462 // getpt returns a pty with O_RDWR|O_NOCTTY.
463 int fd = getpt();
464 ASSERT_NE(-1, fd);
465
466 // grantpt is a no-op.
467 ASSERT_EQ(0, grantpt(fd));
468
469 // ptsname_r should start "/dev/pts/".
470 char name_r[128];
471 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
472 name_r[9] = 0;
473 ASSERT_STREQ("/dev/pts/", name_r);
474
475 close(fd);
476}
477
478TEST(stdlib, posix_openpt) {
479 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
480 ASSERT_NE(-1, fd);
481 close(fd);
482}
483
484TEST(stdlib, ptsname_r_ENOTTY) {
485 errno = 0;
486 char buf[128];
487 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
488 ASSERT_EQ(ENOTTY, errno);
489}
490
491TEST(stdlib, ptsname_r_EINVAL) {
492 int fd = getpt();
493 ASSERT_NE(-1, fd);
494 errno = 0;
495 char* buf = NULL;
496 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
497 ASSERT_EQ(EINVAL, errno);
498 close(fd);
499}
500
501TEST(stdlib, ptsname_r_ERANGE) {
502 int fd = getpt();
503 ASSERT_NE(-1, fd);
504 errno = 0;
505 char buf[1];
506 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
507 ASSERT_EQ(ERANGE, errno);
508 close(fd);
509}
510
511TEST(stdlib, ttyname_r) {
512 int fd = getpt();
513 ASSERT_NE(-1, fd);
514
515 // ttyname_r returns "/dev/ptmx" for a pty.
516 char name_r[128];
517 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
518 ASSERT_STREQ("/dev/ptmx", name_r);
519
520 close(fd);
521}
522
523TEST(stdlib, ttyname_r_ENOTTY) {
524 int fd = open("/dev/null", O_WRONLY);
525 errno = 0;
526 char buf[128];
527 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
528 ASSERT_EQ(ENOTTY, errno);
529 close(fd);
530}
531
532TEST(stdlib, ttyname_r_EINVAL) {
533 int fd = getpt();
534 ASSERT_NE(-1, fd);
535 errno = 0;
536 char* buf = NULL;
537 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
538 ASSERT_EQ(EINVAL, errno);
539 close(fd);
540}
541
542TEST(stdlib, ttyname_r_ERANGE) {
543 int fd = getpt();
544 ASSERT_NE(-1, fd);
545 errno = 0;
546 char buf[1];
547 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
548 ASSERT_EQ(ERANGE, errno);
549 close(fd);
550}
551
552TEST(stdlib, unlockpt_ENOTTY) {
553 int fd = open("/dev/null", O_WRONLY);
554 errno = 0;
555 ASSERT_EQ(-1, unlockpt(fd));
556 ASSERT_EQ(ENOTTY, errno);
557 close(fd);
558}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700559
Elliott Hughesdf143f82016-04-04 17:34:04 -0700560TEST(stdlib, getsubopt) {
561 char* const tokens[] = {
562 const_cast<char*>("a"),
563 const_cast<char*>("b"),
564 const_cast<char*>("foo"),
565 nullptr
566 };
567 std::string input = "a,b,foo=bar,a,unknown";
568 char* subopts = &input[0];
569 char* value = nullptr;
570
571 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
572 ASSERT_EQ(nullptr, value);
573 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
574 ASSERT_EQ(nullptr, value);
575 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
576 ASSERT_STREQ("bar", value);
577 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
578 ASSERT_EQ(nullptr, value);
579
580 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
581}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700582
583TEST(stdlib, mblen) {
584 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
585 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
586 EXPECT_EQ(0, mblen(nullptr, 1));
587
588 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
589
590 // 1-byte UTF-8.
591 EXPECT_EQ(1, mblen("abcdef", 6));
592 // 2-byte UTF-8.
593 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
594 // 3-byte UTF-8.
595 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
596 // 4-byte UTF-8.
597 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
598
599 // Illegal over-long sequence.
600 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
601
602 // "mblen() shall ... return 0 (if s points to the null byte)".
603 EXPECT_EQ(0, mblen("", 1));
604}
Elliott Hughesf826a372017-07-13 09:35:15 -0700605
606template <typename T>
607static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
608 char* end_p;
609
610 // Negative base => invalid.
611 errno = 0;
612 ASSERT_EQ(T(0), fn("123", &end_p, -1));
613 ASSERT_EQ(EINVAL, errno);
614
615 // Base 1 => invalid (base 0 means "please guess").
616 errno = 0;
617 ASSERT_EQ(T(0), fn("123", &end_p, 1));
618 ASSERT_EQ(EINVAL, errno);
619
620 // Base > 36 => invalid.
621 errno = 0;
622 ASSERT_EQ(T(0), fn("123", &end_p, 37));
623 ASSERT_EQ(EINVAL, errno);
624
625 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
626 ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
627 ASSERT_EQ('x', *end_p);
628}
629
630TEST(stdlib, strtol_smoke) {
631 CheckStrToInt(strtol);
632}
633
634TEST(stdlib, strtoll_smoke) {
635 CheckStrToInt(strtoll);
636}
637
638TEST(stdlib, strtoul_smoke) {
639 CheckStrToInt(strtoul);
640}
641
642TEST(stdlib, strtoull_smoke) {
643 CheckStrToInt(strtoull);
644}
645
646TEST(stdlib, strtoimax_smoke) {
647 CheckStrToInt(strtoimax);
648}
649
650TEST(stdlib, strtoumax_smoke) {
651 CheckStrToInt(strtoumax);
652}
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700653
654TEST(stdlib, abs) {
655 ASSERT_EQ(INT_MAX, abs(-INT_MAX));
656 ASSERT_EQ(INT_MAX, abs(INT_MAX));
657}
658
659TEST(stdlib, labs) {
660 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
661 ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
662}
663
664TEST(stdlib, llabs) {
665 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
666 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
667}