source: trunk/third/gmp/tests/mpz/t-sqrtrem.c @ 18191

Revision 18191, 2.8 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem.
2
3Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation,
4Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP 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 2.1 of the License, or (at your
11option) any later version.
12
13The GNU MP 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 MP Library; see the file COPYING.LIB.  If not, write to
20the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21MA 02111-1307, USA. */
22
23#include <stdio.h>
24#include <stdlib.h>
25
26#include "gmp.h"
27#include "gmp-impl.h"
28#include "tests.h"
29
30void dump_abort _PROTO ((mpz_t, mpz_t, mpz_t));
31void debug_mp _PROTO ((mpz_t, int));
32
33int
34main (int argc, char **argv)
35{
36  mpz_t x2;
37  mpz_t x, rem;
38  mpz_t temp, temp2;
39  mp_size_t x2_size;
40  int i;
41  int reps = 20000;
42  gmp_randstate_ptr rands;
43  mpz_t bs;
44  unsigned long size_range;
45
46  tests_start ();
47  rands = RANDS;
48
49  mpz_init (bs);
50
51  if (argc == 2)
52    reps = atoi (argv[1]);
53
54  mpz_init (x2);
55  mpz_init (x);
56  mpz_init (rem);
57  mpz_init (temp);
58  mpz_init (temp2);
59
60  for (i = 0; i < reps; i++)
61    {
62      mpz_urandomb (bs, rands, 32);
63      size_range = mpz_get_ui (bs) % 12 + 2; /* 0..8191 bit operands */
64
65      mpz_urandomb (bs, rands, size_range);
66      x2_size = mpz_get_ui (bs);
67      mpz_rrandomb (x2, rands, x2_size);
68
69      /* printf ("%ld\n", SIZ (x2)); */
70
71      mpz_sqrtrem (x, rem, x2);
72      mpz_mul (temp, x, x);
73
74      /* Is square of result > argument?  */
75      if (mpz_cmp (temp, x2) > 0)
76        dump_abort (x2, x, rem);
77
78      mpz_add_ui (temp2, x, 1);
79      mpz_mul (temp2, temp2, temp2);
80
81      /* Is square of (result + 1) <= argument?  */
82      if (mpz_cmp (temp2, x2) <= 0)
83        dump_abort (x2, x, rem);
84
85      mpz_add (temp2, temp, rem);
86
87      /* Is the remainder wrong?  */
88      if (mpz_cmp (x2, temp2) != 0)
89        dump_abort (x2, x, rem);
90    }
91
92  mpz_clear (bs);
93  mpz_clear (x2);
94  mpz_clear (x);
95  mpz_clear (rem);
96  mpz_clear (temp);
97  mpz_clear (temp2);
98
99  tests_end ();
100  exit (0);
101}
102
103void
104dump_abort (mpz_t x2, mpz_t x, mpz_t rem)
105{
106  fprintf (stderr, "ERROR\n");
107  fprintf (stderr, "x2        = "); debug_mp (x2, -16);
108  fprintf (stderr, "x         = "); debug_mp (x, -16);
109  fprintf (stderr, "remainder = "); debug_mp (rem, -16);
110  abort();
111}
112
113void
114debug_mp (mpz_t x, int base)
115{
116  mpz_out_str (stderr, base, x); fputc ('\n', stderr);
117}
Note: See TracBrowser for help on using the repository browser.