Merge "Implement rand/srand in terms of random/srandom."

This commit is contained in:
Elliott Hughes 2014-07-14 19:19:58 +00:00 committed by Gerrit Code Review
commit 673bff01ae
4 changed files with 30 additions and 65 deletions

View File

@ -177,6 +177,7 @@ libc_bionic_src_files := \
bionic/pthread_sigmask.cpp \
bionic/ptrace.cpp \
bionic/raise.cpp \
bionic/rand.cpp \
bionic/readlink.cpp \
bionic/reboot.cpp \
bionic/recv.cpp \
@ -294,7 +295,6 @@ libc_upstream_netbsd_src_files := \
upstream-netbsd/lib/libc/stdlib/mrand48.c \
upstream-netbsd/lib/libc/stdlib/nrand48.c \
upstream-netbsd/lib/libc/stdlib/_rand48.c \
upstream-netbsd/lib/libc/stdlib/rand.c \
upstream-netbsd/lib/libc/stdlib/rand_r.c \
upstream-netbsd/lib/libc/stdlib/seed48.c \
upstream-netbsd/lib/libc/stdlib/srand48.c \

29
libc/bionic/rand.cpp Normal file
View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2014 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 <stdlib.h>
// The BSD rand/srand is very weak. glibc just uses random/srandom instead.
// Since we're likely to run code intended for glibc, and POSIX doesn't seem
// to disallow this, we go that route too.
int rand() {
return random();
}
void srand(unsigned int seed) {
return srandom(seed);
}

View File

@ -1,57 +0,0 @@
/* $NetBSD: rand.c,v 1.12 2012/06/25 22:32:45 abs Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93";
#else
__RCSID("$NetBSD: rand.c,v 1.12 2012/06/25 22:32:45 abs Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <stdlib.h>
static u_long next = 1;
int
rand(void)
{
/* LINTED integer overflow */
return (int)((next = next * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
}
void
srand(u_int seed)
{
next = seed;
}

View File

@ -53,17 +53,10 @@ TEST(stdlib, random) {
TEST(stdlib, rand) {
srand(0x01020304);
#if defined(__BIONIC__)
EXPECT_EQ(1675538669, rand());
EXPECT_EQ(1678228258, rand());
EXPECT_EQ(1352350131, rand());
EXPECT_EQ(824068976, rand());
#else
EXPECT_EQ(55436735, rand());
EXPECT_EQ(1399865117, rand());
EXPECT_EQ(2032643283, rand());
EXPECT_EQ(571329216, rand());
#endif
}
TEST(stdlib, mrand48) {