xref: /netbsd/tests/lib/libc/t_convfp.c (revision 6550d01e)
1 /*	$NetBSD: t_convfp.c,v 1.1 2010/12/31 04:08:33 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
15  *
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.
27  */
28 
29 #include <atf-c.h>
30 
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 /*
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)
41 
42 /* The same for unsigned long */
43 #define ULONG_TESTVALUE	(LONG_MAX+42UL)
44 
45 static void test1();
46 static void test2();
47 static void test3();
48 
49 ATF_TC(test1);
50 
51 ATF_TC_HEAD(test1, tc)
52 {
53 
54 	atf_tc_set_md_var(tc, "descr", "test conversions to unsigned int/long");
55 }
56 
57 ATF_TC_BODY(test1, tc)
58 {
59 	unsigned int ui;
60 	unsigned long ul;
61 	long double dt;
62 	double d;
63 
64 	/* unsigned int test */
65 	d = UINT_TESTVALUE;
66 	ui = (unsigned int)d;
67 
68 	if (ui != UINT_TESTVALUE)
69 		atf_tc_fail("FAILED: unsigned int %u (0x%x) != %u (0x%x)\n",
70 		    ui, ui, UINT_TESTVALUE, UINT_TESTVALUE);
71 
72 	/* unsigned long vs. {long} double test */
73 	if (sizeof(d) > sizeof(ul)) {
74 		d = ULONG_TESTVALUE;
75 		ul = (unsigned long)d;
76 		printf("testing double vs. long\n");
77 	} else if (sizeof(dt) > sizeof(ul)) {
78 		dt = ULONG_TESTVALUE;
79 		ul = (unsigned long)dt;
80 		printf("testing long double vs. long\n");
81 	} else
82 		atf_tc_skip("no suitable {long} double type found, skipping "
83 		    "\"unsigned long\" test\n"
84 		    "sizeof(long) = %d, sizeof(double) = %d, "
85 		    "sizeof(long double) = %d\n",
86 		    sizeof(ul), sizeof(d), sizeof(dt));
87 
88 	if (ul != ULONG_TESTVALUE) {
89 		atf_tc_fail("unsigned long %lu (0x%lx) != %lu (0x%lx)\n",
90 		    ul, ul, ULONG_TESTVALUE, ULONG_TESTVALUE);
91 		exit(1);
92 	}
93 }
94 
95 ATF_TC(test2);
96 
97 ATF_TC_HEAD(test2, tc)
98 {
99 
100 	atf_tc_set_md_var(tc, "descr", "test double to unsigned long cast");
101 }
102 
103 ATF_TC_BODY(test2, tc)
104 {
105 	double nv;
106 	unsigned long uv;
107 
108 	nv = 5.6;
109 	uv = (unsigned long)nv;
110 
111 	ATF_REQUIRE_EQ_MSG(uv, 5,
112 	    "%.3f casted to unsigned long is %lu\n", nv, uv);
113 }
114 
115 ATF_TC(test3);
116 
117 ATF_TC_HEAD(test3, tc)
118 {
119 
120 	atf_tc_set_md_var(tc, "descr",
121 	    "test double/long double casts to unsigned long");
122 }
123 
124 ATF_TC_BODY(test3, tc)
125 {
126 	double dv = 1.9;
127 	long double ldv = dv;
128 	unsigned long l1 = dv;
129 	unsigned long l2 = ldv;
130 
131 	ATF_REQUIRE_EQ_MSG(l1, 1,
132 	    "double 1.9 casted to unsigned long should be 1, but is %lu\n", l1);
133 
134 	ATF_REQUIRE_EQ_MSG(l2, 1,
135 	    "long double 1.9 casted to unsigned long should be 1, but is %lu\n",
136 	    l2);
137 }
138 
139 ATF_TP_ADD_TCS(tp)
140 {
141 
142 	ATF_TP_ADD_TC(tp, test1);
143 	ATF_TP_ADD_TC(tp, test2);
144 	ATF_TP_ADD_TC(tp, test3);
145 
146 	return atf_no_error();
147 }
148