~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tfits.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test file for:
 
2
 mpfr_fits_sint_p, mpfr_fits_slong_p, mpfr_fits_sshort_p,
 
3
 mpfr_fits_uint_p, mpfr_fits_ulong_p, mpfr_fits_ushort_p
 
4
 
 
5
Copyright 2004, 2005 Free Software Foundation, Inc.
 
6
 
 
7
This file is part of the MPFR Library.
 
8
 
 
9
The MPFR Library is free software; you can redistribute it and/or modify
 
10
it under the terms of the GNU Lesser General Public License as published by
 
11
the Free Software Foundation; either version 2.1 of the License, or (at your
 
12
option) any later version.
 
13
 
 
14
The MPFR Library is distributed in the hope that it will be useful, but
 
15
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
16
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
17
License for more details.
 
18
 
 
19
You should have received a copy of the GNU Lesser General Public License
 
20
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
21
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
22
MA 02110-1301, USA. */
 
23
 
 
24
#if HAVE_CONFIG_H
 
25
# include "config.h"       /* for a build within gmp */
 
26
#endif
 
27
 
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <limits.h>
 
31
 
 
32
/* The ISO C99 standard specifies that in C++ implementations the
 
33
   INTMAX_MAX, ... macros should only be defined if explicitly requested.  */
 
34
#if defined __cplusplus
 
35
# define __STDC_LIMIT_MACROS
 
36
# define __STDC_CONSTANT_MACROS
 
37
#endif
 
38
 
 
39
#ifdef HAVE_STDINT_H
 
40
# include <stdint.h>
 
41
#endif
 
42
#ifdef HAVE_INTTYPES_H
 
43
# include <inttypes.h>
 
44
#endif
 
45
 
 
46
#include "mpfr-test.h"
 
47
 
 
48
#define ERROR1 { printf("Initial error for x="); mpfr_dump(x); exit(1); }
 
49
#define ERROR2 { printf("Error for x="); mpfr_dump(x); exit(1); }
 
50
 
 
51
static void check_intmax (void);
 
52
 
 
53
int
 
54
main (void)
 
