xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/talloc.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1299c6f0cSmrg /* talloc -- test file concerning memory allocation
2299c6f0cSmrg 
3*ba125506Smrg Copyright 2015-2023 Free Software Foundation, Inc.
4299c6f0cSmrg Contributed by the AriC and Caramba projects, INRIA.
5299c6f0cSmrg 
6299c6f0cSmrg This file is part of the GNU MPFR Library.
7299c6f0cSmrg 
8299c6f0cSmrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9299c6f0cSmrg it under the terms of the GNU Lesser General Public License as published by
10299c6f0cSmrg the Free Software Foundation; either version 3 of the License, or (at your
11299c6f0cSmrg option) any later version.
12299c6f0cSmrg 
13299c6f0cSmrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14299c6f0cSmrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15299c6f0cSmrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16299c6f0cSmrg License for more details.
17299c6f0cSmrg 
18299c6f0cSmrg You should have received a copy of the GNU Lesser General Public License
19299c6f0cSmrg 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.,
21299c6f0cSmrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22299c6f0cSmrg 
23299c6f0cSmrg #include "mpfr-test.h"
24299c6f0cSmrg 
25299c6f0cSmrg /* We enable the test only when MPFR_ALLOCA_MAX is not greater than the
26299c6f0cSmrg    default value: we do not want to make the test fail if the user has
27299c6f0cSmrg    redefined MPFR_ALLOCA_MAX to a very large value. */
28299c6f0cSmrg 
29299c6f0cSmrg /* Needed with --with-gmp-build */
30299c6f0cSmrg #ifndef MPFR_ALLOCA_MAX
31299c6f0cSmrg # define MPFR_ALLOCA_MAX 16384
32299c6f0cSmrg #endif
33299c6f0cSmrg 
34299c6f0cSmrg /* ISO C99 is needed by MPFR_DECL_INIT. */
35299c6f0cSmrg #if __MPFR_STDC (199901L) && MPFR_ALLOCA_MAX <= 16384
36299c6f0cSmrg 
37299c6f0cSmrg int
main(void)38299c6f0cSmrg main (void)
39299c6f0cSmrg {
40299c6f0cSmrg   MPFR_DECL_INIT (x, 4 + MPFR_ALLOCA_MAX * CHAR_BIT);
41299c6f0cSmrg   MPFR_DECL_INIT (y, 8 + MPFR_ALLOCA_MAX * CHAR_BIT);
42299c6f0cSmrg 
43299c6f0cSmrg   tests_start_mpfr ();
44299c6f0cSmrg 
45299c6f0cSmrg   /* We do not want to use mpfr_add1sp because if MPFR_WANT_ASSERT >= 2,
46299c6f0cSmrg      mpfr_init is used. The following test is based on the fact that
47299c6f0cSmrg      MPFR_TMP_LIMBS_ALLOC is used in mpfr_add1 before any other form
48299c6f0cSmrg      of memory allocation. In r9454, this crashes because the memory
49299c6f0cSmrg      allocation function used by this macro (under the condition that
50299c6f0cSmrg      the size is > MPFR_ALLOCA_MAX) isn't defined yet. This bug comes
51299c6f0cSmrg      from r8813.
52299c6f0cSmrg      WARNING! Do not add MPFR calls before this test. Otherwise the
53299c6f0cSmrg      mentioned problem may no longer be actually tested, making this
54299c6f0cSmrg      test useless. For other allocation tests, it is better to use a
55299c6f0cSmrg      different test file. */
56299c6f0cSmrg   mpfr_set_ui (x, 1, MPFR_RNDN);
57299c6f0cSmrg   mpfr_set_ui (y, 2, MPFR_RNDN);
58299c6f0cSmrg   mpfr_add (x, x, y, MPFR_RNDN);
59299c6f0cSmrg 
60299c6f0cSmrg   tests_end_mpfr ();
61299c6f0cSmrg   return 0;
62299c6f0cSmrg }
63299c6f0cSmrg 
64299c6f0cSmrg #else
65299c6f0cSmrg 
66299c6f0cSmrg int
main(void)67299c6f0cSmrg main (void)
68299c6f0cSmrg {
69299c6f0cSmrg   return 77;
70299c6f0cSmrg }
71299c6f0cSmrg 
72299c6f0cSmrg #endif
73