xref: /freebsd/contrib/netbsd-tests/lib/libm/t_cosh.c (revision 5b9c547c)
1 /* $NetBSD: t_cosh.c,v 1.6 2014/03/03 10:39:08 martin 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.6 2014/03/03 10:39:08 martin 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 	double eps;
68 	double x;
69 	double y;
70 	size_t i;
71 
72 	for (i = 0; i < __arraycount(values); i++) {
73 		x = values[i].x;
74 		y = values[i].y;
75 		eps = 1e-15 * values[i].e;
76 
77 		if (fabs(cosh(x) - y) > eps)
78 			atf_tc_fail_nonfatal("cosh(%g) != %g\n", x, y);
79 	}
80 }
81 
82 ATF_TC(cosh_nan);
83 ATF_TC_HEAD(cosh_nan, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "Test cosh(NaN) == NaN");
86 }
87 
88 ATF_TC_BODY(cosh_nan, tc)
89 {
90 	const double x = 0.0L / 0.0L;
91 
92 	ATF_CHECK(isnan(x) != 0);
93 	ATF_CHECK(isnan(cosh(x)) != 0);
94 }
95 
96 ATF_TC(cosh_inf_neg);
97 ATF_TC_HEAD(cosh_inf_neg, tc)
98 {
99 	atf_tc_set_md_var(tc, "descr", "Test cosh(-Inf) == +Inf");
100 }
101 
102 ATF_TC_BODY(cosh_inf_neg, tc)
103 {
104 	const double x = -1.0L / 0.0L;
105 	double y = cosh(x);
106 
107 	ATF_CHECK(isinf(y) != 0);
108 	ATF_CHECK(signbit(y) == 0);
109 }
110 
111 ATF_TC(cosh_inf_pos);
112 ATF_TC_HEAD(cosh_inf_pos, tc)
113 {
114 	atf_tc_set_md_var(tc, "descr", "Test cosh(+Inf) == +Inf");
115 }
116 
117 ATF_TC_BODY(cosh_inf_pos, tc)
118 {
119 	const double x = 1.0L / 0.0L;
120 	double y = cosh(x);
121 
122 	ATF_CHECK(isinf(y) != 0);
123 	ATF_CHECK(signbit(y) == 0);
124 }
125 
126 ATF_TC(cosh_zero_neg);
127 ATF_TC_HEAD(cosh_zero_neg, tc)
128 {
129 	atf_tc_set_md_var(tc, "descr", "Test cosh(-0.0) == 1.0");
130 }
131 
132 ATF_TC_BODY(cosh_zero_neg, tc)
133 {
134 	const double x = -0.0L;
135 
136 	if (cosh(x) != 1.0)
137 		atf_tc_fail_nonfatal("cosh(-0.0) != 1.0");
138 }
139 
140 ATF_TC(cosh_zero_pos);
141 ATF_TC_HEAD(cosh_zero_pos, tc)
142 {
143 	atf_tc_set_md_var(tc, "descr", "Test cosh(+0.0) == 1.0");
144 }
145 
146 ATF_TC_BODY(cosh_zero_pos, tc)
147 {
148 	const double x = 0.0L;
149 
150 	if (cosh(x) != 1.0)
151 		atf_tc_fail_nonfatal("cosh(+0.0) != 1.0");
152 }
153 
154 /*
155  * coshf(3)
156  */
157 ATF_TC(coshf_inrange);
158 ATF_TC_HEAD(coshf_inrange, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr", "coshf(x) for some values");
161 }
162 
163 ATF_TC_BODY(coshf_inrange, tc)
164 {
165 	float eps;
166 	float x;
167 	float y;
168 	size_t i;
169 
170 	for (i = 0; i < __arraycount(values); i++) {
171 		x = values[i].x;
172 		y = values[i].y;
173 		eps = 1e-6 * values[i].e;
174 
175 		if (fabsf(coshf(x) - y) > eps)
176 			atf_tc_fail_nonfatal("coshf(%g) != %g\n", x, y);
177 	}
178 }
179 
180 ATF_TC(coshf_nan);
181 ATF_TC_HEAD(coshf_nan, tc)
182 {
183 	atf_tc_set_md_var(tc, "descr", "Test coshf(NaN) == NaN");
184 }
185 
186 ATF_TC_BODY(coshf_nan, tc)
187 {
188 	const float x = 0.0L / 0.0L;
189 
190 	ATF_CHECK(isnan(x) != 0);
191 	ATF_CHECK(isnan(coshf(x)) != 0);
192 }
193 
194 ATF_TC(coshf_inf_neg);
195 ATF_TC_HEAD(coshf_inf_neg, tc)
196 {
197 	atf_tc_set_md_var(tc, "descr", "Test coshf(-Inf) == +Inf");
198 }
199 
200 ATF_TC_BODY(coshf_inf_neg, tc)
201 {
202 	const float x = -1.0L / 0.0L;
203 	float y = coshf(x);
204 
205 	ATF_CHECK(isinf(y) != 0);
206 	ATF_CHECK(signbit(y) == 0);
207 }
208 
209 ATF_TC(coshf_inf_pos);
210 ATF_TC_HEAD(coshf_inf_pos, tc)
211 {
212 	atf_tc_set_md_var(tc, "descr", "Test coshf(+Inf) == +Inf");
213 }
214 
215 ATF_TC_BODY(coshf_inf_pos, tc)
216 {
217 	const float x = 1.0L / 0.0L;
218 	float y = coshf(x);
219 
220 	ATF_CHECK(isinf(y) != 0);
221 	ATF_CHECK(signbit(y) == 0);
222 }
223 
224 ATF_TC(coshf_zero_neg);
225 ATF_TC_HEAD(coshf_zero_neg, tc)
226 {
227 	atf_tc_set_md_var(tc, "descr", "Test coshf(-0.0) == 1.0");
228 }
229 
230 ATF_TC_BODY(coshf_zero_neg, tc)
231 {
232 	const float x = -0.0L;
233 
234 	if (coshf(x) != 1.0)
235 		atf_tc_fail_nonfatal("coshf(-0.0) != 1.0");
236 }
237 
238 ATF_TC(coshf_zero_pos);
239 ATF_TC_HEAD(coshf_zero_pos, tc)
240 {
241 	atf_tc_set_md_var(tc, "descr", "Test coshf(+0.0) == 1.0");
242 }
243 
244 ATF_TC_BODY(coshf_zero_pos, tc)
245 {
246 	const float x = 0.0L;
247 
248 	if (coshf(x) != 1.0)
249 		atf_tc_fail_nonfatal("coshf(+0.0) != 1.0");
250 }
251 
252 ATF_TP_ADD_TCS(tp)
253 {
254 
255 	ATF_TP_ADD_TC(tp, cosh_inrange);
256 	ATF_TP_ADD_TC(tp, cosh_nan);
257 	ATF_TP_ADD_TC(tp, cosh_inf_neg);
258 	ATF_TP_ADD_TC(tp, cosh_inf_pos);
259 	ATF_TP_ADD_TC(tp, cosh_zero_neg);
260 	ATF_TP_ADD_TC(tp, cosh_zero_pos);
261 
262 	ATF_TP_ADD_TC(tp, coshf_inrange);
263 	ATF_TP_ADD_TC(tp, coshf_nan);
264 	ATF_TP_ADD_TC(tp, coshf_inf_neg);
265 	ATF_TP_ADD_TC(tp, coshf_inf_pos);
266 	ATF_TP_ADD_TC(tp, coshf_zero_neg);
267 	ATF_TP_ADD_TC(tp, coshf_zero_pos);
268 
269 	return atf_no_error();
270 }
271