1/* Copyright 2012 Free Software Foundation, Inc.
2
3This file is part of the GNU MP Library test suite.
4
5The GNU MP Library test suite is free software; you can redistribute it
6and/or modify it under the terms of the GNU General Public License as
7published by the Free Software Foundation; either version 3 of the License,
8or (at your option) any later version.
9
10The GNU MP Library test suite is distributed in the hope that it will be
11useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
13Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
17
18
19#include <stdlib.h>		/* for strtol */
20#include <stdio.h>		/* for printf */
21
22#include "gmp-impl.h"
23#include "longlong.h"
24#include "tests/tests.h"
25
26#define MAX_LIMBS 150
27#define COUNT 500
28
29int
30main (int argc, char **argv)
31{
32  gmp_randstate_ptr rands;
33
34  mp_ptr ap, rp, pp, scratch;
35  int count = COUNT;
36  unsigned i;
37  TMP_DECL;
38
39  TMP_MARK;
40
41  TESTS_REPS (count, argv, argc);
42
43  tests_start ();
44  rands = RANDS;
45
46  ap = TMP_ALLOC_LIMBS (MAX_LIMBS);
47  rp = TMP_ALLOC_LIMBS (MAX_LIMBS);
48  pp = TMP_ALLOC_LIMBS (MAX_LIMBS);
49  scratch = TMP_ALLOC_LIMBS (3*MAX_LIMBS); /* For mpn_powlo */
50
51  for (i = 0; i < count; i++)
52    {
53      mp_size_t n;
54      mp_limb_t k;
55      int c;
56
57      n = 1 + gmp_urandomm_ui (rands, MAX_LIMBS);
58
59      if (i & 1)
60	mpn_random2 (ap, n);
61      else
62	mpn_random (ap, n);
63
64      ap[0] |= 1;
65
66      if (i < 100)
67	k = 3 + 2*i;
68      else
69	{
70	  mpn_random (&k, 1);
71	  if (k < 3)
72	    k = 3;
73	  else
74	    k |= 1;
75	}
76      mpn_broot (rp, ap, n, k);
77      mpn_powlo (pp, rp, &k, 1, n, scratch);
78
79      MPN_CMP (c, ap, pp, n);
80      if (c != 0)
81	{
82	  gmp_fprintf (stderr,
83		       "mpn_broot returned bad result: %u limbs\n",
84		       (unsigned) n);
85	  gmp_fprintf (stderr, "k   = %Mx\n", k);
86	  gmp_fprintf (stderr, "a   = %Nx\n", ap, n);
87	  gmp_fprintf (stderr, "r   = %Nx\n", rp, n);
88	  gmp_fprintf (stderr, "r^k = %Nx\n", pp, n);
89	  abort ();
90	}
91    }
92
93  mpn_broot (rp, ap, MAX_LIMBS, 1);
94  if (mpn_cmp (ap, rp, MAX_LIMBS) != 0)
95    {
96      gmp_fprintf (stderr,
97		   "mpn_broot returned bad result: %u limbs\n",
98		   (unsigned) MAX_LIMBS);
99      gmp_fprintf (stderr, "k   = %Mx\n", 1);
100      gmp_fprintf (stderr, "a   = %Nx\n", ap, MAX_LIMBS);
101      gmp_fprintf (stderr, "r   = %Nx\n", rp, MAX_LIMBS);
102      abort ();
103    }
104
105  TMP_FREE;
106  tests_end ();
107  return 0;
108}
109