xref: /freebsd/lib/msun/tests/lround_test.c (revision 06c3fb27)
1 /*-
2  * Copyright (c) 2005 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Test for lround(), lroundf(), llround(), and llroundf().
29  */
30 
31 #include <sys/cdefs.h>
32 #include <fenv.h>
33 #include <limits.h>
34 #include <math.h>
35 #include <stdio.h>
36 
37 #include "test-utils.h"
38 
39 #define	IGNORE	0x12345
40 
41 #define	test(func, x, result, excepts)	do {					\
42 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));			\
43 	long long _r = (func)(x);						\
44 	CHECK_FP_EXCEPTIONS_MSG(excepts, FE_ALL_EXCEPT, "for %s(%s)",		\
45 	    #func, #x);								\
46 	if ((excepts & FE_INVALID) != 0) {					\
47 		ATF_REQUIRE_EQ(result, IGNORE);					\
48 		ATF_CHECK_EQ_MSG(FE_INVALID, fetestexcept(FE_INVALID),		\
49 		    "FE_INVALID not set correctly for %s(%s)", #func, #x);	\
50 	} else {								\
51 		ATF_REQUIRE_MSG(result != IGNORE, "Expected can't be IGNORE!");	\
52 		ATF_REQUIRE_EQ(result, (__STRING(func(_d)), _r));		\
53 	}									\
54 } while (0)
55 
56 #define	testall(x, result, excepts)	do {				\
57 	test(lround, x, result, excepts);				\
58 	test(lroundf, x, result, excepts);				\
59 	test(llround, x, result, excepts);				\
60 	test(llroundf, x, result, excepts);				\
61 } while (0)
62 
63 #pragma STDC FENV_ACCESS ON
64 
65 ATF_TC_WITHOUT_HEAD(main);
66 ATF_TC_BODY(main, tc)
67 {
68 	testall(0.0, 0, 0);
69 	testall(0.25, 0, FE_INEXACT);
70 	testall(0.5, 1, FE_INEXACT);
71 	testall(-0.5, -1, FE_INEXACT);
72 	testall(1.0, 1, 0);
73 	testall(0x12345000p0, 0x12345000, 0);
74 	testall(0x1234.fp0, 0x1235, FE_INEXACT);
75 	testall(INFINITY, IGNORE, FE_INVALID);
76 	testall(NAN, IGNORE, FE_INVALID);
77 
78 #if (LONG_MAX == 0x7fffffffl)
79 	test(lround, 0x7fffffff.8p0, IGNORE, FE_INVALID);
80 	test(lround, -0x80000000.8p0, IGNORE, FE_INVALID);
81 	test(lround, 0x80000000.0p0, IGNORE, FE_INVALID);
82 	test(lround, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT);
83 	test(lround, -0x80000000.4p0, -0x80000000l, FE_INEXACT);
84 	test(lroundf, 0x80000000.0p0f, IGNORE, FE_INVALID);
85 	test(lroundf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
86 #elif (LONG_MAX == 0x7fffffffffffffffll)
87 	test(lround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
88 	test(lroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
89 	test(lround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0);
90 	test(lroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0);
91 	test(lround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
92 	test(lroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
93 	test(lround, -0x8000000000000000.0p0, (long)-0x8000000000000000l, 0);
94 	test(lroundf, -0x8000000000000000.0p0f, (long)-0x8000000000000000l, 0);
95 #else
96 #error "Unsupported long size"
97 #endif
98 
99 #if (LLONG_MAX == 0x7fffffffffffffffLL)
100 	test(llround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
101 	test(llroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
102 	test(llround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0);
103 	test(llroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0);
104 	test(llround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
105 	test(llroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
106 	test(llround, -0x8000000000000000.0p0, (long long)-0x8000000000000000ll, 0);
107 	test(llroundf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ll, 0);
108 #else
109 #error "Unsupported long long size"
110 #endif
111 }
112 
113 ATF_TP_ADD_TCS(tp)
114 {
115 	ATF_TP_ADD_TC(tp, main);
116 
117 	return (atf_no_error());
118 }
119