xref: /freebsd/contrib/netbsd-tests/lib/libm/t_tanh.c (revision d6b92ffa)
1 /* $NetBSD: t_tanh.c,v 1.7 2014/03/03 10:39:08 martin 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.7 2014/03/03 10:39:08 martin 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 	const double x = 0.0L / 0.0L;
49 
50 	ATF_CHECK(isnan(x) != 0);
51 	ATF_CHECK(isnan(tanh(x)) != 0);
52 }
53 
54 ATF_TC(tanh_inf_neg);
55 ATF_TC_HEAD(tanh_inf_neg, tc)
56 {
57 	atf_tc_set_md_var(tc, "descr", "Test tanh(-Inf) == -1.0");
58 }
59 
60 ATF_TC_BODY(tanh_inf_neg, tc)
61 {
62 	const double x = -1.0L / 0.0L;
63 
64 	ATF_CHECK(tanh(x) == -1.0);
65 }
66 
67 ATF_TC(tanh_inf_pos);
68 ATF_TC_HEAD(tanh_inf_pos, tc)
69 {
70 	atf_tc_set_md_var(tc, "descr", "Test tanh(+Inf) == +1.0");
71 }
72 
73 ATF_TC_BODY(tanh_inf_pos, tc)
74 {
75 	const double x = 1.0L / 0.0L;
76 
77 	ATF_CHECK(tanh(x) == 1.0);
78 }
79 
80 ATF_TC(tanh_zero_neg);
81 ATF_TC_HEAD(tanh_zero_neg, tc)
82 {
83 	atf_tc_set_md_var(tc, "descr", "Test tanh(-0.0) == -0.0");
84 }
85 
86 ATF_TC_BODY(tanh_zero_neg, tc)
87 {
88 	const double x = -0.0L;
89 	double y = tanh(x);
90 
91 	ATF_CHECK(x == y);
92 	ATF_CHECK(signbit(x) != 0);
93 
94 	ATF_REQUIRE_MSG(signbit(y) != 0,
95 	    "compiler bug, waiting for newer gcc import, see PR lib/44057");
96 }
97 
98 ATF_TC(tanh_zero_pos);
99 ATF_TC_HEAD(tanh_zero_pos, tc)
100 {
101 	atf_tc_set_md_var(tc, "descr", "Test tanh(+0.0) == +0.0");
102 }
103 
104 ATF_TC_BODY(tanh_zero_pos, tc)
105 {
106 	const double x = 0.0L;
107 	double y = tanh(x);
108 
109 	ATF_CHECK(x == y);
110 	ATF_CHECK(signbit(x) == 0);
111 	ATF_CHECK(signbit(y) == 0);
112 }
113 
114 /*
115  * tanhf(3)
116  */
117 ATF_TC(tanhf_nan);
118 ATF_TC_HEAD(tanhf_nan, tc)
119 {
120 	atf_tc_set_md_var(tc, "descr", "Test tanhf(NaN) == NaN");
121 }
122 
123 ATF_TC_BODY(tanhf_nan, tc)
124 {
125 	const float x = 0.0L / 0.0L;
126 
127 	ATF_CHECK(isnan(x) != 0);
128 	ATF_CHECK(isnan(tanhf(x)) != 0);
129 }
130 
131 ATF_TC(tanhf_inf_neg);
132 ATF_TC_HEAD(tanhf_inf_neg, tc)
133 {
134 	atf_tc_set_md_var(tc, "descr", "Test tanhf(-Inf) == -1.0");
135 }
136 
137 ATF_TC_BODY(tanhf_inf_neg, tc)
138 {
139 	const float x = -1.0L / 0.0L;
140 
141 	ATF_CHECK(tanhf(x) == -1.0);
142 }
143 
144 ATF_TC(tanhf_inf_pos);
145 ATF_TC_HEAD(tanhf_inf_pos, tc)
146 {
147 	atf_tc_set_md_var(tc, "descr", "Test tanhf(+Inf) == +1.0");
148 }
149 
150 ATF_TC_BODY(tanhf_inf_pos, tc)
151 {
152 	const float x = 1.0L / 0.0L;
153 
154 	ATF_CHECK(tanhf(x) == 1.0);
155 }
156 
157 ATF_TC(tanhf_zero_neg);
158 ATF_TC_HEAD(tanhf_zero_neg, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr", "Test tanhf(-0.0) == -0.0");
161 }
162 
163 ATF_TC_BODY(tanhf_zero_neg, tc)
164 {
165 	const float x = -0.0L;
166 	float y = tanh(x);
167 
168 	ATF_CHECK(x == y);
169 	ATF_CHECK(signbit(x) != 0);
170 
171 	ATF_REQUIRE_MSG(signbit(y) != 0,
172 	    "compiler bug, waiting for newer gcc import, see PR lib/44057");
173 }
174 
175 ATF_TC(tanhf_zero_pos);
176 ATF_TC_HEAD(tanhf_zero_pos, tc)
177 {
178 	atf_tc_set_md_var(tc, "descr", "Test tanhf(+0.0) == +0.0");
179 }
180 
181 ATF_TC_BODY(tanhf_zero_pos, tc)
182 {
183 	const float x = 0.0L;
184 	float y = tanhf(x);
185 
186 	ATF_CHECK(x == y);
187 	ATF_CHECK(signbit(x) == 0);
188 	ATF_CHECK(signbit(y) == 0);
189 }
190 
191 ATF_TP_ADD_TCS(tp)
192 {
193 
194 	ATF_TP_ADD_TC(tp, tanh_nan);
195 	ATF_TP_ADD_TC(tp, tanh_inf_neg);
196 	ATF_TP_ADD_TC(tp, tanh_inf_pos);
197 	ATF_TP_ADD_TC(tp, tanh_zero_neg);
198 	ATF_TP_ADD_TC(tp, tanh_zero_pos);
199 
200 	ATF_TP_ADD_TC(tp, tanhf_nan);
201 	ATF_TP_ADD_TC(tp, tanhf_inf_neg);
202 	ATF_TP_ADD_TC(tp, tanhf_inf_pos);
203 	ATF_TP_ADD_TC(tp, tanhf_zero_neg);
204 	ATF_TP_ADD_TC(tp, tanhf_zero_pos);
205 
206 	return atf_no_error();
207 }
208