stdlib_test.cpp revision 774c7f54ff375d71106283d42779b0cc5f238f87
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 <stdlib.h>
20
21TEST(stdlib, drand48) {
22  srand48(0x01020304);
23  EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
24  EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
25  EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
26  EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
27}
28
29TEST(stdlib, lrand48_random_rand) {
30  srand48(0x01020304);
31  EXPECT_EQ(1409163720, lrand48());
32  EXPECT_EQ(397769746, lrand48());
33  EXPECT_EQ(902267124, lrand48());
34  EXPECT_EQ(132366131, lrand48());
35
36#if __BIONIC__
37  // On bionic, random(3) is equivalent to lrand48...
38  srandom(0x01020304);
39  EXPECT_EQ(1409163720, random());
40  EXPECT_EQ(397769746, random());
41  EXPECT_EQ(902267124, random());
42  EXPECT_EQ(132366131, random());
43
44  // ...and rand(3) is the bottom 32 bits.
45  srand(0x01020304);
46  EXPECT_EQ(static_cast<int>(1409163720), rand());
47  EXPECT_EQ(static_cast<int>(397769746), rand());
48  EXPECT_EQ(static_cast<int>(902267124), rand());
49  EXPECT_EQ(static_cast<int>(132366131), rand());
50#endif
51}
52
53TEST(stdlib, mrand48) {
54  srand48(0x01020304);
55  EXPECT_EQ(-1476639856, mrand48());
56  EXPECT_EQ(795539493, mrand48());
57  EXPECT_EQ(1804534249, mrand48());
58  EXPECT_EQ(264732262, mrand48());
59}
60