t_errhandling.c revision 1.2
1/* $NetBSD: t_errhandling.c,v 1.2 2024/09/09 17:03:08 jakllsch Exp $ */ 2 3/*- 4 * Copyright (c) 2024 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__RCSID("$NetBSD: t_errhandling.c,v 1.2 2024/09/09 17:03:08 jakllsch Exp $"); 31 32#include <atf-c.h> 33#include <errno.h> 34#define __TEST_FENV 35#include <fenv.h> 36#include <math.h> 37 38ATF_TC(log); 39ATF_TC_HEAD(log, tc) 40{ 41 atf_tc_set_md_var(tc, "descr", "log of invalid"); 42} 43ATF_TC_BODY(log, tc) 44{ 45 static const struct { 46#ifdef __HAVE_FENV 47 double x; 48 int e; 49#define C(x, e) { x, e } 50#else 51 double x; 52#define C(x, e) { x } 53#endif 54 } cases[] = { 55 C(0, FE_DIVBYZERO), 56 C(-0., FE_DIVBYZERO), 57 C(-1, FE_INVALID), 58 C(-HUGE_VAL, FE_INVALID), 59 }; 60 volatile double y; 61#ifdef __HAVE_FENV 62 int except; 63#endif 64 unsigned i; 65 66 for (i = 0; i < __arraycount(cases); i++) { 67 const volatile double x = cases[i].x; 68 69#ifdef __HAVE_FENV 70 feclearexcept(FE_ALL_EXCEPT); 71#endif 72 errno = 0; 73 y = log(x); 74 if (math_errhandling & MATH_ERREXCEPT) { 75#ifdef __HAVE_FENV 76 ATF_CHECK_MSG(((except = fetestexcept(FE_ALL_EXCEPT)) & 77 cases[i].e) != 0, 78 "expected=0x%x actual=0x%x", cases[i].e, except); 79#else 80 atf_tc_fail_nonfatal("MATH_ERREXCEPT but no fenv.h"); 81#endif 82 } 83 if (math_errhandling & MATH_ERRNO) 84 ATF_CHECK_EQ_MSG(errno, EDOM, "errno=%d", errno); 85 } 86 87 __USE(y); 88} 89 90ATF_TP_ADD_TCS(tp) 91{ 92 93 ATF_TP_ADD_TC(tp, log); 94 95 return atf_no_error(); 96} 97