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