Home | History | Annotate | Line # | Download | only in libm
t_ldexp.c revision 1.4
      1 /* $NetBSD: t_ldexp.c,v 1.4 2011/09/12 17:46:39 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.4 2011/09/12 17:46:39 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_nan);
     45 ATF_TC_HEAD(ldexp_nan, tc)
     46 {
     47 	atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
     48 }
     49 
     50 ATF_TC_BODY(ldexp_nan, tc)
     51 {
     52 #ifndef __vax__
     53 	const double x = 0.0L / 0.0L;
     54 	double y;
     55 	size_t i;
     56 
     57 	ATF_REQUIRE(isnan(x) != 0);
     58 
     59 	for (i = 0; i < __arraycount(exps); i++) {
     60 		y = ldexp(x, exps[i]);
     61 		ATF_CHECK(isnan(y) != 0);
     62 	}
     63 #endif
     64 }
     65 
     66 ATF_TC(ldexp_inf_neg);
     67 ATF_TC_HEAD(ldexp_inf_neg, tc)
     68 {
     69 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
     70 }
     71 
     72 ATF_TC_BODY(ldexp_inf_neg, tc)
     73 {
     74 #ifndef __vax__
     75 	const double x = -1.0L / 0.0L;
     76 	size_t i;
     77 
     78 	for (i = 0; i < __arraycount(exps); i++)
     79 		ATF_CHECK(ldexp(x, exps[i]) == x);
     80 #endif
     81 }
     82 
     83 ATF_TC(ldexp_inf_pos);
     84 ATF_TC_HEAD(ldexp_inf_pos, tc)
     85 {
     86 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
     87 }
     88 
     89 ATF_TC_BODY(ldexp_inf_pos, tc)
     90 {
     91 #ifndef __vax__
     92 	const double x = 1.0L / 0.0L;
     93 	size_t i;
     94 
     95 	for (i = 0; i < __arraycount(exps); i++)
     96 		ATF_CHECK(ldexp(x, exps[i]) == x);
     97 #endif
     98 }
     99 
    100 ATF_TC(ldexp_zero_neg);
    101 ATF_TC_HEAD(ldexp_zero_neg, tc)
    102 {
    103 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
    104 }
    105 
    106 ATF_TC_BODY(ldexp_zero_neg, tc)
    107 {
    108 #ifndef __vax__
    109 	const double x = -0.0L;
    110 	double y;
    111 	size_t i;
    112 
    113 	ATF_REQUIRE(signbit(x) != 0);
    114 
    115 	for (i = 0; i < __arraycount(exps); i++) {
    116 		y = ldexp(x, exps[i]);
    117 		ATF_CHECK(x == y);
    118 		ATF_CHECK(signbit(y) != 0);
    119 	}
    120 #endif
    121 }
    122 
    123 ATF_TC(ldexp_zero_pos);
    124 ATF_TC_HEAD(ldexp_zero_pos, tc)
    125 {
    126 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
    127 }
    128 
    129 ATF_TC_BODY(ldexp_zero_pos, tc)
    130 {
    131 #ifndef __vax__
    132 	const double x = 0.0L;
    133 	double y;
    134 	size_t i;
    135 
    136 	ATF_REQUIRE(signbit(x) == 0);
    137 
    138 	for (i = 0; i < __arraycount(exps); i++) {
    139 		y = ldexp(x, exps[i]);
    140 		ATF_CHECK(x == y);
    141 		ATF_CHECK(signbit(y) == 0);
    142 	}
    143 #endif
    144 }
    145 
    146 ATF_TC(ldexpf_nan);
    147 ATF_TC_HEAD(ldexpf_nan, tc)
    148 {
    149 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
    150 }
    151 
    152 ATF_TC_BODY(ldexpf_nan, tc)
    153 {
    154 #ifndef __vax__
    155 	const float x = 0.0L / 0.0L;
    156 	float y;
    157 	size_t i;
    158 
    159 	ATF_REQUIRE(isnan(x) != 0);
    160 
    161 	for (i = 0; i < __arraycount(exps); i++) {
    162 		y = ldexpf(x, exps[i]);
    163 		ATF_CHECK(isnan(y) != 0);
    164 	}
    165 #endif
    166 }
    167 
    168 /*
    169  * ldexpf(3)
    170  */
    171 
    172 ATF_TC(ldexpf_inf_neg);
    173 ATF_TC_HEAD(ldexpf_inf_neg, tc)
    174 {
    175 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
    176 }
    177 
    178 ATF_TC_BODY(ldexpf_inf_neg, tc)
    179 {
    180 #ifndef __vax__
    181 	const float x = -1.0L / 0.0L;
    182 	size_t i;
    183 
    184 	for (i = 0; i < __arraycount(exps); i++)
    185 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    186 #endif
    187 }
    188 
    189 ATF_TC(ldexpf_inf_pos);
    190 ATF_TC_HEAD(ldexpf_inf_pos, tc)
    191 {
    192 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
    193 }
    194 
    195 ATF_TC_BODY(ldexpf_inf_pos, tc)
    196 {
    197 #ifndef __vax__
    198 	const float x = 1.0L / 0.0L;
    199 	size_t i;
    200 
    201 	for (i = 0; i < __arraycount(exps); i++)
    202 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    203 #endif
    204 }
    205 
    206 ATF_TC(ldexpf_zero_neg);
    207 ATF_TC_HEAD(ldexpf_zero_neg, tc)
    208 {
    209 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
    210 }
    211 
    212 ATF_TC_BODY(ldexpf_zero_neg, tc)
    213 {
    214 #ifndef __vax__
    215 	const float x = -0.0L;
    216 	float y;
    217 	size_t i;
    218 
    219 	ATF_REQUIRE(signbit(x) != 0);
    220 
    221 	for (i = 0; i < __arraycount(exps); i++) {
    222 		y = ldexpf(x, exps[i]);
    223 		ATF_CHECK(x == y);
    224 		ATF_CHECK(signbit(y) != 0);
    225 	}
    226 #endif
    227 }
    228 
    229 ATF_TC(ldexpf_zero_pos);
    230 ATF_TC_HEAD(ldexpf_zero_pos, tc)
    231 {
    232 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
    233 }
    234 
    235 ATF_TC_BODY(ldexpf_zero_pos, tc)
    236 {
    237 #ifndef __vax__
    238 	const float x = 0.0L;
    239 	float y;
    240 	size_t i;
    241 
    242 	ATF_REQUIRE(signbit(x) == 0);
    243 
    244 	for (i = 0; i < __arraycount(exps); i++) {
    245 		y = ldexpf(x, exps[i]);
    246 		ATF_CHECK(x == y);
    247 		ATF_CHECK(signbit(y) == 0);
    248 	}
    249 #endif
    250 }
    251 
    252 ATF_TP_ADD_TCS(tp)
    253 {
    254 
    255 	ATF_TP_ADD_TC(tp, ldexp_nan);
    256 	ATF_TP_ADD_TC(tp, ldexp_inf_neg);
    257 	ATF_TP_ADD_TC(tp, ldexp_inf_pos);
    258 	ATF_TP_ADD_TC(tp, ldexp_zero_neg);
    259 	ATF_TP_ADD_TC(tp, ldexp_zero_pos);
    260 
    261 	ATF_TP_ADD_TC(tp, ldexpf_nan);
    262 	ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
    263 	ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
    264 	ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
    265 	ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
    266 
    267 	return atf_no_error();
    268 }
    269