diff options
author | Elliott Hughes <enh@google.com> | 2012-10-01 13:11:03 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2012-10-01 13:53:41 -0700 |
commit | 774c7f54ff375d71106283d42779b0cc5f238f87 (patch) | |
tree | 3dc911f915d8d120089ca5afd60bb84b6d51e71e /tests/stdlib_test.cpp | |
parent | 5b6346f6d5dca4022fe0044dd2807c19ac596788 (diff) | |
download | android_bionic-774c7f54ff375d71106283d42779b0cc5f238f87.tar.gz android_bionic-774c7f54ff375d71106283d42779b0cc5f238f87.tar.bz2 android_bionic-774c7f54ff375d71106283d42779b0cc5f238f87.zip |
Upgrade to the current NetBSD rand implementation.
Also add basic unit tests.
Change-Id: I7fc7ef61d47c1e8fdf8b8eff67a635220c3afd56
Diffstat (limited to 'tests/stdlib_test.cpp')
-rw-r--r-- | tests/stdlib_test.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp new file mode 100644 index 000000000..a9c62d40a --- /dev/null +++ b/tests/stdlib_test.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stdlib.h> + +TEST(stdlib, drand48) { + srand48(0x01020304); + EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); + EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); + EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); + EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); +} + +TEST(stdlib, lrand48_random_rand) { + srand48(0x01020304); + EXPECT_EQ(1409163720, lrand48()); + EXPECT_EQ(397769746, lrand48()); + EXPECT_EQ(902267124, lrand48()); + EXPECT_EQ(132366131, lrand48()); + +#if __BIONIC__ + // On bionic, random(3) is equivalent to lrand48... + srandom(0x01020304); + EXPECT_EQ(1409163720, random()); + EXPECT_EQ(397769746, random()); + EXPECT_EQ(902267124, random()); + EXPECT_EQ(132366131, random()); + + // ...and rand(3) is the bottom 32 bits. + srand(0x01020304); + EXPECT_EQ(static_cast<int>(1409163720), rand()); + EXPECT_EQ(static_cast<int>(397769746), rand()); + EXPECT_EQ(static_cast<int>(902267124), rand()); + EXPECT_EQ(static_cast<int>(132366131), rand()); +#endif +} + +TEST(stdlib, mrand48) { + srand48(0x01020304); + EXPECT_EQ(-1476639856, mrand48()); + EXPECT_EQ(795539493, mrand48()); + EXPECT_EQ(1804534249, mrand48()); + EXPECT_EQ(264732262, mrand48()); +} |