xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tcomparisons.c (revision b83ebeba7f767758d2778bb0f9d7a76534253621)
1 /* Test file for mpfr_greater_p, mpfr_greaterequal_p, mpfr_less_p,
2    mpfr_lessequal_p, mpfr_lessgreater_p, mpfr_equal_p, mpfr_unordered_p
3    functions.
4 
5 Copyright 2003, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
6 Contributed by the AriC and Caramel projects, INRIA.
7 
8 This file is part of the GNU MPFR Library.
9 
10 The GNU MPFR Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14 
15 The GNU MPFR Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18 License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
22 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 #include "mpfr-test.h"
29 
30 static void
31 cmp_tests (void)
32 {
33   mpfr_t x, y;
34   long i;
35 
36   mpfr_inits (x, y, (mpfr_ptr) 0);
37   for (i = 0; i < 80000; i++)
38     {
39       mpfr_prec_t precx, precy;
40       int signx, signy, cmp;
41       unsigned int cmpbool = 0;
42 
43       precx = (randlimb () % 17) * 11 + MPFR_PREC_MIN;
44       precy = (randlimb () % 17) * 11 + MPFR_PREC_MIN;
45       mpfr_set_prec (x, precx);
46       mpfr_set_prec (y, precy);
47       mpfr_urandomb (x, RANDS);
48       mpfr_urandomb (y, RANDS);
49       signx = randlimb () & 1;
50       signy = randlimb () % 256 ? signx : 1 - signx;
51       /* signy = signx most of the time (most interesting case) */
52       if (signx)
53         mpfr_neg (x, x, MPFR_RNDN);
54       if (signy)
55         mpfr_neg (y, y, MPFR_RNDN);
56       if (i <= 1)
57         mpfr_set_nan (x);
58       if (i == 0 || i == 2)
59         mpfr_set_nan (y);
60       if (mpfr_greater_p (x, y))
61         cmpbool |= 0x01;
62       if (mpfr_greaterequal_p (x, y))
63         cmpbool |= 0x02;
64       if (mpfr_less_p (x, y))
65         cmpbool |= 0x04;
66       if (mpfr_lessequal_p (x, y))
67         cmpbool |= 0x08;
68       if (mpfr_lessgreater_p (x, y))
69         cmpbool |= 0x10;
70       if (mpfr_equal_p (x, y))
71         cmpbool |= 0x20;
72       if (mpfr_unordered_p (x, y))
73         cmpbool |= 0x40;
74       if ((i <= 2 && cmpbool != 0x40) ||
75           (i > 2 && (cmp = mpfr_cmp (x, y),
76                      (cmp == 0 && cmpbool != 0x2a) ||
77                      (cmp < 0 && cmpbool != 0x1c) ||
78                      (cmp > 0 && cmpbool != 0x13))))
79         {
80           printf ("Error in cmp_tests for\nx = ");
81           mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
82           printf (" and\ny = ");
83           mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
84           printf ("\n");
85           exit (1);
86         }
87     }
88   mpfr_clears (x, y, (mpfr_ptr) 0);
89 }
90 
91 static void
92 eq_tests (void)
93 {
94   mpfr_t x, y;
95   long i;
96 
97   mpfr_inits (x, y, (mpfr_ptr) 0);
98   for (i = 0; i < 20000; i++)
99     {
100       mpfr_prec_t precx;
101 
102       precx = (randlimb () % 17) * 11 + MPFR_PREC_MIN;
103       mpfr_set_prec (x, precx);
104       mpfr_set_prec (y, precx + (randlimb () % 64));
105       mpfr_urandomb (x, RANDS);
106       if (randlimb () & 1)
107         mpfr_neg (x, x, MPFR_RNDN);
108       mpfr_set (y, x, MPFR_RNDN);  /* exact -> x = y */
109       if (mpfr_greater_p (x, y) || !mpfr_greaterequal_p (x, y) ||
110           mpfr_less_p (x, y) || !mpfr_lessequal_p (x, y) ||
111           mpfr_lessgreater_p (x, y) || !mpfr_equal_p (x, y) ||
112           mpfr_unordered_p (x, y))
113         {
114           printf ("Error in eq_tests for x = ");
115           mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
116           printf ("\n");
117           exit (1);
118         }
119     }
120   mpfr_clears (x, y, (mpfr_ptr) 0);
121 }
122 
123 int
124 main (void)
125 {
126   tests_start_mpfr ();
127   cmp_tests ();
128   eq_tests ();
129   tests_end_mpfr ();
130   return 0;
131 }
132