55
{
 
56
  mpfr_t x;
 
57
 
 
58
  tests_start_mpfr ();
 
59
 
 
60
  mpfr_init2 (x, 256);
 
61
 
 
62
  /* Check NAN */
 
63
  mpfr_set_nan(x);
 
64
  if (mpfr_fits_ulong_p(x, GMP_RNDN))
 
65
    ERROR1;
 
66
  if (mpfr_fits_slong_p(x, GMP_RNDN))
 
67
    ERROR1;
 
68
  if (mpfr_fits_uint_p(x, GMP_RNDN))
 
69
    ERROR1;
 
70
  if (mpfr_fits_sint_p(x, GMP_RNDN))
 
71
    ERROR1;
 
72
  if (mpfr_fits_ushort_p(x, GMP_RNDN))
 
73
    ERROR1;
 
74
  if (mpfr_fits_sshort_p(x, GMP_RNDN))
 
75
    ERROR1;
 
76
 
 
77
  /* Check INF */
 
78
  mpfr_set_inf(x, 1);
 
79
  if (mpfr_fits_ulong_p(x, GMP_RNDN))
 
80
    ERROR1;
 
81
  if (mpfr_fits_slong_p(x, GMP_RNDN))
 
82
    ERROR1;
 
83
  if (mpfr_fits_uint_p(x, GMP_RNDN))
 
84
    ERROR1;
 
85
  if (mpfr_fits_sint_p(x, GMP_RNDN))
 
86
    ERROR1;
 
87
  if (mpfr_fits_ushort_p(x, GMP_RNDN))
 
88
    ERROR1;
 
89
  if (mpfr_fits_sshort_p(x, GMP_RNDN))
 
90
    ERROR1;
 
91
 
 
92
  /* Check Zero */
 
93
  MPFR_SET_ZERO(x);
 
94
  if (!mpfr_fits_ulong_p(x, GMP_RNDN))
 
95
    ERROR2;
 
96
  if (!mpfr_fits_slong_p(x, GMP_RNDN))
 
97
    ERROR2;
 
98
  if (!mpfr_fits_uint_p(x, GMP_RNDN))
 
99
    ERROR2;
 
100
  if (!mpfr_fits_sint_p(x, GMP_RNDN))
 
101
    ERROR2;
 
102
  if (!mpfr_fits_ushort_p(x, GMP_RNDN))
 
103
    ERROR2;
 
104
  if (!mpfr_fits_sshort_p(x, GMP_RNDN))
 
105
    ERROR2;
 
106
 
 
107
  /* Check small op */
 
108
  mpfr_set_str1 (x, "1@-1");
 
109
  if (!mpfr_fits_ulong_p(x, GMP_RNDN))
 
110
    ERROR2;
 
111
  if (!mpfr_fits_slong_p(x, GMP_RNDN))
 
112
    ERROR2;
 
113
  if (!mpfr_fits_uint_p(x, GMP_RNDN))
 
114
    ERROR2;
 
115
  if (!mpfr_fits_sint_p(x, GMP_RNDN))
 
116
    ERROR2;
 
117
  if (!mpfr_fits_ushort_p(x, GMP_RNDN))
 
118
    ERROR2;
 
119
  if (!mpfr_fits_sshort_p(x, GMP_RNDN))
 
120
    ERROR2;
 
121
 
 
122
  /* Check 17 */
 
123
  mpfr_set_ui (x, 17, GMP_RNDN);
 
124
  if (!mpfr_fits_ulong_p(x, GMP_RNDN))
 
125
    ERROR2;
 
126
  if (!mpfr_fits_slong_p(x, GMP_RNDN))
 
127
    ERROR2;
 
128
  if (!mpfr_fits_uint_p(x, GMP_RNDN))
 
129
    ERROR2;
 
130
  if (!mpfr_fits_sint_p(x, GMP_RNDN))
 
131
    ERROR2;
 
132
  if (!mpfr_fits_ushort_p(x, GMP_RNDN))
 
133
    ERROR2;
 
134
  if (!mpfr_fits_sshort_p(x, GMP_RNDN))
 
135
    ERROR2;
 
136
 
 
137
  /* Check all other values */
 
138
  mpfr_set_ui(x, ULONG_MAX, GMP_RNDN);
 
139
  mpfr_mul_2exp(x, x, 1, GMP_RNDN);
 
140
  if (mpfr_fits_ulong_p(x, GMP_RNDN))
 
141
    ERROR1;
 
142
  if (mpfr_fits_slong_p(x, GMP_RNDN))
 
143
    ERROR1;
 
144
  mpfr_mul_2exp(x, x, 40, GMP_RNDN);
 
145
  if (mpfr_fits_ulong_p(x, GMP_RNDN))
 
146
    ERROR1;
 
147
  if (mpfr_fits_uint_p(x, GMP_RNDN))
 
148
    ERROR1;
 
149
  if (mpfr_fits_sint_p(x, GMP_RNDN))
 
150
    ERROR1;
 
151
  if (mpfr_fits_ushort_p(x, GMP_RNDN))
 
152
    ERROR1;
 
153
  if (mpfr_fits_sshort_p(x, GMP_RNDN))
 
154
    ERROR1;
 
155
 
 
156
  mpfr_set_ui(x, ULONG_MAX, GMP_RNDN);
 
157
  if (!mpfr_fits_ulong_p(x, GMP_RNDN))
 
158
    ERROR2;
 
159
  mpfr_set_ui(x, LONG_MAX, GMP_RNDN);
 
160
  if (!mpfr_fits_slong_p(x, GMP_RNDN))
 
161
    ERROR2;
 
162
  mpfr_set_ui(x, UINT_MAX, GMP_RNDN);
 
163
  if (!mpfr_fits_uint_p(x, GMP_RNDN))
 
164
    ERROR2;
 
165
  mpfr_set_ui(x, INT_MAX, GMP_RNDN);
 
166
  if (!mpfr_fits_sint_p(x, GMP_RNDN))
 
167
    ERROR2;
 
168
  mpfr_set_ui(x, USHRT_MAX, GMP_RNDN);
 
169
  if (!mpfr_fits_ushort_p(x, GMP_RNDN))
 
170
    ERROR2;
 
171
  mpfr_set_ui(x, SHRT_MAX, GMP_RNDN);
 
172
  if (!mpfr_fits_sshort_p(x, GMP_RNDN))
 
173
    ERROR2;
 
174
 
 
175
  mpfr_set_si(x, 1, GMP_RNDN);
 
176
  if (!mpfr_fits_sint_p(x, GMP_RNDN))
 
177
    ERROR2;
 
178
  if (!mpfr_fits_sshort_p(x, GMP_RNDN))
 
179
    ERROR2;
 
180
 
 
181
  /* Check negative value */
 
182
  mpfr_set_si (x, -1, GMP_RNDN);
 
183
  if (!mpfr_fits_sint_p(x, GMP_RNDN))
 
184
    ERROR2;
 
185
  if (!mpfr_fits_sshort_p(x, GMP_RNDN))
 
186
    ERROR2;
 
187
  if (!mpfr_fits_slong_p(x, GMP_RNDN))
 
188
    ERROR2;
 
189
  if (mpfr_fits_uint_p(x, GMP_RNDN))
 
190
    ERROR1;
 
191
  if (mpfr_fits_ushort_p(x, GMP_RNDN))
 
192
    ERROR1;
 
193
  if (mpfr_fits_ulong_p(x, GMP_RNDN))
 
194
    ERROR1;
 
195
 
 
196
  mpfr_clear (x);
 
197
 
 
198
  check_intmax ();
 
199
 
 
200
  tests_end_mpfr ();
 
201
  return 0;
 
202
}
 
