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

Revision 18191, 3.7 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_divisible_p and mpz_divisible_ui_p */
2
3/*
4Copyright 2001 Free Software Foundation, Inc.
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
24#include <stdio.h>
25#include <stdlib.h>
26
27#include "gmp.h"
28#include "gmp-impl.h"
29#include "tests.h"
30
31
32void
33check_one (mpz_srcptr a, mpz_srcptr d, int want)
34{
35  int   got;
36
37  if (mpz_fits_ulong_p (d))
38    {
39      unsigned long  u = mpz_get_ui (d);
40      got = (mpz_divisible_ui_p (a, u) != 0);
41      if (want != got)
42        {
43          printf ("mpz_divisible_ui_p wrong\n");
44          printf ("   expected %d got %d\n", want, got);
45          mpz_trace ("   a", a);
46          printf ("   d=%lu\n", u);
47          mp_trace_base = -16;
48          mpz_trace ("   a", a);
49          printf ("   d=0x%lX\n", u);
50          abort ();
51        }
52    }
53
54  got = (mpz_divisible_p (a, d) != 0);
55  if (want != got)
56    {
57      printf ("mpz_divisible_p wrong\n");
58      printf ("   expected %d got %d\n", want, got);
59      mpz_trace ("   a", a);
60      mpz_trace ("   d", d);
61      mp_trace_base = -16;
62      mpz_trace ("   a", a);
63      mpz_trace ("   d", d);
64      abort ();
65    }
66}
67
68void
69check_data (void)
70{
71  static const struct {
72    const char *a;
73    const char *d;
74    int        want;
75
76  } data[] = {
77
78    { "0",    "1", 1 },
79    { "123",  "1", 1 },
80    { "-123", "1", 1 },
81
82    { "0",  "2", 1 },
83    { "1",  "2", 0 },
84    { "2",  "2", 1 },
85    { "-2", "2", 1 },
86    { "0x100000000000000000000000000000000", "2", 1 },
87    { "0x100000000000000000000000000000001", "2", 0 },
88
89    { "0x3333333333333333", "3", 1 },
90    { "0x3333333333333332", "3", 0 },
91    { "0x33333333333333333333333333333333", "3", 1 },
92    { "0x33333333333333333333333333333332", "3", 0 },
93
94    /* divisor changes from 2 to 1 limb after stripping 2s */
95    {          "0x3333333300000000",         "0x180000000",         1 },
96    {  "0x33333333333333330000000000000000", "0x18000000000000000", 1 },
97    { "0x133333333333333330000000000000000", "0x18000000000000000", 0 },
98  };
99
100  mpz_t   a, d;
101  int     i;
102
103  mpz_init (a);
104  mpz_init (d);
105
106  for (i = 0; i < numberof (data); i++)
107    {
108      mpz_set_str_or_abort (a, data[i].a, 0);
109      mpz_set_str_or_abort (d, data[i].d, 0);
110      check_one (a, d, data[i].want);
111    }
112
113  mpz_clear (a);
114  mpz_clear (d);
115}
116
117void
118check_random (int reps)
119{
120  gmp_randstate_ptr rands = RANDS;
121  mpz_t   a, d, r;
122  int     i;
123  int     want;
124
125  mpz_init (a);
126  mpz_init (d);
127  mpz_init (r);
128
129  for (i = 0; i < reps; i++)
130    {
131      mpz_erandomb (a, rands, 512);
132      mpz_erandomb_nonzero (d, rands, 512);
133
134      mpz_fdiv_r (r, a, d);
135
136      want = (mpz_sgn (r) == 0);
137      check_one (a, d, want);
138
139      mpz_sub (a, a, r);
140      check_one (a, d, 1);
141
142      if (mpz_cmpabs_ui (d, 1L) == 0)
143        continue;
144
145      mpz_add_ui (a, a, 1L);
146      check_one (a, d, 0);
147    }
148
149  mpz_clear (a);
150  mpz_clear (d);
151  mpz_clear (r);
152}
153
154
155int
156main (int argc, char *argv[])
157{
158  int  reps = 1000;
159
160  tests_start ();
161
162  if (argc == 2)
163    reps = atoi (argv[1]);
164
165  check_data ();
166  check_random (reps);
167
168  tests_end ();
169  exit (0);
170}
Note: See TracBrowser for help on using the repository browser.