Import Upstream version 0.8.2
[platform/upstream/mpc.git] / tests / tset.c
1 /* tset.c -- Test file for mpc_set_x and mpc_set_x_x functions.
2
3 Copyright (C) 2009 Philippe Th\'eveny, Paul Zimmermann, Andreas Enge
4
5 This file is part of the MPC Library.
6
7 The MPC Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11
12 The MPC Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the MPC Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include <limits.h> /* for LONG_MAX */
24
25 #if HAVE_INTTYPES_H
26 # include <inttypes.h> /* for intmax_t */
27 #else
28 # if HAVE_STDINT_H
29 #  include <stdint.h>
30 # endif
31 #endif
32
33 #ifdef HAVE_LOCALE_H
34 #include <locale.h>
35 #endif
36
37 #include "mpc-tests.h"
38
39 #define PRINT_ERROR(function_name, precision, a)                \
40   do {                                                          \
41     printf ("Error in "function_name" for prec = %lu\n",        \
42             (unsigned long int) precision);                     \
43     OUT(a);                                                     \
44     exit (1);                                                   \
45   } while (0)
46
47 /* test MPC_SET_X_Y though some functions */
48 static int
49 mpc_set_ui_fr (mpc_ptr z, unsigned long int a, mpfr_srcptr b, mpc_rnd_t rnd)
50   MPC_SET_X_Y (ui, fr, z, a, b, rnd)
51
52 static int
53 mpc_set_fr_ui (mpc_ptr z, mpfr_srcptr a, unsigned long int b, mpc_rnd_t rnd)
54   MPC_SET_X_Y (fr, ui, z, a, b, rnd)
55
56 static int
57 mpc_set_f_si (mpc_ptr z, mpf_t a, long int b, mpc_rnd_t rnd)
58   MPC_SET_X_Y (f, si, z, a, b, rnd)
59
60
61 static void
62 check_set (void)
63 {
64   long int lo;
65   mpz_t mpz;
66   mpq_t mpq;
67   mpf_t mpf;
68   mpfr_t fr;
69   mpc_t x, z;
70   mp_prec_t prec;
71
72   mpz_init (mpz);
73   mpq_init (mpq);
74   mpf_init2 (mpf, 1000);
75   mpfr_init2 (fr, 1000);
76   mpc_init2 (x, 1000);
77   mpc_init2 (z, 1000);
78
79   mpz_set_ui (mpz, 0x4217);
80   mpq_set_si (mpq, -1, 0x4321);
81   mpf_set_q (mpf, mpq);
82
83   for (prec = 2; prec <= 1000; prec++)
84     {
85       mpc_set_prec (z, prec);
86       mpfr_set_prec (fr, prec);
87
88       lo = -prec;
89
90       mpfr_set_d (fr, 1.23456789, GMP_RNDN);
91
92       mpc_set_d (z, 1.23456789, MPC_RNDNN);
93       if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_si (MPC_IM(z), 0) != 0)
94         PRINT_ERROR ("mpc_set", prec, z);
95
96       mpc_set_ui (z, prec, MPC_RNDNN);
97       if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
98           || mpfr_cmp_ui (MPC_IM(z), 0) != 0)
99         PRINT_ERROR ("mpc_set_ui", prec, z);
100
101       mpc_set_d_d (z, 1.23456789, 1.23456789, MPC_RNDNN);
102       if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0)
103         PRINT_ERROR ("mpc_set_d_d", prec, z);
104
105       mpc_set_si (z, lo, MPC_RNDNN);
106       if (mpfr_cmp_si (MPC_RE(z), lo) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0)
107         PRINT_ERROR ("mpc_set_si", prec, z);
108
109       mpfr_set_ld (fr, 1.23456789L, GMP_RNDN);
110
111       mpc_set_ld_ld (z, 1.23456789L, 1.23456789L, MPC_RNDNN);
112       if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0)
113         PRINT_ERROR ("mpc_set_ld_ld", prec, z);
114
115       mpc_set_ui_ui (z, prec, prec, MPC_RNDNN);
116       if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
117           || mpfr_cmp_ui (MPC_IM(z), prec) != 0)
118         PRINT_ERROR ("mpc_set_ui_ui", prec, z);
119
120       mpc_set_ld (z, 1.23456789L, MPC_RNDNN);
121       mpfr_clear_flags ();
122       if (mpfr_cmp (MPC_RE(z), fr) != 0
123           || mpfr_cmp_ui (MPC_IM(z), 0) != 0
124           || mpfr_erangeflag_p())
125         PRINT_ERROR ("mpc_set_ld", prec, z);
126
127       mpc_set_prec (x, prec);
128       mpfr_set_ui(fr, 1, GMP_RNDN);
129       mpfr_div_ui(fr, fr, 3, GMP_RNDN);
130       mpfr_set(MPC_RE(x), fr, GMP_RNDN);
131       mpfr_set(MPC_IM(x), fr, GMP_RNDN);
132
133       mpc_set (z, x, MPC_RNDNN);
134       mpfr_clear_flags (); /* mpc_cmp set erange flag when an operand is a
135                               NaN */
136       if (mpc_cmp (z, x) != 0 || mpfr_erangeflag_p())
137         {
138           printf ("Error in mpc_set for prec = %lu\n",
139                   (unsigned long int) prec);
140           OUT(z);
141           OUT(x);
142           exit (1);
143         }
144
145       mpc_set_si_si (z, lo, lo, MPC_RNDNN);
146       if (mpfr_cmp_si (MPC_RE(z), lo) != 0
147           || mpfr_cmp_si (MPC_IM(z), lo) != 0)
148         PRINT_ERROR ("mpc_set_si_si", prec, z);
149
150       mpc_set_fr (z, fr, MPC_RNDNN);
151       mpfr_clear_flags ();
152       if (mpfr_cmp (MPC_RE(z), fr) != 0
153           || mpfr_cmp_ui (MPC_IM(z), 0) != 0
154           || mpfr_erangeflag_p())
155         PRINT_ERROR ("mpc_set_fr", prec, z);
156
157       mpfr_set_z (fr, mpz, GMP_RNDN);
158       mpc_set_z_z (z, mpz, mpz, MPC_RNDNN);
159       mpfr_clear_flags ();
160       if (mpfr_cmp (MPC_RE(z), fr) != 0
161           || mpfr_cmp (MPC_IM(z), fr) != 0
162           || mpfr_erangeflag_p())
163         PRINT_ERROR ("mpc_set_z_z", prec, z);
164
165       mpc_set_fr_fr (z, fr, fr, MPC_RNDNN);
166       mpfr_clear_flags ();
167       if (mpfr_cmp (MPC_RE(z), fr) != 0
168           || mpfr_cmp (MPC_IM(z), fr) != 0
169           || mpfr_erangeflag_p())
170         PRINT_ERROR ("mpc_set_fr_fr", prec, z);
171
172       mpc_set_z (z, mpz, MPC_RNDNN);
173       mpfr_clear_flags ();
174       if (mpfr_cmp (MPC_RE(z), fr) != 0
175           || mpfr_cmp_ui (MPC_IM(z), 0) != 0
176           || mpfr_erangeflag_p())
177         PRINT_ERROR ("mpc_set_z", prec, z);
178
179       mpfr_set_q (fr, mpq, GMP_RNDN);
180       mpc_set_q_q (z, mpq, mpq, MPC_RNDNN);
181       mpfr_clear_flags ();
182       if (mpfr_cmp (MPC_RE(z), fr) != 0
183           || mpfr_cmp (MPC_IM(z), fr) != 0
184           || mpfr_erangeflag_p())
185         PRINT_ERROR ("mpc_set_q_q", prec, z);
186
187       mpc_set_ui_fr (z, prec, fr, MPC_RNDNN);
188       mpfr_clear_flags ();
189       if (mpfr_cmp_ui (MPC_RE (z), prec) != 0
190           || mpfr_cmp (MPC_IM (z), fr) != 0
191           || mpfr_erangeflag_p ())
192         PRINT_ERROR ("mpc_set_ui_fr", prec, z);
193
194       mpc_set_fr_ui (z, fr, prec, MPC_RNDNN);
195       mpfr_clear_flags ();
196       if (mpfr_cmp (MPC_RE (z), fr) != 0
197           || mpfr_cmp_ui (MPC_IM (z), prec) != 0
198           || mpfr_erangeflag_p())
199         PRINT_ERROR ("mpc_set_fr_ui", prec, z);
200
201       mpc_set_q (z, mpq, MPC_RNDNN);
202       mpfr_clear_flags ();
203       if (mpfr_cmp (MPC_RE(z), fr) != 0
204           || mpfr_cmp_ui (MPC_IM(z), 0) != 0
205           || mpfr_erangeflag_p())
206         PRINT_ERROR ("mpc_set_q", prec, z);
207
208       mpfr_set_f (fr, mpf, GMP_RNDN);
209       mpc_set_f_f (z, mpf, mpf, MPC_RNDNN);
210       mpfr_clear_flags ();
211       if (mpfr_cmp (MPC_RE(z), fr) != 0
212           || mpfr_cmp (MPC_IM(z), fr) != 0
213           || mpfr_erangeflag_p())
214         PRINT_ERROR ("mpc_set_f_f", prec, z);
215
216       mpc_set_f (z, mpf, MPC_RNDNN);
217       mpfr_clear_flags ();
218       if (mpfr_cmp (MPC_RE(z), fr) != 0
219           || mpfr_cmp_ui (MPC_IM(z), 0) != 0
220           || mpfr_erangeflag_p())
221         PRINT_ERROR ("mpc_set_f", prec, z);
222
223       mpc_set_f_si (z, mpf, lo, MPC_RNDNN);
224       mpfr_clear_flags ();
225       if (mpfr_cmp (MPC_RE (z), fr) != 0
226           || mpfr_cmp_si (MPC_IM (z), lo) != 0
227           || mpfr_erangeflag_p ())
228         PRINT_ERROR ("mpc_set_f", prec, z);
229
230       mpc_set_nan (z);
231       if (!mpfr_nan_p (MPC_RE(z)) || !mpfr_nan_p (MPC_IM(z)))
232         PRINT_ERROR ("mpc_set_nan", prec, z);
233
234 #ifdef _MPC_H_HAVE_INTMAX_T
235       {
236         uintmax_t uim = prec;
237         intmax_t im = prec;
238
239         mpc_set_uj (z, uim, MPC_RNDNN);
240         if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
241             || mpfr_cmp_ui (MPC_IM(z), 0) != 0)
242           PRINT_ERROR ("mpc_set_uj", prec, z);
243
244         mpc_set_sj (z, im, MPC_RNDNN);
245         if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
246             || mpfr_cmp_ui (MPC_IM(z), 0) != 0)
247           PRINT_ERROR ("mpc_set_sj (1)", prec, z);
248
249         mpc_set_uj_uj (z, uim, uim, MPC_RNDNN);
250         if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
251             || mpfr_cmp_ui (MPC_IM(z), prec) != 0)
252           PRINT_ERROR ("mpc_set_uj_uj", prec, z);
253
254         mpc_set_sj_sj (z, im, im, MPC_RNDNN);
255         if (mpfr_cmp_ui (MPC_RE(z), prec) != 0
256             || mpfr_cmp_ui (MPC_IM(z), prec) != 0)
257           PRINT_ERROR ("mpc_set_sj_sj (1)", prec, z);
258
259         im = LONG_MAX;
260         if (sizeof (intmax_t) == 2 * sizeof (unsigned long))
261           im = 2 * im * im + 4 * im + 1; /* gives 2^(2n-1)-1 from 2^(n-1)-1 */
262
263         mpc_set_sj (z, im, MPC_RNDNN);
264         if (mpfr_get_sj (MPC_RE(z), GMP_RNDN) != im ||
265             mpfr_cmp_ui (MPC_IM(z), 0) != 0)
266           PRINT_ERROR ("mpc_set_sj (2)", im, z);
267
268         mpc_set_sj_sj (z, im, im, MPC_RNDNN);
269         if (mpfr_get_sj (MPC_RE(z), GMP_RNDN) != im ||
270             mpfr_get_sj (MPC_IM(z), GMP_RNDN) != im)
271           PRINT_ERROR ("mpc_set_sj_sj (2)", im, z);
272       }
273 #endif /* _MPC_H_HAVE_INTMAX_T */
274     }
275
276   mpz_clear (mpz);
277   mpq_clear (mpq);
278   mpf_clear (mpf);
279   mpfr_clear (fr);
280   mpc_clear (x);
281   mpc_clear (z);
282 }
283
284 static void
285 check_set_str (mp_exp_t exp_max)
286 {
287   mpc_t expected;
288   mpc_t got;
289   char *str;
290
291   mpfr_prec_t prec;
292   mp_exp_t exp_min;
293   unsigned int base;
294
295   mpc_init2 (expected, 1024);
296   mpc_init2 (got, 1024);
297
298   exp_min = mpfr_get_emin ();
299   if (exp_max <= 0)
300     exp_max = mpfr_get_emax ();
301   else if (exp_max > mpfr_get_emax ())
302     exp_max = mpfr_get_emax();
303   if (-exp_max > exp_min)
304     exp_min = - exp_max;
305
306   for (prec = 2; prec < 1024; prec += 7)
307     {
308       mpc_set_prec (got, prec);
309       mpc_set_prec (expected, prec);
310
311       base = 2 + (unsigned int) gmp_urandomm_ui (rands, 35);
312          /* uses external variable rands from random.c */
313
314       mpfr_set_nan (MPC_RE (expected));
315       mpfr_set_inf (MPC_IM (expected), prec % 2 - 1);
316       str = mpc_get_str (base, 0, expected, MPC_RNDNN);
317       if (mpfr_nan_p (MPC_RE (got)) == 0
318           || mpfr_cmp (MPC_IM (got), MPC_IM (expected)) != 0)
319         {
320           printf ("Error: mpc_set_str o mpc_get_str != Id\n"
321                   "in base %u with str=\"%s\"\n", base, str);
322           OUT (expected);
323           printf ("     ");
324           OUT (got);
325           exit (1);
326         }
327       mpc_free_str (str);
328
329       test_default_random (expected, exp_min, exp_max, 128, 25);
330       str = mpc_get_str (base, 0, expected, MPC_RNDNN);
331       if (mpc_set_str (got, str, base, MPC_RNDNN) == -1
332           || mpc_cmp (got, expected) != 0)
333         {
334           printf ("Error: mpc_set_str o mpc_get_str != Id\n"
335                   "in base %u with str=\"%s\"\n", base, str);
336           OUT (expected);
337           printf ("     ");
338           OUT (got);
339           exit (1);
340         }
341       mpc_free_str (str);
342     }
343
344 #ifdef HAVE_SETLOCALE
345   {
346     /* Check with ',' as a decimal point */
347     char *old_locale;
348
349     old_locale = setlocale (LC_ALL, "de_DE");
350     if (old_locale != NULL)
351       {
352         str = mpc_get_str (10, 0, expected, MPC_RNDNN);
353         if (mpc_set_str (got, str, 10, MPC_RNDNN) == -1
354             || mpc_cmp (got, expected) != 0)
355           {
356             printf ("Error: mpc_set_str o mpc_get_str != Id\n"
357                     "with str=\"%s\"\n", str);
358             OUT (expected);
359             printf ("     ");
360             OUT (got);
361             exit (1);
362           }
363         mpc_free_str (str);
364
365         setlocale (LC_ALL, old_locale);
366       }
367   }
368 #endif /* HAVE_SETLOCALE */
369
370   /* the real part has a zero exponent in base ten (fixed in r439) */
371   mpc_set_prec (expected, 37);
372   mpc_set_prec (got, 37);
373   mpc_set_str (expected, "921FC04EDp-35  ", 16, GMP_RNDN);
374   str = mpc_get_str (10, 0, expected, MPC_RNDNN);
375   if (mpc_set_str (got, str, 10, MPC_RNDNN) == -1
376       || mpc_cmp (got, expected) != 0)
377     {
378       printf ("Error: mpc_set_str o mpc_get_str != Id\n"
379               "with str=\"%s\"\n", str);
380       OUT (expected);
381       printf ("     ");
382       OUT (got);
383       exit (1);
384     }
385   mpc_free_str (str);
386
387   str = mpc_get_str (1, 0, expected, MPC_RNDNN);
388   if (str != NULL)
389     {
390       printf ("Error: mpc_get_str with base==1 should fail\n");
391       exit (1);
392     }
393
394   mpc_clear (expected);
395   mpc_clear (got);
396 }
397
398 int
399 main (void)
400 {
401   test_start ();
402
403   check_set ();
404   check_set_str (1024);
405
406   test_end ();
407
408   return 0;
409 }