203
 
 
204
static void check_intmax (void)
 
205
{
 
206
#ifdef _MPFR_H_HAVE_INTMAX_T
 
207
  mpfr_t x;
 
208
 
 
209
  mpfr_init2 (x, sizeof(uintmax_t)*CHAR_BIT);
 
210
 
 
211
  /* Check NAN */
 
212
  mpfr_set_nan(x);
 
213
  if (mpfr_fits_uintmax_p(x, GMP_RNDN))
 
214
    ERROR1;
 
215
  if (mpfr_fits_intmax_p(x, GMP_RNDN))
 
216
    ERROR1;
 
217
 
 
218
  /* Check INF */
 
219
  mpfr_set_inf(x, 1);
 
220
  if (mpfr_fits_uintmax_p(x, GMP_RNDN))
 
221
    ERROR1;
 
222
  if (mpfr_fits_intmax_p(x, GMP_RNDN))
 
223
    ERROR1;
 
224
 
 
225
  /* Check Zero */
 
226
  MPFR_SET_ZERO(x);
 
227
  if (!mpfr_fits_uintmax_p(x, GMP_RNDN))
 
228
    ERROR2;
 
229
  if (!mpfr_fits_intmax_p(x, GMP_RNDN))
 
230
    ERROR2;
 
231
 
 
232
  /* Check small op */
 
233
  mpfr_set_str1 (x, "1@-1");
 
234
  if (!mpfr_fits_uintmax_p(x, GMP_RNDN))
 
235
    ERROR2;
 
236
  if (!mpfr_fits_intmax_p(x, GMP_RNDN))
 
237
    ERROR2;
 
238
 
 
239
  /* Check 17 */
 
240
  mpfr_set_ui (x, 17, GMP_RNDN);
 
241
  if (!mpfr_fits_uintmax_p(x, GMP_RNDN))
 
242
    ERROR2;
 
243
  if (!mpfr_fits_intmax_p(x, GMP_RNDN))
 
244
    ERROR2;
 
245
 
 
246
  /* Check hugest */
 
247
  mpfr_set_ui_2exp (x, 42, sizeof (uintmax_t) * 32, GMP_RNDN);
 
248
  if (mpfr_fits_uintmax_p (x, GMP_RNDN))
 
249
    ERROR1;
 
250
  if (mpfr_fits_intmax_p (x, GMP_RNDN))
 
251
    ERROR1;
 
252
 
 
253
  /* Check all other values */
 
254
  mpfr_set_uj (x, UINTMAX_MAX, GMP_RNDN);
 
255
  mpfr_add_ui (x, x, 1, GMP_RNDN);
 
256
  if (mpfr_fits_uintmax_p (x, GMP_RNDN))
 
257
    ERROR1;
 
258
  mpfr_set_uj (x, UINTMAX_MAX, GMP_RNDN);
 
259
  if (!mpfr_fits_uintmax_p (x, GMP_RNDN))
 
260
    ERROR2;
 
261
  mpfr_set_sj (x, INTMAX_MAX, GMP_RNDN);
 
262
  mpfr_add_ui (x, x, 1, GMP_RNDN);
 
263
  if (mpfr_fits_intmax_p (x, GMP_RNDN))
 
264
    ERROR1;
 
265
  mpfr_set_sj (x, INTMAX_MAX, GMP_RNDN);
 
266
  if (!mpfr_fits_intmax_p (x, GMP_RNDN))
 
267
    ERROR2;
 
268
  mpfr_set_sj (x, INTMAX_MIN, GMP_RNDN);
 
269
  if (!mpfr_fits_intmax_p (x, GMP_RNDN))
 
270
    ERROR2;
 
271
  mpfr_sub_ui (x, x, 1, GMP_RNDN);
 
272
  if (mpfr_fits_intmax_p (x, GMP_RNDN))
 
273
    ERROR1;
 
274
 
 
275
  /* Check negative value */
 
276
  mpfr_set_si (x, -1, GMP_RNDN);
 
277
  if (!mpfr_fits_intmax_p (x, GMP_RNDN))
 
278
    ERROR2;
 
279
  if (mpfr_fits_uintmax_p (x, GMP_RNDN))
 
280
    ERROR1;
 
281
 
 
282
  mpfr_clear (x);
 
283
#endif
 
284
}
 
285