Home | History | Annotate | Line # | Download | only in libc
t_convfp.c revision 1.2
      1 /*	$NetBSD: t_convfp.c,v 1.2 2011/06/10 15:21:25 njoly Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <atf-c.h>
     30 
     31 #include <limits.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 
     35 /*
     36  * This value is representable as an unsigned int, but not as an int.
     37  * According to ISO C it must survive the convsion back from a double
     38  * to an unsigned int (everything > -1 and < UINT_MAX+1 has to)
     39  */
     40 #define	UINT_TESTVALUE	(INT_MAX+42U)
     41 
     42 /* The same for unsigned long */
     43 #define ULONG_TESTVALUE	(LONG_MAX+42UL)
     44 
     45 static void test1();
     46 static void test2();
     47 static void test3();
     48 
     49 ATF_TC(test1);
     50 
     51 ATF_TC_HEAD(test1, tc)
     52 {
     53 
     54 	atf_tc_set_md_var(tc, "descr", "test conversions to unsigned int/long");
     55 }
     56 
     57 ATF_TC_BODY(test1, tc)
     58 {
     59 	unsigned int ui;
     60 	unsigned long ul;
     61 	long double dt;
     62 	double d;
     63 
     64 	/* unsigned int test */
     65 	d = UINT_TESTVALUE;
     66 	ui = (unsigned int)d;
     67 
     68 	if (ui != UINT_TESTVALUE)
     69 		atf_tc_fail("FAILED: unsigned int %u (0x%x) != %u (0x%x)\n",
     70 		    ui, ui, UINT_TESTVALUE, UINT_TESTVALUE);
     71 
     72 	/* unsigned long vs. {long} double test */
     73 	if (sizeof(d) > sizeof(ul)) {
     74 		d = ULONG_TESTVALUE;
     75 		ul = (unsigned long)d;
     76 		printf("testing double vs. long\n");
     77 	} else if (sizeof(dt) > sizeof(ul)) {
     78 		dt = ULONG_TESTVALUE;
     79 		ul = (unsigned long)dt;
     80 		printf("testing long double vs. long\n");
     81 	} else
     82 		atf_tc_skip("no suitable {long} double type found, skipping "
     83 		    "\"unsigned long\" test\n"
     84 		    "sizeof(long) = %d, sizeof(double) = %d, "
     85 		    "sizeof(long double) = %d\n",
     86 		    sizeof(ul), sizeof(d), sizeof(dt));
     87 
     88 	if (ul != ULONG_TESTVALUE)
     89 		atf_tc_fail("unsigned long %lu (0x%lx) != %lu (0x%lx)\n",
     90 		    ul, ul, ULONG_TESTVALUE, ULONG_TESTVALUE);
     91 }
     92 
     93 ATF_TC(test2);
     94 
     95 ATF_TC_HEAD(test2, tc)
     96 {
     97 
     98 	atf_tc_set_md_var(tc, "descr", "test double to unsigned long cast");
     99 }
    100 
    101 ATF_TC_BODY(test2, tc)
    102 {
    103 	double nv;
    104 	unsigned long uv;
    105 
    106 	nv = 5.6;
    107 	uv = (unsigned long)nv;
    108 
    109 	ATF_REQUIRE_EQ_MSG(uv, 5,
    110 	    "%.3f casted to unsigned long is %lu\n", nv, uv);
    111 }
    112 
    113 ATF_TC(test3);
    114 
    115 ATF_TC_HEAD(test3, tc)
    116 {
    117 
    118 	atf_tc_set_md_var(tc, "descr",
    119 	    "test double/long double casts to unsigned long");
    120 }
    121 
    122 ATF_TC_BODY(test3, tc)
    123 {
    124 	double dv = 1.9;
    125 	long double ldv = dv;
    126 	unsigned long l1 = dv;
    127 	unsigned long l2 = ldv;
    128 
    129 	ATF_REQUIRE_EQ_MSG(l1, 1,
    130 	    "double 1.9 casted to unsigned long should be 1, but is %lu\n", l1);
    131 
    132 	ATF_REQUIRE_EQ_MSG(l2, 1,
    133 	    "long double 1.9 casted to unsigned long should be 1, but is %lu\n",
    134 	    l2);
    135 }
    136 
    137 ATF_TP_ADD_TCS(tp)
    138 {
    139 
    140 	ATF_TP_ADD_TC(tp, test1);
    141 	ATF_TP_ADD_TC(tp, test2);
    142 	ATF_TP_ADD_TC(tp, test3);
    143 
    144 	return atf_no_error();
    145 }
    146