2011-06-14 15:58:22 +04:00
|
|
|
/* $NetBSD: t_convfp.c,v 1.7 2011/06/14 11:58:22 njoly Exp $ */
|
2010-12-31 07:08:32 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <atf-c.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This value is representable as an unsigned int, but not as an int.
|
|
|
|
* According to ISO C it must survive the convsion back from a double
|
|
|
|
* to an unsigned int (everything > -1 and < UINT_MAX+1 has to)
|
|
|
|
*/
|
|
|
|
#define UINT_TESTVALUE (INT_MAX+42U)
|
|
|
|
|
|
|
|
/* The same for unsigned long */
|
|
|
|
#define ULONG_TESTVALUE (LONG_MAX+42UL)
|
|
|
|
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC(conv_uint);
|
|
|
|
ATF_TC_HEAD(conv_uint, tc)
|
2010-12-31 07:08:32 +03:00
|
|
|
{
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
atf_tc_set_md_var(tc, "descr", "test conversions to unsigned int");
|
2010-12-31 07:08:32 +03:00
|
|
|
}
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC_BODY(conv_uint, tc)
|
2010-12-31 07:08:32 +03:00
|
|
|
{
|
|
|
|
unsigned int ui;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
/* unsigned int test */
|
|
|
|
d = UINT_TESTVALUE;
|
|
|
|
ui = (unsigned int)d;
|
|
|
|
|
|
|
|
if (ui != UINT_TESTVALUE)
|
2011-06-10 19:43:59 +04:00
|
|
|
atf_tc_fail("FAILED: unsigned int %u (0x%x) != %u (0x%x)",
|
2010-12-31 07:08:32 +03:00
|
|
|
ui, ui, UINT_TESTVALUE, UINT_TESTVALUE);
|
2011-06-14 15:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(conv_ulong);
|
|
|
|
|
|
|
|
ATF_TC_HEAD(conv_ulong, tc)
|
|
|
|
{
|
|
|
|
|
|
|
|
atf_tc_set_md_var(tc, "descr", "test conversions to unsigned long");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(conv_ulong, tc)
|
|
|
|
{
|
|
|
|
unsigned long ul;
|
|
|
|
long double dt;
|
|
|
|
double d;
|
2010-12-31 07:08:32 +03:00
|
|
|
|
|
|
|
/* unsigned long vs. {long} double test */
|
|
|
|
if (sizeof(d) > sizeof(ul)) {
|
|
|
|
d = ULONG_TESTVALUE;
|
|
|
|
ul = (unsigned long)d;
|
|
|
|
printf("testing double vs. long\n");
|
|
|
|
} else if (sizeof(dt) > sizeof(ul)) {
|
|
|
|
dt = ULONG_TESTVALUE;
|
|
|
|
ul = (unsigned long)dt;
|
|
|
|
printf("testing long double vs. long\n");
|
2011-06-10 21:10:43 +04:00
|
|
|
} else {
|
2011-06-11 22:03:17 +04:00
|
|
|
printf("sizeof(long) = %zu, sizeof(double) = %zu, "
|
|
|
|
"sizeof(long double) = %zu\n",
|
2010-12-31 07:08:32 +03:00
|
|
|
sizeof(ul), sizeof(d), sizeof(dt));
|
2011-06-14 15:58:22 +04:00
|
|
|
atf_tc_skip("no suitable {long} double type found");
|
2011-06-10 21:10:43 +04:00
|
|
|
}
|
2010-12-31 07:08:32 +03:00
|
|
|
|
2011-06-10 19:21:25 +04:00
|
|
|
if (ul != ULONG_TESTVALUE)
|
2011-06-10 19:43:59 +04:00
|
|
|
atf_tc_fail("unsigned long %lu (0x%lx) != %lu (0x%lx)",
|
2010-12-31 07:08:32 +03:00
|
|
|
ul, ul, ULONG_TESTVALUE, ULONG_TESTVALUE);
|
|
|
|
}
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC(cast_ulong);
|
2010-12-31 07:08:32 +03:00
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC_HEAD(cast_ulong, tc)
|
2010-12-31 07:08:32 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
atf_tc_set_md_var(tc, "descr", "test double to unsigned long cast");
|
|
|
|
}
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC_BODY(cast_ulong, tc)
|
2010-12-31 07:08:32 +03:00
|
|
|
{
|
|
|
|
double nv;
|
|
|
|
unsigned long uv;
|
|
|
|
|
|
|
|
nv = 5.6;
|
|
|
|
uv = (unsigned long)nv;
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_CHECK_EQ_MSG(uv, 5,
|
2011-06-10 19:43:59 +04:00
|
|
|
"%.3f casted to unsigned long is %lu", nv, uv);
|
2010-12-31 07:08:32 +03:00
|
|
|
}
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC(cast_ulong2);
|
2010-12-31 07:08:32 +03:00
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC_HEAD(cast_ulong2, tc)
|
2010-12-31 07:08:32 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
atf_tc_set_md_var(tc, "descr",
|
|
|
|
"test double/long double casts to unsigned long");
|
|
|
|
}
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TC_BODY(cast_ulong2, tc)
|
2010-12-31 07:08:32 +03:00
|
|
|
{
|
|
|
|
double dv = 1.9;
|
|
|
|
long double ldv = dv;
|
|
|
|
unsigned long l1 = dv;
|
|
|
|
unsigned long l2 = ldv;
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_CHECK_EQ_MSG(l1, 1,
|
2011-06-10 19:43:59 +04:00
|
|
|
"double 1.9 casted to unsigned long should be 1, but is %lu", l1);
|
2010-12-31 07:08:32 +03:00
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_CHECK_EQ_MSG(l2, 1,
|
2011-06-10 19:43:59 +04:00
|
|
|
"long double 1.9 casted to unsigned long should be 1, but is %lu",
|
2010-12-31 07:08:32 +03:00
|
|
|
l2);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TP_ADD_TCS(tp)
|
|
|
|
{
|
|
|
|
|
2011-06-14 15:58:22 +04:00
|
|
|
ATF_TP_ADD_TC(tp, conv_uint);
|
|
|
|
ATF_TP_ADD_TC(tp, conv_ulong);
|
|
|
|
ATF_TP_ADD_TC(tp, cast_ulong);
|
|
|
|
ATF_TP_ADD_TC(tp, cast_ulong2);
|
2010-12-31 07:08:32 +03:00
|
|
|
|
|
|
|
return atf_no_error();
|
|
|
|
}
|