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>
37 #include "test-utils.h"
39 #pragma STDC FENV_ACCESS ON
42 * Test whether func(x, y) has the expected result, and make sure no
43 * exceptions are raised.
45 #define TEST(func, type, x, y, expected, rmode) do { \
46 type __x = (x); /* convert before we clear exceptions */ \
48 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); \
49 long double __result = func((__x), (__y)); \
50 CHECK_FP_EXCEPTIONS_MSG(0, ALL_STD_EXCEPT, \
51 #func "(%.20Lg, %.20Lg) rmode%d", (x), (y), rmode); \
52 ATF_CHECK_MSG(fpequal_cs(__result, (expected), true), \
53 #func "(%.20Lg, %.20Lg) rmode%d = %.20Lg, expected %.20Lg\n", \
54 (x), (y), rmode, __result, (expected)); \
58 testall_r(long double big, long double small, int rmode)
60 long double expected_max = isnan(big) ? small : big;
61 long double expected_min = isnan(small) ? big : small;
62 TEST(fmaxf, float, big, small, expected_max, rmode);
63 TEST(fmaxf, float, small, big, expected_max, rmode);
64 TEST(fmax, double, big, small, expected_max, rmode);
65 TEST(fmax, double, small, big, expected_max, rmode);
66 TEST(fmaxl, long double, big, small, expected_max, rmode);
67 TEST(fmaxl, long double, small, big, expected_max, rmode);
68 TEST(fminf, float, big, small, expected_min, rmode);
69 TEST(fminf, float, small, big, expected_min, rmode);
70 TEST(fmin, double, big, small, expected_min, rmode);
71 TEST(fmin, double, small, big, expected_min, rmode);
72 TEST(fminl, long double, big, small, expected_min, rmode);
73 TEST(fminl, long double, small, big, expected_min, rmode);
77 * Test all the functions: fmaxf, fmax, fmaxl, fminf, fmin, and fminl,
78 * in all rounding modes and with the arguments in different orders.
79 * The input 'big' must be >= 'small'.
82 testall(long double big, long double small)
84 static const int rmodes[] = {
85 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
89 for (i = 0; i < 4; i++) {
90 fesetround(rmodes[i]);
91 testall_r(big, small, rmodes[i]);
95 ATF_TC_WITHOUT_HEAD(test1);
96 ATF_TC_BODY(test1, tc)
101 ATF_TC_WITHOUT_HEAD(test2);
102 ATF_TC_BODY(test2, tc)
104 testall(42.0, nextafterf(42.0, -INFINITY));
106 ATF_TC_WITHOUT_HEAD(test3);
107 ATF_TC_BODY(test3, tc)
109 testall(nextafterf(42.0, INFINITY), 42.0);
112 ATF_TC_WITHOUT_HEAD(test4);
113 ATF_TC_BODY(test4, tc)
118 ATF_TC_WITHOUT_HEAD(test5);
119 ATF_TC_BODY(test5, tc)
124 ATF_TC_WITHOUT_HEAD(test6);
125 ATF_TC_BODY(test6, tc)
129 ATF_TC_WITHOUT_HEAD(test7);
130 ATF_TC_BODY(test7, tc)
132 testall(INFINITY, NAN);
135 ATF_TC_WITHOUT_HEAD(test8);
136 ATF_TC_BODY(test8, tc)
138 testall(INFINITY, 1.0);
141 ATF_TC_WITHOUT_HEAD(test9);
142 ATF_TC_BODY(test9, tc)
144 testall(-3.0, -INFINITY);
147 ATF_TC_WITHOUT_HEAD(test10);
148 ATF_TC_BODY(test10, tc)
150 testall(3.0, -INFINITY);
153 ATF_TC_WITHOUT_HEAD(test11);
154 ATF_TC_BODY(test11, tc)
159 ATF_TC_WITHOUT_HEAD(test12);
160 ATF_TC_BODY(test12, tc)
162 /* This test isn't strictly required to work by C99. */
169 ATF_TP_ADD_TC(tp, test1);
170 ATF_TP_ADD_TC(tp, test2);
171 ATF_TP_ADD_TC(tp, test3);
172 ATF_TP_ADD_TC(tp, test4);
173 ATF_TP_ADD_TC(tp, test5);
174 ATF_TP_ADD_TC(tp, test6);
175 ATF_TP_ADD_TC(tp, test7);
176 ATF_TP_ADD_TC(tp, test8);
177 ATF_TP_ADD_TC(tp, test9);
178 ATF_TP_ADD_TC(tp, test10);
179 ATF_TP_ADD_TC(tp, test11);
180 ATF_TP_ADD_TC(tp, test12);
182 return (atf_no_error());