xref: /netbsd-src/external/lgpl3/gmp/dist/mini-gmp/tests/t-lcm.c (revision 19ef5b5b0bcb90f63509df6e78769de1b57c2758)
1 /*
2 
3 Copyright 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 http://www.gnu.org/licenses/.  */
19 
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 
24 #include "testutils.h"
25 
26 #define MAXBITS 400
27 #define COUNT 10000
28 
29 static void
30 dump (const char *label, const mpz_t x)
31 {
32   char *buf = mpz_get_str (NULL, 16, x);
33   fprintf (stderr, "%s: %s\n", label, buf);
34   testfree (buf);
35 }
36 
37 void
38 testmain (int argc, char **argv)
39 {
40   unsigned i;
41   mpz_t a, b, g, s;
42 
43   mpz_init (a);
44   mpz_init (b);
45   mpz_init (g);
46   mpz_init (s);
47 
48   for (i = 0; i < COUNT; i++)
49     {
50       mini_random_op3 (OP_LCM, MAXBITS, a, b, s);
51       mpz_lcm (g, a, b);
52       if (mpz_cmp (g, s))
53 	{
54 	  fprintf (stderr, "mpz_lcm failed:\n");
55 	  dump ("a", a);
56 	  dump ("b", b);
57 	  dump ("r", g);
58 	  dump ("ref", s);
59 	  abort ();
60 	}
61       if (mpz_fits_ulong_p (b))
62 	{
63 	  mpz_set_si (g, 0);
64 	  mpz_lcm_ui (g, a, mpz_get_ui (b));
65 	  if (mpz_cmp (g, s))
66 	    {
67 	      fprintf (stderr, "mpz_lcm_ui failed:\n");
68 	      dump ("a", a);
69 	      dump ("b", b);
70 	      dump ("r", g);
71 	      dump ("ref", s);
72 	      abort ();
73 	    }
74 	}
75     }
76 
77   mpz_clear (a);
78   mpz_clear (b);
79   mpz_clear (g);
80   mpz_clear (s);
81 }
82