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