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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
/*
* 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.
*/
/* Test conversions */
#define IN_CONVERT
#include "test.h"
//#include <_ansi.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
static char buffer[500];
extern double_type doubles[];
//#define GENERATE_VECTORS
/* TEST ATOF ATOFF */
double_type *pd = doubles;
#ifdef _IO_FLOAT_EXACT
#define CONVERT_BITS_DOUBLE 64
#define CONVERT_BITS_FLOAT 32
#else
#define CONVERT_BITS_DOUBLE 61
#define CONVERT_BITS_FLOAT 30
#endif
void
test_strtod (void)
{
char *tail;
double v;
/* On average we'll loose 1/2 a bit, so the test is for within 1 bit */
v = strtod(pd->string, &tail);
test_mok(v, pd->value, CONVERT_BITS_DOUBLE);
test_iok(tail - pd->string, pd->endscan);
}
void
test_strtof (void)
{
char *tail;
float v;
/* On average we'll loose 1/2 a bit, so the test is for within 1 bit */
v = strtof(pd->string, &tail);
test_mok((double) v, pd->value, CONVERT_BITS_FLOAT);
test_iok(tail - pd->string, pd->endscan);
}
#if defined(_HAVE_LONG_DOUBLE) && (__LDBL_MANT_DIG__ == 64 || defined(TINY_STDIO))
#define HAVE_STRTOLD
#endif
#ifdef HAVE_STRTOLD
void
test_strtold (void)
{
char *tail;
long double v;
/* On average we'll loose 1/2 a bit, so the test is for within 1 bit */
v = strtold(pd->string, &tail);
test_mok(v, pd->value, CONVERT_BITS_DOUBLE);
test_iok(tail - pd->string, pd->endscan);
}
#endif
void
test_atof (void)
{
test_mok(atof(pd->string), pd->value, CONVERT_BITS_DOUBLE);
}
#ifndef NO_NEWLIB
void
test_atoff (void)
{
float v = atoff(pd->string);
test_mok((double) v, pd->value, 30);
}
#endif
static
void
iterate (void (*func) (void),
char *name)
{
newfunc(name);
pd = doubles;
while (pd->string) {
line(pd->line);
func();
pd++;
}
}
extern int_type ints[];
int_type *p = ints;
static void
int_iterate (void (*func)(),
char *name)
{
newfunc(name);
p = ints;
while (p->string) {
line(p->line);
errno = 0;
func();
p++;
}
}
void
test_strtol_base (int base,
int_scan_type *pi,
char *string)
{
long r;
char *ptr;
errno = 0;
r = strtol(string, &ptr, base);
test_iok(r, pi->value);
test_eok(errno, pi->errno_val);
test_iok(ptr - string, pi->end);
}
void
test_strtol (void)
{
test_strtol_base(8,&(p->octal), p->string);
test_strtol_base(10,&(p->decimal), p->string);
test_strtol_base(16, &(p->hex), p->string);
test_strtol_base(0, &(p->normal), p->string);
test_strtol_base(26, &(p->alphabetical), p->string);
}
void
test_atoi (void)
{
if (p->decimal.errno_val == 0)
test_iok(atoi(p->string), p->decimal.value);
}
void
test_atol (void)
{
test_iok(atol(p->string), p->decimal.value);
test_eok(errno, p->decimal.errno_val);
}
extern ddouble_type ddoubles[];
ddouble_type *pdd;
static inline char *
check_null(char *s) {
if (s == NULL)
return "(out of memory)";
return s;
}
#ifndef NO_NEWLIB
/* test ECVT and friends */
void
test_ecvtbuf (void)
{
int a2,a3;
char *s;
s = check_null(ecvtbuf(pdd->value, pdd->e1, &a2, &a3, buffer));
test_sok(s,pdd->estring);
test_iok(pdd->e2,a2);
test_iok(pdd->e3,a3);
}
#endif
void
test_ecvt (void)
{
int a2,a3;
char *s;
s = check_null(ecvt(pdd->value, pdd->e1, &a2, &a3));
test_sok(s,pdd->estring);
test_iok(pdd->e2,a2);
test_iok(pdd->e3,a3);
#ifndef NO_NEWLIB
s = check_null(ecvtf(pdd->value, pdd->e1, &a2, &a3));
test_scok(s,pdd->estring, 6);
test_iok(pdd->e2,a2);
test_iok(pdd->e3,a3);
#endif
}
#ifndef NO_NEWLIB
void
test_fcvtbuf (void)
{
int a2,a3;
char *s;
s = check_null(fcvtbuf(pdd->value, pdd->f1, &a2, &a3, buffer));
test_scok(s,pdd->fstring,10);
test_iok(pdd->f2,a2);
test_iok(pdd->f3,a3);
}
#endif
void
test_gcvt (void)
{
char *s = check_null(gcvt(pdd->value, pdd->g1, buffer));
test_scok(s, pdd->gstring, 9);
#ifndef NO_NEWLIB
s = check_null(gcvtf(pdd->value, pdd->g1, buffer));
test_scok2(s, pdd->gstring, pdd->gfstring, 6);
#endif
}
void
test_fcvt (void)
{
int a2,a3;
char *sd;
sd = check_null(fcvt(pdd->value, pdd->f1, &a2, &a3));
test_scok(sd,pdd->fstring,10);
test_iok(pdd->f2,a2);
test_iok(pdd->f3,a3);
#ifndef NO_NEWLIB
char *sf;
double v1;
double v2;
char *sde, *sfe;
/* Test the float version by converting and inspecting the numbers 3
after reconverting */
sf = check_null(fcvtf(pdd->value, pdd->f1, &a2, &a3));
v1 = strtod(sd, &sde);
v2 = strtod(sf, &sfe);
/* float version may return fewer digits; expand to match */
int x = strlen(sd) - strlen(sf);
while (x-- > 0)
v2 *= 10;
if (strlen(sd) == 0) {
test_iok(0, sde - sd);
v1 = 0.0;
}
if (strlen(sf) == 0) {
test_iok(0, sfe - sf);
v2 = 0.0;
}
test_mok(v1, v2,30);
test_iok(pdd->f2,a2);
test_iok(pdd->f3,a3);
#endif
}
static void
diterate (void (*func)(),
char *name)
{
newfunc(name);
pdd = ddoubles;
while (pdd->estring) {
line(pdd->line);
errno = 0;
func();
pdd++;
}
}
void
deltest (void)
{
#if defined(TINY_STDIO) || !defined(NO_FLOATING_POINT)
newfunc("rounding");
line(1);
sprintf(buffer,"%.2f", 9.999);
test_sok(buffer,"10.00");
line(2);
sprintf(buffer,"%.2g", 1.0);
test_sok(buffer,"1");
line(3);
sprintf(buffer,"%.2g", 1.2e-6);
test_sok(buffer,"1.2e-06");
line(4);
sprintf(buffer,"%.0g", 1.0);
test_sok(buffer,"1");
line(5);
sprintf(buffer,"%.0e",1e1);
test_sok(buffer,"1e+01");
line(6);
sprintf(buffer, "%f", 12.3456789);
test_sok(buffer, "12.345679");
line(7);
sprintf(buffer, "%6.3f", 12.3456789);
test_sok(buffer, "12.346");
line(8);
sprintf(buffer,"%.0f", 12.3456789);
test_sok(buffer,"12");
#endif
}
/* Most of what sprint does is tested with the tests of
fcvt/ecvt/gcvt, but here are some more */
void
test_sprint (void)
{
#if defined(TINY_STDIO) || !defined(NO_FLOATING_POINT)
extern sprint_double_type sprint_doubles[];
sprint_double_type *s = sprint_doubles;
#endif
extern sprint_int_type sprint_ints[];
sprint_int_type *si = sprint_ints;
newfunc( "sprintf");
#if defined(TINY_STDIO) || !defined(NO_FLOATING_POINT)
while (s->line)
{
line( s->line);
sprintf(buffer, s->format_string, s->value);
#ifdef GENERATE_VECTORS
if (s->mag)
printf("{__LINE__, %.15e,\t\"%s\", \"%s\", %d },\n",
s->value, buffer, s->format_string, s->mag);
else
printf("{__LINE__, %.15e,\t\"%s\", \"%s\" },\n",
s->value, buffer, s->format_string);
#else
test_scok(buffer, s->result, 12); /* Only check the first 12 digs,
other stuff is random */
#endif
s++;
}
#endif
while (si->line)
{
line( si->line);
if (strchr(si->format_string, 'l'))
sprintf(buffer, si->format_string, (long) si->value);
else
sprintf(buffer, si->format_string, si->value);
#ifdef GENERATE_VECTORS
if (si->value < 0)
printf("__LINE__, -%#09x,\t\"%s\", \"%s\",\n",
-si->value, buffer, si->format_string);
else
printf("__LINE__, %#010x,\t\"%s\", \"%s\",\n",
si->value, buffer, si->format_string);
#else
test_sok(buffer, si->result);
#endif
si++;
}
}
/* Scanf calls strtod etc tested elsewhere, but also has some pattern matching skills */
void
test_scan (void)
{
int i,j;
#if defined(TINY_STDIO) || !defined(NO_FLOATING_POINT)
extern sprint_double_type sprint_doubles[];
sprint_double_type *s = sprint_doubles;
#endif
extern sprint_int_type sprint_ints[];
sprint_int_type *si = sprint_ints;
newfunc( "scanf");
#if defined(TINY_STDIO) || !defined(NO_FLOATING_POINT)
/* Test scanf by converting all the numbers in the sprint vectors
to and from their source and making sure nothing breaks */
while (s->line)
{
double d0,d1;
line( s->line);
sscanf(s->result, "%lg", &d0);
sprintf(buffer, "%.16e", d0);
sscanf(buffer, "%lg", &d1);
if (s->mag)
test_mok(d0, d1, s->mag);
else
test_mok(d0,d1, CONVERT_BITS_DOUBLE);
s++;
}
#endif
/* And integers too */
while (si->line)
{
long d0,d1;
line(si->line);
sscanf(si->result, "%ld", &d0);
sprintf(buffer, "%ld", d0);
sscanf(buffer, "%ld", &d1);
test_iok(d0,d1);
si++;
}
/* And the string matching */
sscanf(" 9","%d", &i);
test_iok(i, 9);
sscanf("foo bar 123 zap 456","foo bar %d zap %d", &i, &j);
test_iok(i, 123);
test_iok(j, 456);
sscanf("magicXYZZYfoobar","magic%[XYZ]", buffer);
test_sok("XYZZY", buffer);
sscanf("magicXYZZYfoobar","%[^XYZ]", buffer);
test_sok("magic", buffer);
}
#ifdef GENERATE_VECTORS
static void
gen_dvec(void)
{
char ebuf[128];
char fbuf[128];
char gbuf[128];
int e_decpt, e_sign;
int f_decpt, f_sign;
strcpy(ebuf, check_null(ecvt(pdd->value, pdd->e1, &e_decpt, &e_sign)));
strcpy(fbuf, check_null(fcvt(pdd->value, pdd->f1, &f_decpt, &f_sign)));
check_null(gcvt(pdd->value, pdd->g1, gbuf));
printf("__LINE__, %.15e,\"%s\",%d,%d,%d,\"%s\",%d,%d,%d,\"%s\",%d,\n\n",
pdd->value,
ebuf, pdd->e1, e_decpt, e_sign,
fbuf, pdd->f1, f_decpt, f_sign,
gbuf, pdd->g1);
}
#endif
extern int _malloc_test_fail;
void
test_cvt (void)
{
deltest();
#ifdef GENERATE_VECTORS
diterate(gen_dvec, "gen");
#else
#ifndef NO_NEWLIB
diterate(test_fcvtbuf,"fcvtbuf");
#endif
diterate(test_fcvt,"fcvt/fcvtf");
diterate(test_gcvt,"gcvt/gcvtf");
#ifndef NO_NEWLIB
diterate(test_ecvtbuf,"ecvtbuf");
#endif
diterate(test_ecvt,"ecvt/ecvtf");
#endif
iterate(test_strtod, "strtod");
#ifdef HAVE_STRTOLD
iterate(test_strtold, "strtold");
#endif
test_scan();
test_sprint();
iterate(test_atof, "atof");
#ifndef NO_NEWLIB
iterate(test_atoff, "atoff");
#endif
iterate(test_strtof, "strtof");
int_iterate(test_atoi,"atoi");
if (sizeof(int) == sizeof(long)) {
int_iterate(test_atol,"atol");
int_iterate(test_strtol, "strtol");
}
}
|