xref: /minix/tests/lib/libm/t_cosh.c (revision ebfedea0)
1 /* $NetBSD: t_cosh.c,v 1.5 2013/04/09 12:11:04 isaki Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_cosh.c,v 1.5 2013/04/09 12:11:04 isaki Exp $");
33 
34 #include <atf-c.h>
35 #include <math.h>
36 #include <stdio.h>
37 
38 static const struct {
39 	double x;
40 	double y;
41 	double e;
42 } values[] = {
43 	{ -10,    11013.23292010332, 1e4, },
44 	{ -2,     3.762195691083631, 1,   },
45 	{ -1,     1.543080634815244, 1,   },
46 	{ -0.05,  1.001250260438369, 1,   },
47 	{ -0.001, 1.000000500000042, 1,   },
48 	{  0,     1,                 1,   },
49 	{  0.001, 1.000000500000042, 1,   },
50 	{  0.05,  1.001250260438369, 1,   },
51 	{  1,     1.543080634815244, 1,   },
52 	{  2,     3.762195691083631, 1,   },
53 	{  10,    11013.23292010332, 1e4, },
54 };
55 
56 /*
57  * cosh(3)
58  */
59 ATF_TC(cosh_inrange);
60 ATF_TC_HEAD(cosh_inrange, tc)
61 {
62 	atf_tc_set_md_var(tc, "descr", "cosh(x) for some values");
63 }
64 
65 ATF_TC_BODY(cosh_inrange, tc)
66 {
67 #ifndef __vax__
68 	double eps;
69 	double x;
70 	double y;
71 	size_t i;
72 
73 	for (i = 0; i < __arraycount(values); i++) {
74 		x = values[i].x;
75 		y = values[i].y;
76 		eps = 1e-15 * values[i].e;
77 
78 		if (fabs(cosh(x) - y) > eps)
79 			atf_tc_fail_nonfatal("cosh(%g) != %g\n", x, y);
80 	}
81 #endif
82 }
83 
84 ATF_TC(cosh_nan);
85 ATF_TC_HEAD(cosh_nan, tc)
86 {
87 	atf_tc_set_md_var(tc, "descr", "Test cosh(NaN) == NaN");
88 }
89 
90 ATF_TC_BODY(cosh_nan, tc)
91 {
92 #ifndef __vax__
93 	const double x = 0.0L / 0.0L;
94 
95 	ATF_CHECK(isnan(x) != 0);
96 	ATF_CHECK(isnan(cosh(x)) != 0);
97 #endif
98 }
99 
100 ATF_TC(cosh_inf_neg);
101 ATF_TC_HEAD(cosh_inf_neg, tc)
102 {
103 	atf_tc_set_md_var(tc, "descr", "Test cosh(-Inf) == +Inf");
104 }
105 
106 ATF_TC_BODY(cosh_inf_neg, tc)
107 {
108 #ifndef __vax__
109 	const double x = -1.0L / 0.0L;
110 	double y = cosh(x);
111 
112 	ATF_CHECK(isinf(y) != 0);
113 	ATF_CHECK(signbit(y) == 0);
114 #endif
115 }
116 
117 ATF_TC(cosh_inf_pos);
118 ATF_TC_HEAD(cosh_inf_pos, tc)
119 {
120 	atf_tc_set_md_var(tc, "descr", "Test cosh(+Inf) == +Inf");
121 }
122 
123 ATF_TC_BODY(cosh_inf_pos, tc)
124 {
125 #ifndef __vax__
126 	const double x = 1.0L / 0.0L;
127 	double y = cosh(x);
128 
129 	ATF_CHECK(isinf(y) != 0);
130 	ATF_CHECK(signbit(y) == 0);
131 #endif
132 }
133 
134 ATF_TC(cosh_zero_neg);
135 ATF_TC_HEAD(cosh_zero_neg, tc)
136 {
137 	atf_tc_set_md_var(tc, "descr", "Test cosh(-0.0) == 1.0");
138 }
139 
140 ATF_TC_BODY(cosh_zero_neg, tc)
141 {
142 #ifndef __vax__
143 	const double x = -0.0L;
144 
145 	if (cosh(x) != 1.0)
146 		atf_tc_fail_nonfatal("cosh(-0.0) != 1.0");
147 #endif
148 }
149 
150 ATF_TC(cosh_zero_pos);
151 ATF_TC_HEAD(cosh_zero_pos, tc)
152 {
153 	atf_tc_set_md_var(tc, "descr", "Test cosh(+0.0) == 1.0");
154 }
155 
156 ATF_TC_BODY(cosh_zero_pos, tc)
157 {
158 #ifndef __vax__
159 	const double x = 0.0L;
160 
161 	if (cosh(x) != 1.0)
162 		atf_tc_fail_nonfatal("cosh(+0.0) != 1.0");
163 #endif
164 }
165 
166 /*
167  * coshf(3)
168  */
169 ATF_TC(coshf_inrange);
170 ATF_TC_HEAD(coshf_inrange, tc)
171 {
172 	atf_tc_set_md_var(tc, "descr", "coshf(x) for some values");
173 }
174 
175 ATF_TC_BODY(coshf_inrange, tc)
176 {
177 #ifndef __vax__
178 	float eps;
179 	float x;
180 	float y;
181 	size_t i;
182 
183 	for (i = 0; i < __arraycount(values); i++) {
184 		x = values[i].x;
185 		y = values[i].y;
186 		eps = 1e-6 * values[i].e;
187 
188 		if (fabsf(coshf(x) - y) > eps)
189 			atf_tc_fail_nonfatal("coshf(%g) != %g\n", x, y);
190 	}
191 #endif
192 }
193 
194 ATF_TC(coshf_nan);
195 ATF_TC_HEAD(coshf_nan, tc)
196 {
197 	atf_tc_set_md_var(tc, "descr", "Test coshf(NaN) == NaN");
198 }
199 
200 ATF_TC_BODY(coshf_nan, tc)
201 {
202 #ifndef __vax__
203 	const float x = 0.0L / 0.0L;
204 
205 	ATF_CHECK(isnan(x) != 0);
206 	ATF_CHECK(isnan(coshf(x)) != 0);
207 #endif
208 }
209 
210 ATF_TC(coshf_inf_neg);
211 ATF_TC_HEAD(coshf_inf_neg, tc)
212 {
213 	atf_tc_set_md_var(tc, "descr", "Test coshf(-Inf) == +Inf");
214 }
215 
216 ATF_TC_BODY(coshf_inf_neg, tc)
217 {
218 #ifndef __vax__
219 	const float x = -1.0L / 0.0L;
220 	float y = coshf(x);
221 
222 	ATF_CHECK(isinf(y) != 0);
223 	ATF_CHECK(signbit(y) == 0);
224 #endif
225 }
226 
227 ATF_TC(coshf_inf_pos);
228 ATF_TC_HEAD(coshf_inf_pos, tc)
229 {
230 	atf_tc_set_md_var(tc, "descr", "Test coshf(+Inf) == +Inf");
231 }
232 
233 ATF_TC_BODY(coshf_inf_pos, tc)
234 {
235 #ifndef __vax__
236 	const float x = 1.0L / 0.0L;
237 	float y = coshf(x);
238 
239 	ATF_CHECK(isinf(y) != 0);
240 	ATF_CHECK(signbit(y) == 0);
241 #endif
242 }
243 
244 ATF_TC(coshf_zero_neg);
245 ATF_TC_HEAD(coshf_zero_neg, tc)
246 {
247 	atf_tc_set_md_var(tc, "descr", "Test coshf(-0.0) == 1.0");
248 }
249 
250 ATF_TC_BODY(coshf_zero_neg, tc)
251 {
252 #ifndef __vax__
253 	const float x = -0.0L;
254 
255 	if (coshf(x) != 1.0)
256 		atf_tc_fail_nonfatal("coshf(-0.0) != 1.0");
257 #endif
258 }
259 
260 ATF_TC(coshf_zero_pos);
261 ATF_TC_HEAD(coshf_zero_pos, tc)
262 {
263 	atf_tc_set_md_var(tc, "descr", "Test coshf(+0.0) == 1.0");
264 }
265 
266 ATF_TC_BODY(coshf_zero_pos, tc)
267 {
268 #ifndef __vax__
269 	const float x = 0.0L;
270 
271 	if (coshf(x) != 1.0)
272 		atf_tc_fail_nonfatal("coshf(+0.0) != 1.0");
273 #endif
274 }
275 
276 ATF_TP_ADD_TCS(tp)
277 {
278 
279 	ATF_TP_ADD_TC(tp, cosh_inrange);
280 	ATF_TP_ADD_TC(tp, cosh_nan);
281 	ATF_TP_ADD_TC(tp, cosh_inf_neg);
282 	ATF_TP_ADD_TC(tp, cosh_inf_pos);
283 	ATF_TP_ADD_TC(tp, cosh_zero_neg);
284 	ATF_TP_ADD_TC(tp, cosh_zero_pos);
285 
286 	ATF_TP_ADD_TC(tp, coshf_inrange);
287 	ATF_TP_ADD_TC(tp, coshf_nan);
288 	ATF_TP_ADD_TC(tp, coshf_inf_neg);
289 	ATF_TP_ADD_TC(tp, coshf_inf_pos);
290 	ATF_TP_ADD_TC(tp, coshf_zero_neg);
291 	ATF_TP_ADD_TC(tp, coshf_zero_pos);
292 
293 	return atf_no_error();
294 }
295