xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tisqrt.c (revision c34236556bea94afcaca1782d7d228301edc3ea0)
1 /* Test file for __gmpfr_isqrt and __gmpfr_cuberoot internal functions.
2 
3 Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 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 
28 static void
29 tst_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 
42 static void
43 tst_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 
56 int
57 main (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