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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2019 Keith Packard
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <wchar.h>
#include <locale.h>
#include <limits.h>
#ifndef __PICOLIBC__
#define printf_float(x) ((double)(x))
#else
#if !defined(_HAS_IO_FLOAT) && !defined(_HAS_IO_DOUBLE)
#define __IO_NO_FLOATING_POINT
#endif
#ifndef _HAS_IO_POS_ARGS
#define NO_POS_ARGS
#endif
#ifndef _HAS_IO_MBCHAR
#define NO_MULTI_BYTE
#endif
#ifndef _HAS_IO_LONG_LONG
#define NO_LONG_LONG
#endif
#ifndef _HAS_IO_C99_FORMATS
#define NO_C99_FORMATS
#endif
#ifdef _HAS_IO_PERCENT_B
#define BINARY_FORMAT
#endif
#endif
#if !defined(__IO_NO_FLOATING_POINT)
static const double test_vals[] = { 1.234567, 1.1, M_PI };
#endif
static int
check_vsnprintf(char *str, size_t size, const char *format, ...)
{
int i;
va_list ap;
va_start(ap, format);
i = vsnprintf(str, size, format, ap);
va_end(ap);
return i;
}
#if !defined(__IO_NO_FLOATING_POINT)
#if _PICOLIBC_PRINTF == 'f'
#define float_type float
#define pow(a, b) powf((float)(a), (float)(b))
#define nextafter(a, b) nextafterf((float)(a), (float)(b))
#define fabs(a) fabsf(a)
#define scanf_format "%f"
#if (defined(__PICOLIBC__) && !defined(__IO_FLOAT_EXACT))
#define ERROR_MAX 1e-6
#else
#define ERROR_MAX 0
#endif
#else
#define float_type double
#define scanf_format "%lf"
#if defined(__PICOLIBC__) && !defined(__IO_FLOAT_EXACT)
#if __SIZEOF_DOUBLE__ == 4
#define ERROR_MAX 1e-6
#else
#define ERROR_MAX 1e-14
#endif
#else
#define ERROR_MAX 0
#endif
#endif
#endif
#ifndef NO_WIDE_IO
static const struct {
const wchar_t * const str;
const wchar_t * const fmt;
int expect;
} wtest[] = {
{ .str = L"foo\n", .fmt = L"foo\nbar", .expect = -1 },
{ .str = L"foo\n", .fmt = L"foo bar", .expect = -1 },
{ .str = L"foo\n", .fmt = L"foo %d", .expect = -1 },
{ .str = L"foo\n", .fmt = L"foo\n%d", .expect = -1 },
{ .str = L"foon", .fmt = L"foonbar", .expect = -1 },
{ .str = L"foon", .fmt = L"foon%d", .expect = -1 },
{ .str = L"foo ", .fmt = L"foo bar", .expect = -1 },
{ .str = L"foo ", .fmt = L"foo %d", .expect = -1 },
{ .str = L"foo\t", .fmt = L"foo\tbar", .expect = -1 },
{ .str = L"foo\t", .fmt = L"foo bar", .expect = -1 },
{ .str = L"foo\t", .fmt = L"foo %d", .expect = -1 },
{ .str = L"foo\t", .fmt = L"foo\t%d", .expect = -1 },
{ .str = L"foo", .fmt = L"foo", .expect = 0 },
{ .str = L"foon", .fmt = L"foo bar", .expect = 0 },
{ .str = L"foon", .fmt = L"foo %d", .expect = 0 },
{ .str = L"foo ", .fmt = L"fooxbar", .expect = 0 },
{ .str = L"foo ", .fmt = L"foox%d", .expect = 0 },
{ .str = L"foo bar", .fmt = L"foon", .expect = 0 },
{ .str = L"foo bar", .fmt = L"foo bar", .expect = 0 },
{ .str = L"foo bar", .fmt = L"foo %d", .expect = 0 },
{ .str = L"foo bar", .fmt = L"foon%d", .expect = 0 },
{ .str = L"foo (nil)", .fmt = L"foo %4p", .expect = 0 },
{ .str = L"foo ", .fmt = L"foo %n", .expect = 0 },
{ .str = L"foo%bar1", .fmt = L"foo%%bar%d", 1 },
{}
};
#endif
#ifndef __IO_NO_FLOATING_POINT
static float_type
int_exp10(int n)
{
float_type a = 1.0;
int sign = n < 0;
if (sign)
n = -n;
while (n--)
a *= (float_type)10.0;
if (sign)
a = 1 / a;
return a;
}
#ifndef NO_FLOAT_EXACT
static int
check_float(const char *test_name, float_type a, const char *buf, const char *head, int zeros,
const char *tail)
{
int z;
int o;
if (strncmp(buf, head, strlen(head)) != 0)
goto fail;
o = strlen(head);
for (z = 0; z < zeros; z++) {
if (buf[o] != '0')
goto fail;
o++;
}
if (strcmp(buf + o, tail) != 0)
goto fail;
return 0;
fail:
printf("float mismatch test %s: %a %.17e \"%s\" isn't in the form \"%s\", %d zeros, \"%s\"\n",
test_name, printf_float(a), printf_float(a), buf, head, zeros, tail);
return 1;
}
#endif
#endif
/*
* Compute arithmetic right shift. For negative values, flip the bits,
* shift and flip back. Otherwise, just shift. Thanks to Per Vognsen
* for this version which both gcc and clang both currently optimize
* to a single asr instruction in small tests.
*/
#define __asr(t, n) \
static inline t __asr_##n(t x, int s) \
{ \
return x < 0 ? ~(~x >> s) : x >> s; \
}
__asr(signed char, signed_char) __asr(short, short) __asr(int, int) __asr(long, long)
__asr(long long, long_long)
int __undefined_shift_size(int x, int s);
#define asr(__x, __s) \
((sizeof(__x) == sizeof(char)) ? (__typeof(__x))__asr_signed_char(__x, __s) \
: (sizeof(__x) == sizeof(short)) ? (__typeof(__x))__asr_short(__x, __s) \
: (sizeof(__x) == sizeof(int)) ? (__typeof(__x))__asr_int(__x, __s) \
: (sizeof(__x) == sizeof(long)) ? (__typeof(__x))__asr_long(__x, __s) \
: (sizeof(__x) == sizeof(long long)) ? (__typeof(__x))__asr_long_long(__x, __s) \
: __undefined_shift_size(__x, __s))
int
main(void)
{
int x = -35;
int y;
char buf[256];
int errors = 0;
#if !defined(__PICOLIBC__) || defined(__MB_CAPABLE)
if (!setlocale(LC_CTYPE, "C.UTF-8")) {
printf("setlocale(LC_CTYPE, \"C.UTF-8\") failed\n");
return 1;
}
#endif
#if 0
double a;
printf ("hello world\n");
for (x = 1; x < 20; x++) {
printf("%.*f\n", x, 9.99999999999);
}
for (a = 1e-10; a < 1e10; a *= 10.0) {
printf("g format: %10.3g %10.3g\n", 1.2345678 * a, 1.1 * a);
fflush(stdout);
}
for (a = 1e-10; a < 1e10; a *= 10.0) {
printf("f format: %10.3f %10.3f\n", 1.2345678 * a, 1.1 * a);
fflush(stdout);
}
for (a = 1e-10; a < 1e10; a *= 10.0) {
printf("e format: %10.3e %10.3e\n", 1.2345678 * a, 1.1 * a);
fflush(stdout);
}
printf ("%g\n", exp(11));
#endif
#ifndef NO_WIDE_IO
unsigned wt;
for (wt = 0; wtest[wt].str; wt++) {
void *extra;
int wtr = swscanf(wtest[wt].str, wtest[wt].fmt, &extra);
if (wtr != wtest[wt].expect) {
printf("%d str %ls fmt %ls expected %d got %d\n", wt, wtest[wt].str, wtest[wt].fmt,
wtest[wt].expect, wtr);
++errors;
}
}
#ifndef NO_MULTI_BYTE
{
wchar_t c;
char test_val[] = "㌰";
int i = sscanf(test_val, "%lc", &c);
if (i != 1 || c != L'㌰') {
printf("%d: %lc != %s or %d != 1\n", __LINE__, (wint_t)c, test_val, i);
++errors;
}
wchar_t wtest_val[] = L"㌰";
char c_mb[MB_LEN_MAX + 1] = { 0 };
i = swscanf(wtest_val, L"%c", c_mb);
if (i != 1 || strcmp(c_mb, test_val) != 0) {
printf("%d: %s != %s or %d != 1\n", __LINE__, c_mb, test_val, i);
++errors;
}
}
#endif
#endif
#if !defined(__IO_NO_FLOATING_POINT)
printf("checking basic floating point\n");
sprintf(buf, "%g", printf_float(0.0f));
if (strcmp(buf, "0") != 0) {
printf("0: wanted \"0\" got \"%s\"\n", buf);
errors++;
fflush(stdout);
}
#endif
#ifndef NO_POS_ARGS
printf("checking pos args\n");
x = y = 0;
int r = sscanf("3 4", "%2$d %1$d", &x, &y);
if (x != 4 || y != 3 || r != 2) {
printf("pos: wanted %d %d (ret %d) got %d %d (ret %d)\n", 4, 3, 2, x, y, r);
errors++;
fflush(stdout);
}
#endif
/*
* test snprintf and vsnprintf to make sure they don't
* overwrite the specified buffer length (even if that is
* zero)
*/
for (x = 0; x <= 6; x++) {
for (y = 0; y < 2; y++) {
char tbuf[10] = "xxxxxxxxx";
const char ref[10] = "xxxxxxxxx";
const char *name = (y == 0 ? "snprintf" : "vsnprintf");
int i = (y == 0 ? snprintf : check_vsnprintf)(tbuf, x, "%s", "123");
int y = x <= 4 ? x : 4;
if (i != 3) {
printf("%s(tbuf, %d, \"%%s\", \"123\") return %d instead of %d\n", name, x, i, 3);
errors++;
}
int l = strlen(tbuf);
if (y > 0 && l != y - 1) {
printf("%s: returned buffer len want %d got %d\n", name, y - 1, l);
errors++;
}
if (y > 0 && strncmp(tbuf, "123", y - 1) != 0) {
#if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 2) || __GNUC__ > 4)
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
strncpy(buf, "123", y - 1);
buf[y - 1] = '\0';
printf("%s: returned buffer want %s got %s\n", name, buf, tbuf);
errors++;
}
if (memcmp(tbuf + y, ref + y, sizeof(tbuf) - y) != 0) {
printf("%s: tail of buf mangled %s\n", name, tbuf + y);
errors++;
}
}
}
#define FMT(prefix, conv) "%" prefix conv
#define VERIFY_BOTH(prefix, oconv, iconv) \
do { \
int __n; \
sprintf(buf, FMT(prefix, oconv), v); \
__n = sscanf(buf, FMT(prefix, iconv), &r); \
if (v != r || __n != 1) { \
printf("\t%3d: " prefix " " oconv \
" wanted " FMT(prefix, oconv) " got " FMT(prefix, oconv) "\n", \
x, v, r); \
errors++; \
fflush(stdout); \
} \
} while (0)
#define VERIFY(prefix, conv) VERIFY_BOTH(prefix, conv, conv)
#ifdef BINARY_FORMAT
printf("checking binary format\n");
#define VERIFY_BINARY(prefix) VERIFY(prefix, "b")
#if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 2) || __GNUC__ > 4)
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif
#else
#define VERIFY_BINARY(prefix)
#endif
#define CHECK_RT(type, prefix) \
do { \
for (x = 0; x < (int)(sizeof(type) * 8); x++) { \
type v = (type)(((uint64_t)0x123456789abcdef0LL >> (64 - sizeof(type) * 8)) >> x); \
type r = ~v; \
VERIFY(prefix, "d"); \
r = ~v; \
VERIFY(prefix, "u"); \
r = ~v; \
VERIFY(prefix, "x"); \
r = ~v; \
VERIFY(prefix, "o"); \
r = ~v; \
VERIFY_BINARY(prefix); \
} \
} while (0)
#define CHECK_RTI(type, prefix) \
do { \
for (x = 0; x < (int)(sizeof(type) * 8); x++) { \
type v = asr((type)((uint64_t)0x123456789abcdef0LL >> (64 - sizeof(type) * 8)), x); \
type r = ~v; \
VERIFY(prefix, "d"); \
r = ~v; \
VERIFY(prefix, "u"); \
r = ~v; \
VERIFY(prefix, "x"); \
r = ~v; \
VERIFY(prefix, "o"); \
r = ~v; \
VERIFY_BINARY(prefix); \
} \
} while (0)
#ifndef __NANO_FORMATTED_IO
CHECK_RT(unsigned char, "hh");
#endif
CHECK_RT(unsigned short, "h");
CHECK_RT(unsigned int, "");
CHECK_RT(unsigned long, "l");
#ifndef NO_LONG_LONG
printf("checking long long\n");
CHECK_RT(unsigned long long, "ll");
#endif
#ifndef NO_C99_FORMATS
printf("checking c99 formats\n");
#ifdef NO_LONG_LONG
if (sizeof(intmax_t) <= sizeof(long))
#endif
{
CHECK_RTI(intmax_t, "j");
}
CHECK_RT(size_t, "z");
CHECK_RTI(ptrdiff_t, "t");
#endif
{
static int i_addr = 12;
void *v = &i_addr;
void *r = (void *)-1;
VERIFY("", "p");
}
#if !defined(__IO_NO_FLOATING_POINT)
printf("checking floating point\n");
#ifndef NO_FLOAT_EXACT
for (x = 0; x < 37; x++) {
float_type m, n, a, b, c;
float_type pow_val = int_exp10(x);
m = 1 / pow_val;
n = m / (float_type)2.0;
a = n;
b = n;
c = m;
/* Make sure the values are on the right side of the rounding value */
for (y = 0; y < 5; y++) {
a = nextafter(a, 2.0);
b = nextafter(b, 0.0);
c = nextafter(c, 0.0);
}
/*
* A value greater than 5 just past the last digit
* rounds up to 0.0...1
*/
sprintf(buf, "%.*f", x, printf_float(a));
if (x == 0)
errors += check_float(">= 0.5", a, buf, "1", x, "");
else
errors += check_float(">= 0.5", a, buf, "0.", x - 1, "1");
/*
* A value greater than 5 in the last digit
* rounds to 0.0...5
*/
sprintf(buf, "%.*f", x + 1, printf_float(a));
errors += check_float(">= 0.5", a, buf, "0.", x, "5");
/*
* A value less than 5 just past the last digit
* rounds down to 0.0...0
*/
sprintf(buf, "%.*f", x, printf_float(b));
if (x == 0)
errors += check_float("< 0.5", b, buf, "0", x, "");
else
errors += check_float("< 0.5", b, buf, "0.", x, "");
/*
* A value less than 5 in the last digit
* rounds to 0.0...5
*/
sprintf(buf, "%.*f", x + 1, printf_float(b));
errors += check_float("< 0.5", b, buf, "0.", x, "5");
/*
* A value less than 1 in the last digit
* rounds to 0.0...1
*/
sprintf(buf, "%.*f", x, printf_float(c));
if (x == 0)
errors += check_float("< 1", c, buf, "1", x, "");
else
errors += check_float("< 1", c, buf, "0.", x - 1, "1");
}
#endif
for (x = -37; x <= 37; x++) {
float_type r;
unsigned t;
for (t = 0; t < sizeof(test_vals) / sizeof(test_vals[0]); t++) {
float_type v = (float_type)test_vals[t] * int_exp10(x);
float_type e;
sprintf(buf, "%.55f", printf_float(v));
sscanf(buf, scanf_format, &r);
e = fabs(v - r) / v;
if (e > (float_type)ERROR_MAX) {
printf("\tf %3d: wanted %.7e %a got %.7e %a (error %.7e %a, buf %s)\n", x,
printf_float(v), printf_float(v), printf_float(r), printf_float(r),
printf_float(e), printf_float(e), buf);
errors++;
fflush(stdout);
}
sprintf(buf, "%.20e", printf_float(v));
sscanf(buf, scanf_format, &r);
e = fabs(v - r) / v;
if (e > (float_type)ERROR_MAX) {
printf("\te %3d: wanted %.7e got %.7e (error %.7e, buf %s)\n", x, printf_float(v),
printf_float(r), printf_float(e), buf);
errors++;
fflush(stdout);
}
sprintf(buf, "%.20g", printf_float(v));
sscanf(buf, scanf_format, &r);
e = fabs(v - r) / v;
if (e > (float_type)ERROR_MAX) {
printf("\tg %3d: wanted %.7e got %.7e (error %.7e, buf %s)\n", x, printf_float(v),
printf_float(r), printf_float(e), buf);
errors++;
fflush(stdout);
}
#ifndef NO_C99_FORMATS
sprintf(buf, "%.20a", printf_float(v));
sscanf(buf, scanf_format, &r);
e = fabs(v - r) / v;
if (e > (float_type)ERROR_MAX) {
printf("\ta %3d: wanted %.7e got %.7e (error %.7e, buf %s)\n", x, printf_float(v),
printf_float(r), printf_float(e), buf);
errors++;
fflush(stdout);
}
#endif
}
#ifndef NO_C99_FORMATS
sprintf(buf, "0x0.0p%+d", x);
sscanf(buf, scanf_format, &r);
if (r != (float_type)0.0) {
printf("\tg %3d: wanted 0.0 got %.7e (buf %s)\n", x, printf_float(r), buf);
errors++;
fflush(stdout);
}
sprintf(buf, "0x1p%+d", x);
sscanf(buf, scanf_format, &r);
if (r != (float_type)ldexp(1.0, x)) {
printf("\tg %3d: wanted 1 got %.7e (buf %s)\n", x, printf_float(r), buf);
errors++;
fflush(stdout);
}
#endif
}
#endif
printf("errors %d\n", errors);
fflush(stdout);
return errors;
}
|