xref: /netbsd-src/external/lgpl3/mpc/dist/tests/tnorm.c (revision 39f28e1e142c5bfb6be935a49cb55e2287fec7ea)
1 /* tnorm -- test file for mpc_norm.
2 
3 Copyright (C) 2008, 2011, 2012, 2013 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 "mpc-tests.h"
22 
23 static void
test_underflow(void)24 test_underflow (void)
25 {
26   mpfr_exp_t emin = mpfr_get_emin ();
27   mpc_t z;
28   mpfr_t f;
29   int inex;
30 
31   mpfr_set_emin (-1); /* smallest positive number is 0.5*2^emin = 0.25 */
32   mpc_init2 (z, 10);
33   mpfr_set_ui_2exp (mpc_realref (z), 1023, -11, MPFR_RNDN); /* exact */
34   mpfr_set_ui_2exp (mpc_imagref (z), 1023, -11, MPFR_RNDN); /* exact */
35   mpfr_init2 (f, 10);
36 
37   inex = mpc_norm (f, z, MPFR_RNDZ); /* should give 511/1024 */
38   if (inex >= 0)
39     {
40       printf ("Error in underflow case (1)\n");
41       printf ("expected inex < 0, got %d\n", inex);
42       exit (1);
43     }
44   if (mpfr_cmp_ui_2exp (f, 511, -10) != 0)
45     {
46       printf ("Error in underflow case (1)\n");
47       printf ("got      ");
48       mpfr_dump (f);
49       printf ("expected ");
50       mpfr_set_ui_2exp (f, 511, -10, MPFR_RNDZ);
51       mpfr_dump (f);
52       exit (1);
53     }
54 
55   inex = mpc_norm (f, z, MPFR_RNDN); /* should give 511/1024 */
56   if (inex >= 0)
57     {
58       printf ("Error in underflow case (2)\n");
59       printf ("expected inex < 0, got %d\n", inex);
60       exit (1);
61     }
62   if (mpfr_cmp_ui_2exp (f, 511, -10) != 0)
63     {
64       printf ("Error in underflow case (2)\n");
65       printf ("got      ");
66       mpfr_dump (f);
67       printf ("expected ");
68       mpfr_set_ui_2exp (f, 511, -10, MPFR_RNDZ);
69       mpfr_dump (f);
70       exit (1);
71     }
72 
73   inex = mpc_norm (f, z, MPFR_RNDU); /* should give 1023/2048 */
74   if (inex <= 0)
75     {
76       printf ("Error in underflow case (3)\n");
77       printf ("expected inex > 0, got %d\n", inex);
78       exit (1);
79     }
80   if (mpfr_cmp_ui_2exp (f, 1023, -11) != 0)
81     {
82       printf ("Error in underflow case (3)\n");
83       printf ("got      ");
84       mpfr_dump (f);
85       printf ("expected ");
86       mpfr_set_ui_2exp (f, 1023, -11, MPFR_RNDZ);
87       mpfr_dump (f);
88       exit (1);
89     }
90 
91   mpc_clear (z);
92   mpfr_clear (f);
93   mpfr_set_emin (emin);
94 }
95 
96 #define MPC_FUNCTION_CALL                                               \
97   P[0].mpfr_inex = mpc_norm (P[1].mpfr, P[2].mpc, P[3].mpfr_rnd)
98 
99 #include "data_check.tpl"
100 #include "tgeneric.tpl"
101 
102 int
main(void)103 main (void)
104 {
105   test_start ();
106 
107   data_check_template ("norm.dsc", "norm.dat");
108 
109   tgeneric_template ("norm.dsc", 2, 1024, 1, 4096);
110 
111   test_underflow ();
112 
113   test_end ();
114 
115   return 0;
116 }
117