1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
* Copyright (c) 1994 Cygnus Support.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* and/or other materials related to such
* distribution and use acknowledge that the software was developed
* at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "test.h"
#include <errno.h>
int
randi (void)
{
static int next;
next = (next * 1103515245) + 12345;
return ((next >> 16) & 0xffff);
}
double randx (void)
{
double res;
do
{
union {
short parts[4];
double res;
} u;
u.parts[0] = randi();
u.parts[1] = randi();
u.parts[2] = randi();
u.parts[3] = randi();
res = u.res;
} while (!finite(res));
return res ;
}
/* Return a random double, but bias for numbers closer to 0 */
double randy (void)
{
int pow;
double r= randx();
r = frexp(r, &pow);
return ldexp(r, randi() & 0x1f);
}
void
test_frexp (void)
{
int i;
double r;
int t;
float xf;
double gives;
int pow;
/* Frexp of x return a and n, where a * 2**n == x, so test this with a
set of random numbers */
for (t = 0; t < 2; t++)
{
for (i = 0; i < 1000; i++)
{
double x = randx();
line(i);
switch (t)
{
case 0:
newfunc("frexp/ldexp");
r = frexp(x, &pow);
if (r > 1.0 || r < -1.0)
{
/* Answer can never be > 1 or < 1 */
test_iok(0,1);
}
gives = ldexp(r ,pow);
test_mok(gives,x,62);
break;
case 1:
newfunc("frexpf/ldexpf");
if (x > (double) FLT_MIN && x < (double) FLT_MAX)
{
/* test floats too, but they have a smaller range so make sure x
isn't too big. Also x can get smaller than a float can
represent to make sure that doesn't happen too */
xf = x;
r = (double) frexpf(xf, &pow);
if (r > 1.0 || r < -1.0)
{
/* Answer can never be > 1 or < -1 */
test_iok(0,1);
}
gives = (double) ldexpf(r ,pow);
test_mok(gives,x, 32);
}
}
}
}
/* test a few numbers manually to make sure frexp/ldexp are not
testing as ok because both are broken */
r = frexp(64.0, &i);
test_mok(r, 0.5,64);
test_iok(i, 7);
r = frexp(96.0, &i);
test_mok(r, 0.75, 64);
test_iok(i, 7);
}
/* Test mod - this is given a real hammering by the strtod type
routines, here are some more tests.
By definition
modf = func(value, &iptr)
(*iptr + modf) == value
we test this
*/
void
test_mod (void)
{
int i;
newfunc("modf");
for (i = 0; i < 1000; i++)
{
double intpart;
double n;
line(i);
n = randx();
if (finite(n) && n != 0.0 )
{
double r = modf(n, &intpart);
line(i);
test_mok(intpart + r, n, 63);
}
}
newfunc("modff");
for (i = 0; i < 1000; i++)
{
float intpart;
double nd;
line(i);
nd = randx() ;
if (nd < (double) FLT_MAX && finitef(nd) && nd != 0.0)
{
float n = nd;
double r = (double) modff(n, &intpart);
line(i);
test_mok((double) intpart + r, (double) n, 32);
}
}
}
/*
Test pow by multiplying logs
*/
void
test_pow (void)
{
unsigned int i;
newfunc("pow");
for (i = 0; i < 1000; i++)
{
double n1;
double n2;
double res;
double shouldbe;
line(i);
n1 = fabs(randy());
n2 = fabs(randy()/100.0);
res = pow(n1, n2);
shouldbe = exp(log(n1) * n2);
test_mok(shouldbe, res,55);
}
newfunc("powf");
for (i = 0; i < 1000; i++)
{
float n1;
float n2;
float res;
float shouldbe;
errno = 0;
line(i);
n1 = fabs(randy());
n2 = fabs(randy()/100.0);
res = powf(n1, n2);
shouldbe = expf(logf(n1) * n2);
if (!errno)
test_mok((double) shouldbe, (double) res,28);
}
}
void
test_math2 (void)
{
test_mod();
test_frexp();
test_pow();
}
|