1 /*-
2 * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/tools/regression/lib/msun/test-fmaxmin.c,v 1.3 2010/12/06 00:02:49 das Exp $
27 */
28
29 /*
30 * Tests for fmax{,f,l}() and fmin{,f,l}.
31 */
32
33 #include <fenv.h>
34 #include <float.h>
35 #include <math.h>
36 #include <stdio.h>
37
38 #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
39 FE_OVERFLOW | FE_UNDERFLOW)
40
41 #pragma STDC FENV_ACCESS ON
42
43 /*
44 * Test for equality with two special rules:
45 * fpequal(NaN, NaN) is true
46 * fpequal(+0.0, -0.0) is false
47 */
48 static inline int
fpequal(long double x,long double y)49 fpequal(long double x, long double y)
50 {
51
52 return ((x == y && !signbit(x) == !signbit(y))
53 || (isnan(x) && isnan(y)));
54 }
55
56 /*
57 * Test whether func(x, y) has the expected result, and make sure no
58 * exceptions are raised.
59 */
60 #define TEST(func, type, x, y, expected) do { \
61 type __x = (x); /* convert before we clear exceptions */ \
62 type __y = (y); \
63 feclearexcept(ALL_STD_EXCEPT); \
64 long double __result = func((__x), (__y)); \
65 if (fetestexcept(ALL_STD_EXCEPT)) { \
66 fprintf(stderr, #func "(%.20Lg, %.20Lg) raised 0x%x\n", \
67 (x), (y), fetestexcept(FE_ALL_EXCEPT)); \
68 ok = 0; \
69 } \
70 if (!fpequal(__result, (expected))) { \
71 fprintf(stderr, #func "(%.20Lg, %.20Lg) = %.20Lg, " \
72 "expected %.20Lg\n", (x), (y), __result, (expected)); \
73 ok = 0; \
74 } \
75 } while (0)
76
77 int
testall_r(long double big,long double small)78 testall_r(long double big, long double small)
79 {
80 int ok;
81
82 long double expected_max = isnan(big) ? small : big;
83 long double expected_min = isnan(small) ? big : small;
84 ok = 1;
85
86 TEST(fmaxf, float, big, small, expected_max);
87 TEST(fmaxf, float, small, big, expected_max);
88 TEST(fmax, double, big, small, expected_max);
89 TEST(fmax, double, small, big, expected_max);
90 TEST(fmaxl, long double, big, small, expected_max);
91 TEST(fmaxl, long double, small, big, expected_max);
92 TEST(fminf, float, big, small, expected_min);
93 TEST(fminf, float, small, big, expected_min);
94 TEST(fmin, double, big, small, expected_min);
95 TEST(fmin, double, small, big, expected_min);
96 TEST(fminl, long double, big, small, expected_min);
97 TEST(fminl, long double, small, big, expected_min);
98
99 return (ok);
100 }
101
102 /*
103 * Test all the functions: fmaxf, fmax, fmaxl, fminf, fmin, and fminl,
104 * in all rounding modes and with the arguments in different orders.
105 * The input 'big' must be >= 'small'.
106 */
107 void
testall(int testnum,long double big,long double small)108 testall(int testnum, long double big, long double small)
109 {
110 static const int rmodes[] = {
111 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
112 };
113 int i;
114
115 for (i = 0; i < 4; i++) {
116 fesetround(rmodes[i]);
117 if (!testall_r(big, small)) {
118 fprintf(stderr, "FAILURE in rounding mode %d\n",
119 rmodes[i]);
120 break;
121 }
122 }
123 printf("%sok %d - big = %.20Lg, small = %.20Lg\n",
124 (i == 4) ? "" : "not ", testnum, big, small);
125 }
126
127 int
main(int argc,char * argv[])128 main(int argc, char *argv[])
129 {
130
131 printf("1..12\n");
132
133 testall(1, 1.0, 0.0);
134 testall(2, 42.0, nextafterf(42.0, -INFINITY));
135 testall(3, nextafterf(42.0, INFINITY), 42.0);
136 testall(4, -5.0, -5.0);
137 testall(5, -3.0, -4.0);
138 testall(6, 1.0, NAN);
139 testall(7, INFINITY, NAN);
140 testall(8, INFINITY, 1.0);
141 testall(9, -3.0, -INFINITY);
142 testall(10, 3.0, -INFINITY);
143 testall(11, NAN, NAN);
144
145 /* This test isn't strictly required to work by C99. */
146 testall(12, 0.0, -0.0);
147
148 return (0);
149 }
150