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