xref: /freebsd/lib/msun/tests/fmaxmin_test.c (revision f078c492a9b57877c723586db26d789cda1b98ea)
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) do {				      \
48 	type __x = (x);	/* convert before we clear exceptions */	      \
49 	type __y = (y);							      \
50 	feclearexcept(ALL_STD_EXCEPT);					      \
51 	long double __result = func((__x), (__y));			      \
52 	if (fetestexcept(ALL_STD_EXCEPT)) {				      \
53 		fprintf(stderr, #func "(%.20Lg, %.20Lg) raised 0x%x\n",	      \
54 			(x), (y), fetestexcept(FE_ALL_EXCEPT));		      \
55 		ok = 0;							      \
56 	}								      \
57 	if (!fpequal(__result, (expected)))	{			      \
58 		fprintf(stderr, #func "(%.20Lg, %.20Lg) = %.20Lg, "	      \
59 			"expected %.20Lg\n", (x), (y), __result, (expected)); \
60 		ok = 0;							      \
61 	}								      \
62 } while (0)
63 
64 static int
65 testall_r(long double big, long double small)
66 {
67 	int ok;
68 
69 	long double expected_max = isnan(big) ? small : big;
70 	long double expected_min = isnan(small) ? big : small;
71 	ok = 1;
72 
73 	TEST(fmaxf, float, big, small, expected_max);
74 	TEST(fmaxf, float, small, big, expected_max);
75 	TEST(fmax, double, big, small, expected_max);
76 	TEST(fmax, double, small, big, expected_max);
77 	TEST(fmaxl, long double, big, small, expected_max);
78 	TEST(fmaxl, long double, small, big, expected_max);
79 	TEST(fminf, float, big, small, expected_min);
80 	TEST(fminf, float, small, big, expected_min);
81 	TEST(fmin, double, big, small, expected_min);
82 	TEST(fmin, double, small, big, expected_min);
83 	TEST(fminl, long double, big, small, expected_min);
84 	TEST(fminl, long double, small, big, expected_min);
85 
86 	return (ok);
87 }
88 
89 static const char *comment = NULL;
90 
91 /*
92  * Test all the functions: fmaxf, fmax, fmaxl, fminf, fmin, and fminl,
93  * in all rounding modes and with the arguments in different orders.
94  * The input 'big' must be >= 'small'.
95  */
96 static void
97 testall(int testnum, long double big, long double small)
98 {
99 	static const int rmodes[] = {
100 		FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO
101 	};
102 	int i;
103 
104 	for (i = 0; i < 4; i++) {
105 		fesetround(rmodes[i]);
106 		if (!testall_r(big, small)) {
107 			fprintf(stderr, "FAILURE in rounding mode %d\n",
108 				rmodes[i]);
109 			break;
110 		}
111 	}
112 	printf("%sok %d - big = %.20Lg, small = %.20Lg%s\n",
113 	       (i == 4) ? "" : "not ", testnum, big, small,
114 	       comment == NULL ? "" : comment);
115 }
116 
117 /* Clang 3.8.0+ fails the invariants for testcase 6, 7, 10, and 11. */
118 #if defined(__clang__) && \
119     ((__clang_major__ >  3)) || \
120     ((__clang_major__ == 3 && __clang_minor__ >= 8))
121 #define	affected_by_bug_208703
122 #endif
123 
124 int
125 main(void)
126 {
127 
128 	printf("1..12\n");
129 
130 	testall(1, 1.0, 0.0);
131 	testall(2, 42.0, nextafterf(42.0, -INFINITY));
132 	testall(3, nextafterf(42.0, INFINITY), 42.0);
133 	testall(4, -5.0, -5.0);
134 	testall(5, -3.0, -4.0);
135 #ifdef affected_by_bug_208703
136 	comment = "# TODO: testcase 6-7 fails invariant with clang 3.8+ (bug 208703)";
137 #endif
138 	testall(6, 1.0, NAN);
139 	testall(7, INFINITY, NAN);
140 	comment = NULL;
141 	testall(8, INFINITY, 1.0);
142 	testall(9, -3.0, -INFINITY);
143 	testall(10, 3.0, -INFINITY);
144 #ifdef affected_by_bug_208703
145 	comment = "# TODO: testcase 11-12 fails invariant with clang 3.8+ (bug 208703)";
146 #endif
147 	testall(11, NAN, NAN);
148 
149 	/* This test isn't strictly required to work by C99. */
150 	testall(12, 0.0, -0.0);
151 	comment = NULL;
152 
153 	return (0);
154 }
155