xref: /netbsd-src/tests/lib/libm/t_tanh.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /* $NetBSD: t_tanh.c,v 1.6 2011/09/12 17:45:51 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
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_tanh.c,v 1.6 2011/09/12 17:45:51 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <math.h>
36 
37 /*
38  * tanh(3)
39  */
40 ATF_TC(tanh_nan);
41 ATF_TC_HEAD(tanh_nan, tc)
42 {
43 	atf_tc_set_md_var(tc, "descr", "Test tanh(NaN) == NaN");
44 }
45 
46 ATF_TC_BODY(tanh_nan, tc)
47 {
48 #ifndef __vax__
49 	const double x = 0.0L / 0.0L;
50 
51 	ATF_CHECK(isnan(x) != 0);
52 	ATF_CHECK(isnan(tanh(x)) != 0);
53 #endif
54 }
55 
56 ATF_TC(tanh_inf_neg);
57 ATF_TC_HEAD(tanh_inf_neg, tc)
58 {
59 	atf_tc_set_md_var(tc, "descr", "Test tanh(-Inf) == -1.0");
60 }
61 
62 ATF_TC_BODY(tanh_inf_neg, tc)
63 {
64 #ifndef __vax__
65 	const double x = -1.0L / 0.0L;
66 
67 	ATF_CHECK(tanh(x) == -1.0);
68 #endif
69 }
70 
71 ATF_TC(tanh_inf_pos);
72 ATF_TC_HEAD(tanh_inf_pos, tc)
73 {
74 	atf_tc_set_md_var(tc, "descr", "Test tanh(+Inf) == +1.0");
75 }
76 
77 ATF_TC_BODY(tanh_inf_pos, tc)
78 {
79 #ifndef __vax__
80 	const double x = 1.0L / 0.0L;
81 
82 	ATF_CHECK(tanh(x) == 1.0);
83 #endif
84 }
85 
86 ATF_TC(tanh_zero_neg);
87 ATF_TC_HEAD(tanh_zero_neg, tc)
88 {
89 	atf_tc_set_md_var(tc, "descr", "Test tanh(-0.0) == -0.0");
90 }
91 
92 ATF_TC_BODY(tanh_zero_neg, tc)
93 {
94 #ifndef __vax__
95 	const double x = -0.0L;
96 	double y = tanh(x);
97 
98 	ATF_CHECK(x == y);
99 	ATF_CHECK(signbit(x) != 0);
100 
101 	ATF_REQUIRE_MSG(signbit(y) != 0,
102 	    "compiler bug, waiting for newer gcc import, see PR lib/44057");
103 #endif
104 }
105 
106 ATF_TC(tanh_zero_pos);
107 ATF_TC_HEAD(tanh_zero_pos, tc)
108 {
109 	atf_tc_set_md_var(tc, "descr", "Test tanh(+0.0) == +0.0");
110 }
111 
112 ATF_TC_BODY(tanh_zero_pos, tc)
113 {
114 #ifndef __vax__
115 	const double x = 0.0L;
116 	double y = tanh(x);
117 
118 	ATF_CHECK(x == y);
119 	ATF_CHECK(signbit(x) == 0);
120 	ATF_CHECK(signbit(y) == 0);
121 #endif
122 }
123 
124 /*
125  * tanhf(3)
126  */
127 ATF_TC(tanhf_nan);
128 ATF_TC_HEAD(tanhf_nan, tc)
129 {
130 	atf_tc_set_md_var(tc, "descr", "Test tanhf(NaN) == NaN");
131 }
132 
133 ATF_TC_BODY(tanhf_nan, tc)
134 {
135 #ifndef __vax__
136 	const float x = 0.0L / 0.0L;
137 
138 	ATF_CHECK(isnan(x) != 0);
139 	ATF_CHECK(isnan(tanhf(x)) != 0);
140 #endif
141 }
142 
143 ATF_TC(tanhf_inf_neg);
144 ATF_TC_HEAD(tanhf_inf_neg, tc)
145 {
146 	atf_tc_set_md_var(tc, "descr", "Test tanhf(-Inf) == -1.0");
147 }
148 
149 ATF_TC_BODY(tanhf_inf_neg, tc)
150 {
151 #ifndef __vax__
152 	const float x = -1.0L / 0.0L;
153 
154 	ATF_CHECK(tanhf(x) == -1.0);
155 #endif
156 }
157 
158 ATF_TC(tanhf_inf_pos);
159 ATF_TC_HEAD(tanhf_inf_pos, tc)
160 {
161 	atf_tc_set_md_var(tc, "descr", "Test tanhf(+Inf) == +1.0");
162 }
163 
164 ATF_TC_BODY(tanhf_inf_pos, tc)
165 {
166 #ifndef __vax__
167 	const float x = 1.0L / 0.0L;
168 
169 	ATF_CHECK(tanhf(x) == 1.0);
170 #endif
171 }
172 
173 ATF_TC(tanhf_zero_neg);
174 ATF_TC_HEAD(tanhf_zero_neg, tc)
175 {
176 	atf_tc_set_md_var(tc, "descr", "Test tanhf(-0.0) == -0.0");
177 }
178 
179 ATF_TC_BODY(tanhf_zero_neg, tc)
180 {
181 #ifndef __vax__
182 	const float x = -0.0L;
183 	float y = tanh(x);
184 
185 	ATF_CHECK(x == y);
186 	ATF_CHECK(signbit(x) != 0);
187 
188 	ATF_REQUIRE_MSG(signbit(y) != 0,
189 	    "compiler bug, waiting for newer gcc import, see PR lib/44057");
190 #endif
191 }
192 
193 ATF_TC(tanhf_zero_pos);
194 ATF_TC_HEAD(tanhf_zero_pos, tc)
195 {
196 	atf_tc_set_md_var(tc, "descr", "Test tanhf(+0.0) == +0.0");
197 }
198 
199 ATF_TC_BODY(tanhf_zero_pos, tc)
200 {
201 #ifndef __vax__
202 	const float x = 0.0L;
203 	float y = tanhf(x);
204 
205 	ATF_CHECK(x == y);
206 	ATF_CHECK(signbit(x) == 0);
207 	ATF_CHECK(signbit(y) == 0);
208 #endif
209 }
210 
211 ATF_TP_ADD_TCS(tp)
212 {
213 
214 	ATF_TP_ADD_TC(tp, tanh_nan);
215 	ATF_TP_ADD_TC(tp, tanh_inf_neg);
216 	ATF_TP_ADD_TC(tp, tanh_inf_pos);
217 	ATF_TP_ADD_TC(tp, tanh_zero_neg);
218 	ATF_TP_ADD_TC(tp, tanh_zero_pos);
219 
220 	ATF_TP_ADD_TC(tp, tanhf_nan);
221 	ATF_TP_ADD_TC(tp, tanhf_inf_neg);
222 	ATF_TP_ADD_TC(tp, tanhf_inf_pos);
223 	ATF_TP_ADD_TC(tp, tanhf_zero_neg);
224 	ATF_TP_ADD_TC(tp, tanhf_zero_pos);
225 
226 	return atf_no_error();
227 }
228