Home | History | Annotate | Line # | Download | only in libm
t_cbrt.c revision 1.1
      1 /* $NetBSD: t_cbrt.c,v 1.1 2011/10/16 08:25:40 jruoho 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.1 2011/10/16 08:25:40 jruoho 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 ATF_TP_ADD_TCS(tp)
    265 {
    266 
    267 	ATF_TP_ADD_TC(tp, cbrt_nan);
    268 	ATF_TP_ADD_TC(tp, cbrt_pow);
    269 	ATF_TP_ADD_TC(tp, cbrt_inf_neg);
    270 	ATF_TP_ADD_TC(tp, cbrt_inf_pos);
    271 	ATF_TP_ADD_TC(tp, cbrt_zero_neg);
    272 	ATF_TP_ADD_TC(tp, cbrt_zero_pos);
    273 
    274 	ATF_TP_ADD_TC(tp, cbrtf_nan);
    275 	ATF_TP_ADD_TC(tp, cbrtf_powf);
    276 	ATF_TP_ADD_TC(tp, cbrtf_inf_neg);
    277 	ATF_TP_ADD_TC(tp, cbrtf_inf_pos);
    278 	ATF_TP_ADD_TC(tp, cbrtf_zero_neg);
    279 	ATF_TP_ADD_TC(tp, cbrtf_zero_pos);
    280 
    281 	return atf_no_error();
    282 }
    283