github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/rand/t-urmui.c (about)

     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.h"
    23  #include "gmp-impl.h"
    24  #include "tests.h"
    25  
    26  /* Expect numbers generated by rstate to obey the limit requested. */
    27  void
    28  check_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  
    66  int
    67  main (int argc, char *argv[])
    68  {
    69    tests_start ();
    70  
    71    call_rand_algs (check_one);
    72  
    73    tests_end ();
    74    exit (0);
    75  }