xref: /openbsd-src/regress/lib/libm/msun/fmaxmin_test.c (revision 498c7b5e98c82ca5a0055f676f2a6e4214e2d9a8)
1*498c7b5eSderaadt /*	$OpenBSD: fmaxmin_test.c,v 1.2 2021/12/13 18:04:28 deraadt Exp $	*/
2c36e572eSmbuhl /*-
3c36e572eSmbuhl  * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
4c36e572eSmbuhl  * All rights reserved.
5c36e572eSmbuhl  *
6c36e572eSmbuhl  * Redistribution and use in source and binary forms, with or without
7c36e572eSmbuhl  * modification, are permitted provided that the following conditions
8c36e572eSmbuhl  * are met:
9c36e572eSmbuhl  * 1. Redistributions of source code must retain the above copyright
10c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer.
11c36e572eSmbuhl  * 2. Redistributions in binary form must reproduce the above copyright
12c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer in the
13c36e572eSmbuhl  *    documentation and/or other materials provided with the distribution.
14c36e572eSmbuhl  *
15c36e572eSmbuhl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c36e572eSmbuhl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c36e572eSmbuhl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c36e572eSmbuhl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c36e572eSmbuhl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c36e572eSmbuhl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c36e572eSmbuhl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c36e572eSmbuhl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c36e572eSmbuhl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c36e572eSmbuhl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c36e572eSmbuhl  * SUCH DAMAGE.
26c36e572eSmbuhl  */
27c36e572eSmbuhl 
28c36e572eSmbuhl #include "macros.h"
29c36e572eSmbuhl 
30c36e572eSmbuhl /*
31c36e572eSmbuhl  * Tests for fmax{,f,l}() and fmin{,f,l}.
32c36e572eSmbuhl  */
33c36e572eSmbuhl 
34c36e572eSmbuhl #include <fenv.h>
35c36e572eSmbuhl #include <float.h>
36c36e572eSmbuhl #include <math.h>
37c36e572eSmbuhl #include <stdio.h>
38c36e572eSmbuhl 
39c36e572eSmbuhl #include "test-utils.h"
40c36e572eSmbuhl 
41c36e572eSmbuhl #pragma STDC FENV_ACCESS ON
42c36e572eSmbuhl 
43c36e572eSmbuhl /*
44c36e572eSmbuhl  * Test whether func(x, y) has the expected result, and make sure no
45c36e572eSmbuhl  * exceptions are raised.
46c36e572eSmbuhl  */
47c36e572eSmbuhl #define	TEST(func, type, x, y, expected, rmode) do {			      \
48c36e572eSmbuhl 	type __x = (x);	/* convert before we clear exceptions */	      \
49c36e572eSmbuhl 	type __y = (y);							      \
50c36e572eSmbuhl 	ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT));		      \
51c36e572eSmbuhl 	long double __result = func((__x), (__y));			      \
52c36e572eSmbuhl 	CHECK_FP_EXCEPTIONS_MSG(0, ALL_STD_EXCEPT,			      \
53c36e572eSmbuhl 	    #func "(%.20Lg, %.20Lg) rmode%d", (x), (y), rmode);		      \
54c36e572eSmbuhl 	ATF_CHECK_MSG(fpequal_cs(__result, (expected), true),		      \
55c36e572eSmbuhl 	    #func "(%.20Lg, %.20Lg) rmode%d = %.20Lg, expected %.20Lg\n",     \
56c36e572eSmbuhl 	    (x), (y), rmode, __result, (expected));			      \
57c36e572eSmbuhl } while (0)
58c36e572eSmbuhl 
59c36e572eSmbuhl static void
testall_r(long double big,long double small,int rmode)60c36e572eSmbuhl testall_r(long double big, long double small, int rmode)
61c36e572eSmbuhl {
62c36e572eSmbuhl 	long double expected_max = isnan(big) ? small : big;
63c36e572eSmbuhl 	long double expected_min = isnan(small) ? big : small;
64c36e572eSmbuhl 	TEST(fmaxf, float, big, small, expected_max, rmode);
65c36e572eSmbuhl 	TEST(fmaxf, float, small, big, expected_max, rmode);
66c36e572eSmbuhl 	TEST(fmax, double, big, small, expected_max, rmode);
67c36e572eSmbuhl 	TEST(fmax, double, small, big, expected_max, rmode);
68c36e572eSmbuhl 	TEST(fmaxl, long double, big, small, expected_max, rmode);
69c36e572eSmbuhl 	TEST(fmaxl, long double, small, big, expected_max, rmode);
70c36e572eSmbuhl 	TEST(fminf, float, big, small, expected_min, rmode);
71c36e572eSmbuhl 	TEST(fminf, float, small, big, expected_min, rmode);
72c36e572eSmbuhl 	TEST(fmin, double, big, small, expected_min, rmode);
73c36e572eSmbuhl 	TEST(fmin, double, small, big, expected_min, rmode);
74c36e572eSmbuhl 	TEST(fminl, long double, big, small, expected_min, rmode);
75c36e572eSmbuhl 	TEST(fminl, long double, small, big, expected_min, rmode);
76c36e572eSmbuhl }
77c36e572eSmbuhl 
78c36e572eSmbuhl /*
79c36e572eSmbuhl  * Test all the functions: fmaxf, fmax, fmaxl, fminf, fmin, and fminl,
80c36e572eSmbuhl  * in all rounding modes and with the arguments in different orders.
81c36e572eSmbuhl  * The input 'big' must be >= 'small'.
82c36e572eSmbuhl  */
83c36e572eSmbuhl static void
testall(long double big,long double small)84c36e572eSmbuhl testall(long double big, long double small)
85c36e572eSmbuhl {
86c36e572eSmbuhl 	static const int rmodes[] = {
87c36e572eSmbuhl 		FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
88c36e572eSmbuhl 	};
89c36e572eSmbuhl 	int i;
90c36e572eSmbuhl 
91c36e572eSmbuhl 	for (i = 0; i < 4; i++) {
92c36e572eSmbuhl 		fesetround(rmodes[i]);
93c36e572eSmbuhl 		testall_r(big, small, rmodes[i]);
94c36e572eSmbuhl 	}
95c36e572eSmbuhl }
96c36e572eSmbuhl 
97c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test1);
ATF_TC_BODY(test1,tc)98c36e572eSmbuhl ATF_TC_BODY(test1, tc)
99c36e572eSmbuhl {
100c36e572eSmbuhl 	testall(1.0, 0.0);
101c36e572eSmbuhl }
102c36e572eSmbuhl 
103c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test2);
ATF_TC_BODY(test2,tc)104c36e572eSmbuhl ATF_TC_BODY(test2, tc)
105c36e572eSmbuhl {
106c36e572eSmbuhl 	testall(42.0, nextafterf(42.0, -INFINITY));
107c36e572eSmbuhl }
108c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test3);
ATF_TC_BODY(test3,tc)109c36e572eSmbuhl ATF_TC_BODY(test3, tc)
110c36e572eSmbuhl {
111c36e572eSmbuhl 	testall(nextafterf(42.0, INFINITY), 42.0);
112c36e572eSmbuhl }
113c36e572eSmbuhl 
114c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test4);
ATF_TC_BODY(test4,tc)115c36e572eSmbuhl ATF_TC_BODY(test4, tc)
116c36e572eSmbuhl {
117c36e572eSmbuhl 	testall(-5.0, -5.0);
118c36e572eSmbuhl }
119c36e572eSmbuhl 
120c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test5);
ATF_TC_BODY(test5,tc)121c36e572eSmbuhl ATF_TC_BODY(test5, tc)
122c36e572eSmbuhl {
123c36e572eSmbuhl 	testall(-3.0, -4.0);
124c36e572eSmbuhl }
125c36e572eSmbuhl 
126c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test6);
ATF_TC_BODY(test6,tc)127c36e572eSmbuhl ATF_TC_BODY(test6, tc)
128c36e572eSmbuhl {
129c36e572eSmbuhl 	testall(1.0, NAN);
130c36e572eSmbuhl }
131c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test7);
ATF_TC_BODY(test7,tc)132c36e572eSmbuhl ATF_TC_BODY(test7, tc)
133c36e572eSmbuhl {
134c36e572eSmbuhl 	testall(INFINITY, NAN);
135c36e572eSmbuhl }
136c36e572eSmbuhl 
137c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test8);
ATF_TC_BODY(test8,tc)138c36e572eSmbuhl ATF_TC_BODY(test8, tc)
139c36e572eSmbuhl {
140c36e572eSmbuhl 	testall(INFINITY, 1.0);
141c36e572eSmbuhl }
142c36e572eSmbuhl 
143c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test9);
ATF_TC_BODY(test9,tc)144c36e572eSmbuhl ATF_TC_BODY(test9, tc)
145c36e572eSmbuhl {
146c36e572eSmbuhl 	testall(-3.0, -INFINITY);
147c36e572eSmbuhl }
148c36e572eSmbuhl 
149c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test10);
ATF_TC_BODY(test10,tc)150c36e572eSmbuhl ATF_TC_BODY(test10, tc)
151c36e572eSmbuhl {
152c36e572eSmbuhl 	testall(3.0, -INFINITY);
153c36e572eSmbuhl }
154c36e572eSmbuhl 
155c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test11);
ATF_TC_BODY(test11,tc)156c36e572eSmbuhl ATF_TC_BODY(test11, tc)
157c36e572eSmbuhl {
158c36e572eSmbuhl 	testall(NAN, NAN);
159c36e572eSmbuhl }
160c36e572eSmbuhl 
161c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(test12);
ATF_TC_BODY(test12,tc)162c36e572eSmbuhl ATF_TC_BODY(test12, tc)
163c36e572eSmbuhl {
164c36e572eSmbuhl 	/* This test isn't strictly required to work by C99. */
165c36e572eSmbuhl 	testall(0.0, -0.0);
166c36e572eSmbuhl }
167c36e572eSmbuhl 
168c36e572eSmbuhl 
ATF_TP_ADD_TCS(tp)169c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
170c36e572eSmbuhl {
171c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test1);
172c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test2);
173c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test3);
174c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test4);
175c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test5);
176c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test6);
177c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test7);
178c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test8);
179c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test9);
180c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test10);
181c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test11);
182c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, test12);
183c36e572eSmbuhl 
184c36e572eSmbuhl 	return (atf_no_error());
185c36e572eSmbuhl }
186