1 /* $NetBSD: t_convfp.c,v 1.7 2011/06/14 11:58:22 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
46 ATF_TC(conv_uint);
ATF_TC_HEAD(conv_uint,tc)47 ATF_TC_HEAD(conv_uint, tc)
48 {
49
50 atf_tc_set_md_var(tc, "descr", "test conversions to unsigned int");
51 }
52
ATF_TC_BODY(conv_uint,tc)53 ATF_TC_BODY(conv_uint, tc)
54 {
55 unsigned int ui;
56 double d;
57
58 /* unsigned int test */
59 d = UINT_TESTVALUE;
60 ui = (unsigned int)d;
61
62 if (ui != UINT_TESTVALUE)
63 atf_tc_fail("FAILED: unsigned int %u (0x%x) != %u (0x%x)",
64 ui, ui, UINT_TESTVALUE, UINT_TESTVALUE);
65 }
66
67 ATF_TC(conv_ulong);
68
ATF_TC_HEAD(conv_ulong,tc)69 ATF_TC_HEAD(conv_ulong, tc)
70 {
71
72 atf_tc_set_md_var(tc, "descr", "test conversions to unsigned long");
73 }
74
ATF_TC_BODY(conv_ulong,tc)75 ATF_TC_BODY(conv_ulong, tc)
76 {
77 unsigned long ul;
78 long double dt;
79 double d;
80
81 /* unsigned long vs. {long} double test */
82 if (sizeof(d) > sizeof(ul)) {
83 d = ULONG_TESTVALUE;
84 ul = (unsigned long)d;
85 printf("testing double vs. long\n");
86 } else if (sizeof(dt) > sizeof(ul)) {
87 dt = ULONG_TESTVALUE;
88 ul = (unsigned long)dt;
89 printf("testing long double vs. long\n");
90 } else {
91 printf("sizeof(long) = %zu, sizeof(double) = %zu, "
92 "sizeof(long double) = %zu\n",
93 sizeof(ul), sizeof(d), sizeof(dt));
94 atf_tc_skip("no suitable {long} double type found");
95 }
96
97 if (ul != ULONG_TESTVALUE)
98 atf_tc_fail("unsigned long %lu (0x%lx) != %lu (0x%lx)",
99 ul, ul, ULONG_TESTVALUE, ULONG_TESTVALUE);
100 }
101
102 ATF_TC(cast_ulong);
103
ATF_TC_HEAD(cast_ulong,tc)104 ATF_TC_HEAD(cast_ulong, tc)
105 {
106
107 atf_tc_set_md_var(tc, "descr", "test double to unsigned long cast");
108 }
109
ATF_TC_BODY(cast_ulong,tc)110 ATF_TC_BODY(cast_ulong, tc)
111 {
112 double nv;
113 unsigned long uv;
114
115 nv = 5.6;
116 uv = (unsigned long)nv;
117
118 ATF_CHECK_EQ_MSG(uv, 5,
119 "%.3f casted to unsigned long is %lu", nv, uv);
120 }
121
122 ATF_TC(cast_ulong2);
123
ATF_TC_HEAD(cast_ulong2,tc)124 ATF_TC_HEAD(cast_ulong2, tc)
125 {
126
127 atf_tc_set_md_var(tc, "descr",
128 "test double/long double casts to unsigned long");
129 }
130
ATF_TC_BODY(cast_ulong2,tc)131 ATF_TC_BODY(cast_ulong2, tc)
132 {
133 double dv = 1.9;
134 long double ldv = dv;
135 unsigned long l1 = dv;
136 unsigned long l2 = ldv;
137
138 ATF_CHECK_EQ_MSG(l1, 1,
139 "double 1.9 casted to unsigned long should be 1, but is %lu", l1);
140
141 ATF_CHECK_EQ_MSG(l2, 1,
142 "long double 1.9 casted to unsigned long should be 1, but is %lu",
143 l2);
144 }
145
ATF_TP_ADD_TCS(tp)146 ATF_TP_ADD_TCS(tp)
147 {
148
149 ATF_TP_ADD_TC(tp, conv_uint);
150 ATF_TP_ADD_TC(tp, conv_ulong);
151 ATF_TP_ADD_TC(tp, cast_ulong);
152 ATF_TP_ADD_TC(tp, cast_ulong2);
153
154 return atf_no_error();
155 }
156