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