2 * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Tests for fmax{,f,l}() and fmin{,f,l}.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
39 #include "test-utils.h"
41 #pragma STDC FENV_ACCESS ON
44 * Test whether func(x, y) has the expected result, and make sure no
45 * exceptions are raised.
47 #define TEST(func, type, x, y, expected, rmode) do { \
48 type __x = (x); /* convert before we clear exceptions */ \
50 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); \
51 long double __result = func((__x), (__y)); \
52 CHECK_FP_EXCEPTIONS_MSG(0, ALL_STD_EXCEPT, \
53 #func "(%.20Lg, %.20Lg) rmode%d", (x), (y), rmode); \
54 ATF_CHECK_MSG(fpequal_cs(__result, (expected), true), \
55 #func "(%.20Lg, %.20Lg) rmode%d = %.20Lg, expected %.20Lg\n", \
56 (x), (y), rmode, __result, (expected)); \
60 testall_r(long double big, long double small, int rmode)
62 long double expected_max = isnan(big) ? small : big;
63 long double expected_min = isnan(small) ? big : small;
64 TEST(fmaxf, float, big, small, expected_max, rmode);
65 TEST(fmaxf, float, small, big, expected_max, rmode);
66 TEST(fmax, double, big, small, expected_max, rmode);
67 TEST(fmax, double, small, big, expected_max, rmode);
68 TEST(fmaxl, long double, big, small, expected_max, rmode);
69 TEST(fmaxl, long double, small, big, expected_max, rmode);
70 TEST(fminf, float, big, small, expected_min, rmode);
71 TEST(fminf, float, small, big, expected_min, rmode);
72 TEST(fmin, double, big, small, expected_min, rmode);
73 TEST(fmin, double, small, big, expected_min, rmode);
74 TEST(fminl, long double, big, small, expected_min, rmode);
75 TEST(fminl, long double, small, big, expected_min, rmode);
79 * Test all the functions: fmaxf, fmax, fmaxl, fminf, fmin, and fminl,
80 * in all rounding modes and with the arguments in different orders.
81 * The input 'big' must be >= 'small'.
84 testall(long double big, long double small)
86 static const int rmodes[] = {
87 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
91 for (i = 0; i < 4; i++) {
92 fesetround(rmodes[i]);
93 testall_r(big, small, rmodes[i]);
97 ATF_TC_WITHOUT_HEAD(test1);
98 ATF_TC_BODY(test1, tc)
103 ATF_TC_WITHOUT_HEAD(test2);
104 ATF_TC_BODY(test2, tc)
106 testall(42.0, nextafterf(42.0, -INFINITY));
108 ATF_TC_WITHOUT_HEAD(test3);
109 ATF_TC_BODY(test3, tc)
111 testall(nextafterf(42.0, INFINITY), 42.0);
114 ATF_TC_WITHOUT_HEAD(test4);
115 ATF_TC_BODY(test4, tc)
120 ATF_TC_WITHOUT_HEAD(test5);
121 ATF_TC_BODY(test5, tc)
126 ATF_TC_WITHOUT_HEAD(test6);
127 ATF_TC_BODY(test6, tc)
131 ATF_TC_WITHOUT_HEAD(test7);
132 ATF_TC_BODY(test7, tc)
134 testall(INFINITY, NAN);
137 ATF_TC_WITHOUT_HEAD(test8);
138 ATF_TC_BODY(test8, tc)
140 testall(INFINITY, 1.0);
143 ATF_TC_WITHOUT_HEAD(test9);
144 ATF_TC_BODY(test9, tc)
146 testall(-3.0, -INFINITY);
149 ATF_TC_WITHOUT_HEAD(test10);
150 ATF_TC_BODY(test10, tc)
152 testall(3.0, -INFINITY);
155 ATF_TC_WITHOUT_HEAD(test11);
156 ATF_TC_BODY(test11, tc)
161 ATF_TC_WITHOUT_HEAD(test12);
162 ATF_TC_BODY(test12, tc)
164 /* This test isn't strictly required to work by C99. */
171 ATF_TP_ADD_TC(tp, test1);
172 ATF_TP_ADD_TC(tp, test2);
173 ATF_TP_ADD_TC(tp, test3);
174 ATF_TP_ADD_TC(tp, test4);
175 ATF_TP_ADD_TC(tp, test5);
176 ATF_TP_ADD_TC(tp, test6);
177 ATF_TP_ADD_TC(tp, test7);
178 ATF_TP_ADD_TC(tp, test8);
179 ATF_TP_ADD_TC(tp, test9);
180 ATF_TP_ADD_TC(tp, test10);
181 ATF_TP_ADD_TC(tp, test11);
182 ATF_TP_ADD_TC(tp, test12);
184 return (atf_no_error());