blob: e5d78122c074d0f7353043cecbc457de55aeaf40 [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>
18
Elliott Hughesb16b7222013-02-04 13:18:00 -080019#include <errno.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080020#include <libgen.h>
21#include <limits.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080022#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070023#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}
Elliott Hughesb16b7222013-02-04 13:18:00 -080064
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}
Elliott Hughesf0777842013-03-01 16:59:46 -080075
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}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700112
113TEST(stdlib, qsort) {
114 struct s {
115 char name[16];
116 static int comparator(const void* lhs, const void* rhs) {
117 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
118 }
119 };
120 s entries[3];
121 strcpy(entries[0].name, "charlie");
122 strcpy(entries[1].name, "bravo");
123 strcpy(entries[2].name, "alpha");
124
125 qsort(entries, 3, sizeof(s), s::comparator);
126 ASSERT_STREQ("alpha", entries[0].name);
127 ASSERT_STREQ("bravo", entries[1].name);
128 ASSERT_STREQ("charlie", entries[2].name);
129
130 qsort(entries, 3, sizeof(s), s::comparator);
131 ASSERT_STREQ("alpha", entries[0].name);
132 ASSERT_STREQ("bravo", entries[1].name);
133 ASSERT_STREQ("charlie", entries[2].name);
134}