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