t_errhandling.c revision 1.1
11.1Sriastrad/* $NetBSD: t_errhandling.c,v 1.1 2024/09/09 15:08:23 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.1Sriastrad#include <sys/cdefs.h> 301.1Sriastrad__RCSID("$NetBSD: t_errhandling.c,v 1.1 2024/09/09 15:08:23 riastradh Exp $"); 311.1Sriastrad 321.1Sriastrad#include <atf-c.h> 331.1Sriastrad#include <errno.h> 341.1Sriastrad#include <fenv.h> 351.1Sriastrad#include <math.h> 361.1Sriastrad 371.1SriastradATF_TC(log); 381.1SriastradATF_TC_HEAD(log, tc) 391.1Sriastrad{ 401.1Sriastrad atf_tc_set_md_var(tc, "descr", "log of invalid"); 411.1Sriastrad} 421.1SriastradATF_TC_BODY(log, tc) 431.1Sriastrad{ 441.1Sriastrad static const struct { 451.1Sriastrad#ifdef __HAVE_FENV 461.1Sriastrad double x; 471.1Sriastrad int e; 481.1Sriastrad#define C(x, e) { x, e } 491.1Sriastrad#else 501.1Sriastrad double x; 511.1Sriastrad#define C(x, e) { x } 521.1Sriastrad#endif 531.1Sriastrad } cases[] = { 541.1Sriastrad C(0, FE_DIVBYZERO), 551.1Sriastrad C(-0., FE_DIVBYZERO), 561.1Sriastrad C(-1, FE_INVALID), 571.1Sriastrad C(-HUGE_VAL, FE_INVALID), 581.1Sriastrad }; 591.1Sriastrad volatile double y; 601.1Sriastrad#ifdef __HAVE_FENV 611.1Sriastrad int except; 621.1Sriastrad#endif 631.1Sriastrad unsigned i; 641.1Sriastrad 651.1Sriastrad for (i = 0; i < __arraycount(cases); i++) { 661.1Sriastrad const volatile double x = cases[i].x; 671.1Sriastrad 681.1Sriastrad#ifdef __HAVE_FENV 691.1Sriastrad feclearexcept(FE_ALL_EXCEPT); 701.1Sriastrad#endif 711.1Sriastrad errno = 0; 721.1Sriastrad y = log(x); 731.1Sriastrad if (math_errhandling & MATH_ERREXCEPT) { 741.1Sriastrad#ifdef __HAVE_FENV 751.1Sriastrad ATF_CHECK_MSG(((except = fetestexcept(FE_ALL_EXCEPT)) & 761.1Sriastrad cases[i].e) != 0, 771.1Sriastrad "expected=0x%x actual=0x%x", cases[i].e, except); 781.1Sriastrad#else 791.1Sriastrad atf_tc_fail_nonfatal("MATH_ERREXCEPT but no fenv.h"); 801.1Sriastrad#endif 811.1Sriastrad } 821.1Sriastrad if (math_errhandling & MATH_ERRNO) 831.1Sriastrad ATF_CHECK_EQ_MSG(errno, EDOM, "errno=%d", errno); 841.1Sriastrad } 851.1Sriastrad 861.1Sriastrad __USE(y); 871.1Sriastrad} 881.1Sriastrad 891.1SriastradATF_TP_ADD_TCS(tp) 901.1Sriastrad{ 911.1Sriastrad 921.1Sriastrad ATF_TP_ADD_TC(tp, log); 931.1Sriastrad 941.1Sriastrad return atf_no_error(); 951.1Sriastrad} 96