summaryrefslogtreecommitdiff
path: root/gmp/tests/mpq/t-aors.c
blob: a65f5d8ab88c18d5c1f7e0f52532df68c688e84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Test mpq_add and mpq_sub.

Copyright 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gmp.h"
#include "gmp-impl.h"
#include "tests.h"


void
check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub)
{
  mpq_t  got;
  int    neg_x, neg_y, swap;

  mpq_init (got);

  MPQ_CHECK_FORMAT (want_add);
  MPQ_CHECK_FORMAT (want_sub);
  MPQ_CHECK_FORMAT (x);
  MPQ_CHECK_FORMAT (y);

  for (swap = 0; swap <= 1; swap++)
    {
      for (neg_x = 0; neg_x <= 1; neg_x++)
        {
          for (neg_y = 0; neg_y <= 1; neg_y++)
            {
              mpq_add (got, x, y);
              MPQ_CHECK_FORMAT (got);
              if (! mpq_equal (got, want_add))
                {
                  printf ("mpq_add wrong\n");
                  mpq_trace ("  x   ", x);
                  mpq_trace ("  y   ", y);
                  mpq_trace ("  got ", got);
                  mpq_trace ("  want", want_add);
                  abort ();
                }

              mpq_sub (got, x, y);
              MPQ_CHECK_FORMAT (got);
              if (! mpq_equal (got, want_sub))
                {
                  printf ("mpq_sub wrong\n");
                  mpq_trace ("  x   ", x);
                  mpq_trace ("  y   ", y);
                  mpq_trace ("  got ", got);
                  mpq_trace ("  want", want_sub);
                  abort ();
                }


              mpq_neg (y, y);
              mpq_swap (want_add, want_sub);
            }

          mpq_neg (x, x);
          mpq_swap (want_add, want_sub);
          mpq_neg (want_add, want_add);
          mpq_neg (want_sub, want_sub);
        }

      mpq_swap (x, y);
      mpq_neg (want_sub, want_sub);
    }

  mpq_clear (got);
}


void
check_data (void)
{
  static const struct {
    const char  *x;
    const char  *y;
    const char  *want_add;
    const char  *want_sub;

  } data[] = {

    { "0", "0", "0", "0" },
    { "1", "0", "1", "1" },
    { "1", "1", "2", "0" },

    { "1/2", "1/2", "1", "0" },
    { "5/6", "14/15", "53/30", "-1/10" },
  };

  mpq_t  x, y, want_add, want_sub;
  int i;

  mpq_init (x);
  mpq_init (y);
  mpq_init (want_add);
  mpq_init (want_sub);

  for (i = 0; i < numberof (data); i++)
    {
      mpq_set_str_or_abort (x, data[i].x, 0);
      mpq_set_str_or_abort (y, data[i].y, 0);
      mpq_set_str_or_abort (want_add, data[i].want_add, 0);
      mpq_set_str_or_abort (want_sub, data[i].want_sub, 0);

      check_all (x, y, want_add, want_sub);
    }

  mpq_clear (x);
  mpq_clear (y);
  mpq_clear (want_add);
  mpq_clear (want_sub);
}


void
check_rand (void)
{
  mpq_t  x, y, want_add, want_sub;
  int i;
  gmp_randstate_ptr  rands = RANDS;

  mpq_init (x);
  mpq_init (y);
  mpq_init (want_add);
  mpq_init (want_sub);

  for (i = 0; i < 500; i++)
    {
      mpz_errandomb (mpq_numref(x), rands, 512L);
      mpz_errandomb_nonzero (mpq_denref(x), rands, 512L);
      mpq_canonicalize (x);

      mpz_errandomb (mpq_numref(y), rands, 512L);
      mpz_errandomb_nonzero (mpq_denref(y), rands, 512L);
      mpq_canonicalize (y);

      refmpq_add (want_add, x, y);
      refmpq_sub (want_sub, x, y);

      check_all (x, y, want_add, want_sub);
    }

  mpq_clear (x);
  mpq_clear (y);
  mpq_clear (want_add);
  mpq_clear (want_sub);
}


int
main (void)
{
  tests_start ();

  check_data ();
  check_rand ();

  tests_end ();

  exit (0);
}