Annotation of OpenXM_contrib/gmp/tests/mpq/t-md_2exp.c, Revision 1.1.1.1
1.1 ohara 1: /* Test mpq_mul_2exp and mpq_div_2exp.
2:
3: Copyright 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: #include "gmp.h"
25: #include "gmp-impl.h"
26: #include "tests.h"
27:
28:
29: struct pair_t {
30: const char *num;
31: const char *den;
32: };
33:
34: int
35: main (void)
36: {
37: static const struct {
38: struct pair_t left;
39: unsigned long n;
40: struct pair_t right;
41:
42: } data[] = {
43: { {"0","1"}, 0, {"0","1"} },
44: { {"0","1"}, 1, {"0","1"} },
45: { {"0","1"}, 2, {"0","1"} },
46:
47: { {"1","1"}, 0, {"1","1"} },
48: { {"1","1"}, 1, {"2","1"} },
49: { {"1","1"}, 2, {"4","1"} },
50: { {"1","1"}, 3, {"8","1"} },
51:
52: { {"1","1"}, 31, {"0x80000000","1"} },
53: { {"1","1"}, 32, {"0x100000000","1"} },
54: { {"1","1"}, 33, {"0x200000000","1"} },
55: { {"1","1"}, 63, {"0x8000000000000000","1"} },
56: { {"1","1"}, 64, {"0x10000000000000000","1"} },
57: { {"1","1"}, 65, {"0x20000000000000000","1"} },
58: { {"1","1"}, 95, {"0x800000000000000000000000","1"} },
59: { {"1","1"}, 96, {"0x1000000000000000000000000","1"} },
60: { {"1","1"}, 97, {"0x2000000000000000000000000","1"} },
61: { {"1","1"}, 127, {"0x80000000000000000000000000000000","1"} },
62: { {"1","1"}, 128, {"0x100000000000000000000000000000000","1"} },
63: { {"1","1"}, 129, {"0x200000000000000000000000000000000","1"} },
64:
65: { {"1","2"}, 31, {"0x40000000","1"} },
66: { {"1","2"}, 32, {"0x80000000","1"} },
67: { {"1","2"}, 33, {"0x100000000","1"} },
68: { {"1","2"}, 63, {"0x4000000000000000","1"} },
69: { {"1","2"}, 64, {"0x8000000000000000","1"} },
70: { {"1","2"}, 65, {"0x10000000000000000","1"} },
71: { {"1","2"}, 95, {"0x400000000000000000000000","1"} },
72: { {"1","2"}, 96, {"0x800000000000000000000000","1"} },
73: { {"1","2"}, 97, {"0x1000000000000000000000000","1"} },
74: { {"1","2"}, 127, {"0x40000000000000000000000000000000","1"} },
75: { {"1","2"}, 128, {"0x80000000000000000000000000000000","1"} },
76: { {"1","2"}, 129, {"0x100000000000000000000000000000000","1"} },
77:
78: { {"1","0x80000000"}, 30, {"1","2"} },
79: { {"1","0x80000000"}, 31, {"1","1"} },
80: { {"1","0x80000000"}, 32, {"2","1"} },
81: { {"1","0x80000000"}, 33, {"4","1"} },
82: { {"1","0x80000000"}, 62, {"0x80000000","1"} },
83: { {"1","0x80000000"}, 63, {"0x100000000","1"} },
84: { {"1","0x80000000"}, 64, {"0x200000000","1"} },
85: { {"1","0x80000000"}, 94, {"0x8000000000000000","1"} },
86: { {"1","0x80000000"}, 95, {"0x10000000000000000","1"} },
87: { {"1","0x80000000"}, 96, {"0x20000000000000000","1"} },
88: { {"1","0x80000000"}, 126, {"0x800000000000000000000000","1"} },
89: { {"1","0x80000000"}, 127, {"0x1000000000000000000000000","1"} },
90: { {"1","0x80000000"}, 128, {"0x2000000000000000000000000","1"} },
91:
92: { {"1","0x100000000"}, 1, {"1","0x80000000"} },
93: { {"1","0x100000000"}, 2, {"1","0x40000000"} },
94: { {"1","0x100000000"}, 3, {"1","0x20000000"} },
95:
96: { {"1","0x10000000000000000"}, 1, {"1","0x8000000000000000"} },
97: { {"1","0x10000000000000000"}, 2, {"1","0x4000000000000000"} },
98: { {"1","0x10000000000000000"}, 3, {"1","0x2000000000000000"} },
99: };
100:
101: void (*fun) _PROTO ((mpq_ptr, mpq_srcptr, unsigned long));
102: const struct pair_t *p_start, *p_want;
103: const char *name;
104: mpq_t sep, got, want;
105: mpq_ptr q;
106: int i, muldiv, sign, overlap;
107:
108: tests_start ();
109:
110: mpq_init (sep);
111: mpq_init (got);
112: mpq_init (want);
113:
114: for (i = 0; i < numberof (data); i++)
115: {
116: for (muldiv = 0; muldiv < 2; muldiv++)
117: {
118: if (muldiv == 0)
119: {
120: fun = mpq_mul_2exp;
121: name = "mpq_mul_2exp";
122: p_start = &data[i].left;
123: p_want = &data[i].right;
124: }
125: else
126: {
127: fun = mpq_div_2exp;
128: name = "mpq_div_2exp";
129: p_start = &data[i].right;
130: p_want = &data[i].left;
131: }
132:
133: for (sign = 0; sign <= 1; sign++)
134: {
135: mpz_set_str_or_abort (mpq_numref(want), p_want->num, 0);
136: mpz_set_str_or_abort (mpq_denref(want), p_want->den, 0);
137: if (sign)
138: mpq_neg (want, want);
139:
140: for (overlap = 0; overlap <= 1; overlap++)
141: {
142: q = overlap ? got : sep;
143:
144: /* initial garbage in "got" */
145: mpq_set_ui (got, 123L, 456L);
146:
147: mpz_set_str_or_abort (mpq_numref(q), p_start->num, 0);
148: mpz_set_str_or_abort (mpq_denref(q), p_start->den, 0);
149: if (sign)
150: mpq_neg (q, q);
151:
152: (*fun) (got, q, data[i].n);
153: MPQ_CHECK_FORMAT (got);
154:
155: if (! mpq_equal (got, want))
156: {
157: printf ("%s wrong at data[%d], sign %d, overlap %d\n",
158: name, i, sign, overlap);
159: printf (" num \"%s\"\n", p_start->num);
160: printf (" den \"%s\"\n", p_start->den);
161: printf (" n %lu\n", data[i].n);
162:
163: printf (" got ");
164: mpq_out_str (stdout, 16, got);
165: printf (" (hex)\n");
166:
167: printf (" want ");
168: mpq_out_str (stdout, 16, want);
169: printf (" (hex)\n");
170:
171: abort ();
172: }
173: }
174: }
175: }
176: }
177:
178: mpq_clear (sep);
179: mpq_clear (got);
180: mpq_clear (want);
181:
182: tests_end ();
183: exit (0);
184: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>