This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH][BZ 14561] Testcase
- From: "Carlos O'Donell" <carlos at redhat dot com>
- To: OndÅej BÃlka <neleai at seznam dot cz>
- Cc: libc-alpha at sourceware dot org
- Date: Tue, 04 Jun 2013 09:08:17 -0400
- 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> <20130603204113 dot GA26974 at domone dot kolej dot mff dot cuni dot cz>
On 06/03/2013 04:41 PM, OndÅej BÃlka wrote:
> 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.
This is a good first pass, but we need to make this easier to debug
when it fails and when we switch to PASS/FAIL test we should be able
to explain which ordering failed.
> ---
> 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. */
This comment needs to clearly explain the way in which the
interleaving is done and how many patterns are generated.
> + 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;
Each of the `return 1'; needs to be able to print the ordering
which lead to the failure (since it might be intermittent).
If that's too difficult to do then I suggest manually (using
a script) unrolling this loop into a static ordering that can
use distinct messages for each failure state.
Feel free to compact the error message printout to something
like: "FAIL: Order was rRr.\n"
> + 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"
>
Cheers,
Carlos.