xref: /netbsd-src/tests/lib/libm/t_cosh.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /* $NetBSD: t_cosh.c,v 1.4 2011/10/18 14:16:42 jruoho 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.4 2011/10/18 14:16:42 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <math.h>
36 #include <stdio.h>
37 
38 /*
39  * cosh(3)
40  */
41 ATF_TC(cosh_def);
42 ATF_TC_HEAD(cosh_def, tc)
43 {
44 	atf_tc_set_md_var(tc, "descr", "Test the definition of cosh(3)");
45 }
46 
47 ATF_TC_BODY(cosh_def, tc)
48 {
49 #ifndef __vax__
50 	const double x[] = { 0.005, 0.05, 0.0, 1.0, 10.0, 20.0 };
51 	const double eps = 1.0e-8;
52 	double y, z;
53 	size_t i;
54 
55 	for (i = 0; i < __arraycount(x); i++) {
56 
57 		y = cosh(x[i]);
58 		z = (exp(x[i]) + exp(-x[i])) / 2;
59 
60 		(void)fprintf(stderr,
61 		    "cosh(%0.03f) = %f\n(exp(%0.03f) + "
62 		    "exp(-%0.03f)) / 2 = %f\n", x[i], y, x[i], x[i], z);
63 
64 		if (fabs(y - z) > eps)
65 			atf_tc_fail_nonfatal("cosh(%0.03f) != %0.03f\n",
66 			    x[i], z);
67 	}
68 #endif
69 }
70 
71 ATF_TC(cosh_nan);
72 ATF_TC_HEAD(cosh_nan, tc)
73 {
74 	atf_tc_set_md_var(tc, "descr", "Test cosh(NaN) == NaN");
75 }
76 
77 ATF_TC_BODY(cosh_nan, tc)
78 {
79 #ifndef __vax__
80 	const double x = 0.0L / 0.0L;
81 
82 	ATF_CHECK(isnan(x) != 0);
83 	ATF_CHECK(isnan(cosh(x)) != 0);
84 #endif
85 }
86 
87 ATF_TC(cosh_inf_neg);
88 ATF_TC_HEAD(cosh_inf_neg, tc)
89 {
90 	atf_tc_set_md_var(tc, "descr", "Test cosh(-Inf) == +Inf");
91 }
92 
93 ATF_TC_BODY(cosh_inf_neg, tc)
94 {
95 #ifndef __vax__
96 	const double x = -1.0L / 0.0L;
97 	double y = cosh(x);
98 
99 	ATF_CHECK(isinf(y) != 0);
100 	ATF_CHECK(signbit(y) == 0);
101 #endif
102 }
103 
104 ATF_TC(cosh_inf_pos);
105 ATF_TC_HEAD(cosh_inf_pos, tc)
106 {
107 	atf_tc_set_md_var(tc, "descr", "Test cosh(+Inf) == +Inf");
108 }
109 
110 ATF_TC_BODY(cosh_inf_pos, tc)
111 {
112 #ifndef __vax__
113 	const double x = 1.0L / 0.0L;
114 	double y = cosh(x);
115 
116 	ATF_CHECK(isinf(y) != 0);
117 	ATF_CHECK(signbit(y) == 0);
118 #endif
119 }
120 
121 ATF_TC(cosh_zero_neg);
122 ATF_TC_HEAD(cosh_zero_neg, tc)
123 {
124 	atf_tc_set_md_var(tc, "descr", "Test cosh(-0.0) == 1.0");
125 }
126 
127 ATF_TC_BODY(cosh_zero_neg, tc)
128 {
129 #ifndef __vax__
130 	const double x = -0.0L;
131 
132 	if (cosh(x) != 1.0)
133 		atf_tc_fail_nonfatal("cosh(-0.0) != 1.0");
134 #endif
135 }
136 
137 ATF_TC(cosh_zero_pos);
138 ATF_TC_HEAD(cosh_zero_pos, tc)
139 {
140 	atf_tc_set_md_var(tc, "descr", "Test cosh(+0.0) == 1.0");
141 }
142 
143 ATF_TC_BODY(cosh_zero_pos, tc)
144 {
145 #ifndef __vax__
146 	const double x = 0.0L;
147 
148 	if (cosh(x) != 1.0)
149 		atf_tc_fail_nonfatal("cosh(+0.0) != 1.0");
150 #endif
151 }
152 
153 /*
154  * coshf(3)
155  */
156 ATF_TC(coshf_def);
157 ATF_TC_HEAD(coshf_def, tc)
158 {
159 	atf_tc_set_md_var(tc, "descr", "Test the definition of coshf(3)");
160 }
161 
162 ATF_TC_BODY(coshf_def, tc)
163 {
164 #ifndef __vax__
165 	const double x[] = { 0.005, 0.05, 0.0, 1.0, 10.0, 20.0 };
166 	const float eps = 1.0e-3;
167 	float y, z;
168 	size_t i;
169 
170 	for (i = 0; i < __arraycount(x); i++) {
171 
172 		y = coshf(x[i]);
173 		z = (expf(x[i]) + expf(-x[i])) / 2;
174 
175 		(void)fprintf(stderr,
176 		    "coshf(%0.03f) = %f\n(expf(%0.03f) + "
177 		    "expf(-%0.03f)) / 2 = %f\n", x[i], y, x[i], x[i], z);
178 
179 		if (fabsf(y - z) > eps)
180 			atf_tc_fail_nonfatal("coshf(%0.03f) != %0.03f\n",
181 			    x[i], z);
182 	}
183 #endif
184 }
185 
186 ATF_TC(coshf_nan);
187 ATF_TC_HEAD(coshf_nan, tc)
188 {
189 	atf_tc_set_md_var(tc, "descr", "Test coshf(NaN) == NaN");
190 }
191 
192 ATF_TC_BODY(coshf_nan, tc)
193 {
194 #ifndef __vax__
195 	const float x = 0.0L / 0.0L;
196 
197 	ATF_CHECK(isnan(x) != 0);
198 	ATF_CHECK(isnan(coshf(x)) != 0);
199 #endif
200 }
201 
202 ATF_TC(coshf_inf_neg);
203 ATF_TC_HEAD(coshf_inf_neg, tc)
204 {
205 	atf_tc_set_md_var(tc, "descr", "Test coshf(-Inf) == +Inf");
206 }
207 
208 ATF_TC_BODY(coshf_inf_neg, tc)
209 {
210 #ifndef __vax__
211 	const float x = -1.0L / 0.0L;
212 	float y = coshf(x);
213 
214 	ATF_CHECK(isinf(y) != 0);
215 	ATF_CHECK(signbit(y) == 0);
216 #endif
217 }
218 
219 ATF_TC(coshf_inf_pos);
220 ATF_TC_HEAD(coshf_inf_pos, tc)
221 {
222 	atf_tc_set_md_var(tc, "descr", "Test coshf(+Inf) == +Inf");
223 }
224 
225 ATF_TC_BODY(coshf_inf_pos, tc)
226 {
227 #ifndef __vax__
228 	const float x = 1.0L / 0.0L;
229 	float y = coshf(x);
230 
231 	ATF_CHECK(isinf(y) != 0);
232 	ATF_CHECK(signbit(y) == 0);
233 #endif
234 }
235 
236 ATF_TC(coshf_zero_neg);
237 ATF_TC_HEAD(coshf_zero_neg, tc)
238 {
239 	atf_tc_set_md_var(tc, "descr", "Test coshf(-0.0) == 1.0");
240 }
241 
242 ATF_TC_BODY(coshf_zero_neg, tc)
243 {
244 #ifndef __vax__
245 	const float x = -0.0L;
246 
247 	if (coshf(x) != 1.0)
248 		atf_tc_fail_nonfatal("coshf(-0.0) != 1.0");
249 #endif
250 }
251 
252 ATF_TC(coshf_zero_pos);
253 ATF_TC_HEAD(coshf_zero_pos, tc)
254 {
255 	atf_tc_set_md_var(tc, "descr", "Test coshf(+0.0) == 1.0");
256 }
257 
258 ATF_TC_BODY(coshf_zero_pos, tc)
259 {
260 #ifndef __vax__
261 	const float x = 0.0L;
262 
263 	if (coshf(x) != 1.0)
264 		atf_tc_fail_nonfatal("coshf(+0.0) != 1.0");
265 #endif
266 }
267 
268 ATF_TP_ADD_TCS(tp)
269 {
270 
271 	ATF_TP_ADD_TC(tp, cosh_def);
272 	ATF_TP_ADD_TC(tp, cosh_nan);
273 	ATF_TP_ADD_TC(tp, cosh_inf_neg);
274 	ATF_TP_ADD_TC(tp, cosh_inf_pos);
275 	ATF_TP_ADD_TC(tp, cosh_zero_neg);
276 	ATF_TP_ADD_TC(tp, cosh_zero_pos);
277 
278 	ATF_TP_ADD_TC(tp, coshf_def);
279 	ATF_TP_ADD_TC(tp, coshf_nan);
280 	ATF_TP_ADD_TC(tp, coshf_inf_neg);
281 	ATF_TP_ADD_TC(tp, coshf_inf_pos);
282 	ATF_TP_ADD_TC(tp, coshf_zero_neg);
283 	ATF_TP_ADD_TC(tp, coshf_zero_pos);
284 
285 	return atf_no_error();
286 }
287