1 /* $OpenBSD: fmaxmin_test.c,v 1.1 2021/10/22 18:00:22 mbuhl Exp $ */ 2 /*- 3 * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 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 #include "macros.h" 29 30 /* 31 * Tests for fmax{,f,l}() and fmin{,f,l}. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <fenv.h> 38 #include <float.h> 39 #include <math.h> 40 #include <stdio.h> 41 42 #include "test-utils.h" 43 44 #pragma STDC FENV_ACCESS ON 45 46 /* 47 * Test whether func(x, y) has the expected result, and make sure no 48 * exceptions are raised. 49 */ 50 #define TEST(func, type, x, y, expected, rmode) do { \ 51 type __x = (x); /* convert before we clear exceptions */ \ 52 type __y = (y); \ 53 ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); \ 54 long double __result = func((__x), (__y)); \ 55 CHECK_FP_EXCEPTIONS_MSG(0, ALL_STD_EXCEPT, \ 56 #func "(%.20Lg, %.20Lg) rmode%d", (x), (y), rmode); \ 57 ATF_CHECK_MSG(fpequal_cs(__result, (expected), true), \ 58 #func "(%.20Lg, %.20Lg) rmode%d = %.20Lg, expected %.20Lg\n", \ 59 (x), (y), rmode, __result, (expected)); \ 60 } while (0) 61 62 static void 63 testall_r(long double big, long double small, int rmode) 64 { 65 long double expected_max = isnan(big) ? small : big; 66 long double expected_min = isnan(small) ? big : small; 67 TEST(fmaxf, float, big, small, expected_max, rmode); 68 TEST(fmaxf, float, small, big, expected_max, rmode); 69 TEST(fmax, double, big, small, expected_max, rmode); 70 TEST(fmax, double, small, big, expected_max, rmode); 71 TEST(fmaxl, long double, big, small, expected_max, rmode); 72 TEST(fmaxl, long double, small, big, expected_max, rmode); 73 TEST(fminf, float, big, small, expected_min, rmode); 74 TEST(fminf, float, small, big, expected_min, rmode); 75 TEST(fmin, double, big, small, expected_min, rmode); 76 TEST(fmin, double, small, big, expected_min, rmode); 77 TEST(fminl, long double, big, small, expected_min, rmode); 78 TEST(fminl, long double, small, big, expected_min, rmode); 79 } 80 81 /* 82 * Test all the functions: fmaxf, fmax, fmaxl, fminf, fmin, and fminl, 83 * in all rounding modes and with the arguments in different orders. 84 * The input 'big' must be >= 'small'. 85 */ 86 static void 87 testall(long double big, long double small) 88 { 89 static const int rmodes[] = { 90 FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO 91 }; 92 int i; 93 94 for (i = 0; i < 4; i++) { 95 fesetround(rmodes[i]); 96 testall_r(big, small, rmodes[i]); 97 } 98 } 99 100 ATF_TC_WITHOUT_HEAD(test1); 101 ATF_TC_BODY(test1, tc) 102 { 103 testall(1.0, 0.0); 104 } 105 106 ATF_TC_WITHOUT_HEAD(test2); 107 ATF_TC_BODY(test2, tc) 108 { 109 testall(42.0, nextafterf(42.0, -INFINITY)); 110 } 111 ATF_TC_WITHOUT_HEAD(test3); 112 ATF_TC_BODY(test3, tc) 113 { 114 testall(nextafterf(42.0, INFINITY), 42.0); 115 } 116 117 ATF_TC_WITHOUT_HEAD(test4); 118 ATF_TC_BODY(test4, tc) 119 { 120 testall(-5.0, -5.0); 121 } 122 123 ATF_TC_WITHOUT_HEAD(test5); 124 ATF_TC_BODY(test5, tc) 125 { 126 testall(-3.0, -4.0); 127 } 128 129 ATF_TC_WITHOUT_HEAD(test6); 130 ATF_TC_BODY(test6, tc) 131 { 132 testall(1.0, NAN); 133 } 134 ATF_TC_WITHOUT_HEAD(test7); 135 ATF_TC_BODY(test7, tc) 136 { 137 testall(INFINITY, NAN); 138 } 139 140 ATF_TC_WITHOUT_HEAD(test8); 141 ATF_TC_BODY(test8, tc) 142 { 143 testall(INFINITY, 1.0); 144 } 145 146 ATF_TC_WITHOUT_HEAD(test9); 147 ATF_TC_BODY(test9, tc) 148 { 149 testall(-3.0, -INFINITY); 150 } 151 152 ATF_TC_WITHOUT_HEAD(test10); 153 ATF_TC_BODY(test10, tc) 154 { 155 testall(3.0, -INFINITY); 156 } 157 158 ATF_TC_WITHOUT_HEAD(test11); 159 ATF_TC_BODY(test11, tc) 160 { 161 testall(NAN, NAN); 162 } 163 164 ATF_TC_WITHOUT_HEAD(test12); 165 ATF_TC_BODY(test12, tc) 166 { 167 /* This test isn't strictly required to work by C99. */ 168 testall(0.0, -0.0); 169 } 170 171 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