aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mini-gmp/tests/t-signed.c
blob: cfa40d7176a0f5d5666019fe6ba3015f6a980867 (plain) (blame)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Exercise some mpz_..._si functions.

Copyright 2013, 2016, 2020 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 <limits.h>
#include <stdio.h>
#include <stdlib.h>

#include "testutils.h"

/* Always called with sz fitting in a signed long, and si is the
   corresponding value. */
int
check_si (const mpz_t sz, long si)
{
  mpz_t t;

  /* Checks on sz/si */
  if ((mpz_cmp_si (sz, si)) != 0)
    {
      printf ("mpz_cmp_si (sz, %ld) != 0.\n", si);
      return 0;
    }
  if (mpz_get_si (sz) != si)
    {
      printf ("mpz_get_si (sz) != %ld.\n", si);
      return 0;
    }

  mpz_init_set_si (t, si);

  if (mpz_cmp (t, sz) != 0)
    {
      printf ("mpz_init_set_si (%ld) failed.\n", si);
      printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n");
      return 0;
    }

  mpz_clear (t);
  return 1;
}

/* Called with mpz_cmp (sz, oz) == c. If sz fits in a signed long,
   si is the coresponding value, and similarly for oz and oi. */
void
check_si_cmp (const mpz_t sz, const mpz_t oz, long si, long oi, int c)
{
  if (mpz_cmp (sz, oz) != c)
    {
      printf ("mpz_cmp (sz, oz) != %i.\n", c);
      goto fail;
    }

  if (mpz_fits_slong_p (sz))
    {
      if (!check_si (sz, si))
	goto fail;
      if (mpz_cmp_si (oz, si) != -c)
	{
	  printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c);
	  goto fail;
	}
    }
  else
    {
      if (mpz_cmp_si (sz, si) != c)
	{
	  printf ("mpz_cmp_si (sz, %ld) != %i.\n", si, c);
	  goto fail;
	}
      if (mpz_cmp_si (sz, -c) != c)
	{
	  printf ("mpz_cmp_si (sz, %i) != %i.\n", -c, c);
	  goto fail;
	}
    }
  if (mpz_fits_slong_p (oz))
    {
      if (!check_si (oz, oi))
	goto fail;
      if (mpz_cmp_si (sz, oi) != c)
	{
	  printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c);
	  goto fail;
	}
    }
  return;

 fail:
  printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
  printf (" si=%ld\n", si);
  printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
  printf (" oi=%ld\n", si);
  abort ();
}

void
try_op_si (int c)
{
  long  si, oi;
  mpz_t sz, oz;
  unsigned overflow_count;

  si = c;
  mpz_init_set_si (sz, si);

  oi = si;
  mpz_init_set (oz, sz);

  /* To get a few tests with operands straddling the border, don't
     stop at the very first operand exceeding a signed long. */
  for (overflow_count = 0; overflow_count < 10; )
    {
      /* c * 2^k */
      mpz_mul_2exp (sz, sz, 1);
      if (mpz_fits_slong_p (sz))
	si *= 2;
      else
	overflow_count++;

      check_si_cmp (sz, oz, si, oi, c);

      /* c * (2^k + 1) */
      if (c == -1)
	mpz_sub_ui (oz, sz, 1);
      else
	mpz_add_ui (oz, sz, 1);
      if (mpz_fits_slong_p (oz))
	oi = si + c;
      else
	overflow_count++;
      check_si_cmp (oz, sz, oi, si, c);

      /* c * (2^K - 1) */
      mpz_mul_si (oz, sz, 2*c);
      if (c == -1)
	mpz_ui_sub (oz, 1, oz); /* oz = sz * 2 + 1 */
      else
	mpz_sub_ui (oz, oz, 1); /* oz = sz * 2 - 1 */
      if (mpz_fits_slong_p (oz))
	oi = (si - c) * 2 + c;
      else
	overflow_count++;

      check_si_cmp (oz, sz, oi, si, c);
    };

  mpz_clear (sz);
  mpz_clear (oz);
}

void
try_fits_slong_p (void)
{
  mpz_t x;
  mpz_init_set_si (x, LONG_MAX);
  if (!mpz_fits_slong_p (x))
    {
      printf ("mpz_fits_slong_p (LONG_MAX) false!\n");
      abort ();
    }
  mpz_add_ui (x, x, 1);
  if (mpz_fits_slong_p (x))
    {
      printf ("mpz_fits_slong_p (LONG_MAX + 1) true!\n");
      abort ();
    }
  mpz_set_si (x, LONG_MIN);
  if (!mpz_fits_slong_p (x))
    {
      printf ("mpz_fits_slong_p (LONG_MIN) false!\n");
      abort ();
    }
  mpz_sub_ui (x, x, 1);
  if (mpz_fits_slong_p (x))
    {
      printf ("mpz_fits_slong_p (LONG_MIN - 1) true!\n");
      abort ();
    }

  mpz_clear (x);
}

void
try_fits_utype_p (void)
{
  mpz_t x;
  mpz_init (x);
  if (!mpz_fits_ulong_p (x))
    {
      printf ("mpz_fits_ulong_p (0) false!\n");
      abort ();
    }
  if (!mpz_fits_uint_p (x))
    {
      printf ("mpz_fits_uint_p (0) false!\n");
      abort ();
    }
  if (!mpz_fits_ushort_p (x))
    {
      printf ("mpz_fits_udhort_p (0) false!\n");
      abort ();
    }
  mpz_set_si (x, -1);
  if (mpz_fits_ulong_p (x))
    {
      printf ("mpz_fits_ulong_p (- 1) true!\n");
      abort ();
    }
  if (mpz_fits_uint_p (x))
    {
      printf ("mpz_fits_uint_p (- 1) true!\n");
      abort ();
    }
  if (mpz_fits_ushort_p (x))
    {
      printf ("mpz_fits_ushort_p (- 1) true!\n");
      abort ();
    }
  mpz_set_ui (x, ULONG_MAX);
  if (!mpz_fits_ulong_p (x))
    {
      printf ("mpz_fits_ulong_p (ULONG_MAX) false!\n");
      abort ();
    }
  mpz_add_ui (x, x, 1);
  if (mpz_fits_ulong_p (x))
    {
      printf ("mpz_fits_ulong_p (ULONG_MAX + 1) true!\n");
      abort ();
    }
  mpz_set_ui (x, UINT_MAX);
  if (!mpz_fits_uint_p (x))
    {
      printf ("mpz_fits_uint_p (UINT_MAX) false!\n");
      abort ();
    }
  mpz_add_ui (x, x, 1);
  if (mpz_fits_uint_p (x))
    {
      printf ("mpz_fits_uint_p (UINT_MAX + 1) true!\n");
      abort ();
    }
  mpz_set_ui (x, USHRT_MAX);
  if (!mpz_fits_ushort_p (x))
    {
      printf ("mpz_fits_ushort_p (USHRT_MAX) false!\n");
      abort ();
    }
  mpz_add_ui (x, x, 1);
  if (mpz_fits_ushort_p (x))
    {
      printf ("mpz_fits_ushort_p (USHRT_MAX + 1) true!\n");
      abort ();
    }

  mpz_clear (x);
}

void
try_fits_sint_p (void)
{
  mpz_t x;
  mpz_init_set_si (x, INT_MAX);
  if (!mpz_fits_sint_p (x))
    {
      printf ("mpz_fits_sint_p (INT_MAX) false!\n");
      abort ();
    }
  mpz_add_ui (x, x, 1);
  if (mpz_fits_sint_p (x))
    {
      printf ("mpz_fits_sint_p (INT_MAX + 1) true!\n");
      abort ();
    }
  mpz_set_si (x, INT_MIN);
  if (!mpz_fits_sint_p (x))
    {
      printf ("mpz_fits_sint_p (INT_MIN) false!\n");
      abort ();
    }
  mpz_sub_ui (x, x, 1);
  if (mpz_fits_sint_p (x))
    {
      printf ("mpz_fits_sint_p (INT_MIN - 1) true!\n");
      abort ();
    }

  mpz_clear (x);
}

void
try_fits_sshort_p (void)
{
  mpz_t x;
  mpz_init_set_si (x, SHRT_MAX);
  if (!mpz_fits_sshort_p (x))
    {
      printf ("mpz_fits_sshort_p (SHRT_MAX) false!\n");
      abort ();
    }
  mpz_add_ui (x, x, 1);
  if (mpz_fits_sshort_p (x))
    {
      printf ("mpz_fits_sshort_p (SHRT_MAX + 1) true!\n");
      abort ();
    }
  mpz_set_si (x, SHRT_MIN);
  if (!mpz_fits_sshort_p (x))
    {
      printf ("mpz_fits_sshort_p (SHRT_MIN) false!\n");
      abort ();
    }
  mpz_sub_ui (x, x, 1);
  if (mpz_fits_sshort_p (x))
    {
      printf ("mpz_fits_sshort_p (SHRT_MIN - 1) true!\n");
      abort ();
    }

  mpz_clear (x);
}

void
testmain (int argc, char *argv[])
{
  try_fits_slong_p ();
  try_fits_sint_p ();
  try_fits_sshort_p ();
  try_fits_utype_p ();
  try_op_si (-1);
  try_op_si (1);
}