1 /* Test mpz_add, mpz_sub, mpz_add_ui, mpz_sub_ui, and mpz_ui_sub.
2 
3 Copyright 2002 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 <stdio.h>
21 #include <stdlib.h>
22 
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "longlong.h"
26 #include "tests.h"
27 
28 void debug_mp (mpz_t, int);
29 void dump_abort (int, const char *, mpz_t, mpz_t);
30 
31 int
main(int argc,char ** argv)32 main (int argc, char **argv)
33 {
34   mpz_t op1, op2, r1, r2;
35   mp_size_t op1n, op2n;
36   unsigned long int op2long;
37   int i;
38   int reps = 100000;
39   gmp_randstate_ptr rands;
40   mpz_t bs;
41   unsigned long bsi, size_range;
42 
43   tests_start ();
44   rands = RANDS;
45 
46   mpz_init (bs);
47 
48   if (argc == 2)
49      reps = atoi (argv[1]);
50 
51   mpz_init (op1);
52   mpz_init (op2);
53   mpz_init (r1);
54   mpz_init (r2);
55 
56   for (i = 0; i < reps; i++)
57     {
58       mpz_urandomb (bs, rands, 32);
59       size_range = mpz_get_ui (bs) % 10 + 2;
60 
61       mpz_urandomb (bs, rands, size_range);
62       op1n = mpz_get_ui (bs);
63       mpz_rrandomb (op1, rands, op1n);
64 
65       mpz_urandomb (bs, rands, size_range);
66       op2n = mpz_get_ui (bs);
67       mpz_rrandomb (op2, rands, op2n);
68 
69       mpz_urandomb (bs, rands, 2);
70       bsi = mpz_get_ui (bs);
71       if ((bsi & 1) != 0)
72 	mpz_neg (op1, op1);
73       if ((bsi & 2) != 0)
74 	mpz_neg (op2, op2);
75 
76       /* printf ("%ld %ld\n", SIZ (multiplier), SIZ (multiplicand)); */
77 
78       mpz_add (r1, op1, op2);
79       mpz_sub (r2, r1, op2);
80       if (mpz_cmp (r2, op1) != 0)
81 	dump_abort (i, "mpz_add or mpz_sub incorrect", op1, op2);
82 
83       if (mpz_fits_ulong_p (op2))
84 	{
85 	  op2long = mpz_get_ui (op2);
86 	  mpz_add_ui (r1, op1, op2long);
87 	  mpz_sub_ui (r2, r1, op2long);
88 	  if (mpz_cmp (r2, op1) != 0)
89 	    dump_abort (i, "mpz_add_ui or mpz_sub_ui incorrect", op1, op2);
90 
91 	  mpz_ui_sub (r1, op2long, op1);
92 	  mpz_sub_ui (r2, op1, op2long);
93 	  mpz_neg (r2, r2);
94 	  if (mpz_cmp (r1, r2) != 0)
95 	    dump_abort (i, "mpz_add_ui or mpz_ui_sub incorrect", op1, op2);
96 	}
97     }
98 
99   mpz_clear (bs);
100   mpz_clear (op1);
101   mpz_clear (op2);
102   mpz_clear (r1);
103   mpz_clear (r2);
104 
105   tests_end ();
106   exit (0);
107 }
108 
109 void
dump_abort(int i,const char * s,mpz_t op1,mpz_t op2)110 dump_abort (int i, const char *s, mpz_t op1, mpz_t op2)
111 {
112   fprintf (stderr, "ERROR: %s in test %d\n", s, i);
113   fprintf (stderr, "op1 = "); debug_mp (op1, -16);
114   fprintf (stderr, "op2 = "); debug_mp (op2, -16);
115   abort();
116 }
117 
118 void
debug_mp(mpz_t x,int base)119 debug_mp (mpz_t x, int base)
120 {
121   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
122 }
123