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