1/* Test mpq_mul_2exp and mpq_div_2exp. 2 3Copyright 2000, 2001 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library. 6 7The GNU MP Library is free software; you can redistribute it and/or modify 8it under the terms of the GNU Lesser General Public License as published by 9the Free Software Foundation; either version 3 of the License, or (at your 10option) any later version. 11 12The GNU MP Library is distributed in the hope that it will be useful, but 13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15License for more details. 16 17You should have received a copy of the GNU Lesser General Public License 18along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 19 20#include <stdio.h> 21#include <stdlib.h> 22#include "gmp.h" 23#include "gmp-impl.h" 24#include "tests.h" 25 26 27struct pair_t { 28 const char *num; 29 const char *den; 30}; 31 32int 33main (void) 34{ 35 static const struct { 36 struct pair_t left; 37 unsigned long n; 38 struct pair_t right; 39 40 } data[] = { 41 { {"0","1"}, 0, {"0","1"} }, 42 { {"0","1"}, 1, {"0","1"} }, 43 { {"0","1"}, 2, {"0","1"} }, 44 45 { {"1","1"}, 0, {"1","1"} }, 46 { {"1","1"}, 1, {"2","1"} }, 47 { {"1","1"}, 2, {"4","1"} }, 48 { {"1","1"}, 3, {"8","1"} }, 49 50 { {"1","1"}, 31, {"0x80000000","1"} }, 51 { {"1","1"}, 32, {"0x100000000","1"} }, 52 { {"1","1"}, 33, {"0x200000000","1"} }, 53 { {"1","1"}, 63, {"0x8000000000000000","1"} }, 54 { {"1","1"}, 64, {"0x10000000000000000","1"} }, 55 { {"1","1"}, 65, {"0x20000000000000000","1"} }, 56 { {"1","1"}, 95, {"0x800000000000000000000000","1"} }, 57 { {"1","1"}, 96, {"0x1000000000000000000000000","1"} }, 58 { {"1","1"}, 97, {"0x2000000000000000000000000","1"} }, 59 { {"1","1"}, 127, {"0x80000000000000000000000000000000","1"} }, 60 { {"1","1"}, 128, {"0x100000000000000000000000000000000","1"} }, 61 { {"1","1"}, 129, {"0x200000000000000000000000000000000","1"} }, 62 63 { {"1","2"}, 31, {"0x40000000","1"} }, 64 { {"1","2"}, 32, {"0x80000000","1"} }, 65 { {"1","2"}, 33, {"0x100000000","1"} }, 66 { {"1","2"}, 63, {"0x4000000000000000","1"} }, 67 { {"1","2"}, 64, {"0x8000000000000000","1"} }, 68 { {"1","2"}, 65, {"0x10000000000000000","1"} }, 69 { {"1","2"}, 95, {"0x400000000000000000000000","1"} }, 70 { {"1","2"}, 96, {"0x800000000000000000000000","1"} }, 71 { {"1","2"}, 97, {"0x1000000000000000000000000","1"} }, 72 { {"1","2"}, 127, {"0x40000000000000000000000000000000","1"} }, 73 { {"1","2"}, 128, {"0x80000000000000000000000000000000","1"} }, 74 { {"1","2"}, 129, {"0x100000000000000000000000000000000","1"} }, 75 76 { {"1","0x80000000"}, 30, {"1","2"} }, 77 { {"1","0x80000000"}, 31, {"1","1"} }, 78 { {"1","0x80000000"}, 32, {"2","1"} }, 79 { {"1","0x80000000"}, 33, {"4","1"} }, 80 { {"1","0x80000000"}, 62, {"0x80000000","1"} }, 81 { {"1","0x80000000"}, 63, {"0x100000000","1"} }, 82 { {"1","0x80000000"}, 64, {"0x200000000","1"} }, 83 { {"1","0x80000000"}, 94, {"0x8000000000000000","1"} }, 84 { {"1","0x80000000"}, 95, {"0x10000000000000000","1"} }, 85 { {"1","0x80000000"}, 96, {"0x20000000000000000","1"} }, 86 { {"1","0x80000000"}, 126, {"0x800000000000000000000000","1"} }, 87 { {"1","0x80000000"}, 127, {"0x1000000000000000000000000","1"} }, 88 { {"1","0x80000000"}, 128, {"0x2000000000000000000000000","1"} }, 89 90 { {"1","0x100000000"}, 1, {"1","0x80000000"} }, 91 { {"1","0x100000000"}, 2, {"1","0x40000000"} }, 92 { {"1","0x100000000"}, 3, {"1","0x20000000"} }, 93 94 { {"1","0x10000000000000000"}, 1, {"1","0x8000000000000000"} }, 95 { {"1","0x10000000000000000"}, 2, {"1","0x4000000000000000"} }, 96 { {"1","0x10000000000000000"}, 3, {"1","0x2000000000000000"} }, 97 }; 98 99 void (*fun) __GMP_PROTO ((mpq_ptr, mpq_srcptr, unsigned long)); 100 const struct pair_t *p_start, *p_want; 101 const char *name; 102 mpq_t sep, got, want; 103 mpq_ptr q; 104 int i, muldiv, sign, overlap; 105 106 tests_start (); 107 108 mpq_init (sep); 109 mpq_init (got); 110 mpq_init (want); 111 112 for (i = 0; i < numberof (data); i++) 113 { 114 for (muldiv = 0; muldiv < 2; muldiv++) 115 { 116 if (muldiv == 0) 117 { 118 fun = mpq_mul_2exp; 119 name = "mpq_mul_2exp"; 120 p_start = &data[i].left; 121 p_want = &data[i].right; 122 } 123 else 124 { 125 fun = mpq_div_2exp; 126 name = "mpq_div_2exp"; 127 p_start = &data[i].right; 128 p_want = &data[i].left; 129 } 130 131 for (sign = 0; sign <= 1; sign++) 132 { 133 mpz_set_str_or_abort (mpq_numref(want), p_want->num, 0); 134 mpz_set_str_or_abort (mpq_denref(want), p_want->den, 0); 135 if (sign) 136 mpq_neg (want, want); 137 138 for (overlap = 0; overlap <= 1; overlap++) 139 { 140 q = overlap ? got : sep; 141 142 /* initial garbage in "got" */ 143 mpq_set_ui (got, 123L, 456L); 144 145 mpz_set_str_or_abort (mpq_numref(q), p_start->num, 0); 146 mpz_set_str_or_abort (mpq_denref(q), p_start->den, 0); 147 if (sign) 148 mpq_neg (q, q); 149 150 (*fun) (got, q, data[i].n); 151 MPQ_CHECK_FORMAT (got); 152 153 if (! mpq_equal (got, want)) 154 { 155 printf ("%s wrong at data[%d], sign %d, overlap %d\n", 156 name, i, sign, overlap); 157 printf (" num \"%s\"\n", p_start->num); 158 printf (" den \"%s\"\n", p_start->den); 159 printf (" n %lu\n", data[i].n); 160 161 printf (" got "); 162 mpq_out_str (stdout, 16, got); 163 printf (" (hex)\n"); 164 165 printf (" want "); 166 mpq_out_str (stdout, 16, want); 167 printf (" (hex)\n"); 168 169 abort (); 170 } 171 } 172 } 173 } 174 } 175 176 mpq_clear (sep); 177 mpq_clear (got); 178 mpq_clear (want); 179 180 tests_end (); 181 exit (0); 182} 183