xref: /freebsd/contrib/netbsd-tests/lib/libm/t_hypot.c (revision 25120662284466ecef976df8f86e97bafdedf991)
1640235e2SEnji Cooper /* $NetBSD: t_hypot.c,v 1.1 2016/01/24 20:26:47 gson Exp $ */
2640235e2SEnji Cooper 
3640235e2SEnji Cooper /*-
4640235e2SEnji Cooper  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5640235e2SEnji Cooper  * All rights reserved.
6640235e2SEnji Cooper  *
7640235e2SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8640235e2SEnji Cooper  * modification, are permitted provided that the following conditions
9640235e2SEnji Cooper  * are met:
10640235e2SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12640235e2SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14640235e2SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15640235e2SEnji Cooper  *
16640235e2SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17640235e2SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18640235e2SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19640235e2SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20640235e2SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21640235e2SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22640235e2SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23640235e2SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24640235e2SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25640235e2SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26640235e2SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
27640235e2SEnji Cooper  */
28640235e2SEnji Cooper 
29640235e2SEnji Cooper #include <atf-c.h>
30640235e2SEnji Cooper #include <math.h>
31640235e2SEnji Cooper 
32640235e2SEnji Cooper ATF_TC(hypot_integer);
ATF_TC_HEAD(hypot_integer,tc)33640235e2SEnji Cooper ATF_TC_HEAD(hypot_integer, tc)
34640235e2SEnji Cooper {
35640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test hypot with integer args");
36640235e2SEnji Cooper }
37640235e2SEnji Cooper 
ATF_TC_BODY(hypot_integer,tc)38640235e2SEnji Cooper ATF_TC_BODY(hypot_integer, tc)
39640235e2SEnji Cooper {
40640235e2SEnji Cooper 	/* volatile so hypotf() won't be evaluated at compile time */
41640235e2SEnji Cooper 	volatile double a = 5;
42640235e2SEnji Cooper 	volatile double b = 12;
43640235e2SEnji Cooper 	ATF_CHECK(hypot(a, b) == 13.0);
44640235e2SEnji Cooper }
45640235e2SEnji Cooper 
46640235e2SEnji Cooper ATF_TC(hypotf_integer);
ATF_TC_HEAD(hypotf_integer,tc)47640235e2SEnji Cooper ATF_TC_HEAD(hypotf_integer, tc)
48640235e2SEnji Cooper {
49640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test hypotf with integer args");
50640235e2SEnji Cooper }
51640235e2SEnji Cooper 
ATF_TC_BODY(hypotf_integer,tc)52640235e2SEnji Cooper ATF_TC_BODY(hypotf_integer, tc)
53640235e2SEnji Cooper {
54640235e2SEnji Cooper 	volatile float a = 5;
55640235e2SEnji Cooper 	volatile float b = 12;
56640235e2SEnji Cooper 	ATF_CHECK(hypotf(a, b) == 13.0f);
57640235e2SEnji Cooper }
58640235e2SEnji Cooper 
59640235e2SEnji Cooper ATF_TC(pr50698);
ATF_TC_HEAD(pr50698,tc)60640235e2SEnji Cooper ATF_TC_HEAD(pr50698, tc)
61640235e2SEnji Cooper {
62640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Check for the bug of PR 50698");
63640235e2SEnji Cooper }
64640235e2SEnji Cooper 
ATF_TC_BODY(pr50698,tc)65640235e2SEnji Cooper ATF_TC_BODY(pr50698, tc)
66640235e2SEnji Cooper {
67640235e2SEnji Cooper 	volatile float a = 1e-18f;
68640235e2SEnji Cooper 	float val = hypotf(a, a);
69640235e2SEnji Cooper 	ATF_CHECK(!isinf(val));
70640235e2SEnji Cooper 	ATF_CHECK(!isnan(val));
71640235e2SEnji Cooper }
72640235e2SEnji Cooper 
73*25120662SDimitry Andric #if __LDBL_MANT_DIG__ == 64
74d3338f33SDimitry Andric ATF_TC(hypotl_near_underflow);
ATF_TC_HEAD(hypotl_near_underflow,tc)75d3338f33SDimitry Andric ATF_TC_HEAD(hypotl_near_underflow, tc)
76d3338f33SDimitry Andric {
77d3338f33SDimitry Andric 	atf_tc_set_md_var(tc, "descr", "Test hypotl near underflow");
78d3338f33SDimitry Andric }
79d3338f33SDimitry Andric 
ATF_TC_BODY(hypotl_near_underflow,tc)80d3338f33SDimitry Andric ATF_TC_BODY(hypotl_near_underflow, tc)
81d3338f33SDimitry Andric {
82d3338f33SDimitry Andric 	volatile long double a = 0x1.b2933cafa0bb7p-16383L;
83d3338f33SDimitry Andric 	volatile long double b = 0x1.fffffffffffffp-16351L;
84d3338f33SDimitry Andric 	volatile long double e = 0x1.fffffffffffffp-16351L;
85d3338f33SDimitry Andric 	volatile long double ulp = __LDBL_EPSILON__;
86d3338f33SDimitry Andric 
87d3338f33SDimitry Andric 	volatile long double val = hypotl(a, b);
88d3338f33SDimitry Andric 
89d3338f33SDimitry Andric 	ATF_CHECK(!isinf(val));
90d3338f33SDimitry Andric 	ATF_CHECK(fabsl(val - e) <= 2 * ulp);
91d3338f33SDimitry Andric }
92*25120662SDimitry Andric #endif /* __LDBL_MANT_DIG__ == 64 */
93d3338f33SDimitry Andric 
ATF_TP_ADD_TCS(tp)94640235e2SEnji Cooper ATF_TP_ADD_TCS(tp)
95640235e2SEnji Cooper {
96640235e2SEnji Cooper 
97640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, hypot_integer);
98640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, hypotf_integer);
99640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, pr50698);
100*25120662SDimitry Andric #if __LDBL_MANT_DIG__ == 64
101d3338f33SDimitry Andric 	ATF_TP_ADD_TC(tp, hypotl_near_underflow);
102*25120662SDimitry Andric #endif /* __LDBL_MANT_DIG__ == 64 */
103640235e2SEnji Cooper 
104640235e2SEnji Cooper 	return atf_no_error();
105640235e2SEnji Cooper }
106