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