stdlib_test.cpp revision f0777843c03deb26b1f78c8edd17c557041696e9
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 <stdint.h>
23#include <stdlib.h>
24
25TEST(stdlib, drand48) {
26  srand48(0x01020304);
27  EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
28  EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
29  EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
30  EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
31}
32
33TEST(stdlib, lrand48_random_rand) {
34  srand48(0x01020304);
35  EXPECT_EQ(1409163720, lrand48());
36  EXPECT_EQ(397769746, lrand48());
37  EXPECT_EQ(902267124, lrand48());
38  EXPECT_EQ(132366131, lrand48());
39
40#if __BIONIC__
41  // On bionic, random(3) is equivalent to lrand48...
42  srandom(0x01020304);
43  EXPECT_EQ(1409163720, random());
44  EXPECT_EQ(397769746, random());
45  EXPECT_EQ(902267124, random());
46  EXPECT_EQ(132366131, random());
47
48  // ...and rand(3) is the bottom 32 bits.
49  srand(0x01020304);
50  EXPECT_EQ(static_cast<int>(1409163720), rand());
51  EXPECT_EQ(static_cast<int>(397769746), rand());
52  EXPECT_EQ(static_cast<int>(902267124), rand());
53  EXPECT_EQ(static_cast<int>(132366131), rand());
54#endif
55}
56
57TEST(stdlib, mrand48) {
58  srand48(0x01020304);
59  EXPECT_EQ(-1476639856, mrand48());
60  EXPECT_EQ(795539493, mrand48());
61  EXPECT_EQ(1804534249, mrand48());
62  EXPECT_EQ(264732262, mrand48());
63}
64
65TEST(stdlib, posix_memalign) {
66  void* p;
67
68  ASSERT_EQ(0, posix_memalign(&p, 512, 128));
69  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
70  free(p);
71
72  // Can't align to a non-power of 2.
73  ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
74}
75
76TEST(stdlib, realpath__NULL_filename) {
77  errno = 0;
78  char* p = realpath(NULL, NULL);
79  ASSERT_TRUE(p == NULL);
80  ASSERT_EQ(EINVAL, errno);
81}
82
83TEST(stdlib, realpath__empty_filename) {
84  errno = 0;
85  char* p = realpath("", NULL);
86  ASSERT_TRUE(p == NULL);
87  ASSERT_EQ(ENOENT, errno);
88}
89
90TEST(stdlib, realpath__ENOENT) {
91  errno = 0;
92  char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
93  ASSERT_TRUE(p == NULL);
94  ASSERT_EQ(ENOENT, errno);
95}
96
97TEST(stdlib, realpath) {
98  // Get the name of this executable.
99  char executable_path[PATH_MAX];
100  int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
101  ASSERT_NE(rc, -1);
102  executable_path[rc] = '\0';
103
104  char buf[PATH_MAX + 1];
105  char* p = realpath("/proc/self/exe", buf);
106  ASSERT_STREQ(executable_path, p);
107
108  p = realpath("/proc/self/exe", NULL);
109  ASSERT_STREQ(executable_path, p);
110  free(p);
111}
112