xref: /freebsd/lib/msun/tests/ilogb_test.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2004 Stefan Farfeleder
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <float.h>
28 #include <limits.h>
29 #include <math.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #include "test-utils.h"
34 
35 ATF_TC_WITHOUT_HEAD(ilogb);
36 ATF_TC_BODY(ilogb, tc)
37 {
38 	char buf[128], *end;
39 	double d;
40 	int e, i;
41 
42 	ATF_CHECK_EQ(FP_ILOGB0, ilogb(0));
43 	ATF_CHECK_EQ(FP_ILOGBNAN, ilogb(NAN));
44 	ATF_CHECK_EQ(INT_MAX, ilogb(INFINITY));
45 	for (e = DBL_MIN_EXP - DBL_MANT_DIG; e < DBL_MAX_EXP; e++) {
46 		snprintf(buf, sizeof(buf), "0x1.p%d", e);
47 		d = strtod(buf, &end);
48 		ATF_CHECK_EQ('\0', *end);
49 		i = ilogb(d);
50 		ATF_CHECK_EQ_MSG(e, i, "ilogb(%g) returned %d not %d", d, i, e);
51 	}
52 }
53 
54 ATF_TC_WITHOUT_HEAD(ilogbf);
55 ATF_TC_BODY(ilogbf, tc)
56 {
57 	char buf[128], *end;
58 	float f;
59 	int e, i;
60 
61 	ATF_CHECK_EQ(FP_ILOGB0, ilogbf(0));
62 	ATF_CHECK_EQ(FP_ILOGBNAN, ilogbf(NAN));
63 	ATF_CHECK_EQ(INT_MAX, ilogbf(INFINITY));
64 	for (e = FLT_MIN_EXP - FLT_MANT_DIG; e < FLT_MAX_EXP; e++) {
65 		snprintf(buf, sizeof(buf), "0x1.p%d", e);
66 		f = strtof(buf, &end);
67 		ATF_CHECK_EQ('\0', *end);
68 		i = ilogbf(f);
69 		ATF_CHECK_EQ_MSG(e, i, "ilogbf(%g) returned %d not %d", f, i,
70 		    e);
71 	}
72 }
73 
74 ATF_TC_WITHOUT_HEAD(ilogbl);
75 ATF_TC_BODY(ilogbl, tc)
76 {
77 	char buf[128], *end;
78 	long double ld;
79 	int e, i;
80 
81 	ATF_CHECK_EQ(FP_ILOGB0, ilogbl(0));
82 	ATF_CHECK_EQ(FP_ILOGBNAN, ilogbl(NAN));
83 	ATF_CHECK_EQ(INT_MAX, ilogbl(INFINITY));
84 	for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e < LDBL_MAX_EXP; e++) {
85 		snprintf(buf, sizeof(buf), "0x1.p%d", e);
86 		ld = strtold(buf, &end);
87 		ATF_CHECK_EQ('\0', *end);
88 		i = ilogbl(ld);
89 		ATF_CHECK_EQ_MSG(e, i, "ilogbl(%Lg) returned %d not %d", ld, i,
90 		    e);
91 	}
92 }
93 
94 ATF_TP_ADD_TCS(tp)
95 {
96 	ATF_TP_ADD_TC(tp, ilogb);
97 	ATF_TP_ADD_TC(tp, ilogbf);
98 	ATF_TP_ADD_TC(tp, ilogbl);
99 
100 	return (atf_no_error());
101 }
102