xref: /freebsd/contrib/netbsd-tests/lib/libm/t_bit.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /* $NetBSD: t_bit.c,v 1.1 2019/04/26 08:52:16 maya Exp $ */
2 
3 /*
4  * Written by Maya Rashish <maya@NetBSD.org>
5  * Public domain.
6  *
7  * Testing signbit{,f,l} function correctly
8  */
9 
10 #include <atf-c.h>
11 #include <float.h>
12 #include <math.h>
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdbool.h>
16 
17 static const struct {
18 	double input;
19 	bool is_negative;
20 } values[] = {
21 	{ -1,		true},
22 	{ -123,		true},
23 	{ -123E6,	true},
24 #ifdef INFINITY
25 	{ -INFINITY,	true},
26 	{ INFINITY,	false},
27 #endif
28 	{ 123E6,	false},
29 	{ 0,		false},
30 	{ -FLT_MIN,	true},
31 	{ FLT_MIN,	false},
32 	/*
33 	 * Cannot be accurately represented as float,
34 	 * but sign should be preserved
35 	 */
36 	{ DBL_MAX,	false},
37 	{ -DBL_MAX,	true},
38 };
39 
40 #ifdef __HAVE_LONG_DOUBLE
41 static const struct {
42 	long double input;
43 	bool is_negative;
44 } ldbl_values[] = {
45 	{ -LDBL_MIN,	true},
46 	{ LDBL_MIN,	false},
47 	{ LDBL_MAX,	false},
48 	{ -LDBL_MAX,	true},
49 };
50 #endif
51 
52 ATF_TC(signbit);
53 ATF_TC_HEAD(signbit, tc)
54 {
55 	atf_tc_set_md_var(tc, "descr","Check that signbit functions correctly");
56 }
57 
58 ATF_TC_BODY(signbit, tc)
59 {
60 	double iterator_d;
61 	float iterator_f;
62 
63 	for (unsigned int i = 0; i < __arraycount(values); i++) {
64 		iterator_d = values[i].input;
65 		iterator_f = (float) values[i].input;
66 		if (signbit(iterator_f) != values[i].is_negative)
67 			atf_tc_fail("%s:%d iteration %d signbitf is wrong"
68 					" about the sign of %f", __func__,
69 					__LINE__, i, iterator_f);
70 		if (signbit(iterator_d) != values[i].is_negative)
71 			atf_tc_fail("%s:%d iteration %d signbit is wrong"
72 					"about the sign of %f", __func__,
73 					__LINE__,i, iterator_d);
74 
75 #ifdef __HAVE_LONG_DOUBLE
76 		long double iterator_l = values[i].input;
77 		if (signbit(iterator_l) != values[i].is_negative)
78 			atf_tc_fail("%s:%d iteration %d signbitl is wrong"
79 					" about the sign of %Lf", __func__,
80 					__LINE__, i, iterator_l);
81 #endif
82 	}
83 
84 #ifdef __HAVE_LONG_DOUBLE
85 	for (unsigned int i = 0; i < __arraycount(ldbl_values); i++) {
86 		if (signbit(ldbl_values[i].input) != ldbl_values[i].is_negative)
87 			atf_tc_fail("%s:%d iteration %d signbitl is"
88 					"wrong about the sign of %Lf",
89 					__func__, __LINE__, i,
90 					ldbl_values[i].input);
91 	}
92 #endif
93 
94 }
95 
96 ATF_TP_ADD_TCS(tp)
97 {
98 
99 	ATF_TP_ADD_TC(tp, signbit);
100 
101 	return atf_no_error();
102 }
103