1/* Test file for mpfr_log2.
2
3Copyright 2001-2002, 2004, 2006-2023 Free Software Foundation, Inc.
4Contributed by the AriC and Caramba projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#include "mpfr-test.h"
24
25#define TEST_FUNCTION mpfr_log2
26#define TEST_RANDOM_POS 8
27#include "tgeneric.c"
28
29static void
30special (void)
31{
32  mpfr_t x;
33  int inex, r;
34
35  mpfr_init (x);
36
37  RND_LOOP (r)
38    {
39      mpfr_set_nan (x);
40      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
41      MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
42
43      mpfr_set_inf (x, -1);
44      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
45      MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
46
47      mpfr_set_inf (x, 1);
48      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
49      MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0);
50
51      mpfr_set_ui (x, 0, MPFR_RNDN);
52      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
53      MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0);
54      mpfr_set_ui (x, 0, MPFR_RNDN);
55      mpfr_neg (x, x, MPFR_RNDN);
56      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
57      MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0);
58
59      mpfr_set_si (x, -1, MPFR_RNDN);
60      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
61      MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
62
63      mpfr_set_si (x, 1, MPFR_RNDN);
64      inex = mpfr_log2 (x, x, (mpfr_rnd_t) r);
65      MPFR_ASSERTN (MPFR_IS_ZERO (x) && MPFR_IS_POS (x) && inex == 0);
66    }
67
68  mpfr_clear (x);
69}
70
71int
72main (int argc, char *argv[])
73{
74  tests_start_mpfr ();
75
76  special ();
77
78  test_generic (MPFR_PREC_MIN, 100, 30);
79
80  data_check ("data/log2", mpfr_log2, "mpfr_log2");
81  bad_cases (mpfr_log2, mpfr_exp2, "mpfr_log2",
82             256, -30, 30, 4, 128, 800, 50);
83
84  tests_end_mpfr ();
85  return 0;
86}
87