1 /* 2 * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 3 * Copyright (c) 2026 Jesús Blázquez <jesuscblazquez@gmail.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Tests for fmaximum{,f,l}(), fminimum{,f,l}(), fmaximum_mag{,f,l}, 30 * fminimum_mag{,f,l}, fmaximum_num{,f,l}, fminimum_num{,f,l} 31 */ 32 33 #include <sys/cdefs.h> 34 #include <fenv.h> 35 #include <float.h> 36 #include <math.h> 37 38 #include "test-utils.h" 39 40 #pragma STDC FENV_ACCESS ON 41 42 /* 43 * Test whether func(x, y) has the expected result, and make sure no 44 * exceptions are raised. 45 */ 46 #define TEST(func, type, x, y, expected, rmode) do { \ 47 type __x = (x); /* convert before we clear exceptions */ \ 48 type __y = (y); \ 49 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); \ 50 long double __result = func((__x), (__y)); \ 51 CHECK_FP_EXCEPTIONS_MSG(0, ALL_STD_EXCEPT, \ 52 #func "(%.20Lg, %.20Lg) rmode%d", (x), (y), rmode); \ 53 ATF_CHECK_MSG(fpequal_cs(__result, (expected), true), \ 54 #func "(%.20Lg, %.20Lg) rmode%d = %.20Lg, expected %.20Lg", \ 55 (x), (y), rmode, __result, (expected)); \ 56 } while (0) 57 58 static void 59 testall_r(long double big, long double small, int rmode) 60 { 61 long double expected_max, expected_min; 62 if (isnan(big) || isnan(small)) { 63 expected_max = NAN; 64 expected_min = expected_max; 65 } else { 66 expected_max = big; 67 expected_min = small; 68 } 69 70 TEST(fmaximumf, float, big, small, expected_max, rmode); 71 TEST(fmaximumf, float, small, big, expected_max, rmode); 72 TEST(fmaximum, double, big, small, expected_max, rmode); 73 TEST(fmaximum, double, small, big, expected_max, rmode); 74 TEST(fmaximuml, long double, big, small, expected_max, rmode); 75 TEST(fmaximuml, long double, small, big, expected_max, rmode); 76 TEST(fminimumf, float, big, small, expected_min, rmode); 77 TEST(fminimumf, float, small, big, expected_min, rmode); 78 TEST(fminimum, double, big, small, expected_min, rmode); 79 TEST(fminimum, double, small, big, expected_min, rmode); 80 TEST(fminimuml, long double, big, small, expected_min, rmode); 81 TEST(fminimuml, long double, small, big, expected_min, rmode); 82 } 83 84 static void 85 testall_mag_r(long double big, long double small, int rmode) { 86 long double expected_max_mag, expected_min_mag; 87 if (isnan(big) || isnan(small)) { 88 expected_max_mag = NAN; 89 expected_min_mag = expected_max_mag; 90 } else { 91 if (fabsl(small) > fabsl(big)) { 92 expected_max_mag = small; 93 expected_min_mag = big; 94 } else { 95 expected_max_mag = big; 96 expected_min_mag = small; 97 } 98 } 99 100 TEST(fmaximum_magf, float, big, small, expected_max_mag, rmode); 101 TEST(fmaximum_magf, float, small, big, expected_max_mag, rmode); 102 TEST(fmaximum_mag, double, big, small, expected_max_mag, rmode); 103 TEST(fmaximum_mag, double, small, big, expected_max_mag, rmode); 104 TEST(fmaximum_magl, long double, big, small, expected_max_mag, rmode); 105 TEST(fmaximum_magl, long double, small, big, expected_max_mag, rmode); 106 TEST(fminimum_magf, float, big, small, expected_min_mag, rmode); 107 TEST(fminimum_magf, float, small, big, expected_min_mag, rmode); 108 TEST(fminimum_mag, double, big, small, expected_min_mag, rmode); 109 TEST(fminimum_mag, double, small, big, expected_min_mag, rmode); 110 TEST(fminimum_magl, long double, big, small, expected_min_mag, rmode); 111 TEST(fminimum_magl, long double, small, big, expected_min_mag, rmode); 112 } 113 114 static void 115 testall_num_r(long double big, long double small, int rmode) { 116 long double expected_max_num = isnan(big) ? small : big; 117 long double expected_min_num = isnan(small) ? big : small; 118 119 TEST(fmaximum_numf, float, big, small, expected_max_num, rmode); 120 TEST(fmaximum_numf, float, small, big, expected_max_num, rmode); 121 TEST(fmaximum_num, double, big, small, expected_max_num, rmode); 122 TEST(fmaximum_num, double, small, big, expected_max_num, rmode); 123 TEST(fmaximum_numl, long double, big, small, expected_max_num, rmode); 124 TEST(fmaximum_numl, long double, small, big, expected_max_num, rmode); 125 TEST(fminimum_numf, float, big, small, expected_min_num, rmode); 126 TEST(fminimum_numf, float, small, big, expected_min_num, rmode); 127 TEST(fminimum_num, double, big, small, expected_min_num, rmode); 128 TEST(fminimum_num, double, small, big, expected_min_num, rmode); 129 TEST(fminimum_numl, long double, big, small, expected_min_num, rmode); 130 TEST(fminimum_numl, long double, small, big, expected_min_num, rmode); 131 } 132 133 /* 134 * Test all the functions: fmaximumf, fmaximum, fmaximuml, fminimumf, fminimum, fminimuml 135 * in all rounding modes and with the arguments in different orders. 136 * The input 'big' must be >= 'small'. 137 */ 138 static void 139 testall(long double big, long double small) 140 { 141 static const int rmodes[] = { 142 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO 143 }; 144 int i; 145 146 for (i = 0; i < 4; i++) { 147 fesetround(rmodes[i]); 148 testall_r(big, small, rmodes[i]); 149 testall_mag_r(big, small, rmodes[i]); 150 testall_num_r(big, small, rmodes[i]); 151 } 152 } 153 154 ATF_TC_WITHOUT_HEAD(test1); 155 ATF_TC_BODY(test1, tc) 156 { 157 testall(1.0, 0.0); 158 } 159 160 ATF_TC_WITHOUT_HEAD(test2); 161 ATF_TC_BODY(test2, tc) 162 { 163 testall(42.0, nextafterf(42.0, -INFINITY)); 164 } 165 ATF_TC_WITHOUT_HEAD(test3); 166 ATF_TC_BODY(test3, tc) 167 { 168 testall(nextafterf(42.0, INFINITY), 42.0); 169 } 170 171 ATF_TC_WITHOUT_HEAD(test4); 172 ATF_TC_BODY(test4, tc) 173 { 174 testall(-5.0, -5.0); 175 } 176 177 ATF_TC_WITHOUT_HEAD(test5); 178 ATF_TC_BODY(test5, tc) 179 { 180 testall(-3.0, -4.0); 181 } 182 183 ATF_TC_WITHOUT_HEAD(test6); 184 ATF_TC_BODY(test6, tc) 185 { 186 testall(1.0, NAN); 187 } 188 ATF_TC_WITHOUT_HEAD(test7); 189 ATF_TC_BODY(test7, tc) 190 { 191 testall(INFINITY, NAN); 192 } 193 194 ATF_TC_WITHOUT_HEAD(test8); 195 ATF_TC_BODY(test8, tc) 196 { 197 testall(INFINITY, 1.0); 198 } 199 200 ATF_TC_WITHOUT_HEAD(test9); 201 ATF_TC_BODY(test9, tc) 202 { 203 testall(-3.0, -INFINITY); 204 } 205 206 ATF_TC_WITHOUT_HEAD(test10); 207 ATF_TC_BODY(test10, tc) 208 { 209 testall(3.0, -INFINITY); 210 } 211 212 ATF_TC_WITHOUT_HEAD(test11); 213 ATF_TC_BODY(test11, tc) 214 { 215 testall(NAN, NAN); 216 } 217 218 ATF_TC_WITHOUT_HEAD(test12); 219 ATF_TC_BODY(test12, tc) 220 { 221 testall(0.0, -0.0); 222 } 223 224 225 ATF_TC_WITHOUT_HEAD(test13); 226 ATF_TC_BODY(test13, tc) 227 { 228 testall(2.0, -2.0); 229 } 230 231 ATF_TC_WITHOUT_HEAD(test14); 232 ATF_TC_BODY(test14, tc) 233 { 234 testall(-0.0, -0.0); 235 } 236 237 ATF_TC_WITHOUT_HEAD(test15); 238 ATF_TC_BODY(test15, tc) 239 { 240 testall(0.0, 0.0); 241 } 242 243 ATF_TP_ADD_TCS(tp) 244 { 245 ATF_TP_ADD_TC(tp, test1); 246 ATF_TP_ADD_TC(tp, test2); 247 ATF_TP_ADD_TC(tp, test3); 248 ATF_TP_ADD_TC(tp, test4); 249 ATF_TP_ADD_TC(tp, test5); 250 ATF_TP_ADD_TC(tp, test6); 251 ATF_TP_ADD_TC(tp, test7); 252 ATF_TP_ADD_TC(tp, test8); 253 ATF_TP_ADD_TC(tp, test9); 254 ATF_TP_ADD_TC(tp, test10); 255 ATF_TP_ADD_TC(tp, test11); 256 ATF_TP_ADD_TC(tp, test12); 257 ATF_TP_ADD_TC(tp, test13); 258 ATF_TP_ADD_TC(tp, test14); 259 ATF_TP_ADD_TC(tp, test15); 260 261 return (atf_no_error()); 262 } 263