Deleted Added
full compact
test-logarithm.c (226378) test-logarithm.c (251241)
1/*-
2 * Copyright (c) 2008-2010 David Schultz <das@FreeBSD.org>
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/*
28 * Tests for corner cases in log*().
29 */
30
31#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2008-2010 David Schultz <das@FreeBSD.org>
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/*
28 * Tests for corner cases in log*().
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-logarithm.c 226378 2011-10-15 05:28:13Z das $");
32__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-logarithm.c 251241 2013-06-02 04:30:03Z das $");
33
34#include <assert.h>
35#include <fenv.h>
36#include <float.h>
37#include <math.h>
38#include <stdio.h>
39
40#ifdef __i386__
41#include <ieeefp.h>
42#endif
43
33
34#include <assert.h>
35#include <fenv.h>
36#include <float.h>
37#include <math.h>
38#include <stdio.h>
39
40#ifdef __i386__
41#include <ieeefp.h>
42#endif
43
44#define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
45 FE_OVERFLOW | FE_UNDERFLOW)
44#include "test-utils.h"
46
47#pragma STDC FENV_ACCESS ON
48
49/*
50 * Test that a function returns the correct value and sets the
51 * exception flags correctly. The exceptmask specifies which
52 * exceptions we should check. We need to be lenient for several
53 * reasoons, but mainly because on some architectures it's impossible
54 * to raise FE_OVERFLOW without raising FE_INEXACT.
55 *
56 * These are macros instead of functions so that assert provides more
57 * meaningful error messages.
58 *
59 * XXX The volatile here is to avoid gcc's bogus constant folding and work
60 * around the lack of support for the FENV_ACCESS pragma.
61 */
62#define test(func, x, result, exceptmask, excepts) do { \
63 volatile long double _d = x; \
64 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
65 assert(fpequal((func)(_d), (result))); \
45
46#pragma STDC FENV_ACCESS ON
47
48/*
49 * Test that a function returns the correct value and sets the
50 * exception flags correctly. The exceptmask specifies which
51 * exceptions we should check. We need to be lenient for several
52 * reasoons, but mainly because on some architectures it's impossible
53 * to raise FE_OVERFLOW without raising FE_INEXACT.
54 *
55 * These are macros instead of functions so that assert provides more
56 * meaningful error messages.
57 *
58 * XXX The volatile here is to avoid gcc's bogus constant folding and work
59 * around the lack of support for the FENV_ACCESS pragma.
60 */
61#define test(func, x, result, exceptmask, excepts) do { \
62 volatile long double _d = x; \
63 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
64 assert(fpequal((func)(_d), (result))); \
66 assert(((func), fetestexcept(exceptmask) == (excepts))); \
65 assert(((void)(func), fetestexcept(exceptmask) == (excepts))); \
67} while (0)
68
69/* Test all the functions that compute log(x). */
70#define testall0(x, result, exceptmask, excepts) do { \
71 test(log, x, result, exceptmask, excepts); \
72 test(logf, x, result, exceptmask, excepts); \
73 test(log2, x, result, exceptmask, excepts); \
74 test(log2f, x, result, exceptmask, excepts); \
75 test(log10, x, result, exceptmask, excepts); \
76 test(log10f, x, result, exceptmask, excepts); \
77} while (0)
78
79/* Test all the functions that compute log(1+x). */
80#define testall1(x, result, exceptmask, excepts) do { \
81 test(log1p, x, result, exceptmask, excepts); \
82 test(log1pf, x, result, exceptmask, excepts); \
83} while (0)
84
66} while (0)
67
68/* Test all the functions that compute log(x). */
69#define testall0(x, result, exceptmask, excepts) do { \
70 test(log, x, result, exceptmask, excepts); \
71 test(logf, x, result, exceptmask, excepts); \
72 test(log2, x, result, exceptmask, excepts); \
73 test(log2f, x, result, exceptmask, excepts); \
74 test(log10, x, result, exceptmask, excepts); \
75 test(log10f, x, result, exceptmask, excepts); \
76} while (0)
77
78/* Test all the functions that compute log(1+x). */
79#define testall1(x, result, exceptmask, excepts) do { \
80 test(log1p, x, result, exceptmask, excepts); \
81 test(log1pf, x, result, exceptmask, excepts); \
82} while (0)
83
85/*
86 * Determine whether x and y are equal, with two special rules:
87 * +0.0 != -0.0
88 * NaN == NaN
89 */
90int
91fpequal(long double x, long double y)
92{
93 return ((x == y && !signbit(x) == !signbit(y)) || isnan(x) && isnan(y));
94}
95
96void
97run_generic_tests(void)
98{
99
100 /* log(1) == 0, no exceptions raised */
101 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
102 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
103 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
104
105 /* log(NaN) == NaN, no exceptions raised */
106 testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
107 testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
108
109 /* log(Inf) == Inf, no exceptions raised */
110 testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
111 testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
112
113 /* log(x) == NaN for x < 0, invalid exception raised */
114 testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
115 testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
116 testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
117 testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
118
119 /* log(0) == -Inf, divide-by-zero exception */
120 testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
121 testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
122 testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
123}
124
125void
126run_log2_tests(void)
127{
128 int i;
129
130 /*
131 * We should insist that log2() return exactly the correct
132 * result and not raise an inexact exception for powers of 2.
133 */
134 feclearexcept(FE_ALL_EXCEPT);
135 for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
136 assert(log2f(ldexpf(1.0, i)) == i);
137 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
138 }
139 for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
140 assert(log2(ldexp(1.0, i)) == i);
141 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
142 }
143}
144
145void
146run_roundingmode_tests(void)
147{
148
149 /*
150 * Corner cases in other rounding modes.
151 */
152 fesetround(FE_DOWNWARD);
153 /* These are still positive per IEEE 754R */
154 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
155 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
156 fesetround(FE_TOWARDZERO);
157 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
158 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
159
160 fesetround(FE_UPWARD);
161 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
162 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
163 /* log1p(-0.0) == -0.0 even when rounding upwards */
164 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
165
166 fesetround(FE_TONEAREST);
167}
168
169int
170main(int argc, char *argv[])
171{
172
173 printf("1..3\n");
174
175 run_generic_tests();
176 printf("ok 1 - logarithm\n");
177
178 run_log2_tests();
179 printf("ok 2 - logarithm\n");
180
181 run_roundingmode_tests();
182 printf("ok 3 - logarithm\n");
183
184 return (0);
185}
84void
85run_generic_tests(void)
86{
87
88 /* log(1) == 0, no exceptions raised */
89 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
90 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
91 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
92
93 /* log(NaN) == NaN, no exceptions raised */
94 testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
95 testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
96
97 /* log(Inf) == Inf, no exceptions raised */
98 testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
99 testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
100
101 /* log(x) == NaN for x < 0, invalid exception raised */
102 testall0(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
103 testall1(-INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
104 testall0(-1.0, NAN, ALL_STD_EXCEPT, FE_INVALID);
105 testall1(-1.5, NAN, ALL_STD_EXCEPT, FE_INVALID);
106
107 /* log(0) == -Inf, divide-by-zero exception */
108 testall0(0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
109 testall0(-0.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
110 testall1(-1.0, -INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_DIVBYZERO);
111}
112
113void
114run_log2_tests(void)
115{
116 int i;
117
118 /*
119 * We should insist that log2() return exactly the correct
120 * result and not raise an inexact exception for powers of 2.
121 */
122 feclearexcept(FE_ALL_EXCEPT);
123 for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
124 assert(log2f(ldexpf(1.0, i)) == i);
125 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
126 }
127 for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
128 assert(log2(ldexp(1.0, i)) == i);
129 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
130 }
131}
132
133void
134run_roundingmode_tests(void)
135{
136
137 /*
138 * Corner cases in other rounding modes.
139 */
140 fesetround(FE_DOWNWARD);
141 /* These are still positive per IEEE 754R */
142 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
143 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
144 fesetround(FE_TOWARDZERO);
145 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
146 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
147
148 fesetround(FE_UPWARD);
149 testall0(1.0, 0.0, ALL_STD_EXCEPT, 0);
150 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
151 /* log1p(-0.0) == -0.0 even when rounding upwards */
152 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
153
154 fesetround(FE_TONEAREST);
155}
156
157int
158main(int argc, char *argv[])
159{
160
161 printf("1..3\n");
162
163 run_generic_tests();
164 printf("ok 1 - logarithm\n");
165
166 run_log2_tests();
167 printf("ok 2 - logarithm\n");
168
169 run_roundingmode_tests();
170 printf("ok 3 - logarithm\n");
171
172 return (0);
173}