xref: /freebsd/contrib/netbsd-tests/lib/libm/t_sinh.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /* $NetBSD: t_sinh.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_sinh.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.23287470339, 1e4, },
44 	{ -2,    -3.626860407847019, 1,   },
45 	{ -1,    -1.175201193643801, 1,   },
46 	{ -0.05, -0.050020835937655, 1,   },
47 	{ -0.001,-0.001000000166667, 1,   },
48 	{  0.001, 0.001000000166667, 1,   },
49 	{  0.05,  0.050020835937655, 1,   },
50 	{  1,     1.175201193643801, 1,   },
51 	{  2,     3.626860407847019, 1,   },
52 	{  10,    11013.23287470339, 1e4, },
53 };
54 
55 /*
56  * sinh(3)
57  */
58 ATF_TC(sinh_inrange);
59 ATF_TC_HEAD(sinh_inrange, tc)
60 {
61 	atf_tc_set_md_var(tc, "descr", "sinh(x) for some values");
62 }
63 
64 ATF_TC_BODY(sinh_inrange, tc)
65 {
66 	double eps;
67 	double x;
68 	double y;
69 	size_t i;
70 
71 	for (i = 0; i < __arraycount(values); i++) {
72 		x = values[i].x;
73 		y = values[i].y;
74 		eps = 1e-15 * values[i].e;
75 
76 		if (fabs(sinh(x) - y) > eps)
77 			atf_tc_fail_nonfatal("sinh(%g) != %g\n", x, y);
78 	}
79 }
80 
81 ATF_TC(sinh_nan);
82 ATF_TC_HEAD(sinh_nan, tc)
83 {
84 	atf_tc_set_md_var(tc, "descr", "Test sinh(NaN) == NaN");
85 }
86 
87 ATF_TC_BODY(sinh_nan, tc)
88 {
89 	const double x = 0.0L / 0.0L;
90 
91 	ATF_CHECK(isnan(x) != 0);
92 	ATF_CHECK(isnan(sinh(x)) != 0);
93 }
94 
95 ATF_TC(sinh_inf_neg);
96 ATF_TC_HEAD(sinh_inf_neg, tc)
97 {
98 	atf_tc_set_md_var(tc, "descr", "Test sinh(-Inf) == -Inf");
99 }
100 
101 ATF_TC_BODY(sinh_inf_neg, tc)
102 {
103 	const double x = -1.0L / 0.0L;
104 	double y = sinh(x);
105 
106 	ATF_CHECK(isinf(y) != 0);
107 	ATF_CHECK(signbit(y) != 0);
108 }
109 
110 ATF_TC(sinh_inf_pos);
111 ATF_TC_HEAD(sinh_inf_pos, tc)
112 {
113 	atf_tc_set_md_var(tc, "descr", "Test sinh(+Inf) == +Inf");
114 }
115 
116 ATF_TC_BODY(sinh_inf_pos, tc)
117 {
118 	const double x = 1.0L / 0.0L;
119 	double y = sinh(x);
120 
121 	ATF_CHECK(isinf(y) != 0);
122 	ATF_CHECK(signbit(y) == 0);
123 }
124 
125 ATF_TC(sinh_zero_neg);
126 ATF_TC_HEAD(sinh_zero_neg, tc)
127 {
128 	atf_tc_set_md_var(tc, "descr", "Test sinh(-0.0) == -0.0");
129 }
130 
131 ATF_TC_BODY(sinh_zero_neg, tc)
132 {
133 	const double x = -0.0L;
134 	double y = sinh(x);
135 
136 	if (fabs(y) > 0.0 || signbit(y) == 0)
137 		atf_tc_fail_nonfatal("sinh(-0.0) != -0.0");
138 }
139 
140 ATF_TC(sinh_zero_pos);
141 ATF_TC_HEAD(sinh_zero_pos, tc)
142 {
143 	atf_tc_set_md_var(tc, "descr", "Test sinh(+0.0) == +0.0");
144 }
145 
146 ATF_TC_BODY(sinh_zero_pos, tc)
147 {
148 	const double x = 0.0L;
149 	double y = sinh(x);
150 
151 	if (fabs(y) > 0.0 || signbit(y) != 0)
152 		atf_tc_fail_nonfatal("sinh(+0.0) != +0.0");
153 }
154 
155 /*
156  * sinhf(3)
157  */
158 ATF_TC(sinhf_inrange);
159 ATF_TC_HEAD(sinhf_inrange, tc)
160 {
161 	atf_tc_set_md_var(tc, "descr", "sinhf(x) for some values");
162 }
163 
164 ATF_TC_BODY(sinhf_inrange, tc)
165 {
166 	float eps;
167 	float x;
168 	float y;
169 	size_t i;
170 
171 	for (i = 0; i < __arraycount(values); i++) {
172 		x = values[i].x;
173 		y = values[i].y;
174 		eps = 1e-6 * values[i].e;
175 
176 		if (fabsf(sinhf(x) - y) > eps)
177 			atf_tc_fail_nonfatal("sinhf(%g) != %g\n", x, y);
178 	}
179 }
180 
181 ATF_TC(sinhf_nan);
182 ATF_TC_HEAD(sinhf_nan, tc)
183 {
184 	atf_tc_set_md_var(tc, "descr", "Test sinhf(NaN) == NaN");
185 }
186 
187 ATF_TC_BODY(sinhf_nan, tc)
188 {
189 	const float x = 0.0L / 0.0L;
190 
191 	ATF_CHECK(isnan(x) != 0);
192 	ATF_CHECK(isnan(sinhf(x)) != 0);
193 }
194 
195 ATF_TC(sinhf_inf_neg);
196 ATF_TC_HEAD(sinhf_inf_neg, tc)
197 {
198 	atf_tc_set_md_var(tc, "descr", "Test sinhf(-Inf) == -Inf");
199 }
200 
201 ATF_TC_BODY(sinhf_inf_neg, tc)
202 {
203 	const float x = -1.0L / 0.0L;
204 	float y = sinhf(x);
205 
206 	ATF_CHECK(isinf(y) != 0);
207 	ATF_CHECK(signbit(y) != 0);
208 }
209 
210 ATF_TC(sinhf_inf_pos);
211 ATF_TC_HEAD(sinhf_inf_pos, tc)
212 {
213 	atf_tc_set_md_var(tc, "descr", "Test sinhf(+Inf) == +Inf");
214 }
215 
216 ATF_TC_BODY(sinhf_inf_pos, tc)
217 {
218 	const float x = 1.0L / 0.0L;
219 	float y = sinhf(x);
220 
221 	ATF_CHECK(isinf(y) != 0);
222 	ATF_CHECK(signbit(y) == 0);
223 }
224 
225 ATF_TC(sinhf_zero_neg);
226 ATF_TC_HEAD(sinhf_zero_neg, tc)
227 {
228 	atf_tc_set_md_var(tc, "descr", "Test sinhf(-0.0) == -0.0");
229 }
230 
231 ATF_TC_BODY(sinhf_zero_neg, tc)
232 {
233 	const float x = -0.0L;
234 	float y = sinhf(x);
235 
236 	if (fabsf(y) > 0.0 || signbit(y) == 0)
237 		atf_tc_fail_nonfatal("sinhf(-0.0) != -0.0");
238 }
239 
240 ATF_TC(sinhf_zero_pos);
241 ATF_TC_HEAD(sinhf_zero_pos, tc)
242 {
243 	atf_tc_set_md_var(tc, "descr", "Test sinhf(+0.0) == +0.0");
244 }
245 
246 ATF_TC_BODY(sinhf_zero_pos, tc)
247 {
248 	const float x = 0.0L;
249 	float y = sinhf(x);
250 
251 	if (fabsf(y) > 0.0 || signbit(y) != 0)
252 		atf_tc_fail_nonfatal("sinhf(+0.0) != +0.0");
253 }
254 
255 ATF_TP_ADD_TCS(tp)
256 {
257 
258 	ATF_TP_ADD_TC(tp, sinh_inrange);
259 	ATF_TP_ADD_TC(tp, sinh_nan);
260 	ATF_TP_ADD_TC(tp, sinh_inf_neg);
261 	ATF_TP_ADD_TC(tp, sinh_inf_pos);
262 	ATF_TP_ADD_TC(tp, sinh_zero_neg);
263 	ATF_TP_ADD_TC(tp, sinh_zero_pos);
264 
265 	ATF_TP_ADD_TC(tp, sinhf_inrange);
266 	ATF_TP_ADD_TC(tp, sinhf_nan);
267 	ATF_TP_ADD_TC(tp, sinhf_inf_neg);
268 	ATF_TP_ADD_TC(tp, sinhf_inf_pos);
269 	ATF_TP_ADD_TC(tp, sinhf_zero_neg);
270 	ATF_TP_ADD_TC(tp, sinhf_zero_pos);
271 
272 	return atf_no_error();
273 }
274