xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tstdint.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1d59437c0Smrg /* Test file for multiple mpfr.h inclusion and intmax_t related functions
2d59437c0Smrg 
3*ba125506Smrg Copyright 2010-2023 Free Software Foundation, Inc.
4efdec83bSmrg Contributed by the AriC and Caramba projects, INRIA.
5d59437c0Smrg 
6d59437c0Smrg This file is part of the GNU MPFR Library.
7d59437c0Smrg 
8d59437c0Smrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9d59437c0Smrg it under the terms of the GNU Lesser General Public License as published by
10d59437c0Smrg the Free Software Foundation; either version 3 of the License, or (at your
11d59437c0Smrg option) any later version.
12d59437c0Smrg 
13d59437c0Smrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14d59437c0Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15d59437c0Smrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16d59437c0Smrg License for more details.
17d59437c0Smrg 
18d59437c0Smrg You should have received a copy of the GNU Lesser General Public License
19d59437c0Smrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
202ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21d59437c0Smrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22d59437c0Smrg 
23*ba125506Smrg #ifdef HAVE_CONFIG_H
24*ba125506Smrg # include "config.h"
25*ba125506Smrg #endif
26*ba125506Smrg 
27d59437c0Smrg #if HAVE_STDINT_H
28d59437c0Smrg 
29d59437c0Smrg #if _MPFR_EXP_FORMAT == 4
30d59437c0Smrg /* If mpfr_exp_t is defined as intmax_t, intmax_t must be defined before
31d59437c0Smrg    the inclusion of mpfr.h (this test doesn't use mpfr-impl.h). */
32d59437c0Smrg # include <stdint.h>
33d59437c0Smrg #endif
34d59437c0Smrg 
35299c6f0cSmrg #ifdef MPFR_USE_MINI_GMP
36299c6f0cSmrg #include "mpfr-test.h"
37299c6f0cSmrg #endif
38299c6f0cSmrg 
39299c6f0cSmrg /* One of the goals of this test is to detect potential issues with the
40299c6f0cSmrg  * following case in user code:
41299c6f0cSmrg  *
42299c6f0cSmrg  * #include <some_lib.h>
43299c6f0cSmrg  * #include <stdint.h>
44299c6f0cSmrg  * #define MPFR_USE_INTMAX_T
45299c6f0cSmrg  * #include <mpfr.h>
46299c6f0cSmrg  *
47299c6f0cSmrg  * where some_lib.h has "#include <mpfr.h>". So, the mpfr.h header file
48299c6f0cSmrg  * is included multiple times, a first time without <stdint.h> before,
49299c6f0cSmrg  * and a second time with <stdint.h> support. We need to make sure that
50299c6f0cSmrg  * the second inclusion is not a no-op due to some #include guard. This
51299c6f0cSmrg  * was fixed in r7320.
52299c6f0cSmrg  *
53299c6f0cSmrg  * With mini-gmp, mpfr-impl.h is included first, but this should not
54299c6f0cSmrg  * affect this test.
55299c6f0cSmrg  *
56299c6f0cSmrg  * Note: If _MPFR_EXP_FORMAT == 4 (which is never the case by default),
57299c6f0cSmrg  * a part of the above check is not done because <stdint.h> is included
58299c6f0cSmrg  * before the first mpfr.h inclusion (see above).
59*ba125506Smrg  *
60*ba125506Smrg  * Moreover, assuming that this test is run on a platform that has
61*ba125506Smrg  * <stdint.h> (most platforms do nowadays), without mini-gmp, this
62*ba125506Smrg  * test also allows one to detect that mpfr.h can be included without
63*ba125506Smrg  * any other inclusion before[*] (such as <stdio.h>). For instance,
64*ba125506Smrg  * it can detect any unprotected use of FILE in the mpfr.h header
65*ba125506Smrg  * file.
66*ba125506Smrg  * [*] possibly except config.h when used, which is normally not the
67*ba125506Smrg  *     case with a normal build. Anyway, if we decided to change that,
68*ba125506Smrg  *     this inclusion would not change anything as config.h would only
69*ba125506Smrg  *     have defines (such as HAVE_STDINT_H) currently provided as "-D"
70*ba125506Smrg  *     compiler arguments.
71299c6f0cSmrg  */
72d59437c0Smrg #include <mpfr.h>
73d59437c0Smrg 
74d59437c0Smrg #include <stdint.h>
75d59437c0Smrg 
76d59437c0Smrg #define MPFR_USE_INTMAX_T
77d59437c0Smrg #include <mpfr.h>
78d59437c0Smrg 
79efdec83bSmrg #include "mpfr-test.h"
80efdec83bSmrg 
81d59437c0Smrg int
main(void)82d59437c0Smrg main (void)
83d59437c0Smrg {
84d59437c0Smrg   mpfr_t x;
85d59437c0Smrg   intmax_t j;
86d59437c0Smrg 
87efdec83bSmrg   tests_start_mpfr ();
88efdec83bSmrg 
89d59437c0Smrg   mpfr_init_set_ui (x, 1, MPFR_RNDN);
90d59437c0Smrg   j = mpfr_get_uj (x, MPFR_RNDN);
91d59437c0Smrg   mpfr_clear (x);
92efdec83bSmrg   if (j != 1)
93efdec83bSmrg     {
942ba2404bSmrg #ifndef NPRINTF_J
952ba2404bSmrg       printf ("Error: got %jd instead of 1.\n", j);
96299c6f0cSmrg #else
97299c6f0cSmrg       printf ("Error: did not get 1.\n");
98299c6f0cSmrg #endif
99efdec83bSmrg       exit (1);
100efdec83bSmrg     }
101efdec83bSmrg 
102efdec83bSmrg   tests_end_mpfr ();
103efdec83bSmrg   return 0;
104d59437c0Smrg }
105d59437c0Smrg 
106d59437c0Smrg #else  /* HAVE_STDINT_H */
107d59437c0Smrg 
108d59437c0Smrg /* The test is disabled. */
109d59437c0Smrg 
110d59437c0Smrg int
main(void)111d59437c0Smrg main (void)
112d59437c0Smrg {
113d59437c0Smrg   return 77;
114d59437c0Smrg }
115d59437c0Smrg 
116d59437c0Smrg #endif  /* HAVE_STDINT_H */
117