Import Upstream version 0.8.2
[platform/upstream/mpc.git] / tests / tpow_ui.c
1 /* test file for mpc_pow_ui.
2
3 Copyright (C) 2009, 2010 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 <limits.h> /* for CHAR_BIT */
23 #include "mpc-tests.h"
24
25 static void
26 compare_mpc_pow (mp_prec_t pmax, int iter, unsigned long nbits)
27 {
28   mp_prec_t p;
29   mpc_t x, y, z, t;
30   unsigned long n;
31   int i, inex_pow, inex_pow_ui;
32   gmp_randstate_t state;
33   mpc_rnd_t rnd;
34
35   gmp_randinit_default (state);
36   mpc_init3 (y, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN);
37   for (p = MPFR_PREC_MIN; p <= pmax; p++)
38     for (i = 0; i < iter; i++)
39       {
40         mpc_init2 (x, p);
41         mpc_init2 (z, p);
42         mpc_init2 (t, p);
43         mpc_urandom (x, state);
44         n = gmp_urandomb_ui (state, nbits); /* 0 <= n < 2^nbits */
45         mpc_set_ui (y, n, MPC_RNDNN);
46         for (rnd = 0; rnd < 16; rnd ++)
47           {
48             inex_pow = mpc_pow (z, x, y, rnd);
49             inex_pow_ui = mpc_pow_ui (t, x, n, rnd);
50             if (mpc_cmp (z, t) != 0)
51               {
52                 printf ("mpc_pow and mpc_pow_ui differ for x=");
53                 mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
54                 printf (" n=%lu\n", n);
55                 printf ("mpc_pow gives ");
56                 mpc_out_str (stdout, 10, 0, z, MPC_RNDNN);
57                 printf ("\nmpc_pow_ui gives ");
58                 mpc_out_str (stdout, 10, 0, t, MPC_RNDNN);
59                 printf ("\n");
60                 exit (1);
61               }
62             if (inex_pow != inex_pow_ui)
63               {
64                 printf ("mpc_pow and mpc_pow_ui gives different flags for x=");
65                 mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
66                 printf (" n=%lu\n", n);
67                 printf ("mpc_pow gives %d\n", inex_pow);
68                 printf ("mpc_pow_ui gives %d\n", inex_pow_ui);
69                 exit (1);
70               }
71           }
72         mpc_clear (x);
73         mpc_clear (z);
74         mpc_clear (t);
75       }
76   mpc_clear (y);
77   gmp_randclear (state);
78 }
79
80 int
81 main (int argc, char *argv[])
82 {
83   mpc_t z;
84
85   DECL_FUNC (CCU, f, mpc_pow_ui);
86
87   if (argc != 1)
88     {
89       mp_prec_t p;
90       unsigned long n, k;
91       mpc_t res;
92       if (argc != 3 && argc != 4)
93         {
94           printf ("Usage: tpow_ui precision exponent [k]\n");
95           exit (1);
96         }
97       p = atoi (argv[1]);
98       n = atoi (argv[2]);
99       k = (argc > 3) ? atoi (argv[3]) : 1;
100       mpc_init2 (z, p);
101       mpc_init2 (res, p);
102       mpfr_const_pi (mpc_realref (z), GMP_RNDN);
103       mpfr_div_2exp (mpc_realref (z), mpc_realref (z), 2, GMP_RNDN);
104       mpfr_const_log2 (mpc_imagref (z), GMP_RNDN);
105       while (k--)
106         mpc_pow_ui (res, z, n, MPC_RNDNN);
107       mpc_clear (z);
108       mpc_clear (res);
109       return 0;
110     }
111
112   test_start ();
113   data_check (f, "pow_ui.dat");
114
115   mpc_init2 (z, 5);
116   mpc_set_ui_ui (z, 3, 2, MPC_RNDNN);
117   mpc_pow_ui (z, z, 3, MPC_RNDNN);
118   if (mpc_cmp_si_si (z, -9, 46) != 0)
119     {
120       printf ("Error for mpc_pow_ui (1)\n");
121       printf ("expected (-9,46)\n");
122       printf ("got ");
123       mpc_out_str (stdout, 10, 0, z, MPC_RNDNN);
124       printf ("\n");
125       exit (1);
126     }
127   mpc_clear (z);
128
129   compare_mpc_pow (100, 5, 19);
130
131   test_end ();
132
133   return 0;
134 }