xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpz/t-mfac_uiui.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* Exercise mpz_mfac_uiui.
2 
3 Copyright 2000-2002, 2012 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 #include "gmp.h"
23 #include "gmp-impl.h"
24 #include "tests.h"
25 
26 
27 /* Usage: t-mfac_uiui [x|num]
28 
29    With no arguments testing goes up to the initial value of "limit" below.
30    With a number argument tests are carried that far, or with a literal "x"
31    tests are continued without limit (this being meant only for development
32    purposes).  */
33 
34 #define MULTIFAC_WHEEL (2*3*11)
35 #define MULTIFAC_WHEEL2 (5*13)
36 
37 int
38 main (int argc, char *argv[])
39 {
40   mpz_t ref[MULTIFAC_WHEEL], ref2[MULTIFAC_WHEEL2], res;
41   unsigned long n, j, m, m2;
42   unsigned long limit = 2222, step = 1;
43 
44   tests_start ();
45 
46   if (argc > 1 && argv[1][0] == 'x')
47     limit = ULONG_MAX;
48   else if (argc > 1)
49     limit = atoi (argv[1]);
50 
51   /* for small limb testing */
52   limit = MIN (limit, MP_LIMB_T_MAX);
53 
54   for (m = 0; m < MULTIFAC_WHEEL; m++)
55     mpz_init_set_ui(ref [m],1);
56   for (m2 = 0; m2 < MULTIFAC_WHEEL2; m2++)
57     mpz_init_set_ui(ref2 [m2],1);
58 
59   mpz_init (res);
60 
61   m = 0;
62   m2 = 0;
63   for (n = 0; n <= limit;)
64     {
65       mpz_mfac_uiui (res, n, MULTIFAC_WHEEL);
66       MPZ_CHECK_FORMAT (res);
67       if (mpz_cmp (ref[m], res) != 0)
68         {
69           printf ("mpz_mfac_uiui(%lu,%d) wrong\n", n, MULTIFAC_WHEEL);
70           printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
71           printf ("  want "); mpz_out_str (stdout, 10, ref[m]); printf("\n");
72           abort ();
73         }
74       mpz_mfac_uiui (res, n, MULTIFAC_WHEEL2);
75       MPZ_CHECK_FORMAT (res);
76       if (mpz_cmp (ref2[m2], res) != 0)
77         {
78           printf ("mpz_mfac_uiui(%lu,%d) wrong\n", n, MULTIFAC_WHEEL2);
79           printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
80           printf ("  want "); mpz_out_str (stdout, 10, ref2[m2]); printf("\n");
81           abort ();
82         }
83       if (n + step <= limit)
84 	for (j = 0; j < step; j++) {
85 	  n++; m++; m2++;
86 	  if (m >= MULTIFAC_WHEEL) m -= MULTIFAC_WHEEL;
87 	  if (m2 >= MULTIFAC_WHEEL2) m2 -= MULTIFAC_WHEEL2;
88 	  mpz_mul_ui (ref[m], ref[m], n); /* Compute a reference, with current library */
89 	  mpz_mul_ui (ref2[m2], ref2[m2], n); /* Compute a reference, with current library */
90 	}
91       else n += step;
92     }
93   mpz_fac_ui (ref[0], n);
94   mpz_mfac_uiui (res, n, 1);
95   MPZ_CHECK_FORMAT (res);
96   if (mpz_cmp (ref[0], res) != 0)
97     {
98       printf ("mpz_mfac_uiui(%lu,1) wrong\n", n);
99       printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
100       printf ("  want "); mpz_out_str (stdout, 10, ref[0]); printf("\n");
101       abort ();
102     }
103 
104   mpz_2fac_ui (ref[0], n);
105   mpz_mfac_uiui (res, n, 2);
106   MPZ_CHECK_FORMAT (res);
107   if (mpz_cmp (ref[0], res) != 0)
108     {
109       printf ("mpz_mfac_uiui(%lu,1) wrong\n", n);
110       printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
111       printf ("  want "); mpz_out_str (stdout, 10, ref[0]); printf("\n");
112       abort ();
113     }
114 
115   n++;
116   mpz_2fac_ui (ref[0], n);
117   mpz_mfac_uiui (res, n, 2);
118   MPZ_CHECK_FORMAT (res);
119   if (mpz_cmp (ref[0], res) != 0)
120     {
121       printf ("mpz_mfac_uiui(%lu,2) wrong\n", n);
122       printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
123       printf ("  want "); mpz_out_str (stdout, 10, ref[0]); printf("\n");
124       abort ();
125     }
126 
127   for (m = 0; m < MULTIFAC_WHEEL; m++)
128     mpz_clear (ref[m]);
129   for (m2 = 0; m2 < MULTIFAC_WHEEL2; m2++)
130     mpz_clear (ref2[m2]);
131   mpz_clear (res);
132 
133   tests_end ();
134 
135   exit (0);
136 }
137