Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / convfp / convfp.c
blobf8b7ff7f459b168bd5aed9950278b59dbc375d56
1 /* $NetBSD: convfp.c,v 1.6 2007/11/07 00:03:09 martin 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 <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
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)
37 */
38 #define UINT_TESTVALUE (INT_MAX+42U)
40 /* The same for unsigned long */
41 #define ULONG_TESTVALUE (LONG_MAX+42UL)
43 static void test1();
44 static void test2();
45 static void test3();
47 int
48 main()
50 test1();
51 test2();
52 test3();
53 printf("PASSED\n");
54 return 0;
57 static void
58 test1()
60 unsigned int ui;
61 unsigned long ul;
62 long double dt;
63 double d;
65 /* unsigned int test */
66 d = UINT_TESTVALUE;
67 ui = (unsigned int)d;
69 if (ui != UINT_TESTVALUE) {
70 printf("FAILED: unsigned int %u (0x%x) != %u (0x%x)\n",
71 ui, ui, UINT_TESTVALUE, UINT_TESTVALUE);
72 exit(1);
75 /* unsigned long vs. {long} double test */
76 if (sizeof(d) > sizeof(ul)) {
77 d = ULONG_TESTVALUE;
78 ul = (unsigned long)d;
79 printf("testing double vs. long\n");
80 } else if (sizeof(dt) > sizeof(ul)) {
81 dt = ULONG_TESTVALUE;
82 ul = (unsigned long)dt;
83 printf("testing long double vs. long\n");
84 } else {
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));
90 return;
93 if (ul != ULONG_TESTVALUE) {
94 printf("FAILED: unsigned long %lu (0x%lx) != %lu (0x%lx)\n",
95 ul, ul, ULONG_TESTVALUE, ULONG_TESTVALUE);
96 exit(1);
100 static void
101 test2()
103 double nv;
104 unsigned long uv;
106 printf("testing double to unsigned long cast\n");
107 nv = 5.6;
108 uv = (unsigned long)nv;
110 if (uv == 5)
111 return;
113 printf("FAILED: %.3f casted to unsigned long is %lu\n", nv, uv);
114 exit(1);
117 static void
118 test3()
120 double dv = 1.9;
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");
126 if (l1 != 1) {
127 printf("FAILED: double 1.9 casted to unsigned long should"
128 " be 1, but is %lu\n", l1);
129 exit(1);
132 if (l2 != 1) {
133 printf("FAILED: long double 1.9 casted to unsigned long should"
134 " be 1, but is %lu\n", l2);
135 exit(1);