1 /* $NetBSD: convfp.c,v 1.6 2007/11/07 00:03:09 martin Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 * This value is representable as an unsigned int, but not as an int.
35 * According to ISO C it must survive the convsion back from a double
36 * to an unsigned int (everything > -1 and < UINT_MAX+1 has to)
38 #define UINT_TESTVALUE (INT_MAX+42U)
40 /* The same for unsigned long */
41 #define ULONG_TESTVALUE (LONG_MAX+42UL)
65 /* unsigned int test */
69 if (ui
!= UINT_TESTVALUE
) {
70 printf("FAILED: unsigned int %u (0x%x) != %u (0x%x)\n",
71 ui
, ui
, UINT_TESTVALUE
, UINT_TESTVALUE
);
75 /* unsigned long vs. {long} double test */
76 if (sizeof(d
) > sizeof(ul
)) {
78 ul
= (unsigned long)d
;
79 printf("testing double vs. long\n");
80 } else if (sizeof(dt
) > sizeof(ul
)) {
82 ul
= (unsigned long)dt
;
83 printf("testing long double vs. long\n");
85 printf("no suitable {long} double type found, skipping "
86 "\"unsigned long\" test\n");
87 printf("sizeof(long) = %d, sizeof(double) = %d, "
88 "sizeof(long double) = %d\n",
89 sizeof(ul
), sizeof(d
), sizeof(dt
));
93 if (ul
!= ULONG_TESTVALUE
) {
94 printf("FAILED: unsigned long %lu (0x%lx) != %lu (0x%lx)\n",
95 ul
, ul
, ULONG_TESTVALUE
, ULONG_TESTVALUE
);
106 printf("testing double to unsigned long cast\n");
108 uv
= (unsigned long)nv
;
113 printf("FAILED: %.3f casted to unsigned long is %lu\n", nv
, uv
);
121 long double ldv
= dv
;
122 unsigned long l1
= dv
;
123 unsigned long l2
= ldv
;
125 printf("Testing double/long double casts to unsigned long\n");
127 printf("FAILED: double 1.9 casted to unsigned long should"
128 " be 1, but is %lu\n", l1
);
133 printf("FAILED: long double 1.9 casted to unsigned long should"
134 " be 1, but is %lu\n", l2
);