1/* Test file for multiple mpfr.h inclusion and intmax_t related functions
2
3Copyright 2010-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#ifdef HAVE_CONFIG_H
24# include "config.h"
25#endif
26
27#if HAVE_STDINT_H
28
29#if _MPFR_EXP_FORMAT == 4
30/* If mpfr_exp_t is defined as intmax_t, intmax_t must be defined before
31   the inclusion of mpfr.h (this test doesn't use mpfr-impl.h). */
32# include <stdint.h>
33#endif
34
35#ifdef MPFR_USE_MINI_GMP
36#include "mpfr-test.h"
37#endif
38
39/* One of the goals of this test is to detect potential issues with the
40 * following case in user code:
41 *
42 * #include <some_lib.h>
43 * #include <stdint.h>
44 * #define MPFR_USE_INTMAX_T
45 * #include <mpfr.h>
46 *
47 * where some_lib.h has "#include <mpfr.h>". So, the mpfr.h header file
48 * is included multiple times, a first time without <stdint.h> before,
49 * and a second time with <stdint.h> support. We need to make sure that
50 * the second inclusion is not a no-op due to some #include guard. This
51 * was fixed in r7320.
52 *
53 * With mini-gmp, mpfr-impl.h is included first, but this should not
54 * affect this test.
55 *
56 * Note: If _MPFR_EXP_FORMAT == 4 (which is never the case by default),
57 * a part of the above check is not done because <stdint.h> is included
58 * before the first mpfr.h inclusion (see above).
59 *
60 * Moreover, assuming that this test is run on a platform that has
61 * <stdint.h> (most platforms do nowadays), without mini-gmp, this
62 * test also allows one to detect that mpfr.h can be included without
63 * any other inclusion before[*] (such as <stdio.h>). For instance,
64 * it can detect any unprotected use of FILE in the mpfr.h header
65 * file.
66 * [*] possibly except config.h when used, which is normally not the
67 *     case with a normal build. Anyway, if we decided to change that,
68 *     this inclusion would not change anything as config.h would only
69 *     have defines (such as HAVE_STDINT_H) currently provided as "-D"
70 *     compiler arguments.
71 */
72#include <mpfr.h>
73
74#include <stdint.h>
75
76#define MPFR_USE_INTMAX_T
77#include <mpfr.h>
78
79#include "mpfr-test.h"
80
81int
82main (void)
83{
84  mpfr_t x;
85  intmax_t j;
86
87  tests_start_mpfr ();
88
89  mpfr_init_set_ui (x, 1, MPFR_RNDN);
90  j = mpfr_get_uj (x, MPFR_RNDN);
91  mpfr_clear (x);
92  if (j != 1)
93    {
94#ifndef NPRINTF_J
95      printf ("Error: got %jd instead of 1.\n", j);
96#else
97      printf ("Error: did not get 1.\n");
98#endif
99      exit (1);
100    }
101
102  tests_end_mpfr ();
103  return 0;
104}
105
106#else  /* HAVE_STDINT_H */
107
108/* The test is disabled. */
109
110int
111main (void)
112{
113  return 77;
114}
115
116#endif  /* HAVE_STDINT_H */
117