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