xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tcomparisons.c (revision ba125506a622fe649968631a56eba5d42ff57863)
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-2023 Free Software Foundation, Inc.
6 Contributed by the AriC and Caramba 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 https://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 "mpfr-test.h"
26 
27 static void
cmp_tests(void)28 cmp_tests (void)
29 {
30   mpfr_t x, y;
31   long i;
32 
33   mpfr_inits (x, y, (mpfr_ptr) 0);
34   for (i = 0; i < 80000; i++)
35     {
36       mpfr_prec_t precx, precy;
37       int signx, signy, cmp;
38       unsigned int cmpbool = 0;
39 
40       precx = (randlimb () % 17) * 11 + MPFR_PREC_MIN;
41       precy = (randlimb () % 17) * 11 + MPFR_PREC_MIN;
42       mpfr_set_prec (x, precx);
43       mpfr_set_prec (y, precy);
44       mpfr_urandomb (x, RANDS);
45       mpfr_urandomb (y, RANDS);
46       signx = RAND_BOOL ();
47       signy = randlimb () % 256 ? signx : 1 - signx;
48       /* signy = signx most of the time (most interesting case) */
49       if (signx)
50         mpfr_neg (x, x, MPFR_RNDN);
51       if (signy)
52         mpfr_neg (y, y, MPFR_RNDN);
53       if (i <= 1)
54         mpfr_set_nan (x);
55       if (i == 0 || i == 2)
56         mpfr_set_nan (y);
57       if (mpfr_greater_p (x, y))
58         cmpbool |= 0x01;
59       if (mpfr_greaterequal_p (x, y))
60         cmpbool |= 0x02;
61       if (mpfr_less_p (x, y))
62         cmpbool |= 0x04;
63       if (mpfr_lessequal_p (x, y))
64         cmpbool |= 0x08;
65       if (mpfr_lessgreater_p (x, y))
66         cmpbool |= 0x10;
67       if (mpfr_equal_p (x, y))
68         cmpbool |= 0x20;
69       if (mpfr_unordered_p (x, y))
70         cmpbool |= 0x40;
71       if ((i <= 2 && cmpbool != 0x40) ||
72           (i > 2 && (cmp = mpfr_cmp (x, y),
73                      (cmp == 0 && cmpbool != 0x2a) ||
74                      (cmp < 0 && cmpbool != 0x1c) ||
75                      (cmp > 0 && cmpbool != 0x13))))
76         {
77           printf ("Error in cmp_tests for\nx = ");
78           mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
79           printf (" and\ny = ");
80           mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
81           printf ("\n");
82           exit (1);
83         }
84     }
85   mpfr_clears (x, y, (mpfr_ptr) 0);
86 }
87 
88 static void
eq_tests(void)89 eq_tests (void)
90 {
91   mpfr_t x, y;
92   long i;
93 
94   mpfr_inits (x, y, (mpfr_ptr) 0);
95   for (i = 0; i < 20000; i++)
96     {
97       mpfr_prec_t precx;
98 
99       precx = (randlimb () % 17) * 11 + MPFR_PREC_MIN;
100       mpfr_set_prec (x, precx);
101       mpfr_set_prec (y, precx + (randlimb () % 64));
102       mpfr_urandomb (x, RANDS);
103       if (RAND_BOOL ())
104         mpfr_neg (x, x, MPFR_RNDN);
105       mpfr_set (y, x, MPFR_RNDN);  /* exact -> x = y */
106       if (mpfr_greater_p (x, y) || !mpfr_greaterequal_p (x, y) ||
107           mpfr_less_p (x, y) || !mpfr_lessequal_p (x, y) ||
108           mpfr_lessgreater_p (x, y) || !mpfr_equal_p (x, y) ||
109           mpfr_unordered_p (x, y))
110         {
111           printf ("Error in eq_tests for x = ");
112           mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
113           printf ("\n");
114           exit (1);
115         }
116     }
117   mpfr_clears (x, y, (mpfr_ptr) 0);
118 }
119 
120 int
main(void)121 main (void)
122 {
123   tests_start_mpfr ();
124   cmp_tests ();
125   eq_tests ();
126   tests_end_mpfr ();
127   return 0;
128 }
129