1 /* Test mpz_mul_ui and mpz_mul_si.
2 
3 Copyright 2001, 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 "tests.h"
26 
27 
28 mpz_t got, want, x;
29 
30 void
compare_si(long y)31 compare_si (long y)
32 {
33   if (mpz_cmp (got, want) != 0)
34     {
35       printf    ("mpz_mul_si wrong\n");
36       mpz_trace ("  x", x);
37       printf    ("  y=%ld (0x%lX)\n", y, y);
38       mpz_trace ("  got ", got);
39       mpz_trace ("  want", want);
40       abort ();
41     }
42 }
43 
44 void
compare_ui(unsigned long y)45 compare_ui (unsigned long y)
46 {
47   if (mpz_cmp (got, want) != 0)
48     {
49       printf    ("mpz_mul_ui wrong\n");
50       mpz_trace ("  x", x);
51       printf    ("  y=%lu (0x%lX)\n", y, y);
52       mpz_trace ("  got ", got);
53       mpz_trace ("  want", want);
54       abort ();
55     }
56 }
57 
58 void
check_samples(void)59 check_samples (void)
60 {
61   {
62     long  y;
63 
64     mpz_set_ui (x, 1L);
65     y = 0;
66     mpz_mul_si (got, x, y);
67     mpz_set_si (want, y);
68     compare_si (y);
69 
70     mpz_set_ui (x, 1L);
71     y = 1;
72     mpz_mul_si (got, x, y);
73     mpz_set_si (want, y);
74     compare_si (y);
75 
76     mpz_set_ui (x, 1L);
77     y = -1;
78     mpz_mul_si (got, x, y);
79     mpz_set_si (want, y);
80     compare_si (y);
81 
82     mpz_set_ui (x, 1L);
83     y = LONG_MIN;
84     mpz_mul_si (got, x, y);
85     mpz_set_si (want, y);
86     compare_si (y);
87 
88     mpz_set_ui (x, 1L);
89     y = LONG_MAX;
90     mpz_mul_si (got, x, y);
91     mpz_set_si (want, y);
92     compare_si (y);
93   }
94 
95   {
96     unsigned long y;
97 
98     mpz_set_ui (x, 1L);
99     y = 0;
100     mpz_mul_ui (got, x, y);
101     mpz_set_ui (want, y);
102     compare_ui (y);
103 
104     mpz_set_ui (x, 1L);
105     y = 1;
106     mpz_mul_ui (got, x, y);
107     mpz_set_ui (want, y);
108     compare_ui (y);
109 
110     mpz_set_ui (x, 1L);
111     y = ULONG_MAX;
112     mpz_mul_ui (got, x, y);
113     mpz_set_ui (want, y);
114     compare_ui (y);
115   }
116 }
117 
118 int
main(int argc,char ** argv)119 main (int argc, char **argv)
120 {
121   tests_start ();
122 
123   mpz_init (x);
124   mpz_init (got);
125   mpz_init (want);
126 
127   check_samples ();
128 
129   mpz_clear (x);
130   mpz_clear (got);
131   mpz_clear (want);
132 
133   tests_end ();
134   exit (0);
135 }
136