t-urbui.c revision 1.1.1.1.2.1 1 1.1 mrg /* Test gmp_urandomb_ui.
2 1.1 mrg
3 1.1 mrg Copyright 2003, 2005 Free Software Foundation, Inc.
4 1.1 mrg
5 1.1.1.1.2.1 yamt This file is part of the GNU MP Library test suite.
6 1.1 mrg
7 1.1.1.1.2.1 yamt The GNU MP Library test suite is free software; you can redistribute it
8 1.1.1.1.2.1 yamt and/or modify it under the terms of the GNU General Public License as
9 1.1.1.1.2.1 yamt published by the Free Software Foundation; either version 3 of the License,
10 1.1.1.1.2.1 yamt or (at your option) any later version.
11 1.1.1.1.2.1 yamt
12 1.1.1.1.2.1 yamt The GNU MP Library test suite is distributed in the hope that it will be
13 1.1.1.1.2.1 yamt useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1.1.1.2.1 yamt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 1.1.1.1.2.1 yamt Public License for more details.
16 1.1 mrg
17 1.1.1.1.2.1 yamt You should have received a copy of the GNU General Public License along with
18 1.1.1.1.2.1 yamt the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
19 1.1 mrg
20 1.1 mrg #include <stdio.h>
21 1.1 mrg #include <stdlib.h>
22 1.1 mrg #include "gmp.h"
23 1.1 mrg #include "gmp-impl.h"
24 1.1 mrg #include "tests.h"
25 1.1 mrg
26 1.1 mrg /* Expect numbers generated by rstate to obey the number of bits requested.
27 1.1 mrg No point testing bits==BITS_PER_ULONG, since any return is acceptable in
28 1.1 mrg that case. */
29 1.1 mrg void
30 1.1 mrg check_one (const char *name, gmp_randstate_ptr rstate)
31 1.1 mrg {
32 1.1 mrg unsigned long bits, limit, got;
33 1.1 mrg int i;
34 1.1 mrg
35 1.1 mrg for (bits = 0; bits < BITS_PER_ULONG; bits++)
36 1.1 mrg {
37 1.1 mrg /* will demand got < limit */
38 1.1 mrg limit = (1L << bits);
39 1.1 mrg
40 1.1 mrg for (i = 0; i < 5; i++)
41 1.1 mrg {
42 1.1 mrg got = gmp_urandomb_ui (rstate, bits);
43 1.1 mrg if (got >= limit)
44 1.1 mrg {
45 1.1 mrg printf ("Return value out of range:\n");
46 1.1 mrg printf (" algorithm: %s\n", name);
47 1.1 mrg printf (" bits: %lu\n", bits);
48 1.1 mrg printf (" limit: %#lx\n", limit);
49 1.1 mrg printf (" got: %#lx\n", got);
50 1.1 mrg abort ();
51 1.1 mrg }
52 1.1 mrg }
53 1.1 mrg }
54 1.1 mrg }
55 1.1 mrg
56 1.1 mrg int
57 1.1 mrg main (int argc, char *argv[])
58 1.1 mrg {
59 1.1 mrg tests_start ();
60 1.1 mrg
61 1.1 mrg call_rand_algs (check_one);
62 1.1 mrg
63 1.1 mrg tests_end ();
64 1.1 mrg exit (0);
65 1.1 mrg }
66