blob: 6e41555b5990eb4b932eb6ab4b2df0b9a1d33d8a [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 Hughes1921dce2017-12-19 10:27:27 -080035#include <limits>
Elliott Hughesf61a06e2017-12-19 16:11:37 -080036#include <string>
Elliott Hughes1921dce2017-12-19 10:27:27 -080037
Christopher Ferriscae21a92018-02-05 18:14:55 -080038#if defined(__BIONIC__)
39 #define ALIGNED_ALLOC_AVAILABLE 1
40#elif defined(__GLIBC_PREREQ)
41 #if __GLIBC_PREREQ(2, 16)
42 #define ALIGNED_ALLOC_AVAILABLE 1
43 #endif
44#endif
45
Elliott Hughes274afe82014-11-06 12:40:08 -080046// The random number generator tests all set the seed, get four values, reset the seed and check
47// that they get the first two values repeated, and then reset the seed and check two more values
48// to rule out the possibility that we're just going round a cycle of four values.
49// TODO: factor this out.
50
Elliott Hughes774c7f52012-10-01 13:11:03 -070051TEST(stdlib, drand48) {
52 srand48(0x01020304);
53 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
54 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
55 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
56 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080057 srand48(0x01020304);
58 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
59 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
60 srand48(0x01020304);
61 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
62 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
63}
64
65TEST(stdlib, erand48) {
66 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
67 unsigned short xsubi[3];
68 memcpy(xsubi, seed, sizeof(seed));
69 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
70 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
71 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
72 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
73 memcpy(xsubi, seed, sizeof(seed));
74 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
75 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
76 memcpy(xsubi, seed, sizeof(seed));
77 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
78 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
79}
80
81TEST(stdlib, lcong48) {
82 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
83 lcong48(p);
84 EXPECT_EQ(1531389981, lrand48());
85 EXPECT_EQ(1598801533, lrand48());
86 EXPECT_EQ(2080534853, lrand48());
87 EXPECT_EQ(1102488897, lrand48());
88 lcong48(p);
89 EXPECT_EQ(1531389981, lrand48());
90 EXPECT_EQ(1598801533, lrand48());
91 lcong48(p);
92 EXPECT_EQ(1531389981, lrand48());
93 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -070094}
95
Elliott Hughesa0beeea2014-06-12 11:48:04 -070096TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070097 srand48(0x01020304);
98 EXPECT_EQ(1409163720, lrand48());
99 EXPECT_EQ(397769746, lrand48());
100 EXPECT_EQ(902267124, lrand48());
101 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800102 srand48(0x01020304);
103 EXPECT_EQ(1409163720, lrand48());
104 EXPECT_EQ(397769746, lrand48());
105 srand48(0x01020304);
106 EXPECT_EQ(1409163720, lrand48());
107 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700108}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700109
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700110TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700111 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700112 EXPECT_EQ(55436735, random());
113 EXPECT_EQ(1399865117, random());
114 EXPECT_EQ(2032643283, random());
115 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800116 srandom(0x01020304);
117 EXPECT_EQ(55436735, random());
118 EXPECT_EQ(1399865117, random());
119 srandom(0x01020304);
120 EXPECT_EQ(55436735, random());
121 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700122}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700123
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700124TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700125 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700126 EXPECT_EQ(55436735, rand());
127 EXPECT_EQ(1399865117, rand());
128 EXPECT_EQ(2032643283, rand());
129 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800130 srand(0x01020304);
131 EXPECT_EQ(55436735, rand());
132 EXPECT_EQ(1399865117, rand());
133 srand(0x01020304);
134 EXPECT_EQ(55436735, rand());
135 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700136}
137
138TEST(stdlib, mrand48) {
139 srand48(0x01020304);
140 EXPECT_EQ(-1476639856, mrand48());
141 EXPECT_EQ(795539493, mrand48());
142 EXPECT_EQ(1804534249, mrand48());
143 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800144 srand48(0x01020304);
145 EXPECT_EQ(-1476639856, mrand48());
146 EXPECT_EQ(795539493, mrand48());
147 srand48(0x01020304);
148 EXPECT_EQ(-1476639856, mrand48());
149 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700150}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800151
Aleksandra Tsvetkova608b4512015-02-27 15:01:59 +0300152TEST(stdlib, jrand48_distribution) {
153 const int iterations = 4096;
154 const int pivot_low = 1536;
155 const int pivot_high = 2560;
156
157 unsigned short xsubi[3];
158 int bits[32] = {};
159
160 for (int iter = 0; iter < iterations; ++iter) {
161 long rand_val = jrand48(xsubi);
162 for (int bit = 0; bit < 32; ++bit) {
163 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
164 }
165 }
166
167 // Check that bit probability is uniform
168 for (int bit = 0; bit < 32; ++bit) {
169 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
170 }
171}
172
173TEST(stdlib, mrand48_distribution) {
174 const int iterations = 4096;
175 const int pivot_low = 1536;
176 const int pivot_high = 2560;
177
178 int bits[32] = {};
179
180 for (int iter = 0; iter < iterations; ++iter) {
181 long rand_val = mrand48();
182 for (int bit = 0; bit < 32; ++bit) {
183 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
184 }
185 }
186
187 // Check that bit probability is uniform
188 for (int bit = 0; bit < 32; ++bit) {
189 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
190 }
191}
192
Christopher Ferris3a32d952017-06-15 13:30:44 -0700193TEST(stdlib, posix_memalign_sweep) {
194 void* ptr;
Elliott Hughesb16b7222013-02-04 13:18:00 -0800195
Christopher Ferris3a32d952017-06-15 13:30:44 -0700196 // These should all fail.
197 for (size_t align = 0; align < sizeof(long); align++) {
198 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
199 << "Unexpected value at align " << align;
200 }
Elliott Hughesb16b7222013-02-04 13:18:00 -0800201
Christopher Ferris3a32d952017-06-15 13:30:44 -0700202 // Verify powers of 2 up to 2048 allocate, and verify that all other
203 // alignment values between the powers of 2 fail.
204 size_t last_align = sizeof(long);
205 for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
206 // Try all of the non power of 2 values from the last until this value.
207 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
208 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
209 << "Unexpected success at align " << fail_align;
210 }
211 ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
212 << "Unexpected failure at align " << align;
213 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
214 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
215 free(ptr);
216 last_align = align;
217 }
218}
219
220TEST(stdlib, posix_memalign_various_sizes) {
221 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
222 for (auto size : sizes) {
223 void* ptr;
224 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
225 << "posix_memalign failed at size " << size;
226 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
227 << "Pointer not aligned at size " << size << " ptr " << ptr;
228 free(ptr);
229 }
230}
231
232TEST(stdlib, posix_memalign_overflow) {
233 void* ptr;
234 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
Elliott Hughesb16b7222013-02-04 13:18:00 -0800235}
Elliott Hughesf0777842013-03-01 16:59:46 -0800236
Christopher Ferriscae21a92018-02-05 18:14:55 -0800237TEST(stdlib, aligned_alloc_sweep) {
238#if defined(ALIGNED_ALLOC_AVAILABLE)
239 // Verify powers of 2 up to 2048 allocate, and verify that all other
240 // alignment values between the powers of 2 fail.
241 size_t last_align = 1;
242 for (size_t align = 1; align <= 2048; align <<= 1) {
243 // Try all of the non power of 2 values from the last until this value.
244 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
245 ASSERT_TRUE(aligned_alloc(fail_align, 256) == nullptr)
246 << "Unexpected success at align " << fail_align;
247 ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align;
248 }
249 void* ptr = aligned_alloc(align, 256);
250 ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align;
251 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
252 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
253 free(ptr);
254 last_align = align;
255 }
256#else
257 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n";
258#endif
259}
260
261TEST(stdlib, aligned_alloc_overflow) {
262#if defined(ALIGNED_ALLOC_AVAILABLE)
263 ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr);
264#else
265 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n";
266#endif
267}
268
269TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) {
270#if defined(ALIGNED_ALLOC_AVAILABLE)
271 for (size_t size = 1; size <= 2048; size++) {
272 void* ptr = aligned_alloc(2048, size);
273 ASSERT_TRUE(ptr != nullptr) << "Failed at size " << std::to_string(size);
274 free(ptr);
275 }
276#else
277 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n";
278#endif
279}
280
Elliott Hughesf0777842013-03-01 16:59:46 -0800281TEST(stdlib, realpath__NULL_filename) {
282 errno = 0;
George Burgess IV95bd4882017-08-14 14:48:55 -0700283 // Work around the compile-time error generated by FORTIFY here.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 const char* path = nullptr;
285 char* p = realpath(path, nullptr);
286 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800287 ASSERT_EQ(EINVAL, errno);
288}
289
290TEST(stdlib, realpath__empty_filename) {
291 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700292 char* p = realpath("", nullptr);
293 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800294 ASSERT_EQ(ENOENT, errno);
295}
296
297TEST(stdlib, realpath__ENOENT) {
298 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700299 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr);
300 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800301 ASSERT_EQ(ENOENT, errno);
302}
303
Elliott Hughes31e072f2014-09-30 16:15:42 -0700304TEST(stdlib, realpath__component_after_non_directory) {
305 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700306 char* p = realpath("/dev/null/.", nullptr);
307 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700308 ASSERT_EQ(ENOTDIR, errno);
309
310 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700311 p = realpath("/dev/null/..", nullptr);
312 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700313 ASSERT_EQ(ENOTDIR, errno);
314}
315
Elliott Hughesf0777842013-03-01 16:59:46 -0800316TEST(stdlib, realpath) {
317 // Get the name of this executable.
318 char executable_path[PATH_MAX];
319 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
320 ASSERT_NE(rc, -1);
321 executable_path[rc] = '\0';
322
323 char buf[PATH_MAX + 1];
324 char* p = realpath("/proc/self/exe", buf);
325 ASSERT_STREQ(executable_path, p);
326
Yi Kong32bc0fc2018-08-02 17:31:13 -0700327 p = realpath("/proc/self/exe", nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800328 ASSERT_STREQ(executable_path, p);
329 free(p);
330}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700331
332TEST(stdlib, qsort) {
333 struct s {
334 char name[16];
335 static int comparator(const void* lhs, const void* rhs) {
336 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
337 }
338 };
339 s entries[3];
340 strcpy(entries[0].name, "charlie");
341 strcpy(entries[1].name, "bravo");
342 strcpy(entries[2].name, "alpha");
343
344 qsort(entries, 3, sizeof(s), s::comparator);
345 ASSERT_STREQ("alpha", entries[0].name);
346 ASSERT_STREQ("bravo", entries[1].name);
347 ASSERT_STREQ("charlie", entries[2].name);
348
349 qsort(entries, 3, sizeof(s), s::comparator);
350 ASSERT_STREQ("alpha", entries[0].name);
351 ASSERT_STREQ("bravo", entries[1].name);
352 ASSERT_STREQ("charlie", entries[2].name);
353}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800354
355static void* TestBug57421_child(void* arg) {
356 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700357 pthread_join(main_thread, nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800358 char* value = getenv("ENVIRONMENT_VARIABLE");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700359 if (value == nullptr) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800360 setenv("ENVIRONMENT_VARIABLE", "value", 1);
361 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700362 return nullptr;
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800363}
364
365static void TestBug57421_main() {
366 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700367 ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
368 pthread_exit(nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800369}
370
371// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
372// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800373
374class stdlib_DeathTest : public BionicDeathTest {};
375
376TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800377 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800378 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
379}
Calin Juravlefe317a32014-02-21 15:11:03 +0000380
Elliott Hughes31165ed2014-09-23 17:34:29 -0700381TEST(stdlib, mkostemp64) {
382 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
Elliott Hughesa7f12942017-12-15 13:55:53 -0800383 AssertCloseOnExec(tf.fd, true);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700384}
385
386TEST(stdlib, mkostemp) {
387 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
Elliott Hughesa7f12942017-12-15 13:55:53 -0800388 AssertCloseOnExec(tf.fd, true);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700389}
390
391TEST(stdlib, mkstemp64) {
392 TemporaryFile tf(mkstemp64);
393 struct stat64 sb;
394 ASSERT_EQ(0, fstat64(tf.fd, &sb));
395 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
396}
397
Calin Juravlefe317a32014-02-21 15:11:03 +0000398TEST(stdlib, mkstemp) {
399 TemporaryFile tf;
400 struct stat sb;
401 ASSERT_EQ(0, fstat(tf.fd, &sb));
402}
403
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700404TEST(stdlib, system) {
405 int status;
406
407 status = system("exit 0");
408 ASSERT_TRUE(WIFEXITED(status));
409 ASSERT_EQ(0, WEXITSTATUS(status));
410
411 status = system("exit 1");
412 ASSERT_TRUE(WIFEXITED(status));
413 ASSERT_EQ(1, WEXITSTATUS(status));
414}
Elliott Hughes5a817382014-03-12 16:12:57 -0700415
416TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700417 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700418}
419
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700420template <typename T>
421static void CheckStrToFloat(T fn(const char* s, char** end)) {
422 FpUlpEq<0, T> pred;
423
424 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
425 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
426 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
427
Dan Albertf6346552016-12-02 12:02:03 -0800428 const char* s = " \t\v\f\r\n9.0";
429 char* p;
430 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
431 EXPECT_EQ(s + strlen(s), p);
432
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700433 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
434 EXPECT_TRUE(isnan(fn("nan", nullptr)));
435 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
436
437 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
438 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
439 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
440
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700441 EXPECT_TRUE(isnan(fn("+nanny", &p)));
442 EXPECT_STREQ("ny", p);
443 EXPECT_TRUE(isnan(fn("nanny", &p)));
444 EXPECT_STREQ("ny", p);
445 EXPECT_TRUE(isnan(fn("-nanny", &p)));
446 EXPECT_STREQ("ny", p);
447
448 EXPECT_EQ(0, fn("muppet", &p));
449 EXPECT_STREQ("muppet", p);
450 EXPECT_EQ(0, fn(" muppet", &p));
451 EXPECT_STREQ(" muppet", p);
452
453 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
454 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
455 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
456
457 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
458 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
459 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
460
461 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
462 EXPECT_STREQ("initude", p);
463 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
464 EXPECT_STREQ("initude", p);
465 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
466 EXPECT_STREQ("initude", p);
467
468 // Check case-insensitivity.
469 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
470 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
471}
472
Elliott Hughes5a817382014-03-12 16:12:57 -0700473TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700474 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700475}
476
477TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700478 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700479}
480
481TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700482 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700483}
Elliott Hughes9f525642014-04-08 17:14:01 -0700484
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700485TEST(stdlib, strtof_2206701) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700486 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr));
487 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700488}
489
490TEST(stdlib, strtod_largest_subnormal) {
491 // This value has been known to cause javac and java to infinite loop.
492 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
Yi Kong32bc0fc2018-08-02 17:31:13 -0700493 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", nullptr));
494 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", nullptr));
495 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", nullptr));
496 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", nullptr));
497 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", nullptr));
498 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", nullptr));
499 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700500}
501
Dan Albertb8425c52014-04-29 17:49:06 -0700502TEST(stdlib, quick_exit) {
503 pid_t pid = fork();
504 ASSERT_NE(-1, pid) << strerror(errno);
505
506 if (pid == 0) {
507 quick_exit(99);
508 }
509
Elliott Hughes33697a02016-01-26 13:04:57 -0800510 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700511}
512
513static int quick_exit_status = 0;
514
515static void quick_exit_1(void) {
516 ASSERT_EQ(quick_exit_status, 0);
517 quick_exit_status = 1;
518}
519
520static void quick_exit_2(void) {
521 ASSERT_EQ(quick_exit_status, 1);
522}
523
524static void not_run(void) {
525 FAIL();
526}
527
528TEST(stdlib, at_quick_exit) {
529 pid_t pid = fork();
530 ASSERT_NE(-1, pid) << strerror(errno);
531
532 if (pid == 0) {
533 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
534 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
535 atexit(not_run);
536 quick_exit(99);
537 }
538
Elliott Hughes33697a02016-01-26 13:04:57 -0800539 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700540}
541
Elliott Hughes9f525642014-04-08 17:14:01 -0700542TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800543 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700544 ASSERT_NE(-1, pid) << strerror(errno);
545
546 if (pid == 0) {
547 _Exit(99);
548 }
549
Elliott Hughes33697a02016-01-26 13:04:57 -0800550 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700551}
Elliott Hughes49167062014-07-25 17:24:00 -0700552
553TEST(stdlib, pty_smoke) {
554 // getpt returns a pty with O_RDWR|O_NOCTTY.
555 int fd = getpt();
556 ASSERT_NE(-1, fd);
557
558 // grantpt is a no-op.
559 ASSERT_EQ(0, grantpt(fd));
560
561 // ptsname_r should start "/dev/pts/".
562 char name_r[128];
563 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
564 name_r[9] = 0;
565 ASSERT_STREQ("/dev/pts/", name_r);
566
567 close(fd);
568}
569
570TEST(stdlib, posix_openpt) {
571 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
572 ASSERT_NE(-1, fd);
573 close(fd);
574}
575
576TEST(stdlib, ptsname_r_ENOTTY) {
577 errno = 0;
578 char buf[128];
579 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
580 ASSERT_EQ(ENOTTY, errno);
581}
582
583TEST(stdlib, ptsname_r_EINVAL) {
584 int fd = getpt();
585 ASSERT_NE(-1, fd);
586 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700587 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700588 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
589 ASSERT_EQ(EINVAL, errno);
590 close(fd);
591}
592
593TEST(stdlib, ptsname_r_ERANGE) {
594 int fd = getpt();
595 ASSERT_NE(-1, fd);
596 errno = 0;
597 char buf[1];
598 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
599 ASSERT_EQ(ERANGE, errno);
600 close(fd);
601}
602
Elliott Hughes728cde52017-11-08 21:53:50 -0800603TEST(stdlib, ttyname) {
604 int fd = getpt();
605 ASSERT_NE(-1, fd);
606
607 // ttyname returns "/dev/ptmx" for a pty.
608 ASSERT_STREQ("/dev/ptmx", ttyname(fd));
609
610 close(fd);
611}
612
Elliott Hughes49167062014-07-25 17:24:00 -0700613TEST(stdlib, ttyname_r) {
614 int fd = getpt();
615 ASSERT_NE(-1, fd);
616
617 // ttyname_r returns "/dev/ptmx" for a pty.
618 char name_r[128];
619 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
620 ASSERT_STREQ("/dev/ptmx", name_r);
621
622 close(fd);
623}
624
625TEST(stdlib, ttyname_r_ENOTTY) {
626 int fd = open("/dev/null", O_WRONLY);
627 errno = 0;
628 char buf[128];
629 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
630 ASSERT_EQ(ENOTTY, errno);
631 close(fd);
632}
633
634TEST(stdlib, ttyname_r_EINVAL) {
635 int fd = getpt();
636 ASSERT_NE(-1, fd);
637 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700638 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700639 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
640 ASSERT_EQ(EINVAL, errno);
641 close(fd);
642}
643
644TEST(stdlib, ttyname_r_ERANGE) {
645 int fd = getpt();
646 ASSERT_NE(-1, fd);
647 errno = 0;
648 char buf[1];
649 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
650 ASSERT_EQ(ERANGE, errno);
651 close(fd);
652}
653
654TEST(stdlib, unlockpt_ENOTTY) {
655 int fd = open("/dev/null", O_WRONLY);
656 errno = 0;
657 ASSERT_EQ(-1, unlockpt(fd));
658 ASSERT_EQ(ENOTTY, errno);
659 close(fd);
660}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700661
Elliott Hughesdf143f82016-04-04 17:34:04 -0700662TEST(stdlib, getsubopt) {
663 char* const tokens[] = {
664 const_cast<char*>("a"),
665 const_cast<char*>("b"),
666 const_cast<char*>("foo"),
667 nullptr
668 };
669 std::string input = "a,b,foo=bar,a,unknown";
670 char* subopts = &input[0];
671 char* value = nullptr;
672
673 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
674 ASSERT_EQ(nullptr, value);
675 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
676 ASSERT_EQ(nullptr, value);
677 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
678 ASSERT_STREQ("bar", value);
679 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
680 ASSERT_EQ(nullptr, value);
681
682 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
683}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700684
685TEST(stdlib, mblen) {
686 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
687 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
688 EXPECT_EQ(0, mblen(nullptr, 1));
689
690 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
691
692 // 1-byte UTF-8.
693 EXPECT_EQ(1, mblen("abcdef", 6));
694 // 2-byte UTF-8.
695 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
696 // 3-byte UTF-8.
697 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
698 // 4-byte UTF-8.
699 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
700
701 // Illegal over-long sequence.
702 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
703
704 // "mblen() shall ... return 0 (if s points to the null byte)".
705 EXPECT_EQ(0, mblen("", 1));
706}
Elliott Hughesf826a372017-07-13 09:35:15 -0700707
708template <typename T>
709static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
710 char* end_p;
711
712 // Negative base => invalid.
713 errno = 0;
714 ASSERT_EQ(T(0), fn("123", &end_p, -1));
715 ASSERT_EQ(EINVAL, errno);
716
717 // Base 1 => invalid (base 0 means "please guess").
718 errno = 0;
719 ASSERT_EQ(T(0), fn("123", &end_p, 1));
720 ASSERT_EQ(EINVAL, errno);
721
722 // Base > 36 => invalid.
723 errno = 0;
724 ASSERT_EQ(T(0), fn("123", &end_p, 37));
725 ASSERT_EQ(EINVAL, errno);
726
727 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
728 ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
729 ASSERT_EQ('x', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800730
731 if (std::numeric_limits<T>::is_signed) {
732 // Minimum (such as -128).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800733 std::string min{std::to_string(std::numeric_limits<T>::min())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800734 end_p = nullptr;
735 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800736 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800737 ASSERT_EQ(0, errno);
738 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800739 // Too negative (such as -129).
740 min.back() = (min.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800741 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800742 errno = 0;
743 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
744 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800745 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800746 }
747
748 // Maximum (such as 127).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800749 std::string max{std::to_string(std::numeric_limits<T>::max())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800750 end_p = nullptr;
751 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800752 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800753 ASSERT_EQ(0, errno);
754 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800755 // Too positive (such as 128).
756 max.back() = (max.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800757 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800758 errno = 0;
759 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
760 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800761 ASSERT_EQ('\0', *end_p);
762
763 // In case of overflow, strto* leaves us pointing past the end of the number,
764 // not at the digit that overflowed.
765 end_p = nullptr;
766 errno = 0;
767 ASSERT_EQ(std::numeric_limits<T>::max(),
768 fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
769 ASSERT_EQ(ERANGE, errno);
770 ASSERT_STREQ("abc", end_p);
771 if (std::numeric_limits<T>::is_signed) {
772 end_p = nullptr;
773 errno = 0;
774 ASSERT_EQ(std::numeric_limits<T>::min(),
775 fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
776 ASSERT_EQ(ERANGE, errno);
777 ASSERT_STREQ("abc", end_p);
778 }
Elliott Hughesf826a372017-07-13 09:35:15 -0700779}
780
781TEST(stdlib, strtol_smoke) {
782 CheckStrToInt(strtol);
783}
784
785TEST(stdlib, strtoll_smoke) {
786 CheckStrToInt(strtoll);
787}
788
789TEST(stdlib, strtoul_smoke) {
790 CheckStrToInt(strtoul);
791}
792
793TEST(stdlib, strtoull_smoke) {
794 CheckStrToInt(strtoull);
795}
796
797TEST(stdlib, strtoimax_smoke) {
798 CheckStrToInt(strtoimax);
799}
800
801TEST(stdlib, strtoumax_smoke) {
802 CheckStrToInt(strtoumax);
803}
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700804
805TEST(stdlib, abs) {
806 ASSERT_EQ(INT_MAX, abs(-INT_MAX));
807 ASSERT_EQ(INT_MAX, abs(INT_MAX));
808}
809
810TEST(stdlib, labs) {
811 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
812 ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
813}
814
815TEST(stdlib, llabs) {
816 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
817 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
818}