blob: 7c86d76bafd68598b477fe17fcba319bdecc043b [file] [log] [blame]
Elliott Hughes774c7f52012-10-01 13:11:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
Calin Juravlefe317a32014-02-21 15:11:03 +000018#include "TemporaryFile.h"
Elliott Hughes774c7f52012-10-01 13:11:03 -070019
Elliott Hughesb16b7222013-02-04 13:18:00 -080020#include <errno.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080021#include <libgen.h>
22#include <limits.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080023#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080024#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070025#include <stdlib.h>
Calin Juravlefe317a32014-02-21 15:11:03 +000026#include <fcntl.h>
Elliott Hughes40488562014-03-12 13:50:38 -070027#include <sys/types.h>
28#include <sys/wait.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070029
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
Elliott Hughesa0beeea2014-06-12 11:48:04 -070038TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070039 srand48(0x01020304);
40 EXPECT_EQ(1409163720, lrand48());
41 EXPECT_EQ(397769746, lrand48());
42 EXPECT_EQ(902267124, lrand48());
43 EXPECT_EQ(132366131, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -070044}
Elliott Hughes774c7f52012-10-01 13:11:03 -070045
Elliott Hughesa0beeea2014-06-12 11:48:04 -070046TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070047 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -070048 EXPECT_EQ(55436735, random());
49 EXPECT_EQ(1399865117, random());
50 EXPECT_EQ(2032643283, random());
51 EXPECT_EQ(571329216, random());
52}
Elliott Hughes774c7f52012-10-01 13:11:03 -070053
Elliott Hughesa0beeea2014-06-12 11:48:04 -070054TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070055 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -070056#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());
Elliott Hughes774c7f52012-10-01 13:11:03 -070066#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}
Elliott Hughesb16b7222013-02-04 13:18:00 -080076
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}
Elliott Hughesf0777842013-03-01 16:59:46 -080087
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}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700124
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}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800147
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}
Calin Juravlefe317a32014-02-21 15:11:03 +0000171
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}
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700184
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}
Elliott Hughes5a817382014-03-12 16:12:57 -0700196
197TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700198 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700199}
200
201TEST(stdlib, strtod) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700202 ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700203}
204
205TEST(stdlib, strtof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700206 ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700207}
208
209TEST(stdlib, strtold) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700210 ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700211}
Elliott Hughes9f525642014-04-08 17:14:01 -0700212
Dan Albertb8425c52014-04-29 17:49:06 -0700213TEST(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
Elliott Hughes9f525642014-04-08 17:14:01 -0700259TEST(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}