t_acos.c revision 1.7
11.7Smartin/* $NetBSD: t_acos.c,v 1.7 2014/03/03 10:38:36 martin Exp $ */
21.1Sjruoho
31.1Sjruoho/*-
41.1Sjruoho * Copyright (c) 2011 The NetBSD Foundation, Inc.
51.1Sjruoho * All rights reserved.
61.1Sjruoho *
71.1Sjruoho * This code is derived from software contributed to The NetBSD Foundation
81.1Sjruoho * by Jukka Ruohonen.
91.1Sjruoho *
101.1Sjruoho * Redistribution and use in source and binary forms, with or without
111.1Sjruoho * modification, are permitted provided that the following conditions
121.1Sjruoho * are met:
131.1Sjruoho * 1. Redistributions of source code must retain the above copyright
141.1Sjruoho *    notice, this list of conditions and the following disclaimer.
151.1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjruoho *    notice, this list of conditions and the following disclaimer in the
171.1Sjruoho *    documentation and/or other materials provided with the distribution.
181.1Sjruoho *
191.1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjruoho * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjruoho * POSSIBILITY OF SUCH DAMAGE.
301.1Sjruoho */
311.1Sjruoho
321.1Sjruoho#include <atf-c.h>
331.1Sjruoho#include <math.h>
341.7Smartin
351.7Smartin#ifdef HAVE_FENV_H
361.6Sdsl#include <fenv.h>
371.7Smartin#endif
381.1Sjruoho
391.5Sdsl/*
401.5Sdsl * Check result of fn(arg) is correct within the bounds.
411.5Sdsl * Should be ok to do the checks using 'double' for 'float' functions.
421.5Sdsl */
431.7Smartin#ifdef HAVE_FENV_H
441.6Sdsl#define T_LIBM_CHECK(subtest, fn, arg, expect, epsilon) do { \
451.5Sdsl	double r = fn(arg); \
461.5Sdsl	double e = fabs(r - expect); \
471.5Sdsl	if (e > epsilon) \
481.6Sdsl		atf_tc_fail_nonfatal( \
491.6Sdsl		    "subtest %zu: " #fn "(%g) is %g not %g (error %g > %g), roundmode %x", \
501.6Sdsl		    subtest, arg, r, expect, e, epsilon, fegetround()); \
511.5Sdsl    } while (0)
521.7Smartin#else
531.7Smartin#define T_LIBM_CHECK(subtest, fn, arg, expect, epsilon) do { \
541.7Smartin	double r = fn(arg); \
551.7Smartin	double e = fabs(r - expect); \
561.7Smartin	if (e > epsilon) \
571.7Smartin		atf_tc_fail_nonfatal( \
581.7Smartin		    "subtest %zu: " #fn "(%g) is %g not %g (error %g > %g)", \
591.7Smartin		    subtest, arg, r, expect, e, epsilon); \
601.7Smartin    } while (0)
611.7Smartin#endif
621.5Sdsl
631.5Sdsl/* Check that the result of fn(arg) is NaN */
641.5Sdsl#ifndef __vax__
651.6Sdsl#define T_LIBM_CHECK_NAN(subtest, fn, arg) do { \
661.5Sdsl	double r = fn(arg); \
671.5Sdsl	if (!isnan(r)) \
681.6Sdsl		atf_tc_fail_nonfatal("subtest %zu: " #fn "(%g) is %g not NaN", \
691.6Sdsl		    subtest, arg, r); \
701.5Sdsl    } while (0)
711.5Sdsl#else
721.5Sdsl/* vax doesn't support NaN */
731.6Sdsl#define T_LIBM_CHECK_NAN(subtest, fn, arg) (void)(arg)
741.5Sdsl#endif
751.5Sdsl
761.5Sdsl#define AFT_LIBM_TEST(name, description) \
771.5SdslATF_TC(name); \
781.5SdslATF_TC_HEAD(name, tc) { atf_tc_set_md_var(tc, "descr", description); } \
791.5SdslATF_TC_BODY(name, tc)
801.4Sisaki
811.1Sjruoho/*
821.5Sdsl * acos(3) and acosf(3)
831.1Sjruoho */
841.1Sjruoho
851.5SdslAFT_LIBM_TEST(acos_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]")
861.1Sjruoho{
871.5Sdsl	static const double x[] = {
881.5Sdsl	    -1.000000001, 1.000000001,
891.5Sdsl	    -1.0000001, 1.0000001,
901.5Sdsl	    -1.1, 1.1,
911.7Smartin#ifndef __vax__
921.5Sdsl	    0.0L / 0.0L,  /* NAN */
931.5Sdsl	    -1.0L / 0.0L, /* -Inf */
941.5Sdsl	    +1.0L / 0.0L, /* +Inf */
951.7Smartin#endif
961.5Sdsl	};
971.1Sjruoho	size_t i;
981.1Sjruoho
991.1Sjruoho	for (i = 0; i < __arraycount(x); i++) {
1001.6Sdsl		T_LIBM_CHECK_NAN(i, acos, x[i]);
1011.5Sdsl		if (i < 2)
1021.5Sdsl			/* Values are too small for float */
1031.5Sdsl			continue;
1041.6Sdsl		T_LIBM_CHECK_NAN(i, acosf, x[i]);
1051.1Sjruoho	}
1061.1Sjruoho}
1071.1Sjruoho
1081.5SdslAFT_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values")
1091.1Sjruoho{
1101.5Sdsl	static const struct {
1111.5Sdsl		double x;
1121.5Sdsl		double y;
1131.5Sdsl	} values[] = {
1141.5Sdsl		{ -1,    M_PI,              },
1151.5Sdsl		{ -0.99, 3.000053180265366, },
1161.5Sdsl		{ -0.5,  2.094395102393195, },
1171.5Sdsl		{ -0.1,  1.670963747956456, },
1181.5Sdsl		{  0,    M_PI / 2,          },
1191.5Sdsl		{  0.1,  1.470628905633337, },
1201.5Sdsl		{  0.5,  1.047197551196598, },
1211.5Sdsl		{  0.99, 0.141539473324427, },
1221.5Sdsl	};
1231.5Sdsl	size_t i;
1241.1Sjruoho
1251.5Sdsl	/*
1261.6Sdsl	 * Note that acos(x) might be calculated as atan2(sqrt(1-x*x),x).
1271.6Sdsl	 * This means that acos(-1) is atan2(+0,-1), if the sign is wrong
1281.6Sdsl	 * the value will be -M_PI (atan2(-0,-1)) not M_PI.
1291.5Sdsl	 */
1301.1Sjruoho
1311.4Sisaki	for (i = 0; i < __arraycount(values); i++) {
1321.6Sdsl		T_LIBM_CHECK(i, acos, values[i].x, values[i].y, 1.0e-15);
1331.6Sdsl		T_LIBM_CHECK(i, acosf, values[i].x, values[i].y, 1.0e-5);
1341.1Sjruoho	}
1351.1Sjruoho}
1361.1Sjruoho
1371.5SdslAFT_LIBM_TEST(acos_one_pos, "Test acos(1.0) == +0.0")
1381.1Sjruoho{
1391.5Sdsl	const double y = acos(1.0);
1401.1Sjruoho
1411.5Sdsl	if (fabs(y) > 0.0 || signbit(y) != 0)
1421.5Sdsl		atf_tc_fail_nonfatal("acos(1.0) != +0.0");
1431.1Sjruoho}
1441.1Sjruoho
1451.5SdslAFT_LIBM_TEST(acosf_one_pos, "Test acosf(1.0) == +0.0")
1461.1Sjruoho{
1471.1Sjruoho	const float y = acosf(1.0);
1481.1Sjruoho
1491.1Sjruoho	if (fabsf(y) > 0.0 || signbit(y) != 0)
1501.1Sjruoho		atf_tc_fail_nonfatal("acosf(1.0) != +0.0");
1511.1Sjruoho}
1521.1Sjruoho
1531.1SjruohoATF_TP_ADD_TCS(tp)
1541.1Sjruoho{
1551.1Sjruoho
1561.1Sjruoho	ATF_TP_ADD_TC(tp, acos_nan);
1571.5Sdsl	ATF_TP_ADD_TC(tp, acos_inrange);
1581.1Sjruoho	ATF_TP_ADD_TC(tp, acos_one_pos);
1591.1Sjruoho	ATF_TP_ADD_TC(tp, acosf_one_pos);
1601.1Sjruoho
1611.1Sjruoho	return atf_no_error();
1621.1Sjruoho}
163