blob: cc8fedb502cc8d27e4150db000f154dc577a87bb [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* Test gmp_urandomm_ui.
2
3Copyright 2003, 2005 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include "gmp-impl.h"
23#include "tests.h"
24
25/* Expect numbers generated by rstate to obey the limit requested. */
26void
27check_one (const char *name, gmp_randstate_ptr rstate)
28{
29 static const unsigned long n_table[] = {
30 1, 2, 3, 4, 5, 6, 7, 8,
31 123, 456, 789,
32
33 255, 256, 257,
34 1023, 1024, 1025,
35 32767, 32768, 32769,
36
37 ULONG_MAX/2-2, ULONG_MAX/2-1, ULONG_MAX/2, ULONG_MAX/2+1, ULONG_MAX/2+2,
38
39 ULONG_MAX-2, ULONG_MAX-1, ULONG_MAX,
40 };
41
42 unsigned long got, n;
43 int i, j;
44
45 for (i = 0; i < numberof (n_table); i++)
46 {
47 n = n_table[i];
48
49 for (j = 0; j < 5; j++)
50 {
51 got = gmp_urandomm_ui (rstate, n);
52 if (got >= n)
53 {
54 printf ("Return value out of range:\n");
55 printf (" algorithm: %s\n", name);
56 printf (" n: %#lx\n", n);
57 printf (" got: %#lx\n", got);
58 abort ();
59 }
60 }
61 }
62}
63
64
65int
66main (int argc, char *argv[])
67{
68 tests_start ();
69
70 call_rand_algs (check_one);
71
72 tests_end ();
73 exit (0);
74}