1193326Sed/* talloc -- test file concerning memory allocation
2193326Sed
3193326SedCopyright 2015-2023 Free Software Foundation, Inc.
4193326SedContributed by the AriC and Caramba projects, INRIA.
5193326Sed
6193326SedThis file is part of the GNU MPFR Library.
7193326Sed
8193326SedThe GNU MPFR Library is free software; you can redistribute it and/or modify
9193326Sedit under the terms of the GNU Lesser General Public License as published by
10193326Sedthe Free Software Foundation; either version 3 of the License, or (at your
11193326Sedoption) any later version.
12193326Sed
13193326SedThe GNU MPFR Library is distributed in the hope that it will be useful, but
14193326SedWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15193326Sedor FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16193326SedLicense for more details.
17193326Sed
18193326SedYou should have received a copy of the GNU Lesser General Public License
19193326Sedalong with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20193326Sedhttps://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21193326Sed51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22193326Sed
23193326Sed#include "mpfr-test.h"
24193326Sed
25193326Sed/* We enable the test only when MPFR_ALLOCA_MAX is not greater than the
26193326Sed   default value: we do not want to make the test fail if the user has
27193326Sed   redefined MPFR_ALLOCA_MAX to a very large value. */
28193326Sed
29193326Sed/* Needed with --with-gmp-build */
30193326Sed#ifndef MPFR_ALLOCA_MAX
31193326Sed# define MPFR_ALLOCA_MAX 16384
32193326Sed#endif
33193326Sed
34193326Sed/* ISO C99 is needed by MPFR_DECL_INIT. */
35193326Sed#if __MPFR_STDC (199901L) && MPFR_ALLOCA_MAX <= 16384
36193326Sed
37193326Sedint
38193326Sedmain (void)
39193326Sed{
40193326Sed  MPFR_DECL_INIT (x, 4 + MPFR_ALLOCA_MAX * CHAR_BIT);
41193326Sed  MPFR_DECL_INIT (y, 8 + MPFR_ALLOCA_MAX * CHAR_BIT);
42193326Sed
43193326Sed  tests_start_mpfr ();
44193326Sed
45193326Sed  /* We do not want to use mpfr_add1sp because if MPFR_WANT_ASSERT >= 2,
46193326Sed     mpfr_init is used. The following test is based on the fact that
47193326Sed     MPFR_TMP_LIMBS_ALLOC is used in mpfr_add1 before any other form
48193326Sed     of memory allocation. In r9454, this crashes because the memory
49193326Sed     allocation function used by this macro (under the condition that
50193326Sed     the size is > MPFR_ALLOCA_MAX) isn't defined yet. This bug comes
51193326Sed     from r8813.
52193326Sed     WARNING! Do not add MPFR calls before this test. Otherwise the
53193326Sed     mentioned problem may no longer be actually tested, making this
54212904Sdim     test useless. For other allocation tests, it is better to use a
55193326Sed     different test file. */
56193326Sed  mpfr_set_ui (x, 1, MPFR_RNDN);
57193326Sed  mpfr_set_ui (y, 2, MPFR_RNDN);
58218893Sdim  mpfr_add (x, x, y, MPFR_RNDN);
59193326Sed
60193326Sed  tests_end_mpfr ();
61193326Sed  return 0;
62193326Sed}
63193326Sed
64193326Sed#else
65206275Srdivacky
66234353Sdimint
67234353Sdimmain (void)
68234353Sdim{
69193326Sed  return 77;
70193326Sed}
71193326Sed
72193326Sed#endif
73193326Sed