xref: /freebsd/contrib/netbsd-tests/lib/libm/t_ilogb.c (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1 /* $NetBSD: t_ilogb.c,v 1.9 2018/06/14 21:57:25 maya 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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_ilogb.c,v 1.9 2018/06/14 21:57:25 maya Exp $");
33 
34 #include <atf-c.h>
35 #include <fenv.h>
36 #include <limits.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) != 0, \
48 	    "r & FE_INVALID == 0 (r=%#x, FE_INVALID=%#x)\n", \
49 	     r, FE_INVALID); \
50 	(void)feclearexcept(FE_ALL_EXCEPT); \
51 } while (/*CONSTCOND*/0)
52 
53 # define ATF_CHECK_RAISED_NOTHING do { \
54 	int r = fetestexcept(FE_ALL_EXCEPT); \
55 	ATF_CHECK_MSG(r == 0, "r=%#x != 0\n", r); \
56 	(void)feclearexcept(FE_ALL_EXCEPT); \
57 } while (/*CONSTCOND*/0)
58 #endif
59 
60 ATF_TC(ilogb);
61 ATF_TC_HEAD(ilogb, tc)
62 {
63 	atf_tc_set_md_var(tc, "descr","Check ilogb family");
64 }
65 
66 ATF_TC_BODY(ilogb, tc)
67 {
68 
69 	ATF_CHECK(ilogbf(0) == FP_ILOGB0);
70 	ATF_CHECK_RAISED_INVALID;
71 	ATF_CHECK(ilogb(0) == FP_ILOGB0);
72 	ATF_CHECK_RAISED_INVALID;
73 #ifdef __HAVE_LONG_DOUBLE
74 	ATF_CHECK(ilogbl(0) == FP_ILOGB0);
75 	ATF_CHECK_RAISED_INVALID;
76 #endif
77 
78 	ATF_CHECK(ilogbf(-0) == FP_ILOGB0);
79 	ATF_CHECK_RAISED_INVALID;
80 	ATF_CHECK(ilogb(-0) == FP_ILOGB0);
81 	ATF_CHECK_RAISED_INVALID;
82 #ifdef __HAVE_LONG_DOUBLE
83 	ATF_CHECK(ilogbl(-0) == FP_ILOGB0);
84 	ATF_CHECK_RAISED_INVALID;
85 #endif
86 
87 	ATF_CHECK(ilogbf(INFINITY) == INT_MAX);
88 	ATF_CHECK_RAISED_INVALID;
89 	ATF_CHECK(ilogb(INFINITY) == INT_MAX);
90 	ATF_CHECK_RAISED_INVALID;
91 #ifdef __HAVE_LONG_DOUBLE
92 	ATF_CHECK(ilogbl(INFINITY) == INT_MAX);
93 	ATF_CHECK_RAISED_INVALID;
94 #endif
95 
96 	ATF_CHECK(ilogbf(-INFINITY) == INT_MAX);
97 	ATF_CHECK_RAISED_INVALID;
98 	ATF_CHECK(ilogb(-INFINITY) == INT_MAX);
99 	ATF_CHECK_RAISED_INVALID;
100 #ifdef __HAVE_LONG_DOUBLE
101 	ATF_CHECK(ilogbl(-INFINITY) == INT_MAX);
102 	ATF_CHECK_RAISED_INVALID;
103 #endif
104 
105 	ATF_CHECK(ilogbf(1024) == 10);
106 	ATF_CHECK_RAISED_NOTHING;
107 	ATF_CHECK(ilogb(1024) == 10);
108 	ATF_CHECK_RAISED_NOTHING;
109 #ifdef __HAVE_LONG_DOUBLE
110 	ATF_CHECK(ilogbl(1024) == 10);
111 	ATF_CHECK_RAISED_NOTHING;
112 #endif
113 
114 #ifndef __vax__
115 	ATF_CHECK(ilogbf(NAN) == FP_ILOGBNAN);
116 	ATF_CHECK_RAISED_INVALID;
117 	ATF_CHECK(ilogb(NAN) == FP_ILOGBNAN);
118 	ATF_CHECK_RAISED_INVALID;
119 #ifdef __HAVE_LONG_DOUBLE
120 	ATF_CHECK(ilogbl(NAN) == FP_ILOGBNAN);
121 	ATF_CHECK_RAISED_INVALID;
122 #endif
123 #endif
124 }
125 
126 ATF_TP_ADD_TCS(tp)
127 {
128 
129 	ATF_TP_ADD_TC(tp, ilogb);
130 
131 	return atf_no_error();
132 }
133