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

     1  /* Test gmp_randclass.
     2  
     3  Copyright 2002, 2003 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 "gmp.h"
    21  #include "gmpxx.h"
    22  #include "gmp-impl.h"
    23  #include "tests.h"
    24  
    25  using namespace std;
    26  
    27  
    28  /* all flavours of initialization */
    29  void
    30  check_randinit (void)
    31  {
    32    {
    33      gmp_randclass r(gmp_randinit_default);
    34    }
    35  
    36    {
    37      mpz_class a(0);
    38      unsigned long c = 0, m2exp = 8;
    39      gmp_randclass r(gmp_randinit_lc_2exp, a, c, m2exp);
    40    }
    41  
    42    {
    43      unsigned long m2exp = 64;
    44      gmp_randclass r(gmp_randinit_lc_2exp_size, m2exp);
    45    }
    46  
    47    /* gmp_randinit_lc_2exp_size, with excessive size */
    48    {
    49      try {
    50        unsigned long m2exp = ULONG_MAX;
    51        gmp_randclass r(gmp_randinit_lc_2exp_size, m2exp);
    52        ASSERT_ALWAYS (0);  /* should not be reached */
    53      } catch (length_error) {
    54      }
    55    }
    56  
    57    {
    58      gmp_randclass r(gmp_randinit_mt);
    59    }
    60  
    61    /* obsolete, but still available */
    62    {
    63      gmp_randalg_t alg = GMP_RAND_ALG_LC;
    64      unsigned long m2exp = 64;
    65      gmp_randclass r(alg, m2exp);
    66    }
    67    {
    68      gmp_randalg_t alg = GMP_RAND_ALG_DEFAULT;
    69      unsigned long m2exp = 64;
    70      gmp_randclass r(alg, m2exp);
    71    }
    72    {
    73      gmp_randalg_t alg = (gmp_randalg_t) 0;
    74      unsigned long m2exp = 64;
    75      gmp_randclass r(alg, m2exp);
    76    }
    77  }
    78  
    79  void
    80  check_mpz (void)
    81  {
    82    {
    83      gmp_randclass r(gmp_randinit_default);
    84      mpz_class a(123);
    85      unsigned int b = 256;
    86      mpz_class c;
    87      r.seed(a);
    88      c = r.get_z_bits(b);
    89    }
    90    {
    91      gmp_randclass r(gmp_randinit_default);
    92      mpz_class a(256);
    93      unsigned long b = 123;
    94      mpz_class c;
    95      r.seed(b);
    96      c = r.get_z_bits(a);
    97    }
    98    {
    99      gmp_randclass r(gmp_randinit_default);
   100      mpz_class a(123), b(256);
   101      mpz_class c;
   102      r.seed(a);
   103      c = r.get_z_range(b);
   104    }
   105  }
   106  
   107  void
   108  check_mpf (void)
   109  {
   110    {
   111      gmp_randclass r(gmp_randinit_default);
   112      mpz_class a(123);
   113      r.seed(a);
   114      mpf_class b;
   115      b = r.get_f();
   116      mpf_class c(r.get_f());
   117      ASSERT_ALWAYS (c.get_prec() == mpf_get_default_prec());
   118      mpf_class d(r.get_f(),212);
   119      ASSERT_ALWAYS (d.get_prec() >= 212);
   120    }
   121    {
   122      gmp_randclass r(gmp_randinit_default);
   123      int a = 123, b = 198;
   124      r.seed(a);
   125      mpf_class c;
   126      c = r.get_f(b);
   127      ASSERT_ALWAYS (c.get_prec() == mpf_get_default_prec());
   128      mpf_class d(r.get_f(b));
   129      ASSERT_ALWAYS (d.get_prec() >= 198);
   130      mpf_class e(r.get_f(b)-r.get_f());
   131      ASSERT_ALWAYS (e.get_prec() >= 198);
   132      mpf_class f(r.get_f(60),300);
   133      ASSERT_ALWAYS (f.get_prec() >= 300);
   134    }
   135  }
   136  
   137  
   138  int
   139  main (void)
   140  {
   141    tests_start();
   142  
   143    check_randinit();
   144    check_mpz();
   145    check_mpf();
   146  
   147    tests_end();
   148    return 0;
   149  }