t_acos.c revision 1.5
11.5Sdsl/* $NetBSD: t_acos.c,v 1.5 2014/03/01 21:08:39 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.1Sjruoho
351.5Sdsl/*
361.5Sdsl * Check result of fn(arg) is correct within the bounds.
371.5Sdsl * Should be ok to do the checks using 'double' for 'float' functions.
381.5Sdsl */
391.5Sdsl#define T_LIBM_CHECK(fn, arg, expect, epsilon) do { \
401.5Sdsl	double r = fn(arg); \
411.5Sdsl	double e = fabs(r - expect); \
421.5Sdsl	if (e > epsilon) \
431.5Sdsl		atf_tc_fail_nonfatal(#fn "(%g) is %g not %g (error %g > %g)", \
441.5Sdsl			arg, r, expect, e, epsilon); \
451.5Sdsl    } while (0)
461.5Sdsl
471.5Sdsl/* Check that the result of fn(arg) is NaN */
481.5Sdsl#ifndef __vax__
491.5Sdsl#define T_LIBM_CHECK_NAN(fn, arg) do { \
501.5Sdsl	double r = fn(arg); \
511.5Sdsl	if (!isnan(r)) \
521.5Sdsl		atf_tc_fail_nonfatal(#fn "(%g) is %g not NaN", arg, r); \
531.5Sdsl    } while (0)
541.5Sdsl#else
551.5Sdsl/* vax doesn't support NaN */
561.5Sdsl#define T_LIBM_CHECK_NAN(fn, arg) (void)(arg)
571.5Sdsl#endif
581.5Sdsl
591.5Sdsl#define AFT_LIBM_TEST(name, description) \
601.5SdslATF_TC(name); \
611.5SdslATF_TC_HEAD(name, tc) { atf_tc_set_md_var(tc, "descr", description); } \
621.5SdslATF_TC_BODY(name, tc)
631.4Sisaki
641.1Sjruoho/*
651.5Sdsl * acos(3) and acosf(3)
661.1Sjruoho */
671.1Sjruoho
681.5SdslAFT_LIBM_TEST(acos_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]")
691.1Sjruoho{
701.5Sdsl	static const double x[] = {
711.5Sdsl	    -1.000000001, 1.000000001,
721.5Sdsl	    -1.0000001, 1.0000001,
731.5Sdsl	    -1.1, 1.1,
741.5Sdsl	    0.0L / 0.0L,  /* NAN */
751.5Sdsl	    -1.0L / 0.0L, /* -Inf */
761.5Sdsl	    +1.0L / 0.0L, /* +Inf */
771.5Sdsl	};
781.1Sjruoho	size_t i;
791.1Sjruoho
801.1Sjruoho	for (i = 0; i < __arraycount(x); i++) {
811.5Sdsl		T_LIBM_CHECK_NAN(acos, x[i]);
821.5Sdsl		if (i < 2)
831.5Sdsl			/* Values are too small for float */
841.5Sdsl			continue;
851.5Sdsl		T_LIBM_CHECK_NAN(acosf, x[i]);
861.1Sjruoho	}
871.1Sjruoho}
881.1Sjruoho
891.5SdslAFT_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values")
901.1Sjruoho{
911.5Sdsl	static const struct {
921.5Sdsl		double x;
931.5Sdsl		double y;
941.5Sdsl	} values[] = {
951.5Sdsl		{ -1,    M_PI,              },
961.5Sdsl		{ -0.99, 3.000053180265366, },
971.5Sdsl		{ -0.5,  2.094395102393195, },
981.5Sdsl		{ -0.1,  1.670963747956456, },
991.5Sdsl		{  0,    M_PI / 2,          },
1001.5Sdsl		{  0.1,  1.470628905633337, },
1011.5Sdsl		{  0.5,  1.047197551196598, },
1021.5Sdsl		{  0.99, 0.141539473324427, },
1031.5Sdsl	};
1041.5Sdsl	size_t i;
1051.1Sjruoho
1061.5Sdsl	/*
1071.5Sdsl	 * Note that acos(x) might be calcualted as atan((1-x*x)/x).
1081.5Sdsl	 * This means that acos(-1) is atan(-0.0), if the sign is lost
1091.5Sdsl	 * the value will be 0 (atan(+0)) not M_PI.
1101.5Sdsl	 */
1111.1Sjruoho
1121.4Sisaki	for (i = 0; i < __arraycount(values); i++) {
1131.5Sdsl		T_LIBM_CHECK(acos, values[i].x, values[i].y, 1.0e-15);
1141.5Sdsl		T_LIBM_CHECK(acosf, values[i].x, values[i].y, 1.0e-5);
1151.1Sjruoho	}
1161.1Sjruoho}
1171.1Sjruoho
1181.5SdslAFT_LIBM_TEST(acos_one_pos, "Test acos(1.0) == +0.0")
1191.1Sjruoho{
1201.1Sjruoho#ifndef __vax__
1211.5Sdsl	const double y = acos(1.0);
1221.1Sjruoho
1231.5Sdsl	if (fabs(y) > 0.0 || signbit(y) != 0)
1241.5Sdsl		atf_tc_fail_nonfatal("acos(1.0) != +0.0");
1251.1Sjruoho#endif
1261.1Sjruoho}
1271.1Sjruoho
1281.5SdslAFT_LIBM_TEST(acosf_one_pos, "Test acosf(1.0) == +0.0")
1291.1Sjruoho{
1301.1Sjruoho#ifndef __vax__
1311.1Sjruoho	const float y = acosf(1.0);
1321.1Sjruoho
1331.1Sjruoho	if (fabsf(y) > 0.0 || signbit(y) != 0)
1341.1Sjruoho		atf_tc_fail_nonfatal("acosf(1.0) != +0.0");
1351.1Sjruoho#endif
1361.1Sjruoho}
1371.1Sjruoho
1381.1SjruohoATF_TP_ADD_TCS(tp)
1391.1Sjruoho{
1401.1Sjruoho
1411.1Sjruoho	ATF_TP_ADD_TC(tp, acos_nan);
1421.5Sdsl	ATF_TP_ADD_TC(tp, acos_inrange);
1431.1Sjruoho	ATF_TP_ADD_TC(tp, acos_one_pos);
1441.1Sjruoho	ATF_TP_ADD_TC(tp, acosf_one_pos);
1451.1Sjruoho
1461.1Sjruoho	return atf_no_error();
1471.1Sjruoho}
148