This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH][BZ 14561] Testcase
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Carlos O'Donell <carlos at redhat dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Mon, 3 Jun 2013 22:41:13 +0200
- Subject: Re: [PATCH][BZ 14561] Testcase
- References: <20130520190628 dot GA22088 at domone dot kolej dot mff dot cuni dot cz> <519E93F3 dot 8060203 at redhat dot com>
On Thu, May 23, 2013 at 06:10:59PM -0400, Carlos O'Donell wrote:
> On 05/20/2013 03:06 PM, OndÅej BÃlka wrote:
> > Hi,
> > Functions rand calls random which causes seed to be shared between them.
> >
> > This patch causes rand and random use separate seeds.
> >
> > Comments?
> >
> > Ondra
> >
> > * stdlib/rand.c: Include stdlib/random.c
> > * stdlib/random.c: Separate rand and random.
>
> This is user visible change and needs a BZ#.
>
Its 14561.
> This also needs a test case similar to what Rich posted
> with a comment indicating the rationale from the standards
> perspective.
>
> Repost with test, BZ#, and fixes below.
>
As testcases can be written independently I decided to post testcase
separetely
Ondra
* bug-srand-srandom-dependency.c: New file.
* Makefile (tests) : Add bug-srand-srandom-dependency.
---
stdlib/Makefile | 3 +-
stdlib/bug-srand-srandom-dependency.c | 65 +++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 1 deletion(-)
create mode 100644 stdlib/bug-srand-srandom-dependency.c
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 17d80e0..d635371 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -71,7 +71,8 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
tst-makecontext2 tst-strtod6 tst-unsetenv1 \
tst-makecontext3 bug-getcontext bug-fmtmsg1 \
tst-secure-getenv tst-strtod-overflow tst-strtod-round \
- tst-tininess tst-strtod-underflow tst-tls-atexit
+ tst-tininess tst-strtod-underflow tst-tls-atexit \
+ bug-srand-srandom-dependency
tests-static := tst-secure-getenv
modules-names = tst-tls-atexit-lib
diff --git a/stdlib/bug-srand-srandom-dependency.c b/stdlib/bug-srand-srandom-dependency.c
new file mode 100644
index 0000000..42ed02f
--- /dev/null
+++ b/stdlib/bug-srand-srandom-dependency.c
@@ -0,0 +1,65 @@
+/* Test that srand and srandom do not interact.
+ Copyright (C) 2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+
+
+#include <stdlib.h>
+
+int
+do_test (int argc, char **argv)
+{
+ int rand1, rand2;
+ int random1, random2;
+ srand (43);
+ rand1 = rand ();
+ rand2 = rand ();
+ srandom (42);
+ random1 = random ();
+ random2 = random ();
+
+ /* We enumerate all combinations how rand and random
+ can be interleaved and test independency. */
+ for (int com = 0; com < 64; com++)
+ {
+ int randstate = 0, randomstate = 0;
+ for (int i = 0; i < 6; i++)
+ if (com & (1 << i))
+ {
+ if (randstate == 0)
+ srand (43);
+ if (randstate == 1 && rand1 != rand ())
+ return 1;
+ if (randstate == 2 && rand2 != rand ())
+ return 1;
+ randstate++;
+ }
+ else
+ {
+ if (randomstate == 0)
+ srandom (42);
+ if (randomstate == 1 && random1 != random ())
+ return 1;
+ if (randomstate == 2 && random2 != random ())
+ return 1;
+ randomstate++;
+ }
+ }
+ return 0;
+}
+
+#include "../test-skeleton.c"
--
1.7.10.4