etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / t_convfp.c
blobde68690a2432a6dfefac9359070a28bc3e5d2e35
1 /* $NetBSD: t_convfp.c,v 1.7 2011/06/14 11:58:22 njoly Exp $ */
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
29 #include <atf-c.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
36 * This value is representable as an unsigned int, but not as an int.
37 * According to ISO C it must survive the convsion back from a double
38 * to an unsigned int (everything > -1 and < UINT_MAX+1 has to)
39 */
40 #define UINT_TESTVALUE (INT_MAX+42U)
42 /* The same for unsigned long */
43 #define ULONG_TESTVALUE (LONG_MAX+42UL)
46 ATF_TC(conv_uint);
47 ATF_TC_HEAD(conv_uint, tc)
50 atf_tc_set_md_var(tc, "descr", "test conversions to unsigned int");
53 ATF_TC_BODY(conv_uint, tc)
55 unsigned int ui;
56 double d;
58 /* unsigned int test */
59 d = UINT_TESTVALUE;
60 ui = (unsigned int)d;
62 if (ui != UINT_TESTVALUE)
63 atf_tc_fail("FAILED: unsigned int %u (0x%x) != %u (0x%x)",
64 ui, ui, UINT_TESTVALUE, UINT_TESTVALUE);
67 ATF_TC(conv_ulong);
69 ATF_TC_HEAD(conv_ulong, tc)
72 atf_tc_set_md_var(tc, "descr", "test conversions to unsigned long");
75 ATF_TC_BODY(conv_ulong, tc)
77 unsigned long ul;
78 long double dt;
79 double d;
81 /* unsigned long vs. {long} double test */
82 if (sizeof(d) > sizeof(ul)) {
83 d = ULONG_TESTVALUE;
84 ul = (unsigned long)d;
85 printf("testing double vs. long\n");
86 } else if (sizeof(dt) > sizeof(ul)) {
87 dt = ULONG_TESTVALUE;
88 ul = (unsigned long)dt;
89 printf("testing long double vs. long\n");
90 } else {
91 printf("sizeof(long) = %zu, sizeof(double) = %zu, "
92 "sizeof(long double) = %zu\n",
93 sizeof(ul), sizeof(d), sizeof(dt));
94 atf_tc_skip("no suitable {long} double type found");
97 if (ul != ULONG_TESTVALUE)
98 atf_tc_fail("unsigned long %lu (0x%lx) != %lu (0x%lx)",
99 ul, ul, ULONG_TESTVALUE, ULONG_TESTVALUE);
102 ATF_TC(cast_ulong);
104 ATF_TC_HEAD(cast_ulong, tc)
107 atf_tc_set_md_var(tc, "descr", "test double to unsigned long cast");
110 ATF_TC_BODY(cast_ulong, tc)
112 double nv;
113 unsigned long uv;
115 nv = 5.6;
116 uv = (unsigned long)nv;
118 ATF_CHECK_EQ_MSG(uv, 5,
119 "%.3f casted to unsigned long is %lu", nv, uv);
122 ATF_TC(cast_ulong2);
124 ATF_TC_HEAD(cast_ulong2, tc)
127 atf_tc_set_md_var(tc, "descr",
128 "test double/long double casts to unsigned long");
131 ATF_TC_BODY(cast_ulong2, tc)
133 double dv = 1.9;
134 long double ldv = dv;
135 unsigned long l1 = dv;
136 unsigned long l2 = ldv;
138 ATF_CHECK_EQ_MSG(l1, 1,
139 "double 1.9 casted to unsigned long should be 1, but is %lu", l1);
141 ATF_CHECK_EQ_MSG(l2, 1,
142 "long double 1.9 casted to unsigned long should be 1, but is %lu",
143 l2);
146 ATF_TP_ADD_TCS(tp)
149 ATF_TP_ADD_TC(tp, conv_uint);
150 ATF_TP_ADD_TC(tp, conv_ulong);
151 ATF_TP_ADD_TC(tp, cast_ulong);
152 ATF_TP_ADD_TC(tp, cast_ulong2);
154 return atf_no_error();