1/* Test file for __gmpfr_isqrt and __gmpfr_cuberoot internal functions.
2
3Copyright 2007-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
25static void
26tst_isqrt (unsigned long n, unsigned long r)
27{
28  unsigned long i;
29
30  i = __gmpfr_isqrt (n);
31  if (i != r)
32    {
33      printf ("Error in __gmpfr_isqrt (%lu): got %lu instead of %lu\n",
34              n, i, r);
35      exit (1);
36    }
37}
38
39static void
40tst_icbrt (unsigned long n, unsigned long r)
41{
42  unsigned long i;
43
44  i = __gmpfr_cuberoot (n);
45  if (i != r)
46    {
47      printf ("Error in __gmpfr_cuberoot (%lu): got %lu instead of %lu\n",
48              n, i, r);
49      exit (1);
50    }
51}
52
53int
54main (void)
55{
56  unsigned long c, i;
57
58  tests_start_mpfr ();
59
60  tst_isqrt (0, 0);
61  tst_isqrt (1, 1);
62  tst_isqrt (2, 1);
63  for (i = 2; i <= 65535; i++)
64    {
65      tst_isqrt (i * i - 1, i - 1);
66      tst_isqrt (i * i, i);
67    }
68  tst_isqrt (4294967295UL, 65535);
69
70  tst_icbrt (0, 0);
71  tst_icbrt (1, 1);
72  tst_icbrt (2, 1);
73  tst_icbrt (3, 1);
74  for (i = 2; i <= 1625; i++)
75    {
76      c = i * i * i;
77      tst_icbrt (c - 4, i - 1);
78      tst_icbrt (c - 3, i - 1);
79      tst_icbrt (c - 2, i - 1);
80      tst_icbrt (c - 1, i - 1);
81      tst_icbrt (c, i);
82      tst_icbrt (c + 1, i);
83      tst_icbrt (c + 2, i);
84      tst_icbrt (c + 3, i);
85      tst_icbrt (c + 4, i);
86    }
87  tst_icbrt (4294967295UL, 1625);
88
89  tests_end_mpfr ();
90  return 0;
91}
92