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