1 /* Test that routines allow reusing a source variable as destination.
3 Test all relevant functions except:
7 mpz_addmul_ui (should this really allow a+=a*c?)
9 Copyright 1996, 1999, 2000, 2001, 2002, 2009 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21 License for more details.
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
36 /* FIXME: When linking to a DLL libgmp, mpz_add etc can't be used as
37 initializers for global variables because they're effectively global
38 variables (function pointers) themselves. Perhaps calling a test
39 function successively with mpz_add etc would be better. */
44 printf ("Test suppressed for windows DLL\n");
49 #else /* ! DLL_EXPORT */
51 void dump __GMP_PROTO ((char *, mpz_t, mpz_t, mpz_t));
53 typedef void (*dss_func) __GMP_PROTO ((mpz_ptr, mpz_srcptr, mpz_srcptr));
54 typedef void (*dsi_func) __GMP_PROTO ((mpz_ptr, mpz_srcptr, unsigned long int));
55 typedef unsigned long int (*dsi_div_func) __GMP_PROTO ((mpz_ptr, mpz_srcptr, unsigned long int));
56 typedef unsigned long int (*ddsi_div_func) __GMP_PROTO ((mpz_ptr, mpz_ptr, mpz_srcptr, unsigned long int));
57 typedef void (*ddss_div_func) __GMP_PROTO ((mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr));
58 typedef void (*ds_func) __GMP_PROTO ((mpz_ptr, mpz_srcptr));
62 mpz_xinvert (mpz_ptr r, mpz_srcptr a, mpz_srcptr b)
65 res = mpz_invert (r, a, b);
70 dss_func dss_funcs[] =
72 mpz_add, mpz_sub, mpz_mul,
73 mpz_cdiv_q, mpz_cdiv_r, mpz_fdiv_q, mpz_fdiv_r, mpz_tdiv_q, mpz_tdiv_r,
75 mpz_gcd, mpz_lcm, mpz_and, mpz_ior, mpz_xor
77 char *dss_func_names[] =
79 "mpz_add", "mpz_sub", "mpz_mul",
80 "mpz_cdiv_q", "mpz_cdiv_r", "mpz_fdiv_q", "mpz_fdiv_r", "mpz_tdiv_q", "mpz_tdiv_r",
82 "mpz_gcd", "mpz_lcm", "mpz_and", "mpz_ior", "mpz_xor"
84 char dss_func_division[] = {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0};
86 dsi_func dsi_funcs[] =
88 /* Don't change order here without changing the code in main(). */
89 mpz_add_ui, mpz_mul_ui, mpz_sub_ui,
90 mpz_fdiv_q_2exp, mpz_fdiv_r_2exp,
91 mpz_cdiv_q_2exp, mpz_cdiv_r_2exp,
92 mpz_tdiv_q_2exp, mpz_tdiv_r_2exp,
96 char *dsi_func_names[] =
98 "mpz_add_ui", "mpz_mul_ui", "mpz_sub_ui",
99 "mpz_fdiv_q_2exp", "mpz_fdiv_r_2exp",
100 "mpz_cdiv_q_2exp", "mpz_cdiv_r_2exp",
101 "mpz_tdiv_q_2exp", "mpz_tdiv_r_2exp",
106 dsi_div_func dsi_div_funcs[] =
108 mpz_cdiv_q_ui, mpz_cdiv_r_ui,
109 mpz_fdiv_q_ui, mpz_fdiv_r_ui,
110 mpz_tdiv_q_ui, mpz_tdiv_r_ui
112 char *dsi_div_func_names[] =
114 "mpz_cdiv_q_ui", "mpz_cdiv_r_ui",
115 "mpz_fdiv_q_ui", "mpz_fdiv_r_ui",
116 "mpz_tdiv_q_ui", "mpz_tdiv_r_ui"
119 ddsi_div_func ddsi_div_funcs[] =
125 char *ddsi_div_func_names[] =
132 ddss_div_func ddss_div_funcs[] =
138 char *ddss_div_func_names[] =
147 mpz_abs, mpz_com, mpz_neg, mpz_sqrt
149 char *ds_func_names[] =
151 "mpz_abs", "mpz_com", "mpz_neg", "mpz_sqrt"
155 /* Really use `defined (__STDC__)' here; we want it to be true for Sun C */
156 #if defined (__STDC__) || defined (__cplusplus)
157 #define FAIL(class,indx,op1,op2,op3) \
159 class##_funcs[indx] = 0; \
160 dump (class##_func_names[indx], op1, op2, op3); \
163 #define FAIL2(fname,op1,op2,op3) \
165 dump (#fname, op1, op2, op3); \
169 #define FAIL(class,indx,op1,op2,op3) \
171 class/**/_funcs[indx] = 0; \
172 dump (class/**/_func_names[indx], op1, op2, op3); \
175 #define FAIL2(fname,op1,op2,op3) \
177 dump ("fname", op1, op2, op3); \
185 main (int argc, char **argv)
188 int pass, reps = 100;
190 unsigned long int in2i;
192 mpz_t res1, res2, res3;
193 mpz_t ref1, ref2, ref3;
195 unsigned long int r1, r2;
197 gmp_randstate_ptr rands;
199 unsigned long bsi, size_range;
202 TESTS_REPS (reps, argv, argc);
219 for (pass = 1; pass <= reps; pass++)
221 mpz_urandomb (bs, rands, 32);
222 size_range = mpz_get_ui (bs) % 17 + 2;
224 mpz_urandomb (bs, rands, size_range);
225 size = mpz_get_ui (bs);
226 mpz_rrandomb (in1, rands, size);
228 mpz_urandomb (bs, rands, size_range);
229 size = mpz_get_ui (bs);
230 mpz_rrandomb (in2, rands, size);
232 mpz_urandomb (bs, rands, size_range);
233 size = mpz_get_ui (bs);
234 mpz_rrandomb (in3, rands, size);
236 mpz_urandomb (bs, rands, 3);
237 bsi = mpz_get_ui (bs);
245 for (i = 0; i < sizeof (dss_funcs) / sizeof (dss_func); i++)
247 if (dss_funcs[i] == 0)
249 if (dss_func_division[i] && mpz_sgn (in2) == 0)
252 (dss_funcs[i]) (ref1, in1, in2);
253 MPZ_CHECK_FORMAT (ref1);
256 (dss_funcs[i]) (res1, res1, in2);
257 MPZ_CHECK_FORMAT (res1);
258 if (mpz_cmp (ref1, res1) != 0)
259 FAIL (dss, i, in1, in2, NULL);
262 (dss_funcs[i]) (res1, in1, res1);
263 MPZ_CHECK_FORMAT (res1);
264 if (mpz_cmp (ref1, res1) != 0)
265 FAIL (dss, i, in1, in2, NULL);
268 for (i = 0; i < sizeof (ddss_div_funcs) / sizeof (ddss_div_func); i++)
270 if (ddss_div_funcs[i] == 0)
272 if (mpz_sgn (in2) == 0)
275 (ddss_div_funcs[i]) (ref1, ref2, in1, in2);
276 MPZ_CHECK_FORMAT (ref1);
277 MPZ_CHECK_FORMAT (ref2);
280 (ddss_div_funcs[i]) (res1, res2, res1, in2);
281 MPZ_CHECK_FORMAT (res1);
282 MPZ_CHECK_FORMAT (res2);
283 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
284 FAIL (ddss_div, i, in1, in2, NULL);
287 (ddss_div_funcs[i]) (res1, res2, res2, in2);
288 MPZ_CHECK_FORMAT (res1);
289 MPZ_CHECK_FORMAT (res2);
290 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
291 FAIL (ddss_div, i, in1, in2, NULL);
294 (ddss_div_funcs[i]) (res1, res2, in1, res1);
295 MPZ_CHECK_FORMAT (res1);
296 MPZ_CHECK_FORMAT (res2);
297 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
298 FAIL (ddss_div, i, in1, in2, NULL);
301 (ddss_div_funcs[i]) (res1, res2, in1, res2);
302 MPZ_CHECK_FORMAT (res1);
303 MPZ_CHECK_FORMAT (res2);
304 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
305 FAIL (ddss_div, i, in1, in2, NULL);
308 for (i = 0; i < sizeof (ds_funcs) / sizeof (ds_func); i++)
310 if (ds_funcs[i] == 0)
312 if (strcmp (ds_func_names[i], "mpz_sqrt") == 0
313 && mpz_sgn (in1) < 0)
316 (ds_funcs[i]) (ref1, in1);
317 MPZ_CHECK_FORMAT (ref1);
320 (ds_funcs[i]) (res1, res1);
321 MPZ_CHECK_FORMAT (res1);
322 if (mpz_cmp (ref1, res1) != 0)
323 FAIL (ds, i, in1, in2, NULL);
326 in2i = mpz_get_ui (in2);
328 for (i = 0; i < sizeof (dsi_funcs) / sizeof (dsi_func); i++)
330 if (dsi_funcs[i] == 0)
332 if (strcmp (dsi_func_names[i], "mpz_fdiv_q_2exp") == 0)
333 /* Limit exponent to something reasonable for the division
334 functions. Without this, we'd normally shift things off
335 the end and just generate the trivial values 1, 0, -1. */
337 if (strcmp (dsi_func_names[i], "mpz_mul_2exp") == 0)
338 /* Limit exponent more for mpz_mul_2exp to save time. */
340 if (strcmp (dsi_func_names[i], "mpz_pow_ui") == 0)
341 /* Limit exponent yet more for mpz_pow_ui to save time. */
344 (dsi_funcs[i]) (ref1, in1, in2i);
345 MPZ_CHECK_FORMAT (ref1);
348 (dsi_funcs[i]) (res1, res1, in2i);
349 MPZ_CHECK_FORMAT (res1);
350 if (mpz_cmp (ref1, res1) != 0)
351 FAIL (dsi, i, in1, in2, NULL);
354 if (in2i != 0) /* Don't divide by 0. */
356 for (i = 0; i < sizeof (dsi_div_funcs) / sizeof (dsi_div_funcs); i++)
358 r1 = (dsi_div_funcs[i]) (ref1, in1, in2i);
359 MPZ_CHECK_FORMAT (ref1);
362 r2 = (dsi_div_funcs[i]) (res1, res1, in2i);
363 MPZ_CHECK_FORMAT (res1);
364 if (mpz_cmp (ref1, res1) != 0 || r1 != r2)
365 FAIL (dsi_div, i, in1, in2, NULL);
368 for (i = 0; i < sizeof (ddsi_div_funcs) / sizeof (ddsi_div_funcs); i++)
370 r1 = (ddsi_div_funcs[i]) (ref1, ref2, in1, in2i);
371 MPZ_CHECK_FORMAT (ref1);
374 r2 = (ddsi_div_funcs[i]) (res1, res2, res1, in2i);
375 MPZ_CHECK_FORMAT (res1);
376 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0 || r1 != r2)
377 FAIL (ddsi_div, i, in1, in2, NULL);
380 (ddsi_div_funcs[i]) (res1, res2, res2, in2i);
381 MPZ_CHECK_FORMAT (res1);
382 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0 || r1 != r2)
383 FAIL (ddsi_div, i, in1, in2, NULL);
387 if (mpz_sgn (in1) >= 0)
389 mpz_sqrtrem (ref1, ref2, in1);
390 MPZ_CHECK_FORMAT (ref1);
391 MPZ_CHECK_FORMAT (ref2);
394 mpz_sqrtrem (res1, res2, res1);
395 MPZ_CHECK_FORMAT (res1);
396 MPZ_CHECK_FORMAT (res2);
397 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
398 FAIL2 (mpz_sqrtrem, in1, NULL, NULL);
401 mpz_sqrtrem (res1, res2, res2);
402 MPZ_CHECK_FORMAT (res1);
403 MPZ_CHECK_FORMAT (res2);
404 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
405 FAIL2 (mpz_sqrtrem, in1, NULL, NULL);
408 if (mpz_sgn (in1) >= 0)
410 mpz_root (ref1, in1, in2i % 0x1000 + 1);
411 MPZ_CHECK_FORMAT (ref1);
414 mpz_root (res1, res1, in2i % 0x1000 + 1);
415 MPZ_CHECK_FORMAT (res1);
416 if (mpz_cmp (ref1, res1) != 0)
417 FAIL2 (mpz_root, in1, in2, NULL);
420 if (mpz_sgn (in1) >= 0)
422 mpz_rootrem (ref1, ref2, in1, in2i % 0x1000 + 1);
423 MPZ_CHECK_FORMAT (ref1);
424 MPZ_CHECK_FORMAT (ref2);
427 mpz_rootrem (res1, res2, res1, in2i % 0x1000 + 1);
428 MPZ_CHECK_FORMAT (res1);
429 MPZ_CHECK_FORMAT (res2);
430 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
431 FAIL2 (mpz_rootrem, in1, in2, NULL);
434 mpz_rootrem (res1, res2, res2, in2i % 0x1000 + 1);
435 MPZ_CHECK_FORMAT (res1);
436 MPZ_CHECK_FORMAT (res2);
437 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0)
438 FAIL2 (mpz_rootrem, in1, in2, NULL);
441 if (pass < reps / 2) /* run fewer tests since gcdext lots of time */
443 mpz_gcdext (ref1, ref2, ref3, in1, in2);
444 MPZ_CHECK_FORMAT (ref1);
445 MPZ_CHECK_FORMAT (ref2);
446 MPZ_CHECK_FORMAT (ref3);
449 mpz_gcdext (res1, res2, res3, res1, in2);
450 MPZ_CHECK_FORMAT (res1);
451 MPZ_CHECK_FORMAT (res2);
452 MPZ_CHECK_FORMAT (res3);
453 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
454 || mpz_cmp (ref3, res3) != 0)
455 FAIL2 (mpz_gcdext, in1, in2, NULL);
458 mpz_gcdext (res1, res2, res3, res2, in2);
459 MPZ_CHECK_FORMAT (res1);
460 MPZ_CHECK_FORMAT (res2);
461 MPZ_CHECK_FORMAT (res3);
462 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
463 || mpz_cmp (ref3, res3) != 0)
464 FAIL2 (mpz_gcdext, in1, in2, NULL);
467 mpz_gcdext (res1, res2, res3, res3, in2);
468 MPZ_CHECK_FORMAT (res1);
469 MPZ_CHECK_FORMAT (res2);
470 MPZ_CHECK_FORMAT (res3);
471 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
472 || mpz_cmp (ref3, res3) != 0)
473 FAIL2 (mpz_gcdext, in1, in2, NULL);
476 mpz_gcdext (res1, res2, res3, in1, res1);
477 MPZ_CHECK_FORMAT (res1);
478 MPZ_CHECK_FORMAT (res2);
479 MPZ_CHECK_FORMAT (res3);
480 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
481 || mpz_cmp (ref3, res3) != 0)
482 FAIL2 (mpz_gcdext, in1, in2, NULL);
485 mpz_gcdext (res1, res2, res3, in1, res2);
486 MPZ_CHECK_FORMAT (res1);
487 MPZ_CHECK_FORMAT (res2);
488 MPZ_CHECK_FORMAT (res3);
489 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
490 || mpz_cmp (ref3, res3) != 0)
491 FAIL2 (mpz_gcdext, in1, in2, NULL);
494 mpz_gcdext (res1, res2, res3, in1, res3);
495 MPZ_CHECK_FORMAT (res1);
496 MPZ_CHECK_FORMAT (res2);
497 MPZ_CHECK_FORMAT (res3);
498 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
499 || mpz_cmp (ref3, res3) != 0)
500 FAIL2 (mpz_gcdext, in1, in2, NULL);
503 mpz_gcdext (res1, res2, NULL, res1, in2);
504 MPZ_CHECK_FORMAT (res1);
505 MPZ_CHECK_FORMAT (res2);
506 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
507 || mpz_cmp (ref3, res3) != 0)
508 FAIL2 (mpz_gcdext, in1, in2, NULL);
511 mpz_gcdext (res1, res2, NULL, res2, in2);
512 MPZ_CHECK_FORMAT (res1);
513 MPZ_CHECK_FORMAT (res2);
514 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
515 || mpz_cmp (ref3, res3) != 0)
516 FAIL2 (mpz_gcdext, in1, in2, NULL);
519 mpz_gcdext (res1, res2, NULL, in1, res1);
520 MPZ_CHECK_FORMAT (res1);
521 MPZ_CHECK_FORMAT (res2);
522 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
523 || mpz_cmp (ref3, res3) != 0)
524 FAIL2 (mpz_gcdext, in1, in2, NULL);
527 mpz_gcdext (res1, res2, NULL, in1, res2);
528 MPZ_CHECK_FORMAT (res1);
529 MPZ_CHECK_FORMAT (res2);
530 if (mpz_cmp (ref1, res1) != 0 || mpz_cmp (ref2, res2) != 0
531 || mpz_cmp (ref3, res3) != 0)
532 FAIL2 (mpz_gcdext, in1, in2, NULL);
535 /* Don't run mpz_powm for huge exponents or when undefined. */
536 if (mpz_sizeinbase (in2, 2) < 250 && mpz_sgn (in3) != 0
537 && (mpz_sgn (in2) >= 0 || mpz_invert (t, in1, in3)))
539 mpz_powm (ref1, in1, in2, in3);
540 MPZ_CHECK_FORMAT (ref1);
543 mpz_powm (res1, res1, in2, in3);
544 MPZ_CHECK_FORMAT (res1);
545 if (mpz_cmp (ref1, res1) != 0)
546 FAIL2 (mpz_powm, in1, in2, in3);
549 mpz_powm (res1, in1, res1, in3);
550 MPZ_CHECK_FORMAT (res1);
551 if (mpz_cmp (ref1, res1) != 0)
552 FAIL2 (mpz_powm, in1, in2, in3);
555 mpz_powm (res1, in1, in2, res1);
556 MPZ_CHECK_FORMAT (res1);
557 if (mpz_cmp (ref1, res1) != 0)
558 FAIL2 (mpz_powm, in1, in2, in3);
561 /* Don't run mpz_powm_ui when undefined. */
562 if (mpz_sgn (in3) != 0)
564 mpz_powm_ui (ref1, in1, in2i, in3);
565 MPZ_CHECK_FORMAT (ref1);
568 mpz_powm_ui (res1, res1, in2i, in3);
569 MPZ_CHECK_FORMAT (res1);
570 if (mpz_cmp (ref1, res1) != 0)
571 FAIL2 (mpz_powm_ui, in1, in2, in3);
574 mpz_powm_ui (res1, in1, in2i, res1);
575 MPZ_CHECK_FORMAT (res1);
576 if (mpz_cmp (ref1, res1) != 0)
577 FAIL2 (mpz_powm_ui, in1, in2, in3);
581 r1 = mpz_gcd_ui (ref1, in1, in2i);
582 MPZ_CHECK_FORMAT (ref1);
585 r2 = mpz_gcd_ui (res1, res1, in2i);
586 MPZ_CHECK_FORMAT (res1);
587 if (mpz_cmp (ref1, res1) != 0)
588 FAIL2 (mpz_gcd_ui, in1, in2, NULL);
591 if (mpz_cmp_ui (in2, 1L) > 0 && mpz_sgn (in1) != 0)
593 /* Test mpz_remove */
594 mpz_remove (ref1, in1, in2);
595 MPZ_CHECK_FORMAT (ref1);
598 mpz_remove (res1, res1, in2);
599 MPZ_CHECK_FORMAT (res1);
600 if (mpz_cmp (ref1, res1) != 0)
601 FAIL2 (mpz_remove, in1, in2, NULL);
604 mpz_remove (res1, in1, res1);
605 MPZ_CHECK_FORMAT (res1);
606 if (mpz_cmp (ref1, res1) != 0)
607 FAIL2 (mpz_remove, in1, in2, NULL);
610 if (mpz_sgn (in2) != 0)
612 /* Test mpz_divexact */
613 mpz_mul (t, in1, in2);
614 mpz_divexact (ref1, t, in2);
615 MPZ_CHECK_FORMAT (ref1);
618 mpz_divexact (res1, res1, in2);
619 MPZ_CHECK_FORMAT (res1);
620 if (mpz_cmp (ref1, res1) != 0)
621 FAIL2 (mpz_divexact, t, in2, NULL);
624 mpz_divexact (res1, t, res1);
625 MPZ_CHECK_FORMAT (res1);
626 if (mpz_cmp (ref1, res1) != 0)
627 FAIL2 (mpz_divexact, t, in2, NULL);
630 if (mpz_sgn (in2) > 0)
632 /* Test mpz_divexact_gcd, same as mpz_divexact */
633 mpz_mul (t, in1, in2);
634 mpz_divexact_gcd (ref1, t, in2);
635 MPZ_CHECK_FORMAT (ref1);
638 mpz_divexact_gcd (res1, res1, in2);
639 MPZ_CHECK_FORMAT (res1);
640 if (mpz_cmp (ref1, res1) != 0)
641 FAIL2 (mpz_divexact_gcd, t, in2, NULL);
644 mpz_divexact_gcd (res1, t, res1);
645 MPZ_CHECK_FORMAT (res1);
646 if (mpz_cmp (ref1, res1) != 0)
647 FAIL2 (mpz_divexact_gcd, t, in2, NULL);
653 fprintf (stderr, "mpz/reuse: %ld error%s\n", failures, "s" + (failures == 1));
674 dump (char *name, mpz_t in1, mpz_t in2, mpz_t in3)
676 printf ("failure in %s (", name);
677 mpz_out_str (stdout, -16, in1);
681 mpz_out_str (stdout, -16, in2);
686 mpz_out_str (stdout, -16, in3);
691 #endif /* ! DLL_EXPORT */