t_acos.c revision 1.6
11.6Sdsl/* $NetBSD: t_acos.c,v 1.6 2014/03/02 22:40:45 dsl 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.6Sdsl#include <fenv.h>
351.1Sjruoho
361.5Sdsl/*
371.5Sdsl * Check result of fn(arg) is correct within the bounds.
381.5Sdsl * Should be ok to do the checks using 'double' for 'float' functions.
391.5Sdsl */
401.6Sdsl#define T_LIBM_CHECK(subtest, fn, arg, expect, epsilon) do { \
411.5Sdsl	double r = fn(arg); \
421.5Sdsl	double e = fabs(r - expect); \
431.5Sdsl	if (e > epsilon) \
441.6Sdsl		atf_tc_fail_nonfatal( \
451.6Sdsl		    "subtest %zu: " #fn "(%g) is %g not %g (error %g > %g), roundmode %x", \
461.6Sdsl		    subtest, arg, r, expect, e, epsilon, fegetround()); \
471.5Sdsl    } while (0)
481.5Sdsl
491.5Sdsl/* Check that the result of fn(arg) is NaN */
501.5Sdsl#ifndef __vax__
511.6Sdsl#define T_LIBM_CHECK_NAN(subtest, fn, arg) do { \
521.5Sdsl	double r = fn(arg); \
531.5Sdsl	if (!isnan(r)) \
541.6Sdsl		atf_tc_fail_nonfatal("subtest %zu: " #fn "(%g) is %g not NaN", \
551.6Sdsl		    subtest, arg, r); \
561.5Sdsl    } while (0)
571.5Sdsl#else
581.5Sdsl/* vax doesn't support NaN */
591.6Sdsl#define T_LIBM_CHECK_NAN(subtest, fn, arg) (void)(arg)
601.5Sdsl#endif
611.5Sdsl
621.5Sdsl#define AFT_LIBM_TEST(name, description) \
631.5SdslATF_TC(name); \
641.5SdslATF_TC_HEAD(name, tc) { atf_tc_set_md_var(tc, "descr", description); } \
651.5SdslATF_TC_BODY(name, tc)
661.4Sisaki
671.1Sjruoho/*
681.5Sdsl * acos(3) and acosf(3)
691.1Sjruoho */
701.1Sjruoho
711.5SdslAFT_LIBM_TEST(acos_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]")
721.1Sjruoho{
731.5Sdsl	static const double x[] = {
741.5Sdsl	    -1.000000001, 1.000000001,
751.5Sdsl	    -1.0000001, 1.0000001,
761.5Sdsl	    -1.1, 1.1,
771.5Sdsl	    0.0L / 0.0L,  /* NAN */
781.5Sdsl	    -1.0L / 0.0L, /* -Inf */
791.5Sdsl	    +1.0L / 0.0L, /* +Inf */
801.5Sdsl	};
811.1Sjruoho	size_t i;
821.1Sjruoho
831.1Sjruoho	for (i = 0; i < __arraycount(x); i++) {
841.6Sdsl		T_LIBM_CHECK_NAN(i, acos, x[i]);
851.5Sdsl		if (i < 2)
861.5Sdsl			/* Values are too small for float */
871.5Sdsl			continue;
881.6Sdsl		T_LIBM_CHECK_NAN(i, acosf, x[i]);
891.1Sjruoho	}
901.1Sjruoho}
911.1Sjruoho
921.5SdslAFT_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values")
931.1Sjruoho{
941.5Sdsl	static const struct {
951.5Sdsl		double x;
961.5Sdsl		double y;
971.5Sdsl	} values[] = {
981.5Sdsl		{ -1,    M_PI,              },
991.5Sdsl		{ -0.99, 3.000053180265366, },
1001.5Sdsl		{ -0.5,  2.094395102393195, },
1011.5Sdsl		{ -0.1,  1.670963747956456, },
1021.5Sdsl		{  0,    M_PI / 2,          },
1031.5Sdsl		{  0.1,  1.470628905633337, },
1041.5Sdsl		{  0.5,  1.047197551196598, },
1051.5Sdsl		{  0.99, 0.141539473324427, },
1061.5Sdsl	};
1071.5Sdsl	size_t i;
1081.1Sjruoho
1091.5Sdsl	/*
1101.6Sdsl	 * Note that acos(x) might be calculated as atan2(sqrt(1-x*x),x).
1111.6Sdsl	 * This means that acos(-1) is atan2(+0,-1), if the sign is wrong
1121.6Sdsl	 * the value will be -M_PI (atan2(-0,-1)) not M_PI.
1131.5Sdsl	 */
1141.1Sjruoho
1151.4Sisaki	for (i = 0; i < __arraycount(values); i++) {
1161.6Sdsl		T_LIBM_CHECK(i, acos, values[i].x, values[i].y, 1.0e-15);
1171.6Sdsl		T_LIBM_CHECK(i, acosf, values[i].x, values[i].y, 1.0e-5);
1181.1Sjruoho	}
1191.1Sjruoho}
1201.1Sjruoho
1211.5SdslAFT_LIBM_TEST(acos_one_pos, "Test acos(1.0) == +0.0")
1221.1Sjruoho{
1231.1Sjruoho#ifndef __vax__
1241.5Sdsl	const double y = acos(1.0);
1251.1Sjruoho
1261.5Sdsl	if (fabs(y) > 0.0 || signbit(y) != 0)
1271.5Sdsl		atf_tc_fail_nonfatal("acos(1.0) != +0.0");
1281.1Sjruoho#endif
1291.1Sjruoho}
1301.1Sjruoho
1311.5SdslAFT_LIBM_TEST(acosf_one_pos, "Test acosf(1.0) == +0.0")
1321.1Sjruoho{
1331.1Sjruoho#ifndef __vax__
1341.1Sjruoho	const float y = acosf(1.0);
1351.1Sjruoho
1361.1Sjruoho	if (fabsf(y) > 0.0 || signbit(y) != 0)
1371.1Sjruoho		atf_tc_fail_nonfatal("acosf(1.0) != +0.0");
1381.1Sjruoho#endif
1391.1Sjruoho}
1401.1Sjruoho
1411.1SjruohoATF_TP_ADD_TCS(tp)
1421.1Sjruoho{
1431.1Sjruoho
1441.1Sjruoho	ATF_TP_ADD_TC(tp, acos_nan);
1451.5Sdsl	ATF_TP_ADD_TC(tp, acos_inrange);
1461.1Sjruoho	ATF_TP_ADD_TC(tp, acos_one_pos);
1471.1Sjruoho	ATF_TP_ADD_TC(tp, acosf_one_pos);
1481.1Sjruoho
1491.1Sjruoho	return atf_no_error();
1501.1Sjruoho}
151