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