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