t_errhandling.c revision 1.3
11.3Sriastrad/*	$NetBSD: t_errhandling.c,v 1.3 2024/09/10 17:36:12 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*-
41.1Sriastrad * Copyright (c) 2024 The NetBSD Foundation, Inc.
51.1Sriastrad * All rights reserved.
61.1Sriastrad *
71.1Sriastrad * Redistribution and use in source and binary forms, with or without
81.1Sriastrad * modification, are permitted provided that the following conditions
91.1Sriastrad * are met:
101.1Sriastrad * 1. Redistributions of source code must retain the above copyright
111.1Sriastrad *    notice, this list of conditions and the following disclaimer.
121.1Sriastrad * 2. Redistributions in binary form must reproduce the above copyright
131.1Sriastrad *    notice, this list of conditions and the following disclaimer in the
141.1Sriastrad *    documentation and/or other materials provided with the distribution.
151.1Sriastrad *
161.1Sriastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sriastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sriastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sriastrad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sriastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sriastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sriastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sriastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sriastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sriastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sriastrad * POSSIBILITY OF SUCH DAMAGE.
271.1Sriastrad */
281.1Sriastrad
291.3Sriastrad#define	__TEST_FENV
301.3Sriastrad
311.1Sriastrad#include <sys/cdefs.h>
321.3Sriastrad__RCSID("$NetBSD: t_errhandling.c,v 1.3 2024/09/10 17:36:12 riastradh Exp $");
331.1Sriastrad
341.1Sriastrad#include <atf-c.h>
351.1Sriastrad#include <errno.h>
361.1Sriastrad#include <fenv.h>
371.1Sriastrad#include <math.h>
381.1Sriastrad
391.1SriastradATF_TC(log);
401.1SriastradATF_TC_HEAD(log, tc)
411.1Sriastrad{
421.1Sriastrad	atf_tc_set_md_var(tc, "descr", "log of invalid");
431.1Sriastrad}
441.1SriastradATF_TC_BODY(log, tc)
451.1Sriastrad{
461.1Sriastrad	static const struct {
471.1Sriastrad#ifdef __HAVE_FENV
481.1Sriastrad		double x;
491.1Sriastrad		int e;
501.1Sriastrad#define	C(x, e)		{ x, e }
511.1Sriastrad#else
521.1Sriastrad		double x;
531.1Sriastrad#define	C(x, e)		{ x }
541.1Sriastrad#endif
551.1Sriastrad	} cases[] = {
561.1Sriastrad		C(0, FE_DIVBYZERO),
571.1Sriastrad		C(-0., FE_DIVBYZERO),
581.1Sriastrad		C(-1, FE_INVALID),
591.1Sriastrad		C(-HUGE_VAL, FE_INVALID),
601.1Sriastrad	};
611.1Sriastrad	volatile double y;
621.1Sriastrad#ifdef __HAVE_FENV
631.1Sriastrad	int except;
641.1Sriastrad#endif
651.1Sriastrad	unsigned i;
661.1Sriastrad
671.1Sriastrad	for (i = 0; i < __arraycount(cases); i++) {
681.1Sriastrad		const volatile double x = cases[i].x;
691.1Sriastrad
701.1Sriastrad#ifdef __HAVE_FENV
711.1Sriastrad		feclearexcept(FE_ALL_EXCEPT);
721.1Sriastrad#endif
731.1Sriastrad		errno = 0;
741.1Sriastrad		y = log(x);
751.1Sriastrad		if (math_errhandling & MATH_ERREXCEPT) {
761.1Sriastrad#ifdef __HAVE_FENV
771.1Sriastrad			ATF_CHECK_MSG(((except = fetestexcept(FE_ALL_EXCEPT)) &
781.1Sriastrad				cases[i].e) != 0,
791.1Sriastrad			    "expected=0x%x actual=0x%x", cases[i].e, except);
801.1Sriastrad#else
811.1Sriastrad			atf_tc_fail_nonfatal("MATH_ERREXCEPT but no fenv.h");
821.1Sriastrad#endif
831.1Sriastrad		}
841.1Sriastrad		if (math_errhandling & MATH_ERRNO)
851.1Sriastrad			ATF_CHECK_EQ_MSG(errno, EDOM, "errno=%d", errno);
861.1Sriastrad	}
871.1Sriastrad
881.1Sriastrad	__USE(y);
891.1Sriastrad}
901.1Sriastrad
911.1SriastradATF_TP_ADD_TCS(tp)
921.1Sriastrad{
931.1Sriastrad
941.1Sriastrad	ATF_TP_ADD_TC(tp, log);
951.1Sriastrad
961.1Sriastrad	return atf_no_error();
971.1Sriastrad}
98