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