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