xref: /freebsd/contrib/netbsd-tests/lib/libm/t_hypot.c (revision 25120662284466ecef976df8f86e97bafdedf991)
1 /* $NetBSD: t_hypot.c,v 1.1 2016/01/24 20:26:47 gson Exp $ */
2 
3 /*-
4  * Copyright (c) 2016 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 <atf-c.h>
30 #include <math.h>
31 
32 ATF_TC(hypot_integer);
ATF_TC_HEAD(hypot_integer,tc)33 ATF_TC_HEAD(hypot_integer, tc)
34 {
35 	atf_tc_set_md_var(tc, "descr", "Test hypot with integer args");
36 }
37 
ATF_TC_BODY(hypot_integer,tc)38 ATF_TC_BODY(hypot_integer, tc)
39 {
40 	/* volatile so hypotf() won't be evaluated at compile time */
41 	volatile double a = 5;
42 	volatile double b = 12;
43 	ATF_CHECK(hypot(a, b) == 13.0);
44 }
45 
46 ATF_TC(hypotf_integer);
ATF_TC_HEAD(hypotf_integer,tc)47 ATF_TC_HEAD(hypotf_integer, tc)
48 {
49 	atf_tc_set_md_var(tc, "descr", "Test hypotf with integer args");
50 }
51 
ATF_TC_BODY(hypotf_integer,tc)52 ATF_TC_BODY(hypotf_integer, tc)
53 {
54 	volatile float a = 5;
55 	volatile float b = 12;
56 	ATF_CHECK(hypotf(a, b) == 13.0f);
57 }
58 
59 ATF_TC(pr50698);
ATF_TC_HEAD(pr50698,tc)60 ATF_TC_HEAD(pr50698, tc)
61 {
62 	atf_tc_set_md_var(tc, "descr", "Check for the bug of PR 50698");
63 }
64 
ATF_TC_BODY(pr50698,tc)65 ATF_TC_BODY(pr50698, tc)
66 {
67 	volatile float a = 1e-18f;
68 	float val = hypotf(a, a);
69 	ATF_CHECK(!isinf(val));
70 	ATF_CHECK(!isnan(val));
71 }
72 
73 #if __LDBL_MANT_DIG__ == 64
74 ATF_TC(hypotl_near_underflow);
ATF_TC_HEAD(hypotl_near_underflow,tc)75 ATF_TC_HEAD(hypotl_near_underflow, tc)
76 {
77 	atf_tc_set_md_var(tc, "descr", "Test hypotl near underflow");
78 }
79 
ATF_TC_BODY(hypotl_near_underflow,tc)80 ATF_TC_BODY(hypotl_near_underflow, tc)
81 {
82 	volatile long double a = 0x1.b2933cafa0bb7p-16383L;
83 	volatile long double b = 0x1.fffffffffffffp-16351L;
84 	volatile long double e = 0x1.fffffffffffffp-16351L;
85 	volatile long double ulp = __LDBL_EPSILON__;
86 
87 	volatile long double val = hypotl(a, b);
88 
89 	ATF_CHECK(!isinf(val));
90 	ATF_CHECK(fabsl(val - e) <= 2 * ulp);
91 }
92 #endif /* __LDBL_MANT_DIG__ == 64 */
93 
ATF_TP_ADD_TCS(tp)94 ATF_TP_ADD_TCS(tp)
95 {
96 
97 	ATF_TP_ADD_TC(tp, hypot_integer);
98 	ATF_TP_ADD_TC(tp, hypotf_integer);
99 	ATF_TP_ADD_TC(tp, pr50698);
100 #if __LDBL_MANT_DIG__ == 64
101 	ATF_TP_ADD_TC(tp, hypotl_near_underflow);
102 #endif /* __LDBL_MANT_DIG__ == 64 */
103 
104 	return atf_no_error();
105 }
106