blob: 1a3fc03d61597fcdfbeb45f62edc7d68090cb918 [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
Aleksandra Tsvetkova608b4512015-02-27 15:01:59 +0300141TEST(stdlib, jrand48_distribution) {
142 const int iterations = 4096;
143 const int pivot_low = 1536;
144 const int pivot_high = 2560;
145
146 unsigned short xsubi[3];
147 int bits[32] = {};
148
149 for (int iter = 0; iter < iterations; ++iter) {
150 long rand_val = jrand48(xsubi);
151 for (int bit = 0; bit < 32; ++bit) {
152 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
153 }
154 }
155
156 // Check that bit probability is uniform
157 for (int bit = 0; bit < 32; ++bit) {
158 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
159 }
160}
161
162TEST(stdlib, mrand48_distribution) {
163 const int iterations = 4096;
164 const int pivot_low = 1536;
165 const int pivot_high = 2560;
166
167 int bits[32] = {};
168
169 for (int iter = 0; iter < iterations; ++iter) {
170 long rand_val = mrand48();
171 for (int bit = 0; bit < 32; ++bit) {
172 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
173 }
174 }
175
176 // Check that bit probability is uniform
177 for (int bit = 0; bit < 32; ++bit) {
178 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
179 }
180}
181
Christopher Ferris3a32d952017-06-15 13:30:44 -0700182TEST(stdlib, posix_memalign_sweep) {
183 void* ptr;
Elliott Hughesb16b7222013-02-04 13:18:00 -0800184
Christopher Ferris3a32d952017-06-15 13:30:44 -0700185 // These should all fail.
186 for (size_t align = 0; align < sizeof(long); align++) {
187 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
188 << "Unexpected value at align " << align;
189 }
Elliott Hughesb16b7222013-02-04 13:18:00 -0800190
Christopher Ferris3a32d952017-06-15 13:30:44 -0700191 // Verify powers of 2 up to 2048 allocate, and verify that all other
192 // alignment values between the powers of 2 fail.
193 size_t last_align = sizeof(long);
194 for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
195 // Try all of the non power of 2 values from the last until this value.
196 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
197 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
198 << "Unexpected success at align " << fail_align;
199 }
200 ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
201 << "Unexpected failure at align " << align;
202 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
203 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
204 free(ptr);
205 last_align = align;
206 }
207}
208
209TEST(stdlib, posix_memalign_various_sizes) {
210 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
211 for (auto size : sizes) {
212 void* ptr;
213 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
214 << "posix_memalign failed at size " << size;
215 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
216 << "Pointer not aligned at size " << size << " ptr " << ptr;
217 free(ptr);
218 }
219}
220
221TEST(stdlib, posix_memalign_overflow) {
222 void* ptr;
223 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
Elliott Hughesb16b7222013-02-04 13:18:00 -0800224}
Elliott Hughesf0777842013-03-01 16:59:46 -0800225
226TEST(stdlib, realpath__NULL_filename) {
227 errno = 0;
George Burgess IV95bd4882017-08-14 14:48:55 -0700228 // Work around the compile-time error generated by FORTIFY here.
229 const char* path = NULL;
230 char* p = realpath(path, NULL);
Elliott Hughesf0777842013-03-01 16:59:46 -0800231 ASSERT_TRUE(p == NULL);
232 ASSERT_EQ(EINVAL, errno);
233}
234
235TEST(stdlib, realpath__empty_filename) {
236 errno = 0;
237 char* p = realpath("", NULL);
238 ASSERT_TRUE(p == NULL);
239 ASSERT_EQ(ENOENT, errno);
240}
241
242TEST(stdlib, realpath__ENOENT) {
243 errno = 0;
244 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
245 ASSERT_TRUE(p == NULL);
246 ASSERT_EQ(ENOENT, errno);
247}
248
Elliott Hughes31e072f2014-09-30 16:15:42 -0700249TEST(stdlib, realpath__component_after_non_directory) {
250 errno = 0;
251 char* p = realpath("/dev/null/.", NULL);
252 ASSERT_TRUE(p == NULL);
253 ASSERT_EQ(ENOTDIR, errno);
254
255 errno = 0;
256 p = realpath("/dev/null/..", NULL);
257 ASSERT_TRUE(p == NULL);
258 ASSERT_EQ(ENOTDIR, errno);
259}
260
Elliott Hughesf0777842013-03-01 16:59:46 -0800261TEST(stdlib, realpath) {
262 // Get the name of this executable.
263 char executable_path[PATH_MAX];
264 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
265 ASSERT_NE(rc, -1);
266 executable_path[rc] = '\0';
267
268 char buf[PATH_MAX + 1];
269 char* p = realpath("/proc/self/exe", buf);
270 ASSERT_STREQ(executable_path, p);
271
272 p = realpath("/proc/self/exe", NULL);
273 ASSERT_STREQ(executable_path, p);
274 free(p);
275}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700276
277TEST(stdlib, qsort) {
278 struct s {
279 char name[16];
280 static int comparator(const void* lhs, const void* rhs) {
281 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
282 }
283 };
284 s entries[3];
285 strcpy(entries[0].name, "charlie");
286 strcpy(entries[1].name, "bravo");
287 strcpy(entries[2].name, "alpha");
288
289 qsort(entries, 3, sizeof(s), s::comparator);
290 ASSERT_STREQ("alpha", entries[0].name);
291 ASSERT_STREQ("bravo", entries[1].name);
292 ASSERT_STREQ("charlie", entries[2].name);
293
294 qsort(entries, 3, sizeof(s), s::comparator);
295 ASSERT_STREQ("alpha", entries[0].name);
296 ASSERT_STREQ("bravo", entries[1].name);
297 ASSERT_STREQ("charlie", entries[2].name);
298}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800299
300static void* TestBug57421_child(void* arg) {
301 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
302 pthread_join(main_thread, NULL);
303 char* value = getenv("ENVIRONMENT_VARIABLE");
304 if (value == NULL) {
305 setenv("ENVIRONMENT_VARIABLE", "value", 1);
306 }
307 return NULL;
308}
309
310static void TestBug57421_main() {
311 pthread_t t;
312 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
313 pthread_exit(NULL);
314}
315
316// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
317// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800318
319class stdlib_DeathTest : public BionicDeathTest {};
320
321TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800322 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800323 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
324}
Calin Juravlefe317a32014-02-21 15:11:03 +0000325
Elliott Hughes31165ed2014-09-23 17:34:29 -0700326TEST(stdlib, mkostemp64) {
327 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
Elliott Hughesa7f12942017-12-15 13:55:53 -0800328 AssertCloseOnExec(tf.fd, true);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700329}
330
331TEST(stdlib, mkostemp) {
332 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
Elliott Hughesa7f12942017-12-15 13:55:53 -0800333 AssertCloseOnExec(tf.fd, true);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700334}
335
336TEST(stdlib, mkstemp64) {
337 TemporaryFile tf(mkstemp64);
338 struct stat64 sb;
339 ASSERT_EQ(0, fstat64(tf.fd, &sb));
340 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
341}
342
Calin Juravlefe317a32014-02-21 15:11:03 +0000343TEST(stdlib, mkstemp) {
344 TemporaryFile tf;
345 struct stat sb;
346 ASSERT_EQ(0, fstat(tf.fd, &sb));
347}
348
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700349TEST(stdlib, system) {
350 int status;
351
352 status = system("exit 0");
353 ASSERT_TRUE(WIFEXITED(status));
354 ASSERT_EQ(0, WEXITSTATUS(status));
355
356 status = system("exit 1");
357 ASSERT_TRUE(WIFEXITED(status));
358 ASSERT_EQ(1, WEXITSTATUS(status));
359}
Elliott Hughes5a817382014-03-12 16:12:57 -0700360
361TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700362 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700363}
364
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700365template <typename T>
366static void CheckStrToFloat(T fn(const char* s, char** end)) {
367 FpUlpEq<0, T> pred;
368
369 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
370 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
371 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
372
Dan Albertf6346552016-12-02 12:02:03 -0800373 const char* s = " \t\v\f\r\n9.0";
374 char* p;
375 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
376 EXPECT_EQ(s + strlen(s), p);
377
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700378 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
379 EXPECT_TRUE(isnan(fn("nan", nullptr)));
380 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
381
382 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
383 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
384 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
385
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700386 EXPECT_TRUE(isnan(fn("+nanny", &p)));
387 EXPECT_STREQ("ny", p);
388 EXPECT_TRUE(isnan(fn("nanny", &p)));
389 EXPECT_STREQ("ny", p);
390 EXPECT_TRUE(isnan(fn("-nanny", &p)));
391 EXPECT_STREQ("ny", p);
392
393 EXPECT_EQ(0, fn("muppet", &p));
394 EXPECT_STREQ("muppet", p);
395 EXPECT_EQ(0, fn(" muppet", &p));
396 EXPECT_STREQ(" muppet", p);
397
398 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
399 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
400 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
401
402 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
403 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
404 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
405
406 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
407 EXPECT_STREQ("initude", p);
408 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
409 EXPECT_STREQ("initude", p);
410 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
411 EXPECT_STREQ("initude", p);
412
413 // Check case-insensitivity.
414 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
415 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
416}
417
Elliott Hughes5a817382014-03-12 16:12:57 -0700418TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700419 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700420}
421
422TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700423 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700424}
425
426TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700427 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700428}
Elliott Hughes9f525642014-04-08 17:14:01 -0700429
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700430TEST(stdlib, strtof_2206701) {
431 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL));
432 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL));
433}
434
435TEST(stdlib, strtod_largest_subnormal) {
436 // This value has been known to cause javac and java to infinite loop.
437 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
438 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL));
439 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL));
440 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL));
441 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL));
442 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL));
443 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL));
444 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL));
445}
446
Dan Albertb8425c52014-04-29 17:49:06 -0700447TEST(stdlib, quick_exit) {
448 pid_t pid = fork();
449 ASSERT_NE(-1, pid) << strerror(errno);
450
451 if (pid == 0) {
452 quick_exit(99);
453 }
454
Elliott Hughes33697a02016-01-26 13:04:57 -0800455 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700456}
457
458static int quick_exit_status = 0;
459
460static void quick_exit_1(void) {
461 ASSERT_EQ(quick_exit_status, 0);
462 quick_exit_status = 1;
463}
464
465static void quick_exit_2(void) {
466 ASSERT_EQ(quick_exit_status, 1);
467}
468
469static void not_run(void) {
470 FAIL();
471}
472
473TEST(stdlib, at_quick_exit) {
474 pid_t pid = fork();
475 ASSERT_NE(-1, pid) << strerror(errno);
476
477 if (pid == 0) {
478 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
479 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
480 atexit(not_run);
481 quick_exit(99);
482 }
483
Elliott Hughes33697a02016-01-26 13:04:57 -0800484 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700485}
486
Elliott Hughes9f525642014-04-08 17:14:01 -0700487TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800488 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700489 ASSERT_NE(-1, pid) << strerror(errno);
490
491 if (pid == 0) {
492 _Exit(99);
493 }
494
Elliott Hughes33697a02016-01-26 13:04:57 -0800495 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700496}
Elliott Hughes49167062014-07-25 17:24:00 -0700497
498TEST(stdlib, pty_smoke) {
499 // getpt returns a pty with O_RDWR|O_NOCTTY.
500 int fd = getpt();
501 ASSERT_NE(-1, fd);
502
503 // grantpt is a no-op.
504 ASSERT_EQ(0, grantpt(fd));
505
506 // ptsname_r should start "/dev/pts/".
507 char name_r[128];
508 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
509 name_r[9] = 0;
510 ASSERT_STREQ("/dev/pts/", name_r);
511
512 close(fd);
513}
514
515TEST(stdlib, posix_openpt) {
516 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
517 ASSERT_NE(-1, fd);
518 close(fd);
519}
520
521TEST(stdlib, ptsname_r_ENOTTY) {
522 errno = 0;
523 char buf[128];
524 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
525 ASSERT_EQ(ENOTTY, errno);
526}
527
528TEST(stdlib, ptsname_r_EINVAL) {
529 int fd = getpt();
530 ASSERT_NE(-1, fd);
531 errno = 0;
532 char* buf = NULL;
533 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
534 ASSERT_EQ(EINVAL, errno);
535 close(fd);
536}
537
538TEST(stdlib, ptsname_r_ERANGE) {
539 int fd = getpt();
540 ASSERT_NE(-1, fd);
541 errno = 0;
542 char buf[1];
543 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
544 ASSERT_EQ(ERANGE, errno);
545 close(fd);
546}
547
Elliott Hughes728cde52017-11-08 21:53:50 -0800548TEST(stdlib, ttyname) {
549 int fd = getpt();
550 ASSERT_NE(-1, fd);
551
552 // ttyname returns "/dev/ptmx" for a pty.
553 ASSERT_STREQ("/dev/ptmx", ttyname(fd));
554
555 close(fd);
556}
557
Elliott Hughes49167062014-07-25 17:24:00 -0700558TEST(stdlib, ttyname_r) {
559 int fd = getpt();
560 ASSERT_NE(-1, fd);
561
562 // ttyname_r returns "/dev/ptmx" for a pty.
563 char name_r[128];
564 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
565 ASSERT_STREQ("/dev/ptmx", name_r);
566
567 close(fd);
568}
569
570TEST(stdlib, ttyname_r_ENOTTY) {
571 int fd = open("/dev/null", O_WRONLY);
572 errno = 0;
573 char buf[128];
574 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
575 ASSERT_EQ(ENOTTY, errno);
576 close(fd);
577}
578
579TEST(stdlib, ttyname_r_EINVAL) {
580 int fd = getpt();
581 ASSERT_NE(-1, fd);
582 errno = 0;
583 char* buf = NULL;
584 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
585 ASSERT_EQ(EINVAL, errno);
586 close(fd);
587}
588
589TEST(stdlib, ttyname_r_ERANGE) {
590 int fd = getpt();
591 ASSERT_NE(-1, fd);
592 errno = 0;
593 char buf[1];
594 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
595 ASSERT_EQ(ERANGE, errno);
596 close(fd);
597}
598
599TEST(stdlib, unlockpt_ENOTTY) {
600 int fd = open("/dev/null", O_WRONLY);
601 errno = 0;
602 ASSERT_EQ(-1, unlockpt(fd));
603 ASSERT_EQ(ENOTTY, errno);
604 close(fd);
605}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700606
Elliott Hughesdf143f82016-04-04 17:34:04 -0700607TEST(stdlib, getsubopt) {
608 char* const tokens[] = {
609 const_cast<char*>("a"),
610 const_cast<char*>("b"),
611 const_cast<char*>("foo"),
612 nullptr
613 };
614 std::string input = "a,b,foo=bar,a,unknown";
615 char* subopts = &input[0];
616 char* value = nullptr;
617
618 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
619 ASSERT_EQ(nullptr, value);
620 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
621 ASSERT_EQ(nullptr, value);
622 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
623 ASSERT_STREQ("bar", value);
624 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
625 ASSERT_EQ(nullptr, value);
626
627 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
628}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700629
630TEST(stdlib, mblen) {
631 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
632 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
633 EXPECT_EQ(0, mblen(nullptr, 1));
634
635 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
636
637 // 1-byte UTF-8.
638 EXPECT_EQ(1, mblen("abcdef", 6));
639 // 2-byte UTF-8.
640 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
641 // 3-byte UTF-8.
642 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
643 // 4-byte UTF-8.
644 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
645
646 // Illegal over-long sequence.
647 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
648
649 // "mblen() shall ... return 0 (if s points to the null byte)".
650 EXPECT_EQ(0, mblen("", 1));
651}
Elliott Hughesf826a372017-07-13 09:35:15 -0700652
653template <typename T>
654static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
655 char* end_p;
656
657 // Negative base => invalid.
658 errno = 0;
659 ASSERT_EQ(T(0), fn("123", &end_p, -1));
660 ASSERT_EQ(EINVAL, errno);
661
662 // Base 1 => invalid (base 0 means "please guess").
663 errno = 0;
664 ASSERT_EQ(T(0), fn("123", &end_p, 1));
665 ASSERT_EQ(EINVAL, errno);
666
667 // Base > 36 => invalid.
668 errno = 0;
669 ASSERT_EQ(T(0), fn("123", &end_p, 37));
670 ASSERT_EQ(EINVAL, errno);
671
672 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
673 ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
674 ASSERT_EQ('x', *end_p);
675}
676
677TEST(stdlib, strtol_smoke) {
678 CheckStrToInt(strtol);
679}
680
681TEST(stdlib, strtoll_smoke) {
682 CheckStrToInt(strtoll);
683}
684
685TEST(stdlib, strtoul_smoke) {
686 CheckStrToInt(strtoul);
687}
688
689TEST(stdlib, strtoull_smoke) {
690 CheckStrToInt(strtoull);
691}
692
693TEST(stdlib, strtoimax_smoke) {
694 CheckStrToInt(strtoimax);
695}
696
697TEST(stdlib, strtoumax_smoke) {
698 CheckStrToInt(strtoumax);
699}
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700700
701TEST(stdlib, abs) {
702 ASSERT_EQ(INT_MAX, abs(-INT_MAX));
703 ASSERT_EQ(INT_MAX, abs(INT_MAX));
704}
705
706TEST(stdlib, labs) {
707 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
708 ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
709}
710
711TEST(stdlib, llabs) {
712 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
713 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
714}