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