Home | History | Annotate | Line # | Download | only in libm
t_ldexp.c revision 1.1
      1 /* $NetBSD: t_ldexp.c,v 1.1 2011/09/12 15:27: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_ldexp.c,v 1.1 2011/09/12 15:27:40 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 NaN with ldexp(3)");
     48 }
     49 
     50 ATF_TC_BODY(ldexp_nan, tc)
     51 {
     52 	const double x = 0.0L / 0.0L;
     53 	double y;
     54 	size_t i;
     55 
     56 	for (i = 0; i < __arraycount(exps); i++) {
     57 		y = ldexp(x, exps[i]);
     58 		ATF_CHECK(isnan(y) != 0);
     59 	}
     60 }
     61 
     62 ATF_TC(ldexp_inf_neg);
     63 ATF_TC_HEAD(ldexp_inf_neg, tc)
     64 {
     65 	atf_tc_set_md_var(tc, "descr", "Test -Inf with ldexp(3)");
     66 }
     67 
     68 ATF_TC_BODY(ldexp_inf_neg, tc)
     69 {
     70 	const double x = -1.0L / 0.0L;
     71 	size_t i;
     72 
     73 	for (i = 0; i < __arraycount(exps); i++)
     74 		ATF_CHECK(ldexp(x, exps[i]) == x);
     75 }
     76 
     77 ATF_TC(ldexp_inf_pos);
     78 ATF_TC_HEAD(ldexp_inf_pos, tc)
     79 {
     80 	atf_tc_set_md_var(tc, "descr", "Test +Inf with ldexp(3)");
     81 }
     82 
     83 ATF_TC_BODY(ldexp_inf_pos, tc)
     84 {
     85 	const double x = 1.0L / 0.0L;
     86 	size_t i;
     87 
     88 	for (i = 0; i < __arraycount(exps); i++)
     89 		ATF_CHECK(ldexp(x, exps[i]) == x);
     90 }
     91 
     92 ATF_TC(ldexp_zero_neg);
     93 ATF_TC_HEAD(ldexp_zero_neg, tc)
     94 {
     95 	atf_tc_set_md_var(tc, "descr", "Test -0.0 with ldexp(3)");
     96 }
     97 
     98 ATF_TC_BODY(ldexp_zero_neg, tc)
     99 {
    100 	const double x = -0.0L;
    101 	size_t i;
    102 
    103 	for (i = 0; i < __arraycount(exps); i++)
    104 		ATF_CHECK(ldexp(x, exps[i]) == x);
    105 }
    106 
    107 ATF_TC(ldexp_zero_pos);
    108 ATF_TC_HEAD(ldexp_zero_pos, tc)
    109 {
    110 	atf_tc_set_md_var(tc, "descr", "Test +0.0 with ldexp(3)");
    111 }
    112 
    113 ATF_TC_BODY(ldexp_zero_pos, tc)
    114 {
    115 	const double x = 0.0L;
    116 	size_t i;
    117 
    118 	for (i = 0; i < __arraycount(exps); i++)
    119 		ATF_CHECK(ldexp(x, exps[i]) == x);
    120 }
    121 
    122 ATF_TC(ldexpf_nan);
    123 ATF_TC_HEAD(ldexpf_nan, tc)
    124 {
    125 	atf_tc_set_md_var(tc, "descr", "Test NaN with ldexpf(3)");
    126 }
    127 
    128 ATF_TC_BODY(ldexpf_nan, tc)
    129 {
    130 	const float x = 0.0L / 0.0L;
    131 	float y;
    132 	size_t i;
    133 
    134 	for (i = 0; i < __arraycount(exps); i++) {
    135 		y = ldexpf(x, exps[i]);
    136 		ATF_CHECK(isnan(y) != 0);
    137 	}
    138 }
    139 
    140 /*
    141  * ldexpf(3)
    142  */
    143 
    144 ATF_TC(ldexpf_inf_neg);
    145 ATF_TC_HEAD(ldexpf_inf_neg, tc)
    146 {
    147 	atf_tc_set_md_var(tc, "descr", "Test -Inf with ldexpf(3)");
    148 }
    149 
    150 ATF_TC_BODY(ldexpf_inf_neg, tc)
    151 {
    152 	const float x = -1.0L / 0.0L;
    153 	size_t i;
    154 
    155 	for (i = 0; i < __arraycount(exps); i++)
    156 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    157 }
    158 
    159 ATF_TC(ldexpf_inf_pos);
    160 ATF_TC_HEAD(ldexpf_inf_pos, tc)
    161 {
    162 	atf_tc_set_md_var(tc, "descr", "Test +Inf with ldexpf(3)");
    163 }
    164 
    165 ATF_TC_BODY(ldexpf_inf_pos, tc)
    166 {
    167 	const float x = 1.0L / 0.0L;
    168 	size_t i;
    169 
    170 	for (i = 0; i < __arraycount(exps); i++)
    171 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    172 }
    173 
    174 ATF_TC(ldexpf_zero_neg);
    175 ATF_TC_HEAD(ldexpf_zero_neg, tc)
    176 {
    177 	atf_tc_set_md_var(tc, "descr", "Test -0.0 with ldexpf(3)");
    178 }
    179 
    180 ATF_TC_BODY(ldexpf_zero_neg, tc)
    181 {
    182 	const float x = -0.0L;
    183 	size_t i;
    184 
    185 	for (i = 0; i < __arraycount(exps); i++)
    186 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    187 }
    188 
    189 ATF_TC(ldexpf_zero_pos);
    190 ATF_TC_HEAD(ldexpf_zero_pos, tc)
    191 {
    192 	atf_tc_set_md_var(tc, "descr", "Test +0.0 with ldexpf(3)");
    193 }
    194 
    195 ATF_TC_BODY(ldexpf_zero_pos, tc)
    196 {
    197 	const float x = 0.0L;
    198 	size_t i;
    199 
    200 	for (i = 0; i < __arraycount(exps); i++)
    201 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    202 }
    203 
    204 ATF_TP_ADD_TCS(tp)
    205 {
    206 
    207 	ATF_TP_ADD_TC(tp, ldexp_nan);
    208 	ATF_TP_ADD_TC(tp, ldexp_inf_neg);
    209 	ATF_TP_ADD_TC(tp, ldexp_inf_pos);
    210 	ATF_TP_ADD_TC(tp, ldexp_zero_neg);
    211 	ATF_TP_ADD_TC(tp, ldexp_zero_pos);
    212 
    213 	ATF_TP_ADD_TC(tp, ldexpf_nan);
    214 	ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
    215 	ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
    216 	ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
    217 	ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
    218 
    219 	return atf_no_error();
    220 }
    221