xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpq/t-set_f.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mpq_set_f.
2 
3 Copyright 2000, 2001 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
23 #include "tests.h"
24 
25 int
main(int argc,char ** argv)26 main (int argc, char **argv)
27 {
28 #if GMP_NAIL_BITS == 0
29   static const struct {
30     int         f_base;
31     const char  *f;
32     int         z_base;
33     const char  *want_num;
34     const char  *want_den;
35 
36   } data[] = {
37 
38     { -2, "0",    16, "0", "1" },
39     { -2, "1",    16, "1", "1" },
40     { -2, "1@1",  16, "2", "1" },
41     { -2, "1@2",  16, "4", "1" },
42     { -2, "1@3",  16, "8", "1" },
43 
44     { -2, "1@30", 16,  "40000000", "1" },
45     { -2, "1@31", 16,  "80000000", "1" },
46     { -2, "1@32", 16, "100000000", "1" },
47     { -2, "1@33", 16, "200000000", "1" },
48     { -2, "1@34", 16, "400000000", "1" },
49 
50     { -2, "1@62", 16,  "4000000000000000", "1" },
51     { -2, "1@63", 16,  "8000000000000000", "1" },
52     { -2, "1@64", 16, "10000000000000000", "1" },
53     { -2, "1@65", 16, "20000000000000000", "1" },
54     { -2, "1@66", 16, "40000000000000000", "1" },
55 
56     { -2, "1@126", 16,  "40000000000000000000000000000000", "1" },
57     { -2, "1@127", 16,  "80000000000000000000000000000000", "1" },
58     { -2, "1@128", 16, "100000000000000000000000000000000", "1" },
59     { -2, "1@129", 16, "200000000000000000000000000000000", "1" },
60     { -2, "1@130", 16, "400000000000000000000000000000000", "1" },
61 
62     { -2, "1@-1",  16, "1", "2" },
63     { -2, "1@-2",  16, "1", "4" },
64     { -2, "1@-3",  16, "1", "8" },
65 
66     { -2, "1@-30", 16, "1",  "40000000" },
67     { -2, "1@-31", 16, "1",  "80000000" },
68     { -2, "1@-32", 16, "1", "100000000" },
69     { -2, "1@-33", 16, "1", "200000000" },
70     { -2, "1@-34", 16, "1", "400000000" },
71 
72     { -2, "1@-62", 16, "1",  "4000000000000000" },
73     { -2, "1@-63", 16, "1",  "8000000000000000" },
74     { -2, "1@-64", 16, "1", "10000000000000000" },
75     { -2, "1@-65", 16, "1", "20000000000000000" },
76     { -2, "1@-66", 16, "1", "40000000000000000" },
77 
78     { -2, "1@-126", 16, "1",  "40000000000000000000000000000000" },
79     { -2, "1@-127", 16, "1",  "80000000000000000000000000000000" },
80     { -2, "1@-128", 16, "1", "100000000000000000000000000000000" },
81     { -2, "1@-129", 16, "1", "200000000000000000000000000000000" },
82     { -2, "1@-130", 16, "1", "400000000000000000000000000000000" },
83 
84     { -2, "1@-30", 16, "1",  "40000000" },
85     { -2, "1@-31", 16, "1",  "80000000" },
86     { -2, "1@-32", 16, "1", "100000000" },
87     { -2, "1@-33", 16, "1", "200000000" },
88     { -2, "1@-34", 16, "1", "400000000" },
89 
90     { -2, "11@-62", 16, "3",  "4000000000000000" },
91     { -2, "11@-63", 16, "3",  "8000000000000000" },
92     { -2, "11@-64", 16, "3", "10000000000000000" },
93     { -2, "11@-65", 16, "3", "20000000000000000" },
94     { -2, "11@-66", 16, "3", "40000000000000000" },
95 
96     { 16, "80000000.00000001", 16, "8000000000000001", "100000000" },
97     { 16, "80000000.00000008", 16, "1000000000000001",  "20000000" },
98     { 16, "80000000.8",        16, "100000001", "2" },
99 
100   };
101 
102   mpf_t  f;
103   mpq_t  got;
104   mpz_t  want_num, want_den;
105   int    i, neg;
106 
107   tests_start ();
108 
109   mpf_init2 (f, 1024L);
110   mpq_init (got);
111   mpz_init (want_num);
112   mpz_init (want_den);
113 
114   for (i = 0; i < numberof (data); i++)
115     {
116       for (neg = 0; neg <= 1; neg++)
117         {
118           mpf_set_str_or_abort (f, data[i].f, data[i].f_base);
119           mpz_set_str_or_abort (want_num, data[i].want_num, data[i].z_base);
120           mpz_set_str_or_abort (want_den, data[i].want_den, data[i].z_base);
121 
122           if (neg)
123             {
124               mpf_neg (f, f);
125               mpz_neg (want_num, want_num);
126             }
127 
128           mpq_set_f (got, f);
129           MPQ_CHECK_FORMAT (got);
130 
131           if (mpz_cmp (mpq_numref(got), want_num) != 0
132               || mpz_cmp (mpq_denref(got), want_den) != 0)
133             {
134               printf ("wrong at data[%d]\n", i);
135               printf ("   f_base %d, z_base %d\n",
136                       data[i].f_base, data[i].z_base);
137 
138               printf ("   f \"%s\" hex ", data[i].f);
139               mpf_out_str (stdout, 16, 0, f);
140               printf ("\n");
141 
142               printf ("   want num 0x");
143               mpz_out_str (stdout, 16, want_num);
144               printf ("\n");
145               printf ("   want den 0x");
146               mpz_out_str (stdout, 16, want_den);
147               printf ("\n");
148 
149               printf ("   got num 0x");
150               mpz_out_str (stdout, 16, mpq_numref(got));
151               printf ("\n");
152               printf ("   got den 0x");
153               mpz_out_str (stdout, 16, mpq_denref(got));
154               printf ("\n");
155 
156               abort ();
157             }
158         }
159     }
160 
161   mpf_clear (f);
162   mpq_clear (got);
163   mpz_clear (want_num);
164   mpz_clear (want_den);
165 
166   tests_end ();
167 #endif
168   exit (0);
169 }
170