Home | History | Annotate | Line # | Download | only in libm
t_ldexp.c revision 1.6
      1 /* $NetBSD: t_ldexp.c,v 1.6 2011/09/14 06:50:43 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_ldexp.c,v 1.6 2011/09/14 06:50:43 jruoho Exp $");
     33 
     34 #include <math.h>
     35 #include <limits.h>
     36 
     37 #include <atf-c.h>
     38 
     39 static const int exps[] = { 0, 1, -1, 100, -100 };
     40 
     41 /*
     42  * ldexp(3)
     43  */
     44 ATF_TC(ldexp_exp2);
     45 ATF_TC_HEAD(ldexp_exp2, tc)
     46 {
     47 	atf_tc_set_md_var(tc, "descr", "Test ldexp(x, n) == x * exp2(n)");
     48 }
     49 
     50 ATF_TC_BODY(ldexp_exp2, tc)
     51 {
     52 #ifndef __vax__
     53 	const double n[] = { 1, 2, 3, 25, 50, 100, 123, 321, 500 };
     54 	const double x = 12.1288221;
     55 	double y;
     56 	size_t i;
     57 
     58 	for (i = 0; i < __arraycount(n); i++) {
     59 		y = ldexp(x, n[i]);
     60 		ATF_CHECK(x * exp2(n[i]));
     61 	}
     62 #endif
     63 }
     64 
     65 ATF_TC(ldexp_nan);
     66 ATF_TC_HEAD(ldexp_nan, tc)
     67 {
     68 	atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
     69 }
     70 
     71 ATF_TC_BODY(ldexp_nan, tc)
     72 {
     73 #ifndef __vax__
     74 	const double x = 0.0L / 0.0L;
     75 	double y;
     76 	size_t i;
     77 
     78 	ATF_REQUIRE(isnan(x) != 0);
     79 
     80 	for (i = 0; i < __arraycount(exps); i++) {
     81 		y = ldexp(x, exps[i]);
     82 		ATF_CHECK(isnan(y) != 0);
     83 	}
     84 #endif
     85 }
     86 
     87 ATF_TC(ldexp_inf_neg);
     88 ATF_TC_HEAD(ldexp_inf_neg, tc)
     89 {
     90 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
     91 }
     92 
     93 ATF_TC_BODY(ldexp_inf_neg, tc)
     94 {
     95 #ifndef __vax__
     96 	const double x = -1.0L / 0.0L;
     97 	size_t i;
     98 
     99 	for (i = 0; i < __arraycount(exps); i++)
    100 		ATF_CHECK(ldexp(x, exps[i]) == x);
    101 #endif
    102 }
    103 
    104 ATF_TC(ldexp_inf_pos);
    105 ATF_TC_HEAD(ldexp_inf_pos, tc)
    106 {
    107 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
    108 }
    109 
    110 ATF_TC_BODY(ldexp_inf_pos, tc)
    111 {
    112 #ifndef __vax__
    113 	const double x = 1.0L / 0.0L;
    114 	size_t i;
    115 
    116 	for (i = 0; i < __arraycount(exps); i++)
    117 		ATF_CHECK(ldexp(x, exps[i]) == x);
    118 #endif
    119 }
    120 
    121 ATF_TC(ldexp_zero_neg);
    122 ATF_TC_HEAD(ldexp_zero_neg, tc)
    123 {
    124 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
    125 }
    126 
    127 ATF_TC_BODY(ldexp_zero_neg, tc)
    128 {
    129 #ifndef __vax__
    130 	const double x = -0.0L;
    131 	double y;
    132 	size_t i;
    133 
    134 	ATF_REQUIRE(signbit(x) != 0);
    135 
    136 	for (i = 0; i < __arraycount(exps); i++) {
    137 		y = ldexp(x, exps[i]);
    138 		ATF_CHECK(x == y);
    139 		ATF_CHECK(signbit(y) != 0);
    140 	}
    141 #endif
    142 }
    143 
    144 ATF_TC(ldexp_zero_pos);
    145 ATF_TC_HEAD(ldexp_zero_pos, tc)
    146 {
    147 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
    148 }
    149 
    150 ATF_TC_BODY(ldexp_zero_pos, tc)
    151 {
    152 #ifndef __vax__
    153 	const double x = 0.0L;
    154 	double y;
    155 	size_t i;
    156 
    157 	ATF_REQUIRE(signbit(x) == 0);
    158 
    159 	for (i = 0; i < __arraycount(exps); i++) {
    160 		y = ldexp(x, exps[i]);
    161 		ATF_CHECK(x == y);
    162 		ATF_CHECK(signbit(y) == 0);
    163 	}
    164 #endif
    165 }
    166 
    167 /*
    168  * ldexpf(3)
    169  */
    170 
    171 ATF_TC(ldexpf_exp2f);
    172 ATF_TC_HEAD(ldexpf_exp2f, tc)
    173 {
    174 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
    175 }
    176 
    177 ATF_TC_BODY(ldexpf_exp2f, tc)
    178 {
    179 #ifndef __vax__
    180 	const float n[] = { 1, 2, 3, 25, 50, 100, 123, 321, 500 };
    181 	const float x = 12.1288221;
    182 	float y;
    183 	size_t i;
    184 
    185 	for (i = 0; i < __arraycount(n); i++) {
    186 		y = ldexpf(x, n[i]);
    187 		ATF_CHECK(x * exp2f(n[i]));
    188 	}
    189 #endif
    190 }
    191 
    192 ATF_TC(ldexpf_nan);
    193 ATF_TC_HEAD(ldexpf_nan, tc)
    194 {
    195 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
    196 }
    197 
    198 ATF_TC_BODY(ldexpf_nan, tc)
    199 {
    200 #ifndef __vax__
    201 	const float x = 0.0L / 0.0L;
    202 	float y;
    203 	size_t i;
    204 
    205 	ATF_REQUIRE(isnan(x) != 0);
    206 
    207 	for (i = 0; i < __arraycount(exps); i++) {
    208 		y = ldexpf(x, exps[i]);
    209 		ATF_CHECK(isnan(y) != 0);
    210 	}
    211 #endif
    212 }
    213 
    214 ATF_TC(ldexpf_inf_neg);
    215 ATF_TC_HEAD(ldexpf_inf_neg, tc)
    216 {
    217 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
    218 }
    219 
    220 ATF_TC_BODY(ldexpf_inf_neg, tc)
    221 {
    222 #ifndef __vax__
    223 	const float x = -1.0L / 0.0L;
    224 	size_t i;
    225 
    226 	for (i = 0; i < __arraycount(exps); i++)
    227 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    228 #endif
    229 }
    230 
    231 ATF_TC(ldexpf_inf_pos);
    232 ATF_TC_HEAD(ldexpf_inf_pos, tc)
    233 {
    234 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
    235 }
    236 
    237 ATF_TC_BODY(ldexpf_inf_pos, tc)
    238 {
    239 #ifndef __vax__
    240 	const float x = 1.0L / 0.0L;
    241 	size_t i;
    242 
    243 	for (i = 0; i < __arraycount(exps); i++)
    244 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    245 #endif
    246 }
    247 
    248 ATF_TC(ldexpf_zero_neg);
    249 ATF_TC_HEAD(ldexpf_zero_neg, tc)
    250 {
    251 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
    252 }
    253 
    254 ATF_TC_BODY(ldexpf_zero_neg, tc)
    255 {
    256 #ifndef __vax__
    257 	const float x = -0.0L;
    258 	float y;
    259 	size_t i;
    260 
    261 	ATF_REQUIRE(signbit(x) != 0);
    262 
    263 	for (i = 0; i < __arraycount(exps); i++) {
    264 		y = ldexpf(x, exps[i]);
    265 		ATF_CHECK(x == y);
    266 		ATF_CHECK(signbit(y) != 0);
    267 	}
    268 #endif
    269 }
    270 
    271 ATF_TC(ldexpf_zero_pos);
    272 ATF_TC_HEAD(ldexpf_zero_pos, tc)
    273 {
    274 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
    275 }
    276 
    277 ATF_TC_BODY(ldexpf_zero_pos, tc)
    278 {
    279 #ifndef __vax__
    280 	const float x = 0.0L;
    281 	float y;
    282 	size_t i;
    283 
    284 	ATF_REQUIRE(signbit(x) == 0);
    285 
    286 	for (i = 0; i < __arraycount(exps); i++) {
    287 		y = ldexpf(x, exps[i]);
    288 		ATF_CHECK(x == y);
    289 		ATF_CHECK(signbit(y) == 0);
    290 	}
    291 #endif
    292 }
    293 
    294 ATF_TP_ADD_TCS(tp)
    295 {
    296 
    297 	ATF_TP_ADD_TC(tp, ldexp_exp2);
    298 	ATF_TP_ADD_TC(tp, ldexp_nan);
    299 	ATF_TP_ADD_TC(tp, ldexp_inf_neg);
    300 	ATF_TP_ADD_TC(tp, ldexp_inf_pos);
    301 	ATF_TP_ADD_TC(tp, ldexp_zero_neg);
    302 	ATF_TP_ADD_TC(tp, ldexp_zero_pos);
    303 
    304 	ATF_TP_ADD_TC(tp, ldexpf_exp2f);
    305 	ATF_TP_ADD_TC(tp, ldexpf_nan);
    306 	ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
    307 	ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
    308 	ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
    309 	ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
    310 
    311 	return atf_no_error();
    312 }
    313