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
27 /*
28 * Tests for fmaximum{,f,l}() and fminimum{,f,l}()
29 */
30
31 #include <sys/cdefs.h>
32 #include <fenv.h>
33 #include <float.h>
34 #include <math.h>
35
36 #include "test-utils.h"
37
38 #pragma STDC FENV_ACCESS ON
39
40 /*
41 * Test whether func(x, y) has the expected result, and make sure no
42 * exceptions are raised.
43 */
44 #define TEST(func, type, x, y, expected, rmode) do { \
45 type __x = (x); /* convert before we clear exceptions */ \
46 type __y = (y); \
47 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); \
48 long double __result = func((__x), (__y)); \
49 CHECK_FP_EXCEPTIONS_MSG(0, ALL_STD_EXCEPT, \
50 #func "(%.20Lg, %.20Lg) rmode%d", (x), (y), rmode); \
51 ATF_CHECK_MSG(fpequal_cs(__result, (expected), true), \
52 #func "(%.20Lg, %.20Lg) rmode%d = %.20Lg, expected %.20Lg", \
53 (x), (y), rmode, __result, (expected)); \
54 } while (0)
55
56 static void
testall_r(long double big,long double small,int rmode)57 testall_r(long double big, long double small, int rmode)
58 {
59 long double expected_max, expected_min;
60 if (isnan(big) || isnan(small)) {
61 expected_max = big + small;
62 expected_min = expected_max;
63 } else {
64 expected_max = big;
65 expected_min = small;
66 }
67
68 TEST(fmaximumf, float, big, small, expected_max, rmode);
69 TEST(fmaximumf, float, small, big, expected_max, rmode);
70 TEST(fmaximum, double, big, small, expected_max, rmode);
71 TEST(fmaximum, double, small, big, expected_max, rmode);
72 TEST(fmaximuml, long double, big, small, expected_max, rmode);
73 TEST(fmaximuml, long double, small, big, expected_max, rmode);
74 TEST(fminimumf, float, big, small, expected_min, rmode);
75 TEST(fminimumf, float, small, big, expected_min, rmode);
76 TEST(fminimum, double, big, small, expected_min, rmode);
77 TEST(fminimum, double, small, big, expected_min, rmode);
78 TEST(fminimuml, long double, big, small, expected_min, rmode);
79 TEST(fminimuml, long double, small, big, expected_min, rmode);
80 }
81
82 /*
83 * Test all the functions: fmaximumf, fmaximum, fmaximuml, fminimumf, fminimum, fminimuml
84 * in all rounding modes and with the arguments in different orders.
85 * The input 'big' must be >= 'small'.
86 */
87 static void
testall(long double big,long double small)88 testall(long double big, long double small)
89 {
90 static const int rmodes[] = {
91 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
92 };
93 int i;
94
95 for (i = 0; i < 4; i++) {
96 fesetround(rmodes[i]);
97 testall_r(big, small, rmodes[i]);
98 }
99 }
100
101 ATF_TC_WITHOUT_HEAD(test1);
ATF_TC_BODY(test1,tc)102 ATF_TC_BODY(test1, tc)
103 {
104 testall(1.0, 0.0);
105 }
106
107 ATF_TC_WITHOUT_HEAD(test2);
ATF_TC_BODY(test2,tc)108 ATF_TC_BODY(test2, tc)
109 {
110 testall(42.0, nextafterf(42.0, -INFINITY));
111 }
112 ATF_TC_WITHOUT_HEAD(test3);
ATF_TC_BODY(test3,tc)113 ATF_TC_BODY(test3, tc)
114 {
115 testall(nextafterf(42.0, INFINITY), 42.0);
116 }
117
118 ATF_TC_WITHOUT_HEAD(test4);
ATF_TC_BODY(test4,tc)119 ATF_TC_BODY(test4, tc)
120 {
121 testall(-5.0, -5.0);
122 }
123
124 ATF_TC_WITHOUT_HEAD(test5);
ATF_TC_BODY(test5,tc)125 ATF_TC_BODY(test5, tc)
126 {
127 testall(-3.0, -4.0);
128 }
129
130 ATF_TC_WITHOUT_HEAD(test6);
ATF_TC_BODY(test6,tc)131 ATF_TC_BODY(test6, tc)
132 {
133 testall(1.0, NAN);
134 }
135 ATF_TC_WITHOUT_HEAD(test7);
ATF_TC_BODY(test7,tc)136 ATF_TC_BODY(test7, tc)
137 {
138 testall(INFINITY, NAN);
139 }
140
141 ATF_TC_WITHOUT_HEAD(test8);
ATF_TC_BODY(test8,tc)142 ATF_TC_BODY(test8, tc)
143 {
144 testall(INFINITY, 1.0);
145 }
146
147 ATF_TC_WITHOUT_HEAD(test9);
ATF_TC_BODY(test9,tc)148 ATF_TC_BODY(test9, tc)
149 {
150 testall(-3.0, -INFINITY);
151 }
152
153 ATF_TC_WITHOUT_HEAD(test10);
ATF_TC_BODY(test10,tc)154 ATF_TC_BODY(test10, tc)
155 {
156 testall(3.0, -INFINITY);
157 }
158
159 ATF_TC_WITHOUT_HEAD(test11);
ATF_TC_BODY(test11,tc)160 ATF_TC_BODY(test11, tc)
161 {
162 testall(NAN, NAN);
163 }
164
165 ATF_TC_WITHOUT_HEAD(test12);
ATF_TC_BODY(test12,tc)166 ATF_TC_BODY(test12, tc)
167 {
168 testall(0.0, -0.0);
169 }
170
171
ATF_TP_ADD_TCS(tp)172 ATF_TP_ADD_TCS(tp)
173 {
174 ATF_TP_ADD_TC(tp, test1);
175 ATF_TP_ADD_TC(tp, test2);
176 ATF_TP_ADD_TC(tp, test3);
177 ATF_TP_ADD_TC(tp, test4);
178 ATF_TP_ADD_TC(tp, test5);
179 ATF_TP_ADD_TC(tp, test6);
180 ATF_TP_ADD_TC(tp, test7);
181 ATF_TP_ADD_TC(tp, test8);
182 ATF_TP_ADD_TC(tp, test9);
183 ATF_TP_ADD_TC(tp, test10);
184 ATF_TP_ADD_TC(tp, test11);
185 ATF_TP_ADD_TC(tp, test12);
186
187 return (atf_no_error());
188 }
189