xref: /freebsd/lib/msun/tests/ilogb_test.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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  * $FreeBSD$
27  */
28 
29 #include <float.h>
30 #include <limits.h>
31 #include <math.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 #include "test-utils.h"
36 
37 ATF_TC_WITHOUT_HEAD(ilogb);
38 ATF_TC_BODY(ilogb, tc)
39 {
40 	char buf[128], *end;
41 	double d;
42 	int e, i;
43 
44 	ATF_CHECK_EQ(FP_ILOGB0, ilogb(0));
45 	ATF_CHECK_EQ(FP_ILOGBNAN, ilogb(NAN));
46 	ATF_CHECK_EQ(INT_MAX, ilogb(INFINITY));
47 	for (e = DBL_MIN_EXP - DBL_MANT_DIG; e < DBL_MAX_EXP; e++) {
48 		snprintf(buf, sizeof(buf), "0x1.p%d", e);
49 		d = strtod(buf, &end);
50 		ATF_CHECK_EQ('\0', *end);
51 		i = ilogb(d);
52 		ATF_CHECK_EQ_MSG(e, i, "ilogb(%g) returned %d not %d", d, i, e);
53 	}
54 }
55 
56 ATF_TC_WITHOUT_HEAD(ilogbf);
57 ATF_TC_BODY(ilogbf, tc)
58 {
59 	char buf[128], *end;
60 	float f;
61 	int e, i;
62 
63 	ATF_CHECK_EQ(FP_ILOGB0, ilogbf(0));
64 	ATF_CHECK_EQ(FP_ILOGBNAN, ilogbf(NAN));
65 	ATF_CHECK_EQ(INT_MAX, ilogbf(INFINITY));
66 	for (e = FLT_MIN_EXP - FLT_MANT_DIG; e < FLT_MAX_EXP; e++) {
67 		snprintf(buf, sizeof(buf), "0x1.p%d", e);
68 		f = strtof(buf, &end);
69 		ATF_CHECK_EQ('\0', *end);
70 		i = ilogbf(f);
71 		ATF_CHECK_EQ_MSG(e, i, "ilogbf(%g) returned %d not %d", f, i,
72 		    e);
73 	}
74 }
75 
76 ATF_TC_WITHOUT_HEAD(ilogbl);
77 ATF_TC_BODY(ilogbl, tc)
78 {
79 	char buf[128], *end;
80 	long double ld;
81 	int e, i;
82 
83 	ATF_CHECK_EQ(FP_ILOGB0, ilogbl(0));
84 	ATF_CHECK_EQ(FP_ILOGBNAN, ilogbl(NAN));
85 	ATF_CHECK_EQ(INT_MAX, ilogbl(INFINITY));
86 	for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e < LDBL_MAX_EXP; e++) {
87 		snprintf(buf, sizeof(buf), "0x1.p%d", e);
88 		ld = strtold(buf, &end);
89 		ATF_CHECK_EQ('\0', *end);
90 		i = ilogbl(ld);
91 		ATF_CHECK_EQ_MSG(e, i, "ilogbl(%Lg) returned %d not %d", ld, i,
92 		    e);
93 	}
94 }
95 
96 ATF_TP_ADD_TCS(tp)
97 {
98 	ATF_TP_ADD_TC(tp, ilogb);
99 	ATF_TP_ADD_TC(tp, ilogbf);
100 	ATF_TP_ADD_TC(tp, ilogbl);
101 
102 	return (atf_no_error());
103 }
104