xref: /minix/tests/lib/libm/t_cbrt.c (revision 84d9c625)
1 /* $NetBSD: t_cbrt.c,v 1.2 2013/11/19 19:24:33 joerg 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_cbrt.c,v 1.2 2013/11/19 19:24:33 joerg Exp $");
33 
34 #include <atf-c.h>
35 #include <math.h>
36 #include <stdio.h>
37 
38 /*
39  * cbrt(3)
40  */
41 ATF_TC(cbrt_nan);
42 ATF_TC_HEAD(cbrt_nan, tc)
43 {
44 	atf_tc_set_md_var(tc, "descr", "Test cbrt(NaN) == NaN");
45 }
46 
47 ATF_TC_BODY(cbrt_nan, tc)
48 {
49 #ifndef __vax__
50 	const double x = 0.0L / 0.0L;
51 
52 	ATF_CHECK(isnan(x) != 0);
53 	ATF_CHECK(isnan(cbrt(x)) != 0);
54 #endif
55 }
56 
57 ATF_TC(cbrt_pow);
58 ATF_TC_HEAD(cbrt_pow, tc)
59 {
60 	atf_tc_set_md_var(tc, "descr", "Test cbrt(3) vs. pow(3)");
61 }
62 
63 ATF_TC_BODY(cbrt_pow, tc)
64 {
65 #ifndef __vax__
66 	const double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.0 };
67 	const double eps = 1.0e-14;
68 	double y, z;
69 	size_t i;
70 
71 	for (i = 0; i < __arraycount(x); i++) {
72 
73 		y = cbrt(x[i]);
74 		z = pow(x[i], 1.0 / 3.0);
75 
76 		if (fabs(y - z) > eps)
77 			atf_tc_fail_nonfatal("cbrt(%0.03f) != "
78 			    "pow(%0.03f, 1/3)\n", x[i], x[i]);
79 	}
80 #endif
81 }
82 
83 ATF_TC(cbrt_inf_neg);
84 ATF_TC_HEAD(cbrt_inf_neg, tc)
85 {
86 	atf_tc_set_md_var(tc, "descr", "Test cbrt(-Inf) == -Inf");
87 }
88 
89 ATF_TC_BODY(cbrt_inf_neg, tc)
90 {
91 #ifndef __vax__
92 	const double x = -1.0L / 0.0L;
93 	double y = cbrt(x);
94 
95 	ATF_CHECK(isinf(y) != 0);
96 	ATF_CHECK(signbit(y) != 0);
97 #endif
98 }
99 
100 ATF_TC(cbrt_inf_pos);
101 ATF_TC_HEAD(cbrt_inf_pos, tc)
102 {
103 	atf_tc_set_md_var(tc, "descr", "Test cbrt(+Inf) == +Inf");
104 }
105 
106 ATF_TC_BODY(cbrt_inf_pos, tc)
107 {
108 #ifndef __vax__
109 	const double x = 1.0L / 0.0L;
110 	double y = cbrt(x);
111 
112 	ATF_CHECK(isinf(y) != 0);
113 	ATF_CHECK(signbit(y) == 0);
114 #endif
115 }
116 
117 ATF_TC(cbrt_zero_neg);
118 ATF_TC_HEAD(cbrt_zero_neg, tc)
119 {
120 	atf_tc_set_md_var(tc, "descr", "Test cbrt(-0.0) == -0.0");
121 }
122 
123 ATF_TC_BODY(cbrt_zero_neg, tc)
124 {
125 #ifndef __vax__
126 	const double x = -0.0L;
127 	double y = cbrt(x);
128 
129 	if (fabs(y) > 0.0 || signbit(y) == 0)
130 		atf_tc_fail_nonfatal("cbrt(-0.0) != -0.0");
131 #endif
132 }
133 
134 ATF_TC(cbrt_zero_pos);
135 ATF_TC_HEAD(cbrt_zero_pos, tc)
136 {
137 	atf_tc_set_md_var(tc, "descr", "Test cbrt(+0.0) == +0.0");
138 }
139 
140 ATF_TC_BODY(cbrt_zero_pos, tc)
141 {
142 #ifndef __vax__
143 	const double x = 0.0L;
144 	double y = cbrt(x);
145 
146 	if (fabs(y) > 0.0 || signbit(y) != 0)
147 		atf_tc_fail_nonfatal("cbrt(+0.0) != +0.0");
148 #endif
149 }
150 
151 /*
152  * cbrtf(3)
153  */
154 ATF_TC(cbrtf_nan);
155 ATF_TC_HEAD(cbrtf_nan, tc)
156 {
157 	atf_tc_set_md_var(tc, "descr", "Test cbrtf(NaN) == NaN");
158 }
159 
160 ATF_TC_BODY(cbrtf_nan, tc)
161 {
162 #ifndef __vax__
163 	const float x = 0.0L / 0.0L;
164 
165 	ATF_CHECK(isnan(x) != 0);
166 	ATF_CHECK(isnan(cbrtf(x)) != 0);
167 #endif
168 }
169 
170 ATF_TC(cbrtf_powf);
171 ATF_TC_HEAD(cbrtf_powf, tc)
172 {
173 	atf_tc_set_md_var(tc, "descr", "Test cbrtf(3) vs. powf(3)");
174 }
175 
176 ATF_TC_BODY(cbrtf_powf, tc)
177 {
178 #ifndef __vax__
179 	const float x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.0 };
180 	const float eps = 1.0e-5;
181 	float y, z;
182 	size_t i;
183 
184 	for (i = 0; i < __arraycount(x); i++) {
185 
186 		y = cbrtf(x[i]);
187 		z = powf(x[i], 1.0 / 3.0);
188 
189 		if (fabsf(y - z) > eps)
190 			atf_tc_fail_nonfatal("cbrtf(%0.03f) != "
191 			    "powf(%0.03f, 1/3)\n", x[i], x[i]);
192 	}
193 #endif
194 }
195 
196 ATF_TC(cbrtf_inf_neg);
197 ATF_TC_HEAD(cbrtf_inf_neg, tc)
198 {
199 	atf_tc_set_md_var(tc, "descr", "Test cbrtf(-Inf) == -Inf");
200 }
201 
202 ATF_TC_BODY(cbrtf_inf_neg, tc)
203 {
204 #ifndef __vax__
205 	const float x = -1.0L / 0.0L;
206 	float y = cbrtf(x);
207 
208 	ATF_CHECK(isinf(y) != 0);
209 	ATF_CHECK(signbit(y) != 0);
210 #endif
211 }
212 
213 ATF_TC(cbrtf_inf_pos);
214 ATF_TC_HEAD(cbrtf_inf_pos, tc)
215 {
216 	atf_tc_set_md_var(tc, "descr", "Test cbrtf(+Inf) == +Inf");
217 }
218 
219 ATF_TC_BODY(cbrtf_inf_pos, tc)
220 {
221 #ifndef __vax__
222 	const float x = 1.0L / 0.0L;
223 	float y = cbrtf(x);
224 
225 	ATF_CHECK(isinf(y) != 0);
226 	ATF_CHECK(signbit(y) == 0);
227 #endif
228 }
229 
230 ATF_TC(cbrtf_zero_neg);
231 ATF_TC_HEAD(cbrtf_zero_neg, tc)
232 {
233 	atf_tc_set_md_var(tc, "descr", "Test cbrtf(-0.0) == -0.0");
234 }
235 
236 ATF_TC_BODY(cbrtf_zero_neg, tc)
237 {
238 #ifndef __vax__
239 	const float x = -0.0L;
240 	float y = cbrtf(x);
241 
242 	if (fabsf(y) > 0.0 || signbit(y) == 0)
243 		atf_tc_fail_nonfatal("cbrtf(-0.0) != -0.0");
244 #endif
245 }
246 
247 ATF_TC(cbrtf_zero_pos);
248 ATF_TC_HEAD(cbrtf_zero_pos, tc)
249 {
250 	atf_tc_set_md_var(tc, "descr", "Test cbrtf(+0.0) == +0.0");
251 }
252 
253 ATF_TC_BODY(cbrtf_zero_pos, tc)
254 {
255 #ifndef __vax__
256 	const float x = 0.0L;
257 	float y = cbrtf(x);
258 
259 	if (fabsf(y) > 0.0 || signbit(y) != 0)
260 		atf_tc_fail_nonfatal("cbrtf(+0.0) != +0.0");
261 #endif
262 }
263 
264 /*
265  * cbrtl(3)
266  */
267 ATF_TC(cbrtl_nan);
268 ATF_TC_HEAD(cbrtl_nan, tc)
269 {
270 	atf_tc_set_md_var(tc, "descr", "Test cbrtl(NaN) == NaN");
271 }
272 
273 ATF_TC_BODY(cbrtl_nan, tc)
274 {
275 #ifndef __vax__
276 	const long double x = 0.0L / 0.0L;
277 
278 	ATF_CHECK(isnan(x) != 0);
279 	ATF_CHECK(isnan(cbrtl(x)) != 0);
280 #endif
281 }
282 
283 ATF_TC(cbrtl_powl);
284 ATF_TC_HEAD(cbrtl_powl, tc)
285 {
286 	atf_tc_set_md_var(tc, "descr", "Test cbrtl(3) vs. powl(3)");
287 }
288 
289 ATF_TC_BODY(cbrtl_powl, tc)
290 {
291 #ifndef __vax__
292 	const long double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.0 };
293 	const long double eps = 1.0e-15;
294 	long double y, z;
295 	size_t i;
296 
297 	for (i = 0; i < __arraycount(x); i++) {
298 
299 		y = cbrtl(x[i]);
300 		z = powl(x[i], 1.0 / 3.0);
301 
302 		if (fabsl(y - z) > eps * fabsl(1 + x[i]))
303 			atf_tc_fail_nonfatal("cbrtl(%0.03Lf) != "
304 			    "powl(%0.03Lf, 1/3)\n", x[i], x[i]);
305 	}
306 #endif
307 }
308 
309 ATF_TC(cbrtl_inf_neg);
310 ATF_TC_HEAD(cbrtl_inf_neg, tc)
311 {
312 	atf_tc_set_md_var(tc, "descr", "Test cbrtl(-Inf) == -Inf");
313 }
314 
315 ATF_TC_BODY(cbrtl_inf_neg, tc)
316 {
317 #ifndef __vax__
318 	const long double x = -1.0L / 0.0L;
319 	long double y = cbrtl(x);
320 
321 	ATF_CHECK(isinf(y) != 0);
322 	ATF_CHECK(signbit(y) != 0);
323 #endif
324 }
325 
326 ATF_TC(cbrtl_inf_pos);
327 ATF_TC_HEAD(cbrtl_inf_pos, tc)
328 {
329 	atf_tc_set_md_var(tc, "descr", "Test cbrtl(+Inf) == +Inf");
330 }
331 
332 ATF_TC_BODY(cbrtl_inf_pos, tc)
333 {
334 #ifndef __vax__
335 	const long double x = 1.0L / 0.0L;
336 	long double y = cbrtl(x);
337 
338 	ATF_CHECK(isinf(y) != 0);
339 	ATF_CHECK(signbit(y) == 0);
340 #endif
341 }
342 
343 ATF_TC(cbrtl_zero_neg);
344 ATF_TC_HEAD(cbrtl_zero_neg, tc)
345 {
346 	atf_tc_set_md_var(tc, "descr", "Test cbrtl(-0.0) == -0.0");
347 }
348 
349 ATF_TC_BODY(cbrtl_zero_neg, tc)
350 {
351 #ifndef __vax__
352 	const long double x = -0.0L;
353 	long double y = cbrtl(x);
354 
355 	if (fabsl(y) > 0.0 || signbit(y) == 0)
356 		atf_tc_fail_nonfatal("cbrtl(-0.0) != -0.0");
357 #endif
358 }
359 
360 ATF_TC(cbrtl_zero_pos);
361 ATF_TC_HEAD(cbrtl_zero_pos, tc)
362 {
363 	atf_tc_set_md_var(tc, "descr", "Test cbrtl(+0.0) == +0.0");
364 }
365 
366 ATF_TC_BODY(cbrtl_zero_pos, tc)
367 {
368 #ifndef __vax__
369 	const long double x = 0.0L;
370 	long double y = cbrtl(x);
371 
372 	if (fabsl(y) > 0.0 || signbit(y) != 0)
373 		atf_tc_fail_nonfatal("cbrtl(+0.0) != +0.0");
374 #endif
375 }
376 
377 ATF_TP_ADD_TCS(tp)
378 {
379 
380 	ATF_TP_ADD_TC(tp, cbrt_nan);
381 	ATF_TP_ADD_TC(tp, cbrt_pow);
382 	ATF_TP_ADD_TC(tp, cbrt_inf_neg);
383 	ATF_TP_ADD_TC(tp, cbrt_inf_pos);
384 	ATF_TP_ADD_TC(tp, cbrt_zero_neg);
385 	ATF_TP_ADD_TC(tp, cbrt_zero_pos);
386 
387 	ATF_TP_ADD_TC(tp, cbrtf_nan);
388 	ATF_TP_ADD_TC(tp, cbrtf_powf);
389 	ATF_TP_ADD_TC(tp, cbrtf_inf_neg);
390 	ATF_TP_ADD_TC(tp, cbrtf_inf_pos);
391 	ATF_TP_ADD_TC(tp, cbrtf_zero_neg);
392 	ATF_TP_ADD_TC(tp, cbrtf_zero_pos);
393 
394 	ATF_TP_ADD_TC(tp, cbrtl_nan);
395 	ATF_TP_ADD_TC(tp, cbrtl_powl);
396 	ATF_TP_ADD_TC(tp, cbrtl_inf_neg);
397 	ATF_TP_ADD_TC(tp, cbrtl_inf_pos);
398 	ATF_TP_ADD_TC(tp, cbrtl_zero_neg);
399 	ATF_TP_ADD_TC(tp, cbrtl_zero_pos);
400 
401 	return atf_no_error();
402 }
403