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