stdlib_test.cpp revision a0beeeabbc8735bc830544cbbb1d920122b8d958
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> 18#include "TemporaryFile.h" 19 20#include <errno.h> 21#include <libgen.h> 22#include <limits.h> 23#include <pthread.h> 24#include <stdint.h> 25#include <stdlib.h> 26#include <fcntl.h> 27#include <sys/types.h> 28#include <sys/wait.h> 29 30TEST(stdlib, drand48) { 31 srand48(0x01020304); 32 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); 33 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); 34 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); 35 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); 36} 37 38TEST(stdlib, lrand48) { 39 srand48(0x01020304); 40 EXPECT_EQ(1409163720, lrand48()); 41 EXPECT_EQ(397769746, lrand48()); 42 EXPECT_EQ(902267124, lrand48()); 43 EXPECT_EQ(132366131, lrand48()); 44} 45 46TEST(stdlib, random) { 47 srandom(0x01020304); 48 EXPECT_EQ(55436735, random()); 49 EXPECT_EQ(1399865117, random()); 50 EXPECT_EQ(2032643283, random()); 51 EXPECT_EQ(571329216, random()); 52} 53 54TEST(stdlib, rand) { 55 srand(0x01020304); 56#if defined(__BIONIC__) 57 EXPECT_EQ(1675538669, rand()); 58 EXPECT_EQ(1678228258, rand()); 59 EXPECT_EQ(1352350131, rand()); 60 EXPECT_EQ(824068976, rand()); 61#else 62 EXPECT_EQ(55436735, rand()); 63 EXPECT_EQ(1399865117, rand()); 64 EXPECT_EQ(2032643283, rand()); 65 EXPECT_EQ(571329216, rand()); 66#endif 67} 68 69TEST(stdlib, mrand48) { 70 srand48(0x01020304); 71 EXPECT_EQ(-1476639856, mrand48()); 72 EXPECT_EQ(795539493, mrand48()); 73 EXPECT_EQ(1804534249, mrand48()); 74 EXPECT_EQ(264732262, mrand48()); 75} 76 77TEST(stdlib, posix_memalign) { 78 void* p; 79 80 ASSERT_EQ(0, posix_memalign(&p, 512, 128)); 81 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512); 82 free(p); 83 84 // Can't align to a non-power of 2. 85 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128)); 86} 87 88TEST(stdlib, realpath__NULL_filename) { 89 errno = 0; 90 char* p = realpath(NULL, NULL); 91 ASSERT_TRUE(p == NULL); 92 ASSERT_EQ(EINVAL, errno); 93} 94 95TEST(stdlib, realpath__empty_filename) { 96 errno = 0; 97 char* p = realpath("", NULL); 98 ASSERT_TRUE(p == NULL); 99 ASSERT_EQ(ENOENT, errno); 100} 101 102TEST(stdlib, realpath__ENOENT) { 103 errno = 0; 104 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL); 105 ASSERT_TRUE(p == NULL); 106 ASSERT_EQ(ENOENT, errno); 107} 108 109TEST(stdlib, realpath) { 110 // Get the name of this executable. 111 char executable_path[PATH_MAX]; 112 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); 113 ASSERT_NE(rc, -1); 114 executable_path[rc] = '\0'; 115 116 char buf[PATH_MAX + 1]; 117 char* p = realpath("/proc/self/exe", buf); 118 ASSERT_STREQ(executable_path, p); 119 120 p = realpath("/proc/self/exe", NULL); 121 ASSERT_STREQ(executable_path, p); 122 free(p); 123} 124 125TEST(stdlib, qsort) { 126 struct s { 127 char name[16]; 128 static int comparator(const void* lhs, const void* rhs) { 129 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name); 130 } 131 }; 132 s entries[3]; 133 strcpy(entries[0].name, "charlie"); 134 strcpy(entries[1].name, "bravo"); 135 strcpy(entries[2].name, "alpha"); 136 137 qsort(entries, 3, sizeof(s), s::comparator); 138 ASSERT_STREQ("alpha", entries[0].name); 139 ASSERT_STREQ("bravo", entries[1].name); 140 ASSERT_STREQ("charlie", entries[2].name); 141 142 qsort(entries, 3, sizeof(s), s::comparator); 143 ASSERT_STREQ("alpha", entries[0].name); 144 ASSERT_STREQ("bravo", entries[1].name); 145 ASSERT_STREQ("charlie", entries[2].name); 146} 147 148static void* TestBug57421_child(void* arg) { 149 pthread_t main_thread = reinterpret_cast<pthread_t>(arg); 150 pthread_join(main_thread, NULL); 151 char* value = getenv("ENVIRONMENT_VARIABLE"); 152 if (value == NULL) { 153 setenv("ENVIRONMENT_VARIABLE", "value", 1); 154 } 155 return NULL; 156} 157 158static void TestBug57421_main() { 159 pthread_t t; 160 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); 161 pthread_exit(NULL); 162} 163 164// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to 165// run this test (which exits normally) in its own process. 166TEST(stdlib_DeathTest, getenv_after_main_thread_exits) { 167 // https://code.google.com/p/android/issues/detail?id=57421 168 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; 169 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); 170} 171 172TEST(stdlib, mkstemp) { 173 TemporaryFile tf; 174 struct stat sb; 175 ASSERT_EQ(0, fstat(tf.fd, &sb)); 176} 177 178TEST(stdlib, mkstemp64) { 179 GenericTemporaryFile<mkstemp64> tf; 180 struct stat64 sb; 181 ASSERT_EQ(0, fstat64(tf.fd, &sb)); 182 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE); 183} 184 185TEST(stdlib, system) { 186 int status; 187 188 status = system("exit 0"); 189 ASSERT_TRUE(WIFEXITED(status)); 190 ASSERT_EQ(0, WEXITSTATUS(status)); 191 192 status = system("exit 1"); 193 ASSERT_TRUE(WIFEXITED(status)); 194 ASSERT_EQ(1, WEXITSTATUS(status)); 195} 196 197TEST(stdlib, atof) { 198 ASSERT_DOUBLE_EQ(1.23, atof("1.23")); 199} 200 201TEST(stdlib, strtod) { 202 ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL)); 203} 204 205TEST(stdlib, strtof) { 206 ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL)); 207} 208 209TEST(stdlib, strtold) { 210 ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL)); 211} 212 213TEST(stdlib, quick_exit) { 214 pid_t pid = fork(); 215 ASSERT_NE(-1, pid) << strerror(errno); 216 217 if (pid == 0) { 218 quick_exit(99); 219 } 220 221 int status; 222 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 223 ASSERT_TRUE(WIFEXITED(status)); 224 ASSERT_EQ(99, WEXITSTATUS(status)); 225} 226 227static int quick_exit_status = 0; 228 229static void quick_exit_1(void) { 230 ASSERT_EQ(quick_exit_status, 0); 231 quick_exit_status = 1; 232} 233 234static void quick_exit_2(void) { 235 ASSERT_EQ(quick_exit_status, 1); 236} 237 238static void not_run(void) { 239 FAIL(); 240} 241 242TEST(stdlib, at_quick_exit) { 243 pid_t pid = fork(); 244 ASSERT_NE(-1, pid) << strerror(errno); 245 246 if (pid == 0) { 247 ASSERT_EQ(at_quick_exit(quick_exit_2), 0); 248 ASSERT_EQ(at_quick_exit(quick_exit_1), 0); 249 atexit(not_run); 250 quick_exit(99); 251 } 252 253 int status; 254 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 255 ASSERT_TRUE(WIFEXITED(status)); 256 ASSERT_EQ(99, WEXITSTATUS(status)); 257} 258 259TEST(unistd, _Exit) { 260 int pid = fork(); 261 ASSERT_NE(-1, pid) << strerror(errno); 262 263 if (pid == 0) { 264 _Exit(99); 265 } 266 267 int status; 268 ASSERT_EQ(pid, waitpid(pid, &status, 0)); 269 ASSERT_TRUE(WIFEXITED(status)); 270 ASSERT_EQ(99, WEXITSTATUS(status)); 271} 272