blob: 0bfb0c38f425d3a93538e16b04dacc7d982df5a5 [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 Hughes774c7f52012-10-01 13:11:03 -070027
28TEST(stdlib, drand48) {
29 srand48(0x01020304);
30 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
31 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
32 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
33 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
34}
35
36TEST(stdlib, lrand48_random_rand) {
37 srand48(0x01020304);
38 EXPECT_EQ(1409163720, lrand48());
39 EXPECT_EQ(397769746, lrand48());
40 EXPECT_EQ(902267124, lrand48());
41 EXPECT_EQ(132366131, lrand48());
42
43#if __BIONIC__
44 // On bionic, random(3) is equivalent to lrand48...
45 srandom(0x01020304);
46 EXPECT_EQ(1409163720, random());
47 EXPECT_EQ(397769746, random());
48 EXPECT_EQ(902267124, random());
49 EXPECT_EQ(132366131, random());
50
51 // ...and rand(3) is the bottom 32 bits.
52 srand(0x01020304);
53 EXPECT_EQ(static_cast<int>(1409163720), rand());
54 EXPECT_EQ(static_cast<int>(397769746), rand());
55 EXPECT_EQ(static_cast<int>(902267124), rand());
56 EXPECT_EQ(static_cast<int>(132366131), rand());
57#endif
58}
59
60TEST(stdlib, mrand48) {
61 srand48(0x01020304);
62 EXPECT_EQ(-1476639856, mrand48());
63 EXPECT_EQ(795539493, mrand48());
64 EXPECT_EQ(1804534249, mrand48());
65 EXPECT_EQ(264732262, mrand48());
66}
Elliott Hughesb16b7222013-02-04 13:18:00 -080067
68TEST(stdlib, posix_memalign) {
69 void* p;
70
71 ASSERT_EQ(0, posix_memalign(&p, 512, 128));
72 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
73 free(p);
74
75 // Can't align to a non-power of 2.
76 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
77}
Elliott Hughesf0777842013-03-01 16:59:46 -080078
79TEST(stdlib, realpath__NULL_filename) {
80 errno = 0;
81 char* p = realpath(NULL, NULL);
82 ASSERT_TRUE(p == NULL);
83 ASSERT_EQ(EINVAL, errno);
84}
85
86TEST(stdlib, realpath__empty_filename) {
87 errno = 0;
88 char* p = realpath("", NULL);
89 ASSERT_TRUE(p == NULL);
90 ASSERT_EQ(ENOENT, errno);
91}
92
93TEST(stdlib, realpath__ENOENT) {
94 errno = 0;
95 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
96 ASSERT_TRUE(p == NULL);
97 ASSERT_EQ(ENOENT, errno);
98}
99
100TEST(stdlib, realpath) {
101 // Get the name of this executable.
102 char executable_path[PATH_MAX];
103 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
104 ASSERT_NE(rc, -1);
105 executable_path[rc] = '\0';
106
107 char buf[PATH_MAX + 1];
108 char* p = realpath("/proc/self/exe", buf);
109 ASSERT_STREQ(executable_path, p);
110
111 p = realpath("/proc/self/exe", NULL);
112 ASSERT_STREQ(executable_path, p);
113 free(p);
114}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700115
116TEST(stdlib, qsort) {
117 struct s {
118 char name[16];
119 static int comparator(const void* lhs, const void* rhs) {
120 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
121 }
122 };
123 s entries[3];
124 strcpy(entries[0].name, "charlie");
125 strcpy(entries[1].name, "bravo");
126 strcpy(entries[2].name, "alpha");
127
128 qsort(entries, 3, sizeof(s), s::comparator);
129 ASSERT_STREQ("alpha", entries[0].name);
130 ASSERT_STREQ("bravo", entries[1].name);
131 ASSERT_STREQ("charlie", entries[2].name);
132
133 qsort(entries, 3, sizeof(s), s::comparator);
134 ASSERT_STREQ("alpha", entries[0].name);
135 ASSERT_STREQ("bravo", entries[1].name);
136 ASSERT_STREQ("charlie", entries[2].name);
137}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800138
139static void* TestBug57421_child(void* arg) {
140 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
141 pthread_join(main_thread, NULL);
142 char* value = getenv("ENVIRONMENT_VARIABLE");
143 if (value == NULL) {
144 setenv("ENVIRONMENT_VARIABLE", "value", 1);
145 }
146 return NULL;
147}
148
149static void TestBug57421_main() {
150 pthread_t t;
151 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
152 pthread_exit(NULL);
153}
154
155// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
156// run this test (which exits normally) in its own process.
157TEST(stdlib_DeathTest, getenv_after_main_thread_exits) {
158 // https://code.google.com/p/android/issues/detail?id=57421
159 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
160 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
161}
Calin Juravlefe317a32014-02-21 15:11:03 +0000162
163TEST(stdlib, mkstemp) {
164 TemporaryFile tf;
165 struct stat sb;
166 ASSERT_EQ(0, fstat(tf.fd, &sb));
167}
168
169TEST(stdlib, mkstemp64) {
170 GenericTemporaryFile<mkstemp64> tf;
171 struct stat64 sb;
172 ASSERT_EQ(0, fstat64(tf.fd, &sb));
173 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
174}
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700175
176TEST(stdlib, system) {
177 int status;
178
179 status = system("exit 0");
180 ASSERT_TRUE(WIFEXITED(status));
181 ASSERT_EQ(0, WEXITSTATUS(status));
182
183 status = system("exit 1");
184 ASSERT_TRUE(WIFEXITED(status));
185 ASSERT_EQ(1, WEXITSTATUS(status));
186}