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