~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/tests/mpz/t-sqrtrem.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpz_add, mpz_add_ui, mpz_cmp, mpz_cmp, mpz_mul, mpz_sqrtrem.
 
2
 
 
3
Copyright 1991, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the GNU MP Library.
 
6
 
 
7
The GNU MP Library is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU Lesser General Public License as published by
 
9
the Free Software Foundation; either version 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
The GNU MP Library is distributed in the hope that it will be useful, but
 
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
15
License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
20
MA 02111-1307, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
 
 
25
#include "gmp.h"
 
26
#include "gmp-impl.h"
 
27
#include "tests.h"
 
28
 
 
29
void dump_abort _PROTO ((mpz_t, mpz_t, mpz_t));
 
30
void debug_mp _PROTO ((mpz_t, int));
 
31
 
 
32
int
 
33
main (int argc, char **argv)
 
34
{
 
35
  mpz_t x2;
 
36
  mpz_t x, rem;
 
37
  mpz_t temp, temp2;
 
38
  mp_size_t x2_size;
 
39
  int i;
 
40
  int reps = 20000;
 
41
  gmp_randstate_ptr rands;
 
42
  mpz_t bs;
 
43
  unsigned long size_range;
 
44
 
 
45
  tests_start ();
 
46
  rands = RANDS;
 
47
 
 
48
  mpz_init (bs);
 
49
 
 
50
  if (argc == 2)
 
51
     gmp_randseed_ui (rands, atoi (argv[1]));
 
52
 
 
53
  mpz_init (x2);
 
54
  mpz_init (x);
 
55
  mpz_init (rem);
 
56
  mpz_init (temp);
 
57
  mpz_init (temp2);
 
58
 
 
59
  for (i = 0; i < reps; i++)
 
60
    {
 
61
      mpz_urandomb (bs, rands, 32);
 
62
      size_range = mpz_get_ui (bs) % 12 + 2; /* 0..8191 bit operands */
 
63
 
 
64
      mpz_urandomb (bs, rands, size_range);
 
65
      x2_size = mpz_get_ui (bs);
 
66
      mpz_rrandomb (x2, rands, x2_size);
 
67
 
 
68
      /* printf ("%ld\n", SIZ (x2)); */
 
69
 
 
70
      mpz_sqrtrem (x, rem, x2);
 
71
      mpz_mul (temp, x, x);
 
72
 
 
73
      /* Is square of result > argument?  */
 
74
      if (mpz_cmp (temp, x2) > 0)
 
75
        dump_abort (x2, x, rem);
 
76
 
 
77
      mpz_add_ui (temp2, x, 1);
 
78
      mpz_mul (temp2, temp2, temp2);
 
79
 
 
80
      /* Is square of (result + 1) <= argument?  */
 
81
      if (mpz_cmp (temp2, x2) <= 0)
 
82
        dump_abort (x2, x, rem);
 
83
 
 
84
      mpz_add (temp2, temp, rem);
 
85
 
 
86
      /* Is the remainder wrong?  */
 
87
      if (mpz_cmp (x2, temp2) != 0)
 
88
        dump_abort (x2, x, rem);
 
89
    }
 
90
 
 
91
  mpz_clear (bs);
 
92
  mpz_clear (x2);
 
93
  mpz_clear (x);
 
94
  mpz_clear (rem);
 
95
  mpz_clear (temp);
 
96
  mpz_clear (temp2);
 
97
 
 
98
  tests_end ();
 
99
  exit (0);
 
100
}
 
101
 
 
102
void
 
103
dump_abort (mpz_t x2, mpz_t x, mpz_t rem)
 
104
{
 
105
  fprintf (stderr, "ERROR\n");
 
106
  fprintf (stderr, "x2        = "); debug_mp (x2, -16);
 
107
  fprintf (stderr, "x         = "); debug_mp (x, -16);
 
108
  fprintf (stderr, "remainder = "); debug_mp (rem, -16);
 
109
  abort();
 
110
}
 
111
 
 
112
void
 
113
debug_mp (mpz_t x, int base)
 
114
{
 
115
  mpz_out_str (stderr, base, x); fputc ('\n', stderr);
 
116
}