Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 1 | /* Test gmp_urandomm_ui. |
| 2 | |
| 3 | Copyright 2003, 2005 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of the GNU MP Library test suite. |
| 6 | |
| 7 | The GNU MP Library test suite is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 3 of the License, |
| 10 | or (at your option) any later version. |
| 11 | |
| 12 | The GNU MP Library test suite is distributed in the hope that it will be |
| 13 | useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 15 | Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along with |
| 18 | the 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. */ |
| 26 | void |
| 27 | check_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 | |
| 65 | int |
| 66 | main (int argc, char *argv[]) |
| 67 | { |
| 68 | tests_start (); |
| 69 | |
| 70 | call_rand_algs (check_one); |
| 71 | |
| 72 | tests_end (); |
| 73 | exit (0); |
| 74 | } |