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