summaryrefslogtreecommitdiff
path: root/tests/cxx/t-rand.cc
blob: d336d084d7c6ff5326cf3d767e1c0c20a8c8c1b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Test gmp_randclass.

Copyright 2002, 2003 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include "gmpxx.h"
#include "gmp-impl.h"
#include "tests.h"

using namespace std;


/* all flavours of initialization */
void
check_randinit (void)
{
  {
    gmp_randclass r(gmp_randinit_default);
  }

  {
    mpz_class a(0);
    unsigned long c = 0, m2exp = 8;
    gmp_randclass r(gmp_randinit_lc_2exp, a, c, m2exp);
  }

  {
    unsigned long m2exp = 64;
    gmp_randclass r(gmp_randinit_lc_2exp_size, m2exp);
  }

  /* gmp_randinit_lc_2exp_size, with excessive size */
  {
    try {
      unsigned long m2exp = ULONG_MAX;
      gmp_randclass r(gmp_randinit_lc_2exp_size, m2exp);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (length_error&) {
    }
  }

  {
    gmp_randclass r(gmp_randinit_mt);
  }

  /* obsolete, but still available */
  {
    gmp_randalg_t alg = GMP_RAND_ALG_LC;
    unsigned long m2exp = 64;
    gmp_randclass r(alg, m2exp);
  }
  {
    gmp_randalg_t alg = GMP_RAND_ALG_DEFAULT;
    unsigned long m2exp = 64;
    gmp_randclass r(alg, m2exp);
  }
  {
    gmp_randalg_t alg = (gmp_randalg_t) 0;
    unsigned long m2exp = 64;
    gmp_randclass r(alg, m2exp);
  }
}

void
check_mpz (void)
{
  {
    gmp_randclass r(gmp_randinit_default);
    mpz_class a(123);
    unsigned int b = 256;
    mpz_class c;
    r.seed(a);
    c = r.get_z_bits(b);
  }
  {
    gmp_randclass r(gmp_randinit_default);
    mpz_class a(256);
    unsigned long b = 123;
    mpz_class c;
    r.seed(b);
    c = r.get_z_bits(a);
  }
  {
    gmp_randclass r(gmp_randinit_default);
    mpz_class a(123), b(256);
    mpz_class c;
    r.seed(a);
    c = r.get_z_range(b);
  }
}

void
check_mpf (void)
{
  {
    gmp_randclass r(gmp_randinit_default);
    mpz_class a(123);
    r.seed(a);
    mpf_class b;
    b = r.get_f();
    mpf_class c(r.get_f());
    ASSERT_ALWAYS (c.get_prec() == mpf_get_default_prec());
    mpf_class d(r.get_f(),212);
    ASSERT_ALWAYS (d.get_prec() >= 212);
  }
  {
    gmp_randclass r(gmp_randinit_default);
    int a = 123, b = 198;
    r.seed(a);
    mpf_class c;
    c = r.get_f(b);
    ASSERT_ALWAYS (c.get_prec() == mpf_get_default_prec());
    mpf_class d(r.get_f(b));
    ASSERT_ALWAYS (d.get_prec() >= 198);
    mpf_class e(r.get_f(b)-r.get_f());
    ASSERT_ALWAYS (e.get_prec() >= 198);
    mpf_class f(r.get_f(60),300);
    ASSERT_ALWAYS (f.get_prec() >= 300);
  }
}


int
main (void)
{
  tests_start();

  check_randinit();
  check_mpz();
  check_mpf();

  tests_end();
  return 0;
}