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
|
/* Test mpq_equal.
Copyright 2001 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "tests.h"
void
check_one (mpq_srcptr x, mpq_srcptr y, int want)
{
int got;
MPQ_CHECK_FORMAT (x);
MPQ_CHECK_FORMAT (y);
got = mpq_equal (x, y);
if ((got != 0) != (want != 0))
{
printf ("mpq_equal got %d want %d\n", got, want);
mpq_trace ("x", x);
mpq_trace ("y", y);
abort ();
}
}
void
check_all (mpq_ptr x, mpq_ptr y, int want)
{
check_one (x, y, want);
check_one (y, x, want);
mpq_neg (x, x);
mpq_neg (y, y);
check_one (x, y, want);
check_one (y, x, want);
}
#define SET4Z(z, size,l3,l2,l1,l0) \
SIZ(z) = size; PTR(z)[3] = l3; PTR(z)[2] = l2; PTR(z)[1] = l1; PTR(z)[0] = l0
#define SET4(q, nsize,n3,n2,n1,n0, dsize,d3,d2,d1,d0) \
SET4Z (mpq_numref(q), nsize,n3,n2,n1,n0); \
SET4Z (mpq_denref(q), dsize,d3,d2,d1,d0)
/* Exercise various combinations of same and slightly different values. */
void
check_various (void)
{
mpq_t x, y;
mpq_init (x);
mpq_init (y);
mpz_realloc (mpq_numref(x), (mp_size_t) 20);
mpz_realloc (mpq_denref(x), (mp_size_t) 20);
mpz_realloc (mpq_numref(y), (mp_size_t) 20);
mpz_realloc (mpq_denref(y), (mp_size_t) 20);
/* 0 == 0 */
SET4 (x, 0,13,12,11,10, 1,23,22,21,1);
SET4 (y, 0,33,32,31,30, 1,43,42,41,1);
check_all (x, y, 1);
/* 83/99 == 83/99 */
SET4 (x, 1,13,12,11,83, 1,23,22,21,99);
SET4 (y, 1,33,32,31,83, 1,43,42,41,99);
check_all (x, y, 1);
/* 1:2:3:4/5:6:7 == 1:2:3:4/5:6:7 */
SET4 (x, 4,1,2,3,4, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 1);
/* various individual changes making != */
SET4 (x, 4,1,2,3,667, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
SET4 (x, 4,1,2,666,4, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
SET4 (x, 4,1,666,3,4, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
#if GMP_NUMB_BITS != 62
SET4 (x, 4,667,2,3,4, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
#endif
SET4 (x, 4,1,2,3,4, 3,88,5,6,667);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
SET4 (x, 4,1,2,3,4, 3,88,5,667,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
SET4 (x, 4,1,2,3,4, 3,88,666,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
SET4 (x, -4,1,2,3,4, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
SET4 (x, 1,1,2,3,4, 3,88,5,6,7);
SET4 (y, 4,1,2,3,4, 3,99,5,6,7);
check_all (x, y, 0);
mpq_clear (x);
mpq_clear (y);
}
int
main (void)
{
tests_start ();
mp_trace_base = -16;
check_various ();
tests_end ();
exit (0);
}
|