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