xref: /netbsd-src/tests/lib/libm/t_errhandling.c (revision 90147b4ca2e501427933a54a6ca6ee4770831470)
1*90147b4cSriastradh /*	$NetBSD: t_errhandling.c,v 1.3 2024/09/10 17:36:12 riastradh Exp $	*/
21f22a48bSriastradh 
31f22a48bSriastradh /*-
41f22a48bSriastradh  * Copyright (c) 2024 The NetBSD Foundation, Inc.
51f22a48bSriastradh  * All rights reserved.
61f22a48bSriastradh  *
71f22a48bSriastradh  * Redistribution and use in source and binary forms, with or without
81f22a48bSriastradh  * modification, are permitted provided that the following conditions
91f22a48bSriastradh  * are met:
101f22a48bSriastradh  * 1. Redistributions of source code must retain the above copyright
111f22a48bSriastradh  *    notice, this list of conditions and the following disclaimer.
121f22a48bSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
131f22a48bSriastradh  *    notice, this list of conditions and the following disclaimer in the
141f22a48bSriastradh  *    documentation and/or other materials provided with the distribution.
151f22a48bSriastradh  *
161f22a48bSriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171f22a48bSriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181f22a48bSriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191f22a48bSriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201f22a48bSriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211f22a48bSriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221f22a48bSriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231f22a48bSriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241f22a48bSriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251f22a48bSriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261f22a48bSriastradh  * POSSIBILITY OF SUCH DAMAGE.
271f22a48bSriastradh  */
281f22a48bSriastradh 
29*90147b4cSriastradh #define	__TEST_FENV
30*90147b4cSriastradh 
311f22a48bSriastradh #include <sys/cdefs.h>
32*90147b4cSriastradh __RCSID("$NetBSD: t_errhandling.c,v 1.3 2024/09/10 17:36:12 riastradh Exp $");
331f22a48bSriastradh 
341f22a48bSriastradh #include <atf-c.h>
351f22a48bSriastradh #include <errno.h>
361f22a48bSriastradh #include <fenv.h>
371f22a48bSriastradh #include <math.h>
381f22a48bSriastradh 
391f22a48bSriastradh ATF_TC(log);
401f22a48bSriastradh ATF_TC_HEAD(log, tc)
411f22a48bSriastradh {
421f22a48bSriastradh 	atf_tc_set_md_var(tc, "descr", "log of invalid");
431f22a48bSriastradh }
441f22a48bSriastradh ATF_TC_BODY(log, tc)
451f22a48bSriastradh {
461f22a48bSriastradh 	static const struct {
471f22a48bSriastradh #ifdef __HAVE_FENV
481f22a48bSriastradh 		double x;
491f22a48bSriastradh 		int e;
501f22a48bSriastradh #define	C(x, e)		{ x, e }
511f22a48bSriastradh #else
521f22a48bSriastradh 		double x;
531f22a48bSriastradh #define	C(x, e)		{ x }
541f22a48bSriastradh #endif
551f22a48bSriastradh 	} cases[] = {
561f22a48bSriastradh 		C(0, FE_DIVBYZERO),
571f22a48bSriastradh 		C(-0., FE_DIVBYZERO),
581f22a48bSriastradh 		C(-1, FE_INVALID),
591f22a48bSriastradh 		C(-HUGE_VAL, FE_INVALID),
601f22a48bSriastradh 	};
611f22a48bSriastradh 	volatile double y;
621f22a48bSriastradh #ifdef __HAVE_FENV
631f22a48bSriastradh 	int except;
641f22a48bSriastradh #endif
651f22a48bSriastradh 	unsigned i;
661f22a48bSriastradh 
671f22a48bSriastradh 	for (i = 0; i < __arraycount(cases); i++) {
681f22a48bSriastradh 		const volatile double x = cases[i].x;
691f22a48bSriastradh 
701f22a48bSriastradh #ifdef __HAVE_FENV
711f22a48bSriastradh 		feclearexcept(FE_ALL_EXCEPT);
721f22a48bSriastradh #endif
731f22a48bSriastradh 		errno = 0;
741f22a48bSriastradh 		y = log(x);
751f22a48bSriastradh 		if (math_errhandling & MATH_ERREXCEPT) {
761f22a48bSriastradh #ifdef __HAVE_FENV
771f22a48bSriastradh 			ATF_CHECK_MSG(((except = fetestexcept(FE_ALL_EXCEPT)) &
781f22a48bSriastradh 				cases[i].e) != 0,
791f22a48bSriastradh 			    "expected=0x%x actual=0x%x", cases[i].e, except);
801f22a48bSriastradh #else
811f22a48bSriastradh 			atf_tc_fail_nonfatal("MATH_ERREXCEPT but no fenv.h");
821f22a48bSriastradh #endif
831f22a48bSriastradh 		}
841f22a48bSriastradh 		if (math_errhandling & MATH_ERRNO)
851f22a48bSriastradh 			ATF_CHECK_EQ_MSG(errno, EDOM, "errno=%d", errno);
861f22a48bSriastradh 	}
871f22a48bSriastradh 
881f22a48bSriastradh 	__USE(y);
891f22a48bSriastradh }
901f22a48bSriastradh 
911f22a48bSriastradh ATF_TP_ADD_TCS(tp)
921f22a48bSriastradh {
931f22a48bSriastradh 
941f22a48bSriastradh 	ATF_TP_ADD_TC(tp, log);
951f22a48bSriastradh 
961f22a48bSriastradh 	return atf_no_error();
971f22a48bSriastradh }
98