xref: /minix/tests/lib/libm/t_sin.c (revision 0a6a1f1d)
1 /* $NetBSD: t_sin.c,v 1.4 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 
32 #include <atf-c.h>
33 #include <math.h>
34 
35 static const struct {
36 	int		angle;
37 	double		x;
38 	double		y;
39 } angles[] = {
40 	{ -180, -3.141592653589793,  0.0000000000000000 },
41 	{ -135, -2.356194490192345, -0.7071067811865476 },
42 	{  -90, -1.570796326794897, -1.0000000000000000 },
43 	{  -45, -0.785398163397448, -0.7071067811865476 },
44 	{    0,  0.000000000000000,  0.0000000000000000 },
45 	{   30,  0.523598775598299,  0.5000000000000000 },
46 	{   45,  0.785398163397448,  0.7071067811865476 },
47 	{   60,  1.047197551196598,  0.8660254037844386 },
48 	{   90,  1.570796326794897,  1.0000000000000000 },
49 	{  120,  2.094395102393195,  0.8660254037844386 },
50 	{  135,  2.356194490192345,  0.7071067811865476 },
51 	{  150,  2.617993877991494,  0.5000000000000000 },
52 	{  180,  3.141592653589793,  0.0000000000000000 },
53 	{  270,  4.712388980384690, -1.0000000000000000 },
54 	{  360,  6.283185307179586,  0.0000000000000000 }
55 };
56 
57 /*
58  * sin(3)
59  */
60 ATF_TC(sin_angles);
ATF_TC_HEAD(sin_angles,tc)61 ATF_TC_HEAD(sin_angles, tc)
62 {
63 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
64 }
65 
ATF_TC_BODY(sin_angles,tc)66 ATF_TC_BODY(sin_angles, tc)
67 {
68 	const double eps = 1.0e-15;
69 	size_t i;
70 
71 	for (i = 0; i < __arraycount(angles); i++) {
72 
73 		if (fabs(sin(angles[i].x) - angles[i].y) > eps)
74 			atf_tc_fail_nonfatal("sin(%d deg) != %0.01f",
75 			    angles[i].angle, angles[i].y);
76 	}
77 }
78 
79 ATF_TC(sin_nan);
ATF_TC_HEAD(sin_nan,tc)80 ATF_TC_HEAD(sin_nan, tc)
81 {
82 	atf_tc_set_md_var(tc, "descr", "Test sin(NaN) == NaN");
83 }
84 
ATF_TC_BODY(sin_nan,tc)85 ATF_TC_BODY(sin_nan, tc)
86 {
87 	const double x = 0.0L / 0.0L;
88 
89 	ATF_CHECK(isnan(x) != 0);
90 	ATF_CHECK(isnan(sin(x)) != 0);
91 }
92 
93 ATF_TC(sin_inf_neg);
ATF_TC_HEAD(sin_inf_neg,tc)94 ATF_TC_HEAD(sin_inf_neg, tc)
95 {
96 	atf_tc_set_md_var(tc, "descr", "Test sin(-Inf) == NaN");
97 }
98 
ATF_TC_BODY(sin_inf_neg,tc)99 ATF_TC_BODY(sin_inf_neg, tc)
100 {
101 	const double x = -1.0L / 0.0L;
102 
103 	ATF_CHECK(isnan(sin(x)) != 0);
104 }
105 
106 ATF_TC(sin_inf_pos);
ATF_TC_HEAD(sin_inf_pos,tc)107 ATF_TC_HEAD(sin_inf_pos, tc)
108 {
109 	atf_tc_set_md_var(tc, "descr", "Test sin(+Inf) == NaN");
110 }
111 
ATF_TC_BODY(sin_inf_pos,tc)112 ATF_TC_BODY(sin_inf_pos, tc)
113 {
114 	const double x = 1.0L / 0.0L;
115 
116 	ATF_CHECK(isnan(sin(x)) != 0);
117 }
118 
119 
120 ATF_TC(sin_zero_neg);
ATF_TC_HEAD(sin_zero_neg,tc)121 ATF_TC_HEAD(sin_zero_neg, tc)
122 {
123 	atf_tc_set_md_var(tc, "descr", "Test sin(-0.0) == -0.0");
124 }
125 
ATF_TC_BODY(sin_zero_neg,tc)126 ATF_TC_BODY(sin_zero_neg, tc)
127 {
128 	const double x = -0.0L;
129 
130 	ATF_CHECK(sin(x) == x);
131 }
132 
133 ATF_TC(sin_zero_pos);
ATF_TC_HEAD(sin_zero_pos,tc)134 ATF_TC_HEAD(sin_zero_pos, tc)
135 {
136 	atf_tc_set_md_var(tc, "descr", "Test sin(+0.0) == +0.0");
137 }
138 
ATF_TC_BODY(sin_zero_pos,tc)139 ATF_TC_BODY(sin_zero_pos, tc)
140 {
141 	const double x = 0.0L;
142 
143 	ATF_CHECK(sin(x) == x);
144 }
145 
146 /*
147  * sinf(3)
148  */
149 ATF_TC(sinf_angles);
ATF_TC_HEAD(sinf_angles,tc)150 ATF_TC_HEAD(sinf_angles, tc)
151 {
152 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
153 }
154 
ATF_TC_BODY(sinf_angles,tc)155 ATF_TC_BODY(sinf_angles, tc)
156 {
157 	const float eps = 1.0e-6;
158 	float x, y;
159 	size_t i;
160 
161 	for (i = 0; i < __arraycount(angles); i++) {
162 
163 		x = angles[i].x;
164 		y = angles[i].y;
165 
166 		if (fabsf(sinf(x) - y) > eps)
167 			atf_tc_fail_nonfatal("sinf(%d deg) != %0.01f",
168 			    angles[i].angle, angles[i].y);
169 	}
170 }
171 
172 ATF_TC(sinf_nan);
ATF_TC_HEAD(sinf_nan,tc)173 ATF_TC_HEAD(sinf_nan, tc)
174 {
175 	atf_tc_set_md_var(tc, "descr", "Test sinf(NaN) == NaN");
176 }
177 
ATF_TC_BODY(sinf_nan,tc)178 ATF_TC_BODY(sinf_nan, tc)
179 {
180 	const float x = 0.0L / 0.0L;
181 
182 	ATF_CHECK(isnan(x) != 0);
183 	ATF_CHECK(isnan(sinf(x)) != 0);
184 }
185 
186 ATF_TC(sinf_inf_neg);
ATF_TC_HEAD(sinf_inf_neg,tc)187 ATF_TC_HEAD(sinf_inf_neg, tc)
188 {
189 	atf_tc_set_md_var(tc, "descr", "Test sinf(-Inf) == NaN");
190 }
191 
ATF_TC_BODY(sinf_inf_neg,tc)192 ATF_TC_BODY(sinf_inf_neg, tc)
193 {
194 	const float x = -1.0L / 0.0L;
195 
196 	if (isnan(sinf(x)) == 0) {
197 		atf_tc_expect_fail("PR lib/45362");
198 		atf_tc_fail("sinf(-Inf) != NaN");
199 	}
200 }
201 
202 ATF_TC(sinf_inf_pos);
ATF_TC_HEAD(sinf_inf_pos,tc)203 ATF_TC_HEAD(sinf_inf_pos, tc)
204 {
205 	atf_tc_set_md_var(tc, "descr", "Test sinf(+Inf) == NaN");
206 }
207 
ATF_TC_BODY(sinf_inf_pos,tc)208 ATF_TC_BODY(sinf_inf_pos, tc)
209 {
210 	const float x = 1.0L / 0.0L;
211 
212 	if (isnan(sinf(x)) == 0) {
213 		atf_tc_expect_fail("PR lib/45362");
214 		atf_tc_fail("sinf(+Inf) != NaN");
215 	}
216 }
217 
218 
219 ATF_TC(sinf_zero_neg);
ATF_TC_HEAD(sinf_zero_neg,tc)220 ATF_TC_HEAD(sinf_zero_neg, tc)
221 {
222 	atf_tc_set_md_var(tc, "descr", "Test sinf(-0.0) == -0.0");
223 }
224 
ATF_TC_BODY(sinf_zero_neg,tc)225 ATF_TC_BODY(sinf_zero_neg, tc)
226 {
227 	const float x = -0.0L;
228 
229 	ATF_CHECK(sinf(x) == x);
230 }
231 
232 ATF_TC(sinf_zero_pos);
ATF_TC_HEAD(sinf_zero_pos,tc)233 ATF_TC_HEAD(sinf_zero_pos, tc)
234 {
235 	atf_tc_set_md_var(tc, "descr", "Test sinf(+0.0) == +0.0");
236 }
237 
ATF_TC_BODY(sinf_zero_pos,tc)238 ATF_TC_BODY(sinf_zero_pos, tc)
239 {
240 	const float x = 0.0L;
241 
242 	ATF_CHECK(sinf(x) == x);
243 }
244 
ATF_TP_ADD_TCS(tp)245 ATF_TP_ADD_TCS(tp)
246 {
247 
248 	ATF_TP_ADD_TC(tp, sin_angles);
249 	ATF_TP_ADD_TC(tp, sin_nan);
250 	ATF_TP_ADD_TC(tp, sin_inf_neg);
251 	ATF_TP_ADD_TC(tp, sin_inf_pos);
252 	ATF_TP_ADD_TC(tp, sin_zero_neg);
253 	ATF_TP_ADD_TC(tp, sin_zero_pos);
254 
255 	ATF_TP_ADD_TC(tp, sinf_angles);
256 	ATF_TP_ADD_TC(tp, sinf_nan);
257 	ATF_TP_ADD_TC(tp, sinf_inf_neg);
258 	ATF_TP_ADD_TC(tp, sinf_inf_pos);
259 	ATF_TP_ADD_TC(tp, sinf_zero_neg);
260 	ATF_TP_ADD_TC(tp, sinf_zero_pos);
261 
262 	return atf_no_error();
263 }
264