stdlib_test.cpp revision b16b72248bd109b6073df6a45aeffaa69e38cfc6
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 <stdint.h>
21#include <stdlib.h>
22
23TEST(stdlib, drand48) {
24  srand48(0x01020304);
25  EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
26  EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
27  EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
28  EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
29}
30
31TEST(stdlib, lrand48_random_rand) {
32  srand48(0x01020304);
33  EXPECT_EQ(1409163720, lrand48());
34  EXPECT_EQ(397769746, lrand48());
35  EXPECT_EQ(902267124, lrand48());
36  EXPECT_EQ(132366131, lrand48());
37
38#if __BIONIC__
39  // On bionic, random(3) is equivalent to lrand48...
40  srandom(0x01020304);
41  EXPECT_EQ(1409163720, random());
42  EXPECT_EQ(397769746, random());
43  EXPECT_EQ(902267124, random());
44  EXPECT_EQ(132366131, random());
45
46  // ...and rand(3) is the bottom 32 bits.
47  srand(0x01020304);
48  EXPECT_EQ(static_cast<int>(1409163720), rand());
49  EXPECT_EQ(static_cast<int>(397769746), rand());
50  EXPECT_EQ(static_cast<int>(902267124), rand());
51  EXPECT_EQ(static_cast<int>(132366131), rand());
52#endif
53}
54
55TEST(stdlib, mrand48) {
56  srand48(0x01020304);
57  EXPECT_EQ(-1476639856, mrand48());
58  EXPECT_EQ(795539493, mrand48());
59  EXPECT_EQ(1804534249, mrand48());
60  EXPECT_EQ(264732262, mrand48());
61}
62
63TEST(stdlib, posix_memalign) {
64  void* p;
65
66  ASSERT_EQ(0, posix_memalign(&p, 512, 128));
67  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
68  free(p);
69
70  // Can't align to a non-power of 2.
71  ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
72}
73