11.11Sriastrad/* $NetBSD: t_acos.c,v 1.11 2018/11/07 03:59:36 riastradh 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.9Sdsl#include "t_libm.h"
351.4Sisaki
361.1Sjruoho/*
371.5Sdsl * acos(3) and acosf(3)
381.1Sjruoho */
391.1Sjruoho
401.10SdslATF_LIBM_TEST(acos_is_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]")
411.1Sjruoho{
421.5Sdsl	static const double x[] = {
431.5Sdsl	    -1.000000001, 1.000000001,
441.5Sdsl	    -1.0000001, 1.0000001,
451.5Sdsl	    -1.1, 1.1,
461.7Smartin#ifndef __vax__
471.9Sdsl	    T_LIBM_NAN,
481.9Sdsl	    T_LIBM_MINUS_INF, T_LIBM_PLUS_INF,
491.7Smartin#endif
501.5Sdsl	};
511.9Sdsl	unsigned int i;
521.1Sjruoho
531.1Sjruoho	for (i = 0; i < __arraycount(x); i++) {
541.6Sdsl		T_LIBM_CHECK_NAN(i, acos, x[i]);
551.5Sdsl		if (i < 2)
561.5Sdsl			/* Values are too small for float */
571.5Sdsl			continue;
581.6Sdsl		T_LIBM_CHECK_NAN(i, acosf, x[i]);
591.1Sjruoho	}
601.1Sjruoho}
611.1Sjruoho
621.10SdslATF_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values")
631.1Sjruoho{
641.5Sdsl	static const struct {
651.5Sdsl		double x;
661.5Sdsl		double y;
671.5Sdsl	} values[] = {
681.5Sdsl		{ -1,    M_PI,              },
691.5Sdsl		{ -0.99, 3.000053180265366, },
701.5Sdsl		{ -0.5,  2.094395102393195, },
711.5Sdsl		{ -0.1,  1.670963747956456, },
721.5Sdsl		{  0,    M_PI / 2,          },
731.5Sdsl		{  0.1,  1.470628905633337, },
741.5Sdsl		{  0.5,  1.047197551196598, },
751.11Sriastrad		{  0.99, 0.1415394733244273, },
761.5Sdsl	};
771.9Sdsl	unsigned int i;
781.1Sjruoho
791.5Sdsl	/*
801.6Sdsl	 * Note that acos(x) might be calculated as atan2(sqrt(1-x*x),x).
811.6Sdsl	 * This means that acos(-1) is atan2(+0,-1), if the sign is wrong
821.6Sdsl	 * the value will be -M_PI (atan2(-0,-1)) not M_PI.
831.5Sdsl	 */
841.1Sjruoho
851.4Sisaki	for (i = 0; i < __arraycount(values); i++) {
861.6Sdsl		T_LIBM_CHECK(i, acos, values[i].x, values[i].y, 1.0e-15);
871.6Sdsl		T_LIBM_CHECK(i, acosf, values[i].x, values[i].y, 1.0e-5);
881.1Sjruoho	}
891.1Sjruoho}
901.1Sjruoho
911.10SdslATF_LIBM_TEST(acos_is_plus_zero, "Test acosf(1.0) == +0.0")
921.1Sjruoho{
931.9Sdsl	T_LIBM_CHECK_PLUS_ZERO(0, acos, 1.0);
941.9Sdsl	T_LIBM_CHECK_PLUS_ZERO(0, acosf, 1.0);
951.1Sjruoho}
961.1Sjruoho
971.1SjruohoATF_TP_ADD_TCS(tp)
981.1Sjruoho{
991.1Sjruoho
1001.5Sdsl	ATF_TP_ADD_TC(tp, acos_inrange);
1011.9Sdsl	ATF_TP_ADD_TC(tp, acos_is_nan);
1021.9Sdsl	ATF_TP_ADD_TC(tp, acos_is_plus_zero);
1031.1Sjruoho
1041.1Sjruoho	return atf_no_error();
1051.1Sjruoho}
106