Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 18 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 19 | #include "BionicDeathTest.h" |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 20 | #include "math_data_test.h" |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 21 | #include "TemporaryFile.h" |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 22 | #include "utils.h" |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 24 | #include <errno.h> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 25 | #include <fcntl.h> |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 26 | #include <libgen.h> |
| 27 | #include <limits.h> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 28 | #include <math.h> |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 29 | #include <pthread.h> |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 30 | #include <stdint.h> |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
Elliott Hughes | 4048856 | 2014-03-12 13:50:38 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 35 | #include <limits> |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 36 | #include <string> |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 38 | // The random number generator tests all set the seed, get four values, reset the seed and check |
| 39 | // that they get the first two values repeated, and then reset the seed and check two more values |
| 40 | // to rule out the possibility that we're just going round a cycle of four values. |
| 41 | // TODO: factor this out. |
| 42 | |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 43 | TEST(stdlib, drand48) { |
| 44 | srand48(0x01020304); |
| 45 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 46 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 47 | EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); |
| 48 | EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 49 | srand48(0x01020304); |
| 50 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 51 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 52 | srand48(0x01020304); |
| 53 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 54 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 55 | } |
| 56 | |
| 57 | TEST(stdlib, erand48) { |
| 58 | const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 }; |
| 59 | unsigned short xsubi[3]; |
| 60 | memcpy(xsubi, seed, sizeof(seed)); |
| 61 | EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); |
| 62 | EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); |
| 63 | EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi)); |
| 64 | EXPECT_DOUBLE_EQ(0.44658343479654405, 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 | memcpy(xsubi, seed, sizeof(seed)); |
| 69 | EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); |
| 70 | EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); |
| 71 | } |
| 72 | |
| 73 | TEST(stdlib, lcong48) { |
| 74 | unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e }; |
| 75 | lcong48(p); |
| 76 | EXPECT_EQ(1531389981, lrand48()); |
| 77 | EXPECT_EQ(1598801533, lrand48()); |
| 78 | EXPECT_EQ(2080534853, lrand48()); |
| 79 | EXPECT_EQ(1102488897, lrand48()); |
| 80 | lcong48(p); |
| 81 | EXPECT_EQ(1531389981, lrand48()); |
| 82 | EXPECT_EQ(1598801533, lrand48()); |
| 83 | lcong48(p); |
| 84 | EXPECT_EQ(1531389981, lrand48()); |
| 85 | EXPECT_EQ(1598801533, lrand48()); |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 88 | TEST(stdlib, lrand48) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 89 | srand48(0x01020304); |
| 90 | EXPECT_EQ(1409163720, lrand48()); |
| 91 | EXPECT_EQ(397769746, lrand48()); |
| 92 | EXPECT_EQ(902267124, lrand48()); |
| 93 | EXPECT_EQ(132366131, lrand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 94 | srand48(0x01020304); |
| 95 | EXPECT_EQ(1409163720, lrand48()); |
| 96 | EXPECT_EQ(397769746, lrand48()); |
| 97 | srand48(0x01020304); |
| 98 | EXPECT_EQ(1409163720, lrand48()); |
| 99 | EXPECT_EQ(397769746, lrand48()); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 100 | } |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 102 | TEST(stdlib, random) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 103 | srandom(0x01020304); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 104 | EXPECT_EQ(55436735, random()); |
| 105 | EXPECT_EQ(1399865117, random()); |
| 106 | EXPECT_EQ(2032643283, random()); |
| 107 | EXPECT_EQ(571329216, random()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 108 | srandom(0x01020304); |
| 109 | EXPECT_EQ(55436735, random()); |
| 110 | EXPECT_EQ(1399865117, random()); |
| 111 | srandom(0x01020304); |
| 112 | EXPECT_EQ(55436735, random()); |
| 113 | EXPECT_EQ(1399865117, random()); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 114 | } |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 115 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 116 | TEST(stdlib, rand) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 117 | srand(0x01020304); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 118 | EXPECT_EQ(55436735, rand()); |
| 119 | EXPECT_EQ(1399865117, rand()); |
| 120 | EXPECT_EQ(2032643283, rand()); |
| 121 | EXPECT_EQ(571329216, rand()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 122 | srand(0x01020304); |
| 123 | EXPECT_EQ(55436735, rand()); |
| 124 | EXPECT_EQ(1399865117, rand()); |
| 125 | srand(0x01020304); |
| 126 | EXPECT_EQ(55436735, rand()); |
| 127 | EXPECT_EQ(1399865117, rand()); |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST(stdlib, mrand48) { |
| 131 | srand48(0x01020304); |
| 132 | EXPECT_EQ(-1476639856, mrand48()); |
| 133 | EXPECT_EQ(795539493, mrand48()); |
| 134 | EXPECT_EQ(1804534249, mrand48()); |
| 135 | EXPECT_EQ(264732262, mrand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 136 | srand48(0x01020304); |
| 137 | EXPECT_EQ(-1476639856, mrand48()); |
| 138 | EXPECT_EQ(795539493, mrand48()); |
| 139 | srand48(0x01020304); |
| 140 | EXPECT_EQ(-1476639856, mrand48()); |
| 141 | EXPECT_EQ(795539493, mrand48()); |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 142 | } |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 143 | |
Aleksandra Tsvetkova | 608b451 | 2015-02-27 15:01:59 +0300 | [diff] [blame] | 144 | TEST(stdlib, jrand48_distribution) { |
| 145 | const int iterations = 4096; |
| 146 | const int pivot_low = 1536; |
| 147 | const int pivot_high = 2560; |
| 148 | |
| 149 | unsigned short xsubi[3]; |
| 150 | int bits[32] = {}; |
| 151 | |
| 152 | for (int iter = 0; iter < iterations; ++iter) { |
| 153 | long rand_val = jrand48(xsubi); |
| 154 | for (int bit = 0; bit < 32; ++bit) { |
| 155 | bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Check that bit probability is uniform |
| 160 | for (int bit = 0; bit < 32; ++bit) { |
| 161 | EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | TEST(stdlib, mrand48_distribution) { |
| 166 | const int iterations = 4096; |
| 167 | const int pivot_low = 1536; |
| 168 | const int pivot_high = 2560; |
| 169 | |
| 170 | int bits[32] = {}; |
| 171 | |
| 172 | for (int iter = 0; iter < iterations; ++iter) { |
| 173 | long rand_val = mrand48(); |
| 174 | for (int bit = 0; bit < 32; ++bit) { |
| 175 | bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Check that bit probability is uniform |
| 180 | for (int bit = 0; bit < 32; ++bit) { |
| 181 | EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high)); |
| 182 | } |
| 183 | } |
| 184 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 185 | TEST(stdlib, posix_memalign_sweep) { |
| 186 | void* ptr; |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 187 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 188 | // These should all fail. |
| 189 | for (size_t align = 0; align < sizeof(long); align++) { |
| 190 | ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256)) |
| 191 | << "Unexpected value at align " << align; |
| 192 | } |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 193 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 194 | // Verify powers of 2 up to 2048 allocate, and verify that all other |
| 195 | // alignment values between the powers of 2 fail. |
| 196 | size_t last_align = sizeof(long); |
| 197 | for (size_t align = sizeof(long); align <= 2048; align <<= 1) { |
| 198 | // Try all of the non power of 2 values from the last until this value. |
| 199 | for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) { |
| 200 | ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256)) |
| 201 | << "Unexpected success at align " << fail_align; |
| 202 | } |
| 203 | ASSERT_EQ(0, posix_memalign(&ptr, align, 256)) |
| 204 | << "Unexpected failure at align " << align; |
| 205 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 206 | << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align; |
| 207 | free(ptr); |
| 208 | last_align = align; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | TEST(stdlib, posix_memalign_various_sizes) { |
| 213 | std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000}; |
| 214 | for (auto size : sizes) { |
| 215 | void* ptr; |
| 216 | ASSERT_EQ(0, posix_memalign(&ptr, 16, 1)) |
| 217 | << "posix_memalign failed at size " << size; |
| 218 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf) |
| 219 | << "Pointer not aligned at size " << size << " ptr " << ptr; |
| 220 | free(ptr); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | TEST(stdlib, posix_memalign_overflow) { |
| 225 | void* ptr; |
| 226 | ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX)); |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 227 | } |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 228 | |
| 229 | TEST(stdlib, realpath__NULL_filename) { |
| 230 | errno = 0; |
George Burgess IV | 95bd488 | 2017-08-14 14:48:55 -0700 | [diff] [blame] | 231 | // Work around the compile-time error generated by FORTIFY here. |
| 232 | const char* path = NULL; |
| 233 | char* p = realpath(path, NULL); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 234 | ASSERT_TRUE(p == NULL); |
| 235 | ASSERT_EQ(EINVAL, errno); |
| 236 | } |
| 237 | |
| 238 | TEST(stdlib, realpath__empty_filename) { |
| 239 | errno = 0; |
| 240 | char* p = realpath("", NULL); |
| 241 | ASSERT_TRUE(p == NULL); |
| 242 | ASSERT_EQ(ENOENT, errno); |
| 243 | } |
| 244 | |
| 245 | TEST(stdlib, realpath__ENOENT) { |
| 246 | errno = 0; |
| 247 | char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL); |
| 248 | ASSERT_TRUE(p == NULL); |
| 249 | ASSERT_EQ(ENOENT, errno); |
| 250 | } |
| 251 | |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 252 | TEST(stdlib, realpath__component_after_non_directory) { |
| 253 | errno = 0; |
| 254 | char* p = realpath("/dev/null/.", NULL); |
| 255 | ASSERT_TRUE(p == NULL); |
| 256 | ASSERT_EQ(ENOTDIR, errno); |
| 257 | |
| 258 | errno = 0; |
| 259 | p = realpath("/dev/null/..", NULL); |
| 260 | ASSERT_TRUE(p == NULL); |
| 261 | ASSERT_EQ(ENOTDIR, errno); |
| 262 | } |
| 263 | |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 264 | TEST(stdlib, realpath) { |
| 265 | // Get the name of this executable. |
| 266 | char executable_path[PATH_MAX]; |
| 267 | int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 268 | ASSERT_NE(rc, -1); |
| 269 | executable_path[rc] = '\0'; |
| 270 | |
| 271 | char buf[PATH_MAX + 1]; |
| 272 | char* p = realpath("/proc/self/exe", buf); |
| 273 | ASSERT_STREQ(executable_path, p); |
| 274 | |
| 275 | p = realpath("/proc/self/exe", NULL); |
| 276 | ASSERT_STREQ(executable_path, p); |
| 277 | free(p); |
| 278 | } |
Elliott Hughes | 0b25f63 | 2013-04-11 18:08:34 -0700 | [diff] [blame] | 279 | |
| 280 | TEST(stdlib, qsort) { |
| 281 | struct s { |
| 282 | char name[16]; |
| 283 | static int comparator(const void* lhs, const void* rhs) { |
| 284 | return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name); |
| 285 | } |
| 286 | }; |
| 287 | s entries[3]; |
| 288 | strcpy(entries[0].name, "charlie"); |
| 289 | strcpy(entries[1].name, "bravo"); |
| 290 | strcpy(entries[2].name, "alpha"); |
| 291 | |
| 292 | qsort(entries, 3, sizeof(s), s::comparator); |
| 293 | ASSERT_STREQ("alpha", entries[0].name); |
| 294 | ASSERT_STREQ("bravo", entries[1].name); |
| 295 | ASSERT_STREQ("charlie", entries[2].name); |
| 296 | |
| 297 | qsort(entries, 3, sizeof(s), s::comparator); |
| 298 | ASSERT_STREQ("alpha", entries[0].name); |
| 299 | ASSERT_STREQ("bravo", entries[1].name); |
| 300 | ASSERT_STREQ("charlie", entries[2].name); |
| 301 | } |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 302 | |
| 303 | static void* TestBug57421_child(void* arg) { |
| 304 | pthread_t main_thread = reinterpret_cast<pthread_t>(arg); |
| 305 | pthread_join(main_thread, NULL); |
| 306 | char* value = getenv("ENVIRONMENT_VARIABLE"); |
| 307 | if (value == NULL) { |
| 308 | setenv("ENVIRONMENT_VARIABLE", "value", 1); |
| 309 | } |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | static void TestBug57421_main() { |
| 314 | pthread_t t; |
| 315 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); |
| 316 | pthread_exit(NULL); |
| 317 | } |
| 318 | |
| 319 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 320 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 321 | |
| 322 | class stdlib_DeathTest : public BionicDeathTest {}; |
| 323 | |
| 324 | TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) { |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 325 | // https://code.google.com/p/android/issues/detail?id=57421 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 326 | ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); |
| 327 | } |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 328 | |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 329 | TEST(stdlib, mkostemp64) { |
| 330 | TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); }); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 331 | AssertCloseOnExec(tf.fd, true); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | TEST(stdlib, mkostemp) { |
| 335 | TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); }); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 336 | AssertCloseOnExec(tf.fd, true); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | TEST(stdlib, mkstemp64) { |
| 340 | TemporaryFile tf(mkstemp64); |
| 341 | struct stat64 sb; |
| 342 | ASSERT_EQ(0, fstat64(tf.fd, &sb)); |
| 343 | ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE); |
| 344 | } |
| 345 | |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 346 | TEST(stdlib, mkstemp) { |
| 347 | TemporaryFile tf; |
| 348 | struct stat sb; |
| 349 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 350 | } |
| 351 | |
Elliott Hughes | 3cdf573 | 2014-03-11 12:54:44 -0700 | [diff] [blame] | 352 | TEST(stdlib, system) { |
| 353 | int status; |
| 354 | |
| 355 | status = system("exit 0"); |
| 356 | ASSERT_TRUE(WIFEXITED(status)); |
| 357 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 358 | |
| 359 | status = system("exit 1"); |
| 360 | ASSERT_TRUE(WIFEXITED(status)); |
| 361 | ASSERT_EQ(1, WEXITSTATUS(status)); |
| 362 | } |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 363 | |
| 364 | TEST(stdlib, atof) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 365 | ASSERT_DOUBLE_EQ(1.23, atof("1.23")); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 368 | template <typename T> |
| 369 | static void CheckStrToFloat(T fn(const char* s, char** end)) { |
| 370 | FpUlpEq<0, T> pred; |
| 371 | |
| 372 | EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr)); |
| 373 | EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr)); |
| 374 | EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr)); |
| 375 | |
Dan Albert | f634655 | 2016-12-02 12:02:03 -0800 | [diff] [blame] | 376 | const char* s = " \t\v\f\r\n9.0"; |
| 377 | char* p; |
| 378 | EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p)); |
| 379 | EXPECT_EQ(s + strlen(s), p); |
| 380 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 381 | EXPECT_TRUE(isnan(fn("+nan", nullptr))); |
| 382 | EXPECT_TRUE(isnan(fn("nan", nullptr))); |
| 383 | EXPECT_TRUE(isnan(fn("-nan", nullptr))); |
| 384 | |
| 385 | EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr))); |
| 386 | EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr))); |
| 387 | EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr))); |
| 388 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 389 | EXPECT_TRUE(isnan(fn("+nanny", &p))); |
| 390 | EXPECT_STREQ("ny", p); |
| 391 | EXPECT_TRUE(isnan(fn("nanny", &p))); |
| 392 | EXPECT_STREQ("ny", p); |
| 393 | EXPECT_TRUE(isnan(fn("-nanny", &p))); |
| 394 | EXPECT_STREQ("ny", p); |
| 395 | |
| 396 | EXPECT_EQ(0, fn("muppet", &p)); |
| 397 | EXPECT_STREQ("muppet", p); |
| 398 | EXPECT_EQ(0, fn(" muppet", &p)); |
| 399 | EXPECT_STREQ(" muppet", p); |
| 400 | |
| 401 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr)); |
| 402 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr)); |
| 403 | EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr)); |
| 404 | |
| 405 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr)); |
| 406 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr)); |
| 407 | EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr)); |
| 408 | |
| 409 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p)); |
| 410 | EXPECT_STREQ("initude", p); |
| 411 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p)); |
| 412 | EXPECT_STREQ("initude", p); |
| 413 | EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p)); |
| 414 | EXPECT_STREQ("initude", p); |
| 415 | |
| 416 | // Check case-insensitivity. |
| 417 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr)); |
| 418 | EXPECT_TRUE(isnan(fn("NaN", nullptr))); |
| 419 | } |
| 420 | |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 421 | TEST(stdlib, strtod) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 422 | CheckStrToFloat(strtod); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | TEST(stdlib, strtof) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 426 | CheckStrToFloat(strtof); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | TEST(stdlib, strtold) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 430 | CheckStrToFloat(strtold); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 431 | } |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 432 | |
Elliott Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 433 | TEST(stdlib, strtof_2206701) { |
| 434 | ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL)); |
| 435 | ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL)); |
| 436 | } |
| 437 | |
| 438 | TEST(stdlib, strtod_largest_subnormal) { |
| 439 | // This value has been known to cause javac and java to infinite loop. |
| 440 | // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ |
| 441 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL)); |
| 442 | ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL)); |
| 443 | ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL)); |
| 444 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL)); |
| 445 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL)); |
| 446 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL)); |
| 447 | ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL)); |
| 448 | } |
| 449 | |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 450 | TEST(stdlib, quick_exit) { |
| 451 | pid_t pid = fork(); |
| 452 | ASSERT_NE(-1, pid) << strerror(errno); |
| 453 | |
| 454 | if (pid == 0) { |
| 455 | quick_exit(99); |
| 456 | } |
| 457 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 458 | AssertChildExited(pid, 99); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static int quick_exit_status = 0; |
| 462 | |
| 463 | static void quick_exit_1(void) { |
| 464 | ASSERT_EQ(quick_exit_status, 0); |
| 465 | quick_exit_status = 1; |
| 466 | } |
| 467 | |
| 468 | static void quick_exit_2(void) { |
| 469 | ASSERT_EQ(quick_exit_status, 1); |
| 470 | } |
| 471 | |
| 472 | static void not_run(void) { |
| 473 | FAIL(); |
| 474 | } |
| 475 | |
| 476 | TEST(stdlib, at_quick_exit) { |
| 477 | pid_t pid = fork(); |
| 478 | ASSERT_NE(-1, pid) << strerror(errno); |
| 479 | |
| 480 | if (pid == 0) { |
| 481 | ASSERT_EQ(at_quick_exit(quick_exit_2), 0); |
| 482 | ASSERT_EQ(at_quick_exit(quick_exit_1), 0); |
| 483 | atexit(not_run); |
| 484 | quick_exit(99); |
| 485 | } |
| 486 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 487 | AssertChildExited(pid, 99); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 490 | TEST(unistd, _Exit) { |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 491 | pid_t pid = fork(); |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 492 | ASSERT_NE(-1, pid) << strerror(errno); |
| 493 | |
| 494 | if (pid == 0) { |
| 495 | _Exit(99); |
| 496 | } |
| 497 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 498 | AssertChildExited(pid, 99); |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 499 | } |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 500 | |
| 501 | TEST(stdlib, pty_smoke) { |
| 502 | // getpt returns a pty with O_RDWR|O_NOCTTY. |
| 503 | int fd = getpt(); |
| 504 | ASSERT_NE(-1, fd); |
| 505 | |
| 506 | // grantpt is a no-op. |
| 507 | ASSERT_EQ(0, grantpt(fd)); |
| 508 | |
| 509 | // ptsname_r should start "/dev/pts/". |
| 510 | char name_r[128]; |
| 511 | ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r))); |
| 512 | name_r[9] = 0; |
| 513 | ASSERT_STREQ("/dev/pts/", name_r); |
| 514 | |
| 515 | close(fd); |
| 516 | } |
| 517 | |
| 518 | TEST(stdlib, posix_openpt) { |
| 519 | int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 520 | ASSERT_NE(-1, fd); |
| 521 | close(fd); |
| 522 | } |
| 523 | |
| 524 | TEST(stdlib, ptsname_r_ENOTTY) { |
| 525 | errno = 0; |
| 526 | char buf[128]; |
| 527 | ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf))); |
| 528 | ASSERT_EQ(ENOTTY, errno); |
| 529 | } |
| 530 | |
| 531 | TEST(stdlib, ptsname_r_EINVAL) { |
| 532 | int fd = getpt(); |
| 533 | ASSERT_NE(-1, fd); |
| 534 | errno = 0; |
| 535 | char* buf = NULL; |
| 536 | ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128)); |
| 537 | ASSERT_EQ(EINVAL, errno); |
| 538 | close(fd); |
| 539 | } |
| 540 | |
| 541 | TEST(stdlib, ptsname_r_ERANGE) { |
| 542 | int fd = getpt(); |
| 543 | ASSERT_NE(-1, fd); |
| 544 | errno = 0; |
| 545 | char buf[1]; |
| 546 | ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf))); |
| 547 | ASSERT_EQ(ERANGE, errno); |
| 548 | close(fd); |
| 549 | } |
| 550 | |
Elliott Hughes | 728cde5 | 2017-11-08 21:53:50 -0800 | [diff] [blame] | 551 | TEST(stdlib, ttyname) { |
| 552 | int fd = getpt(); |
| 553 | ASSERT_NE(-1, fd); |
| 554 | |
| 555 | // ttyname returns "/dev/ptmx" for a pty. |
| 556 | ASSERT_STREQ("/dev/ptmx", ttyname(fd)); |
| 557 | |
| 558 | close(fd); |
| 559 | } |
| 560 | |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 561 | TEST(stdlib, ttyname_r) { |
| 562 | int fd = getpt(); |
| 563 | ASSERT_NE(-1, fd); |
| 564 | |
| 565 | // ttyname_r returns "/dev/ptmx" for a pty. |
| 566 | char name_r[128]; |
| 567 | ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r))); |
| 568 | ASSERT_STREQ("/dev/ptmx", name_r); |
| 569 | |
| 570 | close(fd); |
| 571 | } |
| 572 | |
| 573 | TEST(stdlib, ttyname_r_ENOTTY) { |
| 574 | int fd = open("/dev/null", O_WRONLY); |
| 575 | errno = 0; |
| 576 | char buf[128]; |
| 577 | ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf))); |
| 578 | ASSERT_EQ(ENOTTY, errno); |
| 579 | close(fd); |
| 580 | } |
| 581 | |
| 582 | TEST(stdlib, ttyname_r_EINVAL) { |
| 583 | int fd = getpt(); |
| 584 | ASSERT_NE(-1, fd); |
| 585 | errno = 0; |
| 586 | char* buf = NULL; |
| 587 | ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128)); |
| 588 | ASSERT_EQ(EINVAL, errno); |
| 589 | close(fd); |
| 590 | } |
| 591 | |
| 592 | TEST(stdlib, ttyname_r_ERANGE) { |
| 593 | int fd = getpt(); |
| 594 | ASSERT_NE(-1, fd); |
| 595 | errno = 0; |
| 596 | char buf[1]; |
| 597 | ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf))); |
| 598 | ASSERT_EQ(ERANGE, errno); |
| 599 | close(fd); |
| 600 | } |
| 601 | |
| 602 | TEST(stdlib, unlockpt_ENOTTY) { |
| 603 | int fd = open("/dev/null", O_WRONLY); |
| 604 | errno = 0; |
| 605 | ASSERT_EQ(-1, unlockpt(fd)); |
| 606 | ASSERT_EQ(ENOTTY, errno); |
| 607 | close(fd); |
| 608 | } |
Elliott Hughes | b05ec5a | 2014-09-23 14:53:10 -0700 | [diff] [blame] | 609 | |
Elliott Hughes | df143f8 | 2016-04-04 17:34:04 -0700 | [diff] [blame] | 610 | TEST(stdlib, getsubopt) { |
| 611 | char* const tokens[] = { |
| 612 | const_cast<char*>("a"), |
| 613 | const_cast<char*>("b"), |
| 614 | const_cast<char*>("foo"), |
| 615 | nullptr |
| 616 | }; |
| 617 | std::string input = "a,b,foo=bar,a,unknown"; |
| 618 | char* subopts = &input[0]; |
| 619 | char* value = nullptr; |
| 620 | |
| 621 | ASSERT_EQ(0, getsubopt(&subopts, tokens, &value)); |
| 622 | ASSERT_EQ(nullptr, value); |
| 623 | ASSERT_EQ(1, getsubopt(&subopts, tokens, &value)); |
| 624 | ASSERT_EQ(nullptr, value); |
| 625 | ASSERT_EQ(2, getsubopt(&subopts, tokens, &value)); |
| 626 | ASSERT_STREQ("bar", value); |
| 627 | ASSERT_EQ(0, getsubopt(&subopts, tokens, &value)); |
| 628 | ASSERT_EQ(nullptr, value); |
| 629 | |
| 630 | ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value)); |
| 631 | } |
Elliott Hughes | 6f6f905 | 2016-04-28 14:54:52 -0700 | [diff] [blame] | 632 | |
| 633 | TEST(stdlib, mblen) { |
| 634 | // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings, |
| 635 | // respectively, do or do not have state-dependent encodings." We're always UTF-8. |
| 636 | EXPECT_EQ(0, mblen(nullptr, 1)); |
| 637 | |
| 638 | ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8")); |
| 639 | |
| 640 | // 1-byte UTF-8. |
| 641 | EXPECT_EQ(1, mblen("abcdef", 6)); |
| 642 | // 2-byte UTF-8. |
| 643 | EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6)); |
| 644 | // 3-byte UTF-8. |
| 645 | EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6)); |
| 646 | // 4-byte UTF-8. |
| 647 | EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6)); |
| 648 | |
| 649 | // Illegal over-long sequence. |
| 650 | ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6)); |
| 651 | |
| 652 | // "mblen() shall ... return 0 (if s points to the null byte)". |
| 653 | EXPECT_EQ(0, mblen("", 1)); |
| 654 | } |
Elliott Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 655 | |
| 656 | template <typename T> |
| 657 | static void CheckStrToInt(T fn(const char* s, char** end, int base)) { |
| 658 | char* end_p; |
| 659 | |
| 660 | // Negative base => invalid. |
| 661 | errno = 0; |
| 662 | ASSERT_EQ(T(0), fn("123", &end_p, -1)); |
| 663 | ASSERT_EQ(EINVAL, errno); |
| 664 | |
| 665 | // Base 1 => invalid (base 0 means "please guess"). |
| 666 | errno = 0; |
| 667 | ASSERT_EQ(T(0), fn("123", &end_p, 1)); |
| 668 | ASSERT_EQ(EINVAL, errno); |
| 669 | |
| 670 | // Base > 36 => invalid. |
| 671 | errno = 0; |
| 672 | ASSERT_EQ(T(0), fn("123", &end_p, 37)); |
| 673 | ASSERT_EQ(EINVAL, errno); |
| 674 | |
| 675 | // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'. |
| 676 | ASSERT_EQ(T(0), fn("0xy", &end_p, 16)); |
| 677 | ASSERT_EQ('x', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 678 | |
| 679 | if (std::numeric_limits<T>::is_signed) { |
| 680 | // Minimum (such as -128). |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 681 | std::string min{std::to_string(std::numeric_limits<T>::min())}; |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 682 | end_p = nullptr; |
| 683 | errno = 0; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 684 | ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 685 | ASSERT_EQ(0, errno); |
| 686 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 687 | // Too negative (such as -129). |
| 688 | min.back() = (min.back() + 1); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 689 | end_p = nullptr; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 690 | errno = 0; |
| 691 | ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); |
| 692 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 693 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | // Maximum (such as 127). |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 697 | std::string max{std::to_string(std::numeric_limits<T>::max())}; |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 698 | end_p = nullptr; |
| 699 | errno = 0; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 700 | ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 701 | ASSERT_EQ(0, errno); |
| 702 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 703 | // Too positive (such as 128). |
| 704 | max.back() = (max.back() + 1); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 705 | end_p = nullptr; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 706 | errno = 0; |
| 707 | ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); |
| 708 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame^] | 709 | ASSERT_EQ('\0', *end_p); |
| 710 | |
| 711 | // In case of overflow, strto* leaves us pointing past the end of the number, |
| 712 | // not at the digit that overflowed. |
| 713 | end_p = nullptr; |
| 714 | errno = 0; |
| 715 | ASSERT_EQ(std::numeric_limits<T>::max(), |
| 716 | fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0)); |
| 717 | ASSERT_EQ(ERANGE, errno); |
| 718 | ASSERT_STREQ("abc", end_p); |
| 719 | if (std::numeric_limits<T>::is_signed) { |
| 720 | end_p = nullptr; |
| 721 | errno = 0; |
| 722 | ASSERT_EQ(std::numeric_limits<T>::min(), |
| 723 | fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0)); |
| 724 | ASSERT_EQ(ERANGE, errno); |
| 725 | ASSERT_STREQ("abc", end_p); |
| 726 | } |
Elliott Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | TEST(stdlib, strtol_smoke) { |
| 730 | CheckStrToInt(strtol); |
| 731 | } |
| 732 | |
| 733 | TEST(stdlib, strtoll_smoke) { |
| 734 | CheckStrToInt(strtoll); |
| 735 | } |
| 736 | |
| 737 | TEST(stdlib, strtoul_smoke) { |
| 738 | CheckStrToInt(strtoul); |
| 739 | } |
| 740 | |
| 741 | TEST(stdlib, strtoull_smoke) { |
| 742 | CheckStrToInt(strtoull); |
| 743 | } |
| 744 | |
| 745 | TEST(stdlib, strtoimax_smoke) { |
| 746 | CheckStrToInt(strtoimax); |
| 747 | } |
| 748 | |
| 749 | TEST(stdlib, strtoumax_smoke) { |
| 750 | CheckStrToInt(strtoumax); |
| 751 | } |
Elliott Hughes | 00d8a8b | 2017-09-07 16:42:13 -0700 | [diff] [blame] | 752 | |
| 753 | TEST(stdlib, abs) { |
| 754 | ASSERT_EQ(INT_MAX, abs(-INT_MAX)); |
| 755 | ASSERT_EQ(INT_MAX, abs(INT_MAX)); |
| 756 | } |
| 757 | |
| 758 | TEST(stdlib, labs) { |
| 759 | ASSERT_EQ(LONG_MAX, labs(-LONG_MAX)); |
| 760 | ASSERT_EQ(LONG_MAX, labs(LONG_MAX)); |
| 761 | } |
| 762 | |
| 763 | TEST(stdlib, llabs) { |
| 764 | ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX)); |
| 765 | ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX)); |
| 766 | } |