t_errhandling.c revision 1.1
1/* $NetBSD: t_errhandling.c,v 1.1 2024/09/09 15:08:23 riastradh 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.1 2024/09/09 15:08:23 riastradh Exp $"); 31 32#include <atf-c.h> 33#include <errno.h> 34#include <fenv.h> 35#include <math.h> 36 37ATF_TC(log); 38ATF_TC_HEAD(log, tc) 39{ 40 atf_tc_set_md_var(tc, "descr", "log of invalid"); 41} 42ATF_TC_BODY(log, tc) 43{ 44 static const struct { 45#ifdef __HAVE_FENV 46 double x; 47 int e; 48#define C(x, e) { x, e } 49#else 50 double x; 51#define C(x, e) { x } 52#endif 53 } cases[] = { 54 C(0, FE_DIVBYZERO), 55 C(-0., FE_DIVBYZERO), 56 C(-1, FE_INVALID), 57 C(-HUGE_VAL, FE_INVALID), 58 }; 59 volatile double y; 60#ifdef __HAVE_FENV 61 int except; 62#endif 63 unsigned i; 64 65 for (i = 0; i < __arraycount(cases); i++) { 66 const volatile double x = cases[i].x; 67 68#ifdef __HAVE_FENV 69 feclearexcept(FE_ALL_EXCEPT); 70#endif 71 errno = 0; 72 y = log(x); 73 if (math_errhandling & MATH_ERREXCEPT) { 74#ifdef __HAVE_FENV 75 ATF_CHECK_MSG(((except = fetestexcept(FE_ALL_EXCEPT)) & 76 cases[i].e) != 0, 77 "expected=0x%x actual=0x%x", cases[i].e, except); 78#else 79 atf_tc_fail_nonfatal("MATH_ERREXCEPT but no fenv.h"); 80#endif 81 } 82 if (math_errhandling & MATH_ERRNO) 83 ATF_CHECK_EQ_MSG(errno, EDOM, "errno=%d", errno); 84 } 85 86 __USE(y); 87} 88 89ATF_TP_ADD_TCS(tp) 90{ 91 92 ATF_TP_ADD_TC(tp, log); 93 94 return atf_no_error(); 95} 96