stdlib_test.cpp revision 877ec6d90418ff1d6597147d355a2229fdffae7e
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 19#include <errno.h> 20#include <libgen.h> 21#include <limits.h> 22#include <pthread.h> 23#include <stdint.h> 24#include <stdlib.h> 25 26TEST(stdlib, drand48) { 27 srand48(0x01020304); 28 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); 29 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); 30 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); 31 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); 32} 33 34TEST(stdlib, lrand48_random_rand) { 35 srand48(0x01020304); 36 EXPECT_EQ(1409163720, lrand48()); 37 EXPECT_EQ(397769746, lrand48()); 38 EXPECT_EQ(902267124, lrand48()); 39 EXPECT_EQ(132366131, lrand48()); 40 41#if __BIONIC__ 42 // On bionic, random(3) is equivalent to lrand48... 43 srandom(0x01020304); 44 EXPECT_EQ(1409163720, random()); 45 EXPECT_EQ(397769746, random()); 46 EXPECT_EQ(902267124, random()); 47 EXPECT_EQ(132366131, random()); 48 49 // ...and rand(3) is the bottom 32 bits. 50 srand(0x01020304); 51 EXPECT_EQ(static_cast<int>(1409163720), rand()); 52 EXPECT_EQ(static_cast<int>(397769746), rand()); 53 EXPECT_EQ(static_cast<int>(902267124), rand()); 54 EXPECT_EQ(static_cast<int>(132366131), rand()); 55#endif 56} 57 58TEST(stdlib, mrand48) { 59 srand48(0x01020304); 60 EXPECT_EQ(-1476639856, mrand48()); 61 EXPECT_EQ(795539493, mrand48()); 62 EXPECT_EQ(1804534249, mrand48()); 63 EXPECT_EQ(264732262, mrand48()); 64} 65 66TEST(stdlib, posix_memalign) { 67 void* p; 68 69 ASSERT_EQ(0, posix_memalign(&p, 512, 128)); 70 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512); 71 free(p); 72 73 // Can't align to a non-power of 2. 74 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128)); 75} 76 77TEST(stdlib, realpath__NULL_filename) { 78 errno = 0; 79 char* p = realpath(NULL, NULL); 80 ASSERT_TRUE(p == NULL); 81 ASSERT_EQ(EINVAL, errno); 82} 83 84TEST(stdlib, realpath__empty_filename) { 85 errno = 0; 86 char* p = realpath("", NULL); 87 ASSERT_TRUE(p == NULL); 88 ASSERT_EQ(ENOENT, errno); 89} 90 91TEST(stdlib, realpath__ENOENT) { 92 errno = 0; 93 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL); 94 ASSERT_TRUE(p == NULL); 95 ASSERT_EQ(ENOENT, errno); 96} 97 98TEST(stdlib, realpath) { 99 // Get the name of this executable. 100 char executable_path[PATH_MAX]; 101 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); 102 ASSERT_NE(rc, -1); 103 executable_path[rc] = '\0'; 104 105 char buf[PATH_MAX + 1]; 106 char* p = realpath("/proc/self/exe", buf); 107 ASSERT_STREQ(executable_path, p); 108 109 p = realpath("/proc/self/exe", NULL); 110 ASSERT_STREQ(executable_path, p); 111 free(p); 112} 113 114TEST(stdlib, qsort) { 115 struct s { 116 char name[16]; 117 static int comparator(const void* lhs, const void* rhs) { 118 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name); 119 } 120 }; 121 s entries[3]; 122 strcpy(entries[0].name, "charlie"); 123 strcpy(entries[1].name, "bravo"); 124 strcpy(entries[2].name, "alpha"); 125 126 qsort(entries, 3, sizeof(s), s::comparator); 127 ASSERT_STREQ("alpha", entries[0].name); 128 ASSERT_STREQ("bravo", entries[1].name); 129 ASSERT_STREQ("charlie", entries[2].name); 130 131 qsort(entries, 3, sizeof(s), s::comparator); 132 ASSERT_STREQ("alpha", entries[0].name); 133 ASSERT_STREQ("bravo", entries[1].name); 134 ASSERT_STREQ("charlie", entries[2].name); 135} 136 137static void* TestBug57421_child(void* arg) { 138 pthread_t main_thread = reinterpret_cast<pthread_t>(arg); 139 pthread_join(main_thread, NULL); 140 char* value = getenv("ENVIRONMENT_VARIABLE"); 141 if (value == NULL) { 142 setenv("ENVIRONMENT_VARIABLE", "value", 1); 143 } 144 return NULL; 145} 146 147static void TestBug57421_main() { 148 pthread_t t; 149 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); 150 pthread_exit(NULL); 151} 152 153// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to 154// run this test (which exits normally) in its own process. 155TEST(stdlib_DeathTest, getenv_after_main_thread_exits) { 156 // https://code.google.com/p/android/issues/detail?id=57421 157 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; 158 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); 159} 160