xref: /netbsd-src/external/lgpl3/mpc/dist/tests/tset.c (revision 2dd295436a0082eb4f8d294f4aa73c223413d0f2)
1 /* tset -- Test file for mpc_set_x and mpc_set_x_x functions.
2 
3 Copyright (C) 2009, 2010, 2011, 2012, 2022 INRIA
4 
5 This file is part of GNU MPC.
6 
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
19 */
20 
21 #include "config.h"
22 #include <locale.h>
23 #include <limits.h> /* for LONG_MAX */
24 
25 #ifdef HAVE_INTTYPES_H
26 # include <inttypes.h> /* for intmax_t */
27 #else
28 # ifdef HAVE_STDINT_H
29 #  include <stdint.h>
30 # endif
31 #endif
32 
33 #ifdef HAVE_COMPLEX_H
34 # include <complex.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     MPC_OUT(a);                                                 \
44     exit (1);                                                   \
45   } while (0)
46 
47 /* test MPC_SET_X_Y through 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   mpfr_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       unsigned long int u = (unsigned long int) prec;
86 
87       mpc_set_prec (z, prec);
88       mpfr_set_prec (fr, prec);
89 
90       lo = -prec;
91 
92       mpfr_set_d (fr, 1.23456789, MPFR_RNDN);
93 
94       mpc_set_d (z, 1.23456789, MPC_RNDNN);
95       if (mpfr_cmp (mpc_realref(z), fr) != 0 || mpfr_cmp_si (mpc_imagref(z), 0) != 0)
96         PRINT_ERROR ("mpc_set_d", prec, z);
97 
98 #if defined HAVE_COMPLEX_H
99       mpc_set_dc (z, I*1.23456789+1.23456789, MPC_RNDNN);
100       if (mpfr_cmp (mpc_realref(z), fr) != 0 || mpfr_cmp (mpc_imagref(z), fr) != 0)
101         PRINT_ERROR ("mpc_set_c", prec, z);
102 #endif
103 
104       mpc_set_ui (z, u, MPC_RNDNN);
105       if (mpfr_cmp_ui (mpc_realref(z), u) != 0
106           || mpfr_cmp_ui (mpc_imagref(z), 0) != 0)
107         PRINT_ERROR ("mpc_set_ui", prec, z);
108 
109       mpc_set_d_d (z, 1.23456789, 1.23456789, MPC_RNDNN);
110       if (mpfr_cmp (mpc_realref(z), fr) != 0 || mpfr_cmp (mpc_imagref(z), fr) != 0)
111         PRINT_ERROR ("mpc_set_d_d", prec, z);
112 
113       mpc_set_si (z, lo, MPC_RNDNN);
114       if (mpfr_cmp_si (mpc_realref(z), lo) != 0 || mpfr_cmp_ui (mpc_imagref(z), 0) != 0)
115         PRINT_ERROR ("mpc_set_si", prec, z);
116 
117       mpfr_set_ld (fr, 1.23456789L, MPFR_RNDN);
118 
119       mpc_set_ld_ld (z, 1.23456789L, 1.23456789L, MPC_RNDNN);
120       if (mpfr_cmp (mpc_realref(z), fr) != 0 || mpfr_cmp (mpc_imagref(z), fr) != 0)
121         PRINT_ERROR ("mpc_set_ld_ld", prec, z);
122 
123 #if defined HAVE_COMPLEX_H
124       mpc_set_ldc (z, I*1.23456789L+1.23456789L, MPC_RNDNN);
125       if (mpfr_cmp (mpc_realref(z), fr) != 0 || mpfr_cmp (mpc_imagref(z), fr) != 0)
126         PRINT_ERROR ("mpc_set_lc", prec, z);
127 #endif
128       mpc_set_ui_ui (z, u, u, MPC_RNDNN);
129       if (mpfr_cmp_ui (mpc_realref(z), u) != 0
130           || mpfr_cmp_ui (mpc_imagref(z), u) != 0)
131         PRINT_ERROR ("mpc_set_ui_ui", prec, z);
132 
133       mpc_set_ld (z, 1.23456789L, MPC_RNDNN);
134       mpfr_clear_flags ();
135       if (mpfr_cmp (mpc_realref(z), fr) != 0
136           || mpfr_cmp_ui (mpc_imagref(z), 0) != 0
137           || mpfr_erangeflag_p())
138         PRINT_ERROR ("mpc_set_ld", prec, z);
139 
140       mpc_set_prec (x, prec);
141       mpfr_set_ui(fr, 1, MPFR_RNDN);
142       mpfr_div_ui(fr, fr, 3, MPFR_RNDN);
143       mpfr_set(mpc_realref(x), fr, MPFR_RNDN);
144       mpfr_set(mpc_imagref(x), fr, MPFR_RNDN);
145 
146       mpc_set (z, x, MPC_RNDNN);
147       mpfr_clear_flags (); /* mpc_cmp set erange flag when an operand is a
148                               NaN */
149       if (mpc_cmp (z, x) != 0 || mpfr_erangeflag_p())
150         {
151           printf ("Error in mpc_set for prec = %lu\n",
152                   (unsigned long int) prec);
153           MPC_OUT(z);
154           MPC_OUT(x);
155           exit (1);
156         }
157 
158       mpc_set_si_si (z, lo, lo, MPC_RNDNN);
159       if (mpfr_cmp_si (mpc_realref(z), lo) != 0
160           || mpfr_cmp_si (mpc_imagref(z), lo) != 0)
161         PRINT_ERROR ("mpc_set_si_si", prec, z);
162 
163       mpc_set_fr (z, fr, MPC_RNDNN);
164       mpfr_clear_flags ();
165       if (mpfr_cmp (mpc_realref(z), fr) != 0
166           || mpfr_cmp_ui (mpc_imagref(z), 0) != 0
167           || mpfr_erangeflag_p())
168         PRINT_ERROR ("mpc_set_fr", prec, z);
169 
170       mpfr_set_z (fr, mpz, MPFR_RNDN);
171       mpc_set_z_z (z, mpz, mpz, MPC_RNDNN);
172       mpfr_clear_flags ();
173       if (mpfr_cmp (mpc_realref(z), fr) != 0
174           || mpfr_cmp (mpc_imagref(z), fr) != 0
175           || mpfr_erangeflag_p())
176         PRINT_ERROR ("mpc_set_z_z", prec, z);
177 
178       mpc_set_fr_fr (z, fr, fr, MPC_RNDNN);
179       mpfr_clear_flags ();
180       if (mpfr_cmp (mpc_realref(z), fr) != 0
181           || mpfr_cmp (mpc_imagref(z), fr) != 0
182           || mpfr_erangeflag_p())
183         PRINT_ERROR ("mpc_set_fr_fr", prec, z);
184 
185       mpc_set_z (z, mpz, MPC_RNDNN);
186       mpfr_clear_flags ();
187       if (mpfr_cmp (mpc_realref(z), fr) != 0
188           || mpfr_cmp_ui (mpc_imagref(z), 0) != 0
189           || mpfr_erangeflag_p())
190         PRINT_ERROR ("mpc_set_z", prec, z);
191 
192       mpfr_set_q (fr, mpq, MPFR_RNDN);
193       mpc_set_q_q (z, mpq, mpq, MPC_RNDNN);
194       mpfr_clear_flags ();
195       if (mpfr_cmp (mpc_realref(z), fr) != 0
196           || mpfr_cmp (mpc_imagref(z), fr) != 0
197           || mpfr_erangeflag_p())
198         PRINT_ERROR ("mpc_set_q_q", prec, z);
199 
200       mpc_set_ui_fr (z, u, fr, MPC_RNDNN);
201       mpfr_clear_flags ();
202       if (mpfr_cmp_ui (mpc_realref (z), u) != 0
203           || mpfr_cmp (mpc_imagref (z), fr) != 0
204           || mpfr_erangeflag_p ())
205         PRINT_ERROR ("mpc_set_ui_fr", prec, z);
206 
207       mpc_set_fr_ui (z, fr, u, MPC_RNDNN);
208       mpfr_clear_flags ();
209       if (mpfr_cmp (mpc_realref (z), fr) != 0
210           || mpfr_cmp_ui (mpc_imagref (z), u) != 0
211           || mpfr_erangeflag_p())
212         PRINT_ERROR ("mpc_set_fr_ui", prec, z);
213 
214       mpc_set_q (z, mpq, MPC_RNDNN);
215       mpfr_clear_flags ();
216       if (mpfr_cmp (mpc_realref(z), fr) != 0
217           || mpfr_cmp_ui (mpc_imagref(z), 0) != 0
218           || mpfr_erangeflag_p())
219         PRINT_ERROR ("mpc_set_q", prec, z);
220 
221       mpfr_set_f (fr, mpf, MPFR_RNDN);
222       mpc_set_f_f (z, mpf, mpf, MPC_RNDNN);
223       mpfr_clear_flags ();
224       if (mpfr_cmp (mpc_realref(z), fr) != 0
225           || mpfr_cmp (mpc_imagref(z), fr) != 0
226           || mpfr_erangeflag_p())
227         PRINT_ERROR ("mpc_set_f_f", prec, z);
228 
229       mpc_set_f (z, mpf, MPC_RNDNN);
230       mpfr_clear_flags ();
231       if (mpfr_cmp (mpc_realref(z), fr) != 0
232           || mpfr_cmp_ui (mpc_imagref(z), 0) != 0
233           || mpfr_erangeflag_p())
234         PRINT_ERROR ("mpc_set_f", prec, z);
235 
236       mpc_set_f_si (z, mpf, lo, MPC_RNDNN);
237       mpfr_clear_flags ();
238       if (mpfr_cmp (mpc_realref (z), fr) != 0
239           || mpfr_cmp_si (mpc_imagref (z), lo) != 0
240           || mpfr_erangeflag_p ())
241         PRINT_ERROR ("mpc_set_f", prec, z);
242 
243       mpc_set_nan (z);
244       if (!mpfr_nan_p (mpc_realref(z)) || !mpfr_nan_p (mpc_imagref(z)))
245         PRINT_ERROR ("mpc_set_nan", prec, z);
246 
247 #ifdef _MPC_H_HAVE_INTMAX_T
248       {
249         uintmax_t uim = (uintmax_t) prec;
250         intmax_t im = (intmax_t) prec;
251 
252         mpc_set_uj (z, uim, MPC_RNDNN);
253         if (mpfr_cmp_ui (mpc_realref(z), u) != 0
254             || mpfr_cmp_ui (mpc_imagref(z), 0) != 0)
255           PRINT_ERROR ("mpc_set_uj", prec, z);
256 
257         mpc_set_sj (z, im, MPC_RNDNN);
258         if (mpfr_cmp_ui (mpc_realref(z), u) != 0
259             || mpfr_cmp_ui (mpc_imagref(z), 0) != 0)
260           PRINT_ERROR ("mpc_set_sj (1)", prec, z);
261 
262         mpc_set_uj_uj (z, uim, uim, MPC_RNDNN);
263         if (mpfr_cmp_ui (mpc_realref(z), u) != 0
264             || mpfr_cmp_ui (mpc_imagref(z), u) != 0)
265           PRINT_ERROR ("mpc_set_uj_uj", prec, z);
266 
267         mpc_set_sj_sj (z, im, im, MPC_RNDNN);
268         if (mpfr_cmp_ui (mpc_realref(z), u) != 0
269             || mpfr_cmp_ui (mpc_imagref(z), u) != 0)
270           PRINT_ERROR ("mpc_set_sj_sj (1)", prec, z);
271 
272         im = LONG_MAX;
273         if (sizeof (intmax_t) == 2 * sizeof (unsigned long))
274           im = 2 * im * im + 4 * im + 1; /* gives 2^(2n-1)-1 from 2^(n-1)-1 */
275 
276         mpc_set_sj (z, im, MPC_RNDNN);
277         if (mpfr_get_sj (mpc_realref(z), MPFR_RNDN) != im ||
278             mpfr_cmp_ui (mpc_imagref(z), 0) != 0)
279           PRINT_ERROR ("mpc_set_sj (2)", im, z);
280 
281         mpc_set_sj_sj (z, im, im, MPC_RNDNN);
282         if (mpfr_get_sj (mpc_realref(z), MPFR_RNDN) != im ||
283             mpfr_get_sj (mpc_imagref(z), MPFR_RNDN) != im)
284           PRINT_ERROR ("mpc_set_sj_sj (2)", im, z);
285       }
286 #endif /* _MPC_H_HAVE_INTMAX_T */
287 
288 #if defined HAVE_COMPLEX_H
289       {
290         double _Complex c = 1.0 - 2.0*I, d;
291         long double _Complex lc = c, ld;
292 
293          mpc_set_dc (z, c, MPC_RNDNN);
294          if ((d = mpc_get_dc (z, MPC_RNDNN)) != c)
295            {
296              printf ("expected (%f,%f)\n", creal (c), cimag (c));
297              printf ("got      (%f,%f)\n", creal (d), cimag (d));
298              PRINT_ERROR ("mpc_get_dc", prec, z);
299            }
300          mpc_set_ldc (z, lc, MPC_RNDNN);
301          if ((ld = mpc_get_ldc (z, MPC_RNDNN)) != lc)
302            {
303              printf ("expected (%Lf,%Lf)\n", creall (lc), cimagl (lc));
304              printf ("got      (%Lf,%Lf)\n", creall (ld), cimagl (ld));
305              PRINT_ERROR ("mpc_get_ldc", prec, z);
306            }
307       }
308 #endif
309     }
310 
311   mpz_clear (mpz);
312   mpq_clear (mpq);
313   mpf_clear (mpf);
314   mpfr_clear (fr);
315   mpc_clear (x);
316   mpc_clear (z);
317 }
318 
319 static void
320 check_set_str (mpfr_exp_t exp_max)
321 {
322   mpc_t expected;
323   mpc_t got;
324   char *str;
325 
326   mpfr_prec_t prec;
327   mpfr_exp_t exp_min;
328   int base;
329 
330   mpc_init2 (expected, 1024);
331   mpc_init2 (got, 1024);
332 
333   exp_min = mpfr_get_emin ();
334   if (exp_max <= 0)
335     exp_max = mpfr_get_emax ();
336   else if (exp_max > mpfr_get_emax ())
337     exp_max = mpfr_get_emax();
338   if (-exp_max > exp_min)
339     exp_min = - exp_max;
340 
341   for (prec = 2; prec < 1024; prec += 7)
342     {
343       mpc_set_prec (got, prec);
344       mpc_set_prec (expected, prec);
345 
346       base = 2 + (int) gmp_urandomm_ui (rands, 35);
347          /* uses external variable rands from random.c */
348 
349       mpfr_set_nan (mpc_realref (expected));
350       mpfr_set_inf (mpc_imagref (expected), prec % 2 - 1);
351       str = mpc_get_str (base, 0, expected, MPC_RNDNN);
352       if (mpfr_nan_p (mpc_realref (got)) == 0
353           || mpfr_cmp (mpc_imagref (got), mpc_imagref (expected)) != 0)
354         {
355           printf ("Error: mpc_set_str o mpc_get_str != Id\n"
356                   "in base %u with str=\"%s\"\n", base, str);
357           MPC_OUT (expected);
358           printf ("     ");
359           MPC_OUT (got);
360           exit (1);
361         }
362       mpc_free_str (str);
363 
364       test_default_random (expected, exp_min, exp_max, 128, 25);
365       str = mpc_get_str (base, 0, expected, MPC_RNDNN);
366       if (mpc_set_str (got, str, base, MPC_RNDNN) == -1
367           || mpc_cmp (got, expected) != 0)
368         {
369           printf ("Error: mpc_set_str o mpc_get_str != Id\n"
370                   "in base %u with str=\"%s\"\n", base, str);
371           MPC_OUT (expected);
372           printf ("     ");
373           MPC_OUT (got);
374           exit (1);
375         }
376       mpc_free_str (str);
377     }
378 
379 #ifdef HAVE_SETLOCALE
380   {
381     /* Check with ',' as a decimal point */
382     char *old_locale;
383 
384     old_locale = setlocale (LC_ALL, "de_DE");
385     if (old_locale != NULL)
386       {
387         str = mpc_get_str (10, 0, expected, MPC_RNDNN);
388         if (mpc_set_str (got, str, 10, MPC_RNDNN) == -1
389             || mpc_cmp (got, expected) != 0)
390           {
391             printf ("Error: mpc_set_str o mpc_get_str != Id\n"
392                     "with str=\"%s\"\n", str);
393             MPC_OUT (expected);
394             printf ("     ");
395             MPC_OUT (got);
396             exit (1);
397           }
398         mpc_free_str (str);
399 
400         setlocale (LC_ALL, old_locale);
401       }
402   }
403 #endif /* HAVE_SETLOCALE */
404 
405   /* the real part has a zero exponent in base ten (fixed in r439) */
406   mpc_set_prec (expected, 37);
407   mpc_set_prec (got, 37);
408   mpc_set_str (expected, "921FC04EDp-35  ", 16, MPFR_RNDN);
409   str = mpc_get_str (10, 0, expected, MPC_RNDNN);
410   if (mpc_set_str (got, str, 10, MPC_RNDNN) == -1
411       || mpc_cmp (got, expected) != 0)
412     {
413       printf ("Error: mpc_set_str o mpc_get_str != Id\n"
414               "with str=\"%s\"\n", str);
415       MPC_OUT (expected);
416       printf ("     ");
417       MPC_OUT (got);
418       exit (1);
419     }
420   mpc_free_str (str);
421 
422   str = mpc_get_str (1, 0, expected, MPC_RNDNN);
423   if (str != NULL)
424     {
425       printf ("Error: mpc_get_str with base==1 should fail\n");
426       exit (1);
427     }
428 
429   mpc_clear (expected);
430   mpc_clear (got);
431 }
432 
433 int
434 main (void)
435 {
436   test_start ();
437 
438   check_set ();
439   check_set_str (1024);
440 
441   test_end ();
442 
443   return 0;
444 }
445