xref: /freebsd/lib/msun/tests/fmaxmin_test.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
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 fmax{,f,l}() and fmin{,f,l}.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <fenv.h>
35 #include <float.h>
36 #include <math.h>
37 #include <stdio.h>
38 
39 #include "test-utils.h"
40 
41 #pragma STDC FENV_ACCESS ON
42 
43 /*
44  * Test whether func(x, y) has the expected result, and make sure no
45  * exceptions are raised.
46  */
47 #define	TEST(func, type, x, y, expected, rmode) do {			      \
48 	type __x = (x);	/* convert before we clear exceptions */	      \
49 	type __y = (y);							      \
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));			      \
57 } while (0)
58 
59 static void
60 testall_r(long double big, long double small, int rmode)
61 {
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);
76 }
77 
78 /*
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'.
82  */
83 static void
84 testall(long double big, long double small)
85 {
86 	static const int rmodes[] = {
87 		FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
88 	};
89 	int i;
90 
91 	for (i = 0; i < 4; i++) {
92 		fesetround(rmodes[i]);
93 		testall_r(big, small, rmodes[i]);
94 	}
95 }
96 
97 ATF_TC_WITHOUT_HEAD(test1);
98 ATF_TC_BODY(test1, tc)
99 {
100 	testall(1.0, 0.0);
101 }
102 
103 ATF_TC_WITHOUT_HEAD(test2);
104 ATF_TC_BODY(test2, tc)
105 {
106 	testall(42.0, nextafterf(42.0, -INFINITY));
107 }
108 ATF_TC_WITHOUT_HEAD(test3);
109 ATF_TC_BODY(test3, tc)
110 {
111 	testall(nextafterf(42.0, INFINITY), 42.0);
112 }
113 
114 ATF_TC_WITHOUT_HEAD(test4);
115 ATF_TC_BODY(test4, tc)
116 {
117 	testall(-5.0, -5.0);
118 }
119 
120 ATF_TC_WITHOUT_HEAD(test5);
121 ATF_TC_BODY(test5, tc)
122 {
123 	testall(-3.0, -4.0);
124 }
125 
126 ATF_TC_WITHOUT_HEAD(test6);
127 ATF_TC_BODY(test6, tc)
128 {
129 	testall(1.0, NAN);
130 }
131 ATF_TC_WITHOUT_HEAD(test7);
132 ATF_TC_BODY(test7, tc)
133 {
134 	testall(INFINITY, NAN);
135 }
136 
137 ATF_TC_WITHOUT_HEAD(test8);
138 ATF_TC_BODY(test8, tc)
139 {
140 	testall(INFINITY, 1.0);
141 }
142 
143 ATF_TC_WITHOUT_HEAD(test9);
144 ATF_TC_BODY(test9, tc)
145 {
146 	testall(-3.0, -INFINITY);
147 }
148 
149 ATF_TC_WITHOUT_HEAD(test10);
150 ATF_TC_BODY(test10, tc)
151 {
152 	testall(3.0, -INFINITY);
153 }
154 
155 ATF_TC_WITHOUT_HEAD(test11);
156 ATF_TC_BODY(test11, tc)
157 {
158 	testall(NAN, NAN);
159 }
160 
161 ATF_TC_WITHOUT_HEAD(test12);
162 ATF_TC_BODY(test12, tc)
163 {
164 	/* This test isn't strictly required to work by C99. */
165 	testall(0.0, -0.0);
166 }
167 
168 
169 ATF_TP_ADD_TCS(tp)
170 {
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);
183 
184 	return (atf_no_error());
185 }
186