xref: /freebsd/contrib/netbsd-tests/lib/libm/t_ilogb.c (revision cdebaff820b2a4915a16cedfd511823d78aab171)
1 /* $NetBSD: t_ilogb.c,v 1.6 2016/08/26 08:01:55 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Maya Rashish.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifdef __FreeBSD__
33 #include <limits.h>
34 #endif
35 #include <atf-c.h>
36 #include <fenv.h>
37 #include <math.h>
38 
39 #ifndef __HAVE_FENV
40 
41 # define ATF_CHECK_RAISED_INVALID
42 # define ATF_CHECK_RAISED_NOTHING
43 
44 #else
45 # define ATF_CHECK_RAISED_INVALID do { \
46 	int r = fetestexcept(FE_ALL_EXCEPT); \
47 	ATF_CHECK_MSG(r == FE_INVALID, "r=%#x != %#x\n", r, FE_INVALID); \
48 	(void)feclearexcept(FE_ALL_EXCEPT); \
49 } while (/*CONSTCOND*/0)
50 
51 # define ATF_CHECK_RAISED_NOTHING do { \
52 	int r = fetestexcept(FE_ALL_EXCEPT); \
53 	ATF_CHECK_MSG(r == 0, "r=%#x != 0\n", r); \
54 	(void)feclearexcept(FE_ALL_EXCEPT); \
55 } while (/*CONSTCOND*/0)
56 #endif
57 
58 ATF_TC(ilogb);
59 ATF_TC_HEAD(ilogb, tc)
60 {
61 	atf_tc_set_md_var(tc, "descr","Check ilogb family");
62 }
63 
64 ATF_TC_BODY(ilogb, tc)
65 {
66 
67 	ATF_CHECK(ilogbf(0) == FP_ILOGB0);
68 	ATF_CHECK_RAISED_INVALID;
69 	ATF_CHECK(ilogb(0) == FP_ILOGB0);
70 	ATF_CHECK_RAISED_INVALID;
71 #ifdef __HAVE_LONG_DOUBLE
72 	ATF_CHECK(ilogbl(0) == FP_ILOGB0);
73 	ATF_CHECK_RAISED_INVALID;
74 #endif
75 
76 	ATF_CHECK(ilogbf(-0) == FP_ILOGB0);
77 	ATF_CHECK_RAISED_INVALID;
78 	ATF_CHECK(ilogb(-0) == FP_ILOGB0);
79 	ATF_CHECK_RAISED_INVALID;
80 #ifdef __HAVE_LONG_DOUBLE
81 	ATF_CHECK(ilogbl(-0) == FP_ILOGB0);
82 	ATF_CHECK_RAISED_INVALID;
83 #endif
84 
85 	ATF_CHECK(ilogbf(INFINITY) == INT_MAX);
86 	ATF_CHECK_RAISED_INVALID;
87 	ATF_CHECK(ilogb(INFINITY) == INT_MAX);
88 	ATF_CHECK_RAISED_INVALID;
89 #ifdef __HAVE_LONG_DOUBLE
90 	ATF_CHECK(ilogbl(INFINITY) == INT_MAX);
91 	ATF_CHECK_RAISED_INVALID;
92 #endif
93 
94 	ATF_CHECK(ilogbf(-INFINITY) == INT_MAX);
95 	ATF_CHECK_RAISED_INVALID;
96 	ATF_CHECK(ilogb(-INFINITY) == INT_MAX);
97 	ATF_CHECK_RAISED_INVALID;
98 #ifdef __HAVE_LONG_DOUBLE
99 	ATF_CHECK(ilogbl(-INFINITY) == INT_MAX);
100 	ATF_CHECK_RAISED_INVALID;
101 #endif
102 
103 	ATF_CHECK(ilogbf(1024) == 10);
104 	ATF_CHECK_RAISED_NOTHING;
105 	ATF_CHECK(ilogb(1024) == 10);
106 	ATF_CHECK_RAISED_NOTHING;
107 #ifdef __HAVE_LONG_DOUBLE
108 	ATF_CHECK(ilogbl(1024) == 10);
109 	ATF_CHECK_RAISED_NOTHING;
110 #endif
111 
112 #ifndef __vax__
113 	ATF_CHECK(ilogbf(NAN) == FP_ILOGBNAN);
114 	ATF_CHECK_RAISED_INVALID;
115 	ATF_CHECK(ilogb(NAN) == FP_ILOGBNAN);
116 	ATF_CHECK_RAISED_INVALID;
117 #ifdef __HAVE_LONG_DOUBLE
118 	ATF_CHECK(ilogbl(NAN) == FP_ILOGBNAN);
119 	ATF_CHECK_RAISED_INVALID;
120 #endif
121 #endif
122 }
123 
124 ATF_TP_ADD_TCS(tp)
125 {
126 
127 	ATF_TP_ADD_TC(tp, ilogb);
128 
129 	return atf_no_error();
130 }
131