1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_cosh.c,v 1.6 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 #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_cosh.c,v 1.6 2014/03/03 10:39:08 martin Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <atf-c.h>
3511be35a1SLionel Sambuc #include <math.h>
3611be35a1SLionel Sambuc #include <stdio.h>
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc static const struct {
3911be35a1SLionel Sambuc double x;
4011be35a1SLionel Sambuc double y;
4111be35a1SLionel Sambuc double e;
4211be35a1SLionel Sambuc } values[] = {
4311be35a1SLionel Sambuc { -10, 11013.23292010332, 1e4, },
4411be35a1SLionel Sambuc { -2, 3.762195691083631, 1, },
4511be35a1SLionel Sambuc { -1, 1.543080634815244, 1, },
4611be35a1SLionel Sambuc { -0.05, 1.001250260438369, 1, },
4711be35a1SLionel Sambuc { -0.001, 1.000000500000042, 1, },
4811be35a1SLionel Sambuc { 0, 1, 1, },
4911be35a1SLionel Sambuc { 0.001, 1.000000500000042, 1, },
5011be35a1SLionel Sambuc { 0.05, 1.001250260438369, 1, },
5111be35a1SLionel Sambuc { 1, 1.543080634815244, 1, },
5211be35a1SLionel Sambuc { 2, 3.762195691083631, 1, },
5311be35a1SLionel Sambuc { 10, 11013.23292010332, 1e4, },
5411be35a1SLionel Sambuc };
5511be35a1SLionel Sambuc
5611be35a1SLionel Sambuc /*
5711be35a1SLionel Sambuc * cosh(3)
5811be35a1SLionel Sambuc */
5911be35a1SLionel Sambuc ATF_TC(cosh_inrange);
ATF_TC_HEAD(cosh_inrange,tc)6011be35a1SLionel Sambuc ATF_TC_HEAD(cosh_inrange, tc)
6111be35a1SLionel Sambuc {
6211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "cosh(x) for some values");
6311be35a1SLionel Sambuc }
6411be35a1SLionel Sambuc
ATF_TC_BODY(cosh_inrange,tc)6511be35a1SLionel Sambuc ATF_TC_BODY(cosh_inrange, tc)
6611be35a1SLionel Sambuc {
6711be35a1SLionel Sambuc double eps;
6811be35a1SLionel Sambuc double x;
6911be35a1SLionel Sambuc double y;
7011be35a1SLionel Sambuc size_t i;
7111be35a1SLionel Sambuc
7211be35a1SLionel Sambuc for (i = 0; i < __arraycount(values); i++) {
7311be35a1SLionel Sambuc x = values[i].x;
7411be35a1SLionel Sambuc y = values[i].y;
7511be35a1SLionel Sambuc eps = 1e-15 * values[i].e;
7611be35a1SLionel Sambuc
7711be35a1SLionel Sambuc if (fabs(cosh(x) - y) > eps)
7811be35a1SLionel Sambuc atf_tc_fail_nonfatal("cosh(%g) != %g\n", x, y);
7911be35a1SLionel Sambuc }
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc ATF_TC(cosh_nan);
ATF_TC_HEAD(cosh_nan,tc)8311be35a1SLionel Sambuc ATF_TC_HEAD(cosh_nan, tc)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test cosh(NaN) == NaN");
8611be35a1SLionel Sambuc }
8711be35a1SLionel Sambuc
ATF_TC_BODY(cosh_nan,tc)8811be35a1SLionel Sambuc ATF_TC_BODY(cosh_nan, tc)
8911be35a1SLionel Sambuc {
9011be35a1SLionel Sambuc const double x = 0.0L / 0.0L;
9111be35a1SLionel Sambuc
9211be35a1SLionel Sambuc ATF_CHECK(isnan(x) != 0);
9311be35a1SLionel Sambuc ATF_CHECK(isnan(cosh(x)) != 0);
9411be35a1SLionel Sambuc }
9511be35a1SLionel Sambuc
9611be35a1SLionel Sambuc ATF_TC(cosh_inf_neg);
ATF_TC_HEAD(cosh_inf_neg,tc)9711be35a1SLionel Sambuc ATF_TC_HEAD(cosh_inf_neg, tc)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test cosh(-Inf) == +Inf");
10011be35a1SLionel Sambuc }
10111be35a1SLionel Sambuc
ATF_TC_BODY(cosh_inf_neg,tc)10211be35a1SLionel Sambuc ATF_TC_BODY(cosh_inf_neg, tc)
10311be35a1SLionel Sambuc {
10411be35a1SLionel Sambuc const double x = -1.0L / 0.0L;
10511be35a1SLionel Sambuc double y = cosh(x);
10611be35a1SLionel Sambuc
10711be35a1SLionel Sambuc ATF_CHECK(isinf(y) != 0);
10811be35a1SLionel Sambuc ATF_CHECK(signbit(y) == 0);
10911be35a1SLionel Sambuc }
11011be35a1SLionel Sambuc
11111be35a1SLionel Sambuc ATF_TC(cosh_inf_pos);
ATF_TC_HEAD(cosh_inf_pos,tc)11211be35a1SLionel Sambuc ATF_TC_HEAD(cosh_inf_pos, tc)
11311be35a1SLionel Sambuc {
11411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test cosh(+Inf) == +Inf");
11511be35a1SLionel Sambuc }
11611be35a1SLionel Sambuc
ATF_TC_BODY(cosh_inf_pos,tc)11711be35a1SLionel Sambuc ATF_TC_BODY(cosh_inf_pos, tc)
11811be35a1SLionel Sambuc {
11911be35a1SLionel Sambuc const double x = 1.0L / 0.0L;
12011be35a1SLionel Sambuc double y = cosh(x);
12111be35a1SLionel Sambuc
12211be35a1SLionel Sambuc ATF_CHECK(isinf(y) != 0);
12311be35a1SLionel Sambuc ATF_CHECK(signbit(y) == 0);
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc ATF_TC(cosh_zero_neg);
ATF_TC_HEAD(cosh_zero_neg,tc)12711be35a1SLionel Sambuc ATF_TC_HEAD(cosh_zero_neg, tc)
12811be35a1SLionel Sambuc {
12911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test cosh(-0.0) == 1.0");
13011be35a1SLionel Sambuc }
13111be35a1SLionel Sambuc
ATF_TC_BODY(cosh_zero_neg,tc)13211be35a1SLionel Sambuc ATF_TC_BODY(cosh_zero_neg, tc)
13311be35a1SLionel Sambuc {
13411be35a1SLionel Sambuc const double x = -0.0L;
13511be35a1SLionel Sambuc
13611be35a1SLionel Sambuc if (cosh(x) != 1.0)
13711be35a1SLionel Sambuc atf_tc_fail_nonfatal("cosh(-0.0) != 1.0");
13811be35a1SLionel Sambuc }
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc ATF_TC(cosh_zero_pos);
ATF_TC_HEAD(cosh_zero_pos,tc)14111be35a1SLionel Sambuc ATF_TC_HEAD(cosh_zero_pos, tc)
14211be35a1SLionel Sambuc {
14311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test cosh(+0.0) == 1.0");
14411be35a1SLionel Sambuc }
14511be35a1SLionel Sambuc
ATF_TC_BODY(cosh_zero_pos,tc)14611be35a1SLionel Sambuc ATF_TC_BODY(cosh_zero_pos, tc)
14711be35a1SLionel Sambuc {
14811be35a1SLionel Sambuc const double x = 0.0L;
14911be35a1SLionel Sambuc
15011be35a1SLionel Sambuc if (cosh(x) != 1.0)
15111be35a1SLionel Sambuc atf_tc_fail_nonfatal("cosh(+0.0) != 1.0");
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc /*
15511be35a1SLionel Sambuc * coshf(3)
15611be35a1SLionel Sambuc */
15711be35a1SLionel Sambuc ATF_TC(coshf_inrange);
ATF_TC_HEAD(coshf_inrange,tc)15811be35a1SLionel Sambuc ATF_TC_HEAD(coshf_inrange, tc)
15911be35a1SLionel Sambuc {
16011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "coshf(x) for some values");
16111be35a1SLionel Sambuc }
16211be35a1SLionel Sambuc
ATF_TC_BODY(coshf_inrange,tc)16311be35a1SLionel Sambuc ATF_TC_BODY(coshf_inrange, tc)
16411be35a1SLionel Sambuc {
16511be35a1SLionel Sambuc float eps;
16611be35a1SLionel Sambuc float x;
16711be35a1SLionel Sambuc float y;
16811be35a1SLionel Sambuc size_t i;
16911be35a1SLionel Sambuc
17011be35a1SLionel Sambuc for (i = 0; i < __arraycount(values); i++) {
17111be35a1SLionel Sambuc x = values[i].x;
17211be35a1SLionel Sambuc y = values[i].y;
17311be35a1SLionel Sambuc eps = 1e-6 * values[i].e;
17411be35a1SLionel Sambuc
17511be35a1SLionel Sambuc if (fabsf(coshf(x) - y) > eps)
17611be35a1SLionel Sambuc atf_tc_fail_nonfatal("coshf(%g) != %g\n", x, y);
17711be35a1SLionel Sambuc }
17811be35a1SLionel Sambuc }
17911be35a1SLionel Sambuc
18011be35a1SLionel Sambuc ATF_TC(coshf_nan);
ATF_TC_HEAD(coshf_nan,tc)18111be35a1SLionel Sambuc ATF_TC_HEAD(coshf_nan, tc)
18211be35a1SLionel Sambuc {
18311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test coshf(NaN) == NaN");
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc
ATF_TC_BODY(coshf_nan,tc)18611be35a1SLionel Sambuc ATF_TC_BODY(coshf_nan, tc)
18711be35a1SLionel Sambuc {
18811be35a1SLionel Sambuc const float x = 0.0L / 0.0L;
18911be35a1SLionel Sambuc
19011be35a1SLionel Sambuc ATF_CHECK(isnan(x) != 0);
19111be35a1SLionel Sambuc ATF_CHECK(isnan(coshf(x)) != 0);
19211be35a1SLionel Sambuc }
19311be35a1SLionel Sambuc
19411be35a1SLionel Sambuc ATF_TC(coshf_inf_neg);
ATF_TC_HEAD(coshf_inf_neg,tc)19511be35a1SLionel Sambuc ATF_TC_HEAD(coshf_inf_neg, tc)
19611be35a1SLionel Sambuc {
19711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test coshf(-Inf) == +Inf");
19811be35a1SLionel Sambuc }
19911be35a1SLionel Sambuc
ATF_TC_BODY(coshf_inf_neg,tc)20011be35a1SLionel Sambuc ATF_TC_BODY(coshf_inf_neg, tc)
20111be35a1SLionel Sambuc {
20211be35a1SLionel Sambuc const float x = -1.0L / 0.0L;
20311be35a1SLionel Sambuc float y = coshf(x);
20411be35a1SLionel Sambuc
20511be35a1SLionel Sambuc ATF_CHECK(isinf(y) != 0);
20611be35a1SLionel Sambuc ATF_CHECK(signbit(y) == 0);
20711be35a1SLionel Sambuc }
20811be35a1SLionel Sambuc
20911be35a1SLionel Sambuc ATF_TC(coshf_inf_pos);
ATF_TC_HEAD(coshf_inf_pos,tc)21011be35a1SLionel Sambuc ATF_TC_HEAD(coshf_inf_pos, tc)
21111be35a1SLionel Sambuc {
21211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test coshf(+Inf) == +Inf");
21311be35a1SLionel Sambuc }
21411be35a1SLionel Sambuc
ATF_TC_BODY(coshf_inf_pos,tc)21511be35a1SLionel Sambuc ATF_TC_BODY(coshf_inf_pos, tc)
21611be35a1SLionel Sambuc {
21711be35a1SLionel Sambuc const float x = 1.0L / 0.0L;
21811be35a1SLionel Sambuc float y = coshf(x);
21911be35a1SLionel Sambuc
22011be35a1SLionel Sambuc ATF_CHECK(isinf(y) != 0);
22111be35a1SLionel Sambuc ATF_CHECK(signbit(y) == 0);
22211be35a1SLionel Sambuc }
22311be35a1SLionel Sambuc
22411be35a1SLionel Sambuc ATF_TC(coshf_zero_neg);
ATF_TC_HEAD(coshf_zero_neg,tc)22511be35a1SLionel Sambuc ATF_TC_HEAD(coshf_zero_neg, tc)
22611be35a1SLionel Sambuc {
22711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test coshf(-0.0) == 1.0");
22811be35a1SLionel Sambuc }
22911be35a1SLionel Sambuc
ATF_TC_BODY(coshf_zero_neg,tc)23011be35a1SLionel Sambuc ATF_TC_BODY(coshf_zero_neg, tc)
23111be35a1SLionel Sambuc {
23211be35a1SLionel Sambuc const float x = -0.0L;
23311be35a1SLionel Sambuc
23411be35a1SLionel Sambuc if (coshf(x) != 1.0)
23511be35a1SLionel Sambuc atf_tc_fail_nonfatal("coshf(-0.0) != 1.0");
23611be35a1SLionel Sambuc }
23711be35a1SLionel Sambuc
23811be35a1SLionel Sambuc ATF_TC(coshf_zero_pos);
ATF_TC_HEAD(coshf_zero_pos,tc)23911be35a1SLionel Sambuc ATF_TC_HEAD(coshf_zero_pos, tc)
24011be35a1SLionel Sambuc {
24111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test coshf(+0.0) == 1.0");
24211be35a1SLionel Sambuc }
24311be35a1SLionel Sambuc
ATF_TC_BODY(coshf_zero_pos,tc)24411be35a1SLionel Sambuc ATF_TC_BODY(coshf_zero_pos, tc)
24511be35a1SLionel Sambuc {
24611be35a1SLionel Sambuc const float x = 0.0L;
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc if (coshf(x) != 1.0)
24911be35a1SLionel Sambuc atf_tc_fail_nonfatal("coshf(+0.0) != 1.0");
25011be35a1SLionel Sambuc }
25111be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)25211be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
25311be35a1SLionel Sambuc {
25411be35a1SLionel Sambuc
25511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cosh_inrange);
25611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cosh_nan);
25711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cosh_inf_neg);
25811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cosh_inf_pos);
25911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cosh_zero_neg);
26011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cosh_zero_pos);
26111be35a1SLionel Sambuc
26211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, coshf_inrange);
26311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, coshf_nan);
26411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, coshf_inf_neg);
26511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, coshf_inf_pos);
26611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, coshf_zero_neg);
26711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, coshf_zero_pos);
26811be35a1SLionel Sambuc
26911be35a1SLionel Sambuc return atf_no_error();
27011be35a1SLionel Sambuc }
271