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 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 38 | #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 Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 46 | // 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 Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 51 | TEST(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 Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 57 | 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 | |
| 65 | TEST(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 | |
| 81 | TEST(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 Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 96 | TEST(stdlib, lrand48) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 97 | srand48(0x01020304); |
| 98 | EXPECT_EQ(1409163720, lrand48()); |
| 99 | EXPECT_EQ(397769746, lrand48()); |
| 100 | EXPECT_EQ(902267124, lrand48()); |
| 101 | EXPECT_EQ(132366131, lrand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 102 | 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 Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 108 | } |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 109 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 110 | TEST(stdlib, random) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 111 | srandom(0x01020304); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 112 | EXPECT_EQ(55436735, random()); |
| 113 | EXPECT_EQ(1399865117, random()); |
| 114 | EXPECT_EQ(2032643283, random()); |
| 115 | EXPECT_EQ(571329216, random()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 116 | 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 Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 122 | } |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 123 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 124 | TEST(stdlib, rand) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 125 | srand(0x01020304); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 126 | EXPECT_EQ(55436735, rand()); |
| 127 | EXPECT_EQ(1399865117, rand()); |
| 128 | EXPECT_EQ(2032643283, rand()); |
| 129 | EXPECT_EQ(571329216, rand()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 130 | 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 Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST(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 Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 144 | 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 Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 150 | } |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 151 | |
Aleksandra Tsvetkova | 608b451 | 2015-02-27 15:01:59 +0300 | [diff] [blame] | 152 | TEST(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 | |
| 173 | TEST(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 Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 193 | TEST(stdlib, posix_memalign_sweep) { |
| 194 | void* ptr; |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 195 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 196 | // 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 Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 201 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 202 | // 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 | |
| 220 | TEST(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 | |
| 232 | TEST(stdlib, posix_memalign_overflow) { |
| 233 | void* ptr; |
| 234 | ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX)); |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 235 | } |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 236 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 237 | TEST(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 | |
| 261 | TEST(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 | |
| 269 | TEST(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 Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 281 | TEST(stdlib, realpath__NULL_filename) { |
| 282 | errno = 0; |
George Burgess IV | 95bd488 | 2017-08-14 14:48:55 -0700 | [diff] [blame] | 283 | // Work around the compile-time error generated by FORTIFY here. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 284 | const char* path = nullptr; |
| 285 | char* p = realpath(path, nullptr); |
| 286 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 287 | ASSERT_EQ(EINVAL, errno); |
| 288 | } |
| 289 | |
| 290 | TEST(stdlib, realpath__empty_filename) { |
| 291 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 292 | char* p = realpath("", nullptr); |
| 293 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 294 | ASSERT_EQ(ENOENT, errno); |
| 295 | } |
| 296 | |
| 297 | TEST(stdlib, realpath__ENOENT) { |
| 298 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 299 | char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", 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 | |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 304 | TEST(stdlib, realpath__component_after_non_directory) { |
| 305 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 306 | char* p = realpath("/dev/null/.", nullptr); |
| 307 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 308 | ASSERT_EQ(ENOTDIR, errno); |
| 309 | |
| 310 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 311 | p = realpath("/dev/null/..", nullptr); |
| 312 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 313 | ASSERT_EQ(ENOTDIR, errno); |
| 314 | } |
| 315 | |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 316 | TEST(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 Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 327 | p = realpath("/proc/self/exe", nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 328 | ASSERT_STREQ(executable_path, p); |
| 329 | free(p); |
| 330 | } |
Elliott Hughes | 0b25f63 | 2013-04-11 18:08:34 -0700 | [diff] [blame] | 331 | |
| 332 | TEST(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 Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 354 | |
| 355 | static void* TestBug57421_child(void* arg) { |
| 356 | pthread_t main_thread = reinterpret_cast<pthread_t>(arg); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 357 | pthread_join(main_thread, nullptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 358 | char* value = getenv("ENVIRONMENT_VARIABLE"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 359 | if (value == nullptr) { |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 360 | setenv("ENVIRONMENT_VARIABLE", "value", 1); |
| 361 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 362 | return nullptr; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void TestBug57421_main() { |
| 366 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 367 | ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); |
| 368 | pthread_exit(nullptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 369 | } |
| 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 Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 373 | |
| 374 | class stdlib_DeathTest : public BionicDeathTest {}; |
| 375 | |
| 376 | TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) { |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 377 | // https://code.google.com/p/android/issues/detail?id=57421 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 378 | ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); |
| 379 | } |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 380 | |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 381 | TEST(stdlib, mkostemp64) { |
| 382 | TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); }); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 383 | AssertCloseOnExec(tf.fd, true); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | TEST(stdlib, mkostemp) { |
| 387 | TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); }); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 388 | AssertCloseOnExec(tf.fd, true); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | TEST(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 Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 398 | TEST(stdlib, mkstemp) { |
| 399 | TemporaryFile tf; |
| 400 | struct stat sb; |
| 401 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 402 | } |
| 403 | |
Elliott Hughes | 3cdf573 | 2014-03-11 12:54:44 -0700 | [diff] [blame] | 404 | TEST(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 Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 415 | |
| 416 | TEST(stdlib, atof) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 417 | ASSERT_DOUBLE_EQ(1.23, atof("1.23")); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 420 | template <typename T> |
| 421 | static 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 Albert | f634655 | 2016-12-02 12:02:03 -0800 | [diff] [blame] | 428 | 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 Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 433 | 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 Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 441 | 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 Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 473 | TEST(stdlib, strtod) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 474 | CheckStrToFloat(strtod); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | TEST(stdlib, strtof) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 478 | CheckStrToFloat(strtof); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST(stdlib, strtold) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 482 | CheckStrToFloat(strtold); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 483 | } |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 484 | |
Elliott Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 485 | TEST(stdlib, strtof_2206701) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 486 | ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr)); |
| 487 | ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr)); |
Elliott Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | TEST(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 Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 493 | 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 Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 502 | TEST(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 Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 510 | AssertChildExited(pid, 99); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int quick_exit_status = 0; |
| 514 | |
| 515 | static void quick_exit_1(void) { |
| 516 | ASSERT_EQ(quick_exit_status, 0); |
| 517 | quick_exit_status = 1; |
| 518 | } |
| 519 | |
| 520 | static void quick_exit_2(void) { |
| 521 | ASSERT_EQ(quick_exit_status, 1); |
| 522 | } |
| 523 | |
| 524 | static void not_run(void) { |
| 525 | FAIL(); |
| 526 | } |
| 527 | |
| 528 | TEST(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 Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 539 | AssertChildExited(pid, 99); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 542 | TEST(unistd, _Exit) { |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 543 | pid_t pid = fork(); |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 544 | ASSERT_NE(-1, pid) << strerror(errno); |
| 545 | |
| 546 | if (pid == 0) { |
| 547 | _Exit(99); |
| 548 | } |
| 549 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 550 | AssertChildExited(pid, 99); |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 551 | } |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 552 | |
| 553 | TEST(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 | |
| 570 | TEST(stdlib, posix_openpt) { |
| 571 | int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 572 | ASSERT_NE(-1, fd); |
| 573 | close(fd); |
| 574 | } |
| 575 | |
| 576 | TEST(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 | |
| 583 | TEST(stdlib, ptsname_r_EINVAL) { |
| 584 | int fd = getpt(); |
| 585 | ASSERT_NE(-1, fd); |
| 586 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 587 | char* buf = nullptr; |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 588 | ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128)); |
| 589 | ASSERT_EQ(EINVAL, errno); |
| 590 | close(fd); |
| 591 | } |
| 592 | |
| 593 | TEST(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 Hughes | 728cde5 | 2017-11-08 21:53:50 -0800 | [diff] [blame] | 603 | TEST(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 Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 613 | TEST(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 | |
| 625 | TEST(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 | |
| 634 | TEST(stdlib, ttyname_r_EINVAL) { |
| 635 | int fd = getpt(); |
| 636 | ASSERT_NE(-1, fd); |
| 637 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 638 | char* buf = nullptr; |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 639 | ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128)); |
| 640 | ASSERT_EQ(EINVAL, errno); |
| 641 | close(fd); |
| 642 | } |
| 643 | |
| 644 | TEST(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 | |
| 654 | TEST(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 Hughes | b05ec5a | 2014-09-23 14:53:10 -0700 | [diff] [blame] | 661 | |
Elliott Hughes | df143f8 | 2016-04-04 17:34:04 -0700 | [diff] [blame] | 662 | TEST(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 Hughes | 6f6f905 | 2016-04-28 14:54:52 -0700 | [diff] [blame] | 684 | |
| 685 | TEST(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 Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 707 | |
| 708 | template <typename T> |
| 709 | static 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 Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 730 | |
| 731 | if (std::numeric_limits<T>::is_signed) { |
| 732 | // Minimum (such as -128). |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 733 | std::string min{std::to_string(std::numeric_limits<T>::min())}; |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 734 | end_p = nullptr; |
| 735 | errno = 0; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 736 | 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] | 737 | ASSERT_EQ(0, errno); |
| 738 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 739 | // Too negative (such as -129). |
| 740 | min.back() = (min.back() + 1); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 741 | end_p = nullptr; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 742 | errno = 0; |
| 743 | ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); |
| 744 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 745 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // Maximum (such as 127). |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 749 | std::string max{std::to_string(std::numeric_limits<T>::max())}; |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 750 | end_p = nullptr; |
| 751 | errno = 0; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 752 | 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] | 753 | ASSERT_EQ(0, errno); |
| 754 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 755 | // Too positive (such as 128). |
| 756 | max.back() = (max.back() + 1); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 757 | end_p = nullptr; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 758 | errno = 0; |
| 759 | ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); |
| 760 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 761 | 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 Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | TEST(stdlib, strtol_smoke) { |
| 782 | CheckStrToInt(strtol); |
| 783 | } |
| 784 | |
| 785 | TEST(stdlib, strtoll_smoke) { |
| 786 | CheckStrToInt(strtoll); |
| 787 | } |
| 788 | |
| 789 | TEST(stdlib, strtoul_smoke) { |
| 790 | CheckStrToInt(strtoul); |
| 791 | } |
| 792 | |
| 793 | TEST(stdlib, strtoull_smoke) { |
| 794 | CheckStrToInt(strtoull); |
| 795 | } |
| 796 | |
| 797 | TEST(stdlib, strtoimax_smoke) { |
| 798 | CheckStrToInt(strtoimax); |
| 799 | } |
| 800 | |
| 801 | TEST(stdlib, strtoumax_smoke) { |
| 802 | CheckStrToInt(strtoumax); |
| 803 | } |
Elliott Hughes | 00d8a8b | 2017-09-07 16:42:13 -0700 | [diff] [blame] | 804 | |
| 805 | TEST(stdlib, abs) { |
| 806 | ASSERT_EQ(INT_MAX, abs(-INT_MAX)); |
| 807 | ASSERT_EQ(INT_MAX, abs(INT_MAX)); |
| 808 | } |
| 809 | |
| 810 | TEST(stdlib, labs) { |
| 811 | ASSERT_EQ(LONG_MAX, labs(-LONG_MAX)); |
| 812 | ASSERT_EQ(LONG_MAX, labs(LONG_MAX)); |
| 813 | } |
| 814 | |
| 815 | TEST(stdlib, llabs) { |
| 816 | ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX)); |
| 817 | ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX)); |
| 818 | } |