1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_sin.c,v 1.4 2014/03/03 10:39:08 martin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc
3211be35a1SLionel Sambuc #include <atf-c.h>
3311be35a1SLionel Sambuc #include <math.h>
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc static const struct {
3611be35a1SLionel Sambuc int angle;
3711be35a1SLionel Sambuc double x;
3811be35a1SLionel Sambuc double y;
3911be35a1SLionel Sambuc } angles[] = {
4011be35a1SLionel Sambuc { -180, -3.141592653589793, 0.0000000000000000 },
4111be35a1SLionel Sambuc { -135, -2.356194490192345, -0.7071067811865476 },
4211be35a1SLionel Sambuc { -90, -1.570796326794897, -1.0000000000000000 },
4311be35a1SLionel Sambuc { -45, -0.785398163397448, -0.7071067811865476 },
4411be35a1SLionel Sambuc { 0, 0.000000000000000, 0.0000000000000000 },
4511be35a1SLionel Sambuc { 30, 0.523598775598299, 0.5000000000000000 },
4611be35a1SLionel Sambuc { 45, 0.785398163397448, 0.7071067811865476 },
4711be35a1SLionel Sambuc { 60, 1.047197551196598, 0.8660254037844386 },
4811be35a1SLionel Sambuc { 90, 1.570796326794897, 1.0000000000000000 },
4911be35a1SLionel Sambuc { 120, 2.094395102393195, 0.8660254037844386 },
5011be35a1SLionel Sambuc { 135, 2.356194490192345, 0.7071067811865476 },
5111be35a1SLionel Sambuc { 150, 2.617993877991494, 0.5000000000000000 },
5211be35a1SLionel Sambuc { 180, 3.141592653589793, 0.0000000000000000 },
5311be35a1SLionel Sambuc { 270, 4.712388980384690, -1.0000000000000000 },
5411be35a1SLionel Sambuc { 360, 6.283185307179586, 0.0000000000000000 }
5511be35a1SLionel Sambuc };
5611be35a1SLionel Sambuc
5711be35a1SLionel Sambuc /*
5811be35a1SLionel Sambuc * sin(3)
5911be35a1SLionel Sambuc */
6011be35a1SLionel Sambuc ATF_TC(sin_angles);
ATF_TC_HEAD(sin_angles,tc)6111be35a1SLionel Sambuc ATF_TC_HEAD(sin_angles, tc)
6211be35a1SLionel Sambuc {
6311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test some selected angles");
6411be35a1SLionel Sambuc }
6511be35a1SLionel Sambuc
ATF_TC_BODY(sin_angles,tc)6611be35a1SLionel Sambuc ATF_TC_BODY(sin_angles, tc)
6711be35a1SLionel Sambuc {
6811be35a1SLionel Sambuc const double eps = 1.0e-15;
6911be35a1SLionel Sambuc size_t i;
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc for (i = 0; i < __arraycount(angles); i++) {
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc if (fabs(sin(angles[i].x) - angles[i].y) > eps)
7411be35a1SLionel Sambuc atf_tc_fail_nonfatal("sin(%d deg) != %0.01f",
7511be35a1SLionel Sambuc angles[i].angle, angles[i].y);
7611be35a1SLionel Sambuc }
7711be35a1SLionel Sambuc }
7811be35a1SLionel Sambuc
7911be35a1SLionel Sambuc ATF_TC(sin_nan);
ATF_TC_HEAD(sin_nan,tc)8011be35a1SLionel Sambuc ATF_TC_HEAD(sin_nan, tc)
8111be35a1SLionel Sambuc {
8211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sin(NaN) == NaN");
8311be35a1SLionel Sambuc }
8411be35a1SLionel Sambuc
ATF_TC_BODY(sin_nan,tc)8511be35a1SLionel Sambuc ATF_TC_BODY(sin_nan, tc)
8611be35a1SLionel Sambuc {
8711be35a1SLionel Sambuc const double x = 0.0L / 0.0L;
8811be35a1SLionel Sambuc
8911be35a1SLionel Sambuc ATF_CHECK(isnan(x) != 0);
9011be35a1SLionel Sambuc ATF_CHECK(isnan(sin(x)) != 0);
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc
9311be35a1SLionel Sambuc ATF_TC(sin_inf_neg);
ATF_TC_HEAD(sin_inf_neg,tc)9411be35a1SLionel Sambuc ATF_TC_HEAD(sin_inf_neg, tc)
9511be35a1SLionel Sambuc {
9611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sin(-Inf) == NaN");
9711be35a1SLionel Sambuc }
9811be35a1SLionel Sambuc
ATF_TC_BODY(sin_inf_neg,tc)9911be35a1SLionel Sambuc ATF_TC_BODY(sin_inf_neg, tc)
10011be35a1SLionel Sambuc {
10111be35a1SLionel Sambuc const double x = -1.0L / 0.0L;
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc ATF_CHECK(isnan(sin(x)) != 0);
10411be35a1SLionel Sambuc }
10511be35a1SLionel Sambuc
10611be35a1SLionel Sambuc ATF_TC(sin_inf_pos);
ATF_TC_HEAD(sin_inf_pos,tc)10711be35a1SLionel Sambuc ATF_TC_HEAD(sin_inf_pos, tc)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sin(+Inf) == NaN");
11011be35a1SLionel Sambuc }
11111be35a1SLionel Sambuc
ATF_TC_BODY(sin_inf_pos,tc)11211be35a1SLionel Sambuc ATF_TC_BODY(sin_inf_pos, tc)
11311be35a1SLionel Sambuc {
11411be35a1SLionel Sambuc const double x = 1.0L / 0.0L;
11511be35a1SLionel Sambuc
11611be35a1SLionel Sambuc ATF_CHECK(isnan(sin(x)) != 0);
11711be35a1SLionel Sambuc }
11811be35a1SLionel Sambuc
11911be35a1SLionel Sambuc
12011be35a1SLionel Sambuc ATF_TC(sin_zero_neg);
ATF_TC_HEAD(sin_zero_neg,tc)12111be35a1SLionel Sambuc ATF_TC_HEAD(sin_zero_neg, tc)
12211be35a1SLionel Sambuc {
12311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sin(-0.0) == -0.0");
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
ATF_TC_BODY(sin_zero_neg,tc)12611be35a1SLionel Sambuc ATF_TC_BODY(sin_zero_neg, tc)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc const double x = -0.0L;
12911be35a1SLionel Sambuc
13011be35a1SLionel Sambuc ATF_CHECK(sin(x) == x);
13111be35a1SLionel Sambuc }
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc ATF_TC(sin_zero_pos);
ATF_TC_HEAD(sin_zero_pos,tc)13411be35a1SLionel Sambuc ATF_TC_HEAD(sin_zero_pos, tc)
13511be35a1SLionel Sambuc {
13611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sin(+0.0) == +0.0");
13711be35a1SLionel Sambuc }
13811be35a1SLionel Sambuc
ATF_TC_BODY(sin_zero_pos,tc)13911be35a1SLionel Sambuc ATF_TC_BODY(sin_zero_pos, tc)
14011be35a1SLionel Sambuc {
14111be35a1SLionel Sambuc const double x = 0.0L;
14211be35a1SLionel Sambuc
14311be35a1SLionel Sambuc ATF_CHECK(sin(x) == x);
14411be35a1SLionel Sambuc }
14511be35a1SLionel Sambuc
14611be35a1SLionel Sambuc /*
14711be35a1SLionel Sambuc * sinf(3)
14811be35a1SLionel Sambuc */
14911be35a1SLionel Sambuc ATF_TC(sinf_angles);
ATF_TC_HEAD(sinf_angles,tc)15011be35a1SLionel Sambuc ATF_TC_HEAD(sinf_angles, tc)
15111be35a1SLionel Sambuc {
15211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test some selected angles");
15311be35a1SLionel Sambuc }
15411be35a1SLionel Sambuc
ATF_TC_BODY(sinf_angles,tc)15511be35a1SLionel Sambuc ATF_TC_BODY(sinf_angles, tc)
15611be35a1SLionel Sambuc {
15711be35a1SLionel Sambuc const float eps = 1.0e-6;
15811be35a1SLionel Sambuc float x, y;
15911be35a1SLionel Sambuc size_t i;
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc for (i = 0; i < __arraycount(angles); i++) {
16211be35a1SLionel Sambuc
16311be35a1SLionel Sambuc x = angles[i].x;
16411be35a1SLionel Sambuc y = angles[i].y;
16511be35a1SLionel Sambuc
16611be35a1SLionel Sambuc if (fabsf(sinf(x) - y) > eps)
16711be35a1SLionel Sambuc atf_tc_fail_nonfatal("sinf(%d deg) != %0.01f",
16811be35a1SLionel Sambuc angles[i].angle, angles[i].y);
16911be35a1SLionel Sambuc }
17011be35a1SLionel Sambuc }
17111be35a1SLionel Sambuc
17211be35a1SLionel Sambuc ATF_TC(sinf_nan);
ATF_TC_HEAD(sinf_nan,tc)17311be35a1SLionel Sambuc ATF_TC_HEAD(sinf_nan, tc)
17411be35a1SLionel Sambuc {
17511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sinf(NaN) == NaN");
17611be35a1SLionel Sambuc }
17711be35a1SLionel Sambuc
ATF_TC_BODY(sinf_nan,tc)17811be35a1SLionel Sambuc ATF_TC_BODY(sinf_nan, tc)
17911be35a1SLionel Sambuc {
18011be35a1SLionel Sambuc const float x = 0.0L / 0.0L;
18111be35a1SLionel Sambuc
18211be35a1SLionel Sambuc ATF_CHECK(isnan(x) != 0);
18311be35a1SLionel Sambuc ATF_CHECK(isnan(sinf(x)) != 0);
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc
18611be35a1SLionel Sambuc ATF_TC(sinf_inf_neg);
ATF_TC_HEAD(sinf_inf_neg,tc)18711be35a1SLionel Sambuc ATF_TC_HEAD(sinf_inf_neg, tc)
18811be35a1SLionel Sambuc {
18911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sinf(-Inf) == NaN");
19011be35a1SLionel Sambuc }
19111be35a1SLionel Sambuc
ATF_TC_BODY(sinf_inf_neg,tc)19211be35a1SLionel Sambuc ATF_TC_BODY(sinf_inf_neg, tc)
19311be35a1SLionel Sambuc {
19411be35a1SLionel Sambuc const float x = -1.0L / 0.0L;
19511be35a1SLionel Sambuc
19611be35a1SLionel Sambuc if (isnan(sinf(x)) == 0) {
19711be35a1SLionel Sambuc atf_tc_expect_fail("PR lib/45362");
19811be35a1SLionel Sambuc atf_tc_fail("sinf(-Inf) != NaN");
19911be35a1SLionel Sambuc }
20011be35a1SLionel Sambuc }
20111be35a1SLionel Sambuc
20211be35a1SLionel Sambuc ATF_TC(sinf_inf_pos);
ATF_TC_HEAD(sinf_inf_pos,tc)20311be35a1SLionel Sambuc ATF_TC_HEAD(sinf_inf_pos, tc)
20411be35a1SLionel Sambuc {
20511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sinf(+Inf) == NaN");
20611be35a1SLionel Sambuc }
20711be35a1SLionel Sambuc
ATF_TC_BODY(sinf_inf_pos,tc)20811be35a1SLionel Sambuc ATF_TC_BODY(sinf_inf_pos, tc)
20911be35a1SLionel Sambuc {
21011be35a1SLionel Sambuc const float x = 1.0L / 0.0L;
21111be35a1SLionel Sambuc
21211be35a1SLionel Sambuc if (isnan(sinf(x)) == 0) {
21311be35a1SLionel Sambuc atf_tc_expect_fail("PR lib/45362");
21411be35a1SLionel Sambuc atf_tc_fail("sinf(+Inf) != NaN");
21511be35a1SLionel Sambuc }
21611be35a1SLionel Sambuc }
21711be35a1SLionel Sambuc
21811be35a1SLionel Sambuc
21911be35a1SLionel Sambuc ATF_TC(sinf_zero_neg);
ATF_TC_HEAD(sinf_zero_neg,tc)22011be35a1SLionel Sambuc ATF_TC_HEAD(sinf_zero_neg, tc)
22111be35a1SLionel Sambuc {
22211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sinf(-0.0) == -0.0");
22311be35a1SLionel Sambuc }
22411be35a1SLionel Sambuc
ATF_TC_BODY(sinf_zero_neg,tc)22511be35a1SLionel Sambuc ATF_TC_BODY(sinf_zero_neg, tc)
22611be35a1SLionel Sambuc {
22711be35a1SLionel Sambuc const float x = -0.0L;
22811be35a1SLionel Sambuc
22911be35a1SLionel Sambuc ATF_CHECK(sinf(x) == x);
23011be35a1SLionel Sambuc }
23111be35a1SLionel Sambuc
23211be35a1SLionel Sambuc ATF_TC(sinf_zero_pos);
ATF_TC_HEAD(sinf_zero_pos,tc)23311be35a1SLionel Sambuc ATF_TC_HEAD(sinf_zero_pos, tc)
23411be35a1SLionel Sambuc {
23511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test sinf(+0.0) == +0.0");
23611be35a1SLionel Sambuc }
23711be35a1SLionel Sambuc
ATF_TC_BODY(sinf_zero_pos,tc)23811be35a1SLionel Sambuc ATF_TC_BODY(sinf_zero_pos, tc)
23911be35a1SLionel Sambuc {
24011be35a1SLionel Sambuc const float x = 0.0L;
24111be35a1SLionel Sambuc
24211be35a1SLionel Sambuc ATF_CHECK(sinf(x) == x);
24311be35a1SLionel Sambuc }
24411be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)24511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
24611be35a1SLionel Sambuc {
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sin_angles);
24911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sin_nan);
25011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sin_inf_neg);
25111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sin_inf_pos);
25211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sin_zero_neg);
25311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sin_zero_pos);
25411be35a1SLionel Sambuc
25511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sinf_angles);
25611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sinf_nan);
25711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sinf_inf_neg);
25811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sinf_inf_pos);
25911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sinf_zero_neg);
26011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sinf_zero_pos);
26111be35a1SLionel Sambuc
26211be35a1SLionel Sambuc return atf_no_error();
26311be35a1SLionel Sambuc }
264