1/* Test mpf_cmp_d.
2
3Copyright 2001, 2003, 2003, 2005 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "gmp-impl.h"
25#include "tests.h"
26
27
28#define SGN(n)  ((n) > 0 ? 1 : (n) < 0 ? -1 : 0)
29
30void
31check_one (const char *name, mpf_srcptr x, double y, int cmp)
32{
33  int   got;
34
35  got = mpf_cmp_d (x, y);
36  if (SGN(got) != cmp)
37    {
38      int i;
39      printf    ("mpf_cmp_d wrong (from %s)\n", name);
40      printf    ("  got  %d\n", got);
41      printf    ("  want %d\n", cmp);
42      mpf_trace ("  x", x);
43      printf    ("  y %g\n", y);
44      mp_trace_base=-16;
45      mpf_trace ("  x", x);
46      printf    ("  y %g\n", y);
47      printf    ("  y");
48      for (i = 0; i < sizeof(y); i++)
49        printf (" %02X", (unsigned) ((unsigned char *) &y)[i]);
50      printf ("\n");
51      abort ();
52    }
53}
54
55void
56check_infinity (void)
57{
58  mpf_t   x;
59  double  y = tests_infinity_d ();
60  if (y == 0.0)
61    return;
62
63  mpf_init (x);
64
65  /* 0 cmp inf */
66  mpf_set_ui (x, 0L);
67  check_one ("check_infinity", x,  y, -1);
68  check_one ("check_infinity", x, -y,  1);
69
70  /* 123 cmp inf */
71  mpf_set_ui (x, 123L);
72  check_one ("check_infinity", x,  y, -1);
73  check_one ("check_infinity", x, -y,  1);
74
75  /* -123 cmp inf */
76  mpf_set_si (x, -123L);
77  check_one ("check_infinity", x,  y, -1);
78  check_one ("check_infinity", x, -y,  1);
79
80  /* 2^5000 cmp inf */
81  mpf_set_ui (x, 1L);
82  mpf_mul_2exp (x, x, 5000L);
83  check_one ("check_infinity", x,  y, -1);
84  check_one ("check_infinity", x, -y,  1);
85
86  /* -2^5000 cmp inf */
87  mpf_neg (x, x);
88  check_one ("check_infinity", x,  y, -1);
89  check_one ("check_infinity", x, -y,  1);
90
91  mpf_clear (x);
92}
93
94int
95main (int argc, char *argv[])
96{
97  tests_start ();
98
99  check_infinity ();
100
101  tests_end ();
102  exit (0);
103}
104