/* tarith.c
*
+ * Copyright (c) 2021 Cosmin Truta
* Copyright (c) 2011-2013 John Cunningham Bowler
*
- * Last changed in libpng 1.6.0 [February 14, 2013]
- *
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
int minorarith = 0;
while (--argc > 0)
+ {
if (strcmp(*++argv, "-a") == 0)
showall = 1;
else if (strcmp(*argv, "-e") == 0 && argc > 0)
fprintf(stderr, "unknown argument %s\n", *argv);
return 1;
}
+ }
do
{
- png_size_t index;
+ size_t index;
int state, failed = 0;
char buffer[64];
*/
if (buffer[precision+7] != 71)
{
- fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n", test,
- precision, buffer, (unsigned long)strlen(buffer));
+ fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n",
+ test, precision, buffer, (unsigned long)strlen(buffer));
failed = 1;
}
if (test >= 0 && strcmp(buffer, "inf") ||
test < 0 && strcmp(buffer, "-inf"))
{
- fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n", test,
- precision, buffer);
+ fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n",
+ test, precision, buffer);
failed = 1;
}
}
else if (!png_check_fp_number(buffer, precision+10, &state, &index) ||
buffer[index] != 0)
{
- fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n", test,
- precision, buffer, buffer[index]);
+ fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n",
+ test, precision, buffer, buffer[index]);
failed = 1;
}
else if (PNG_FP_IS_NEGATIVE(state) && !(test < 0))
}
else if (PNG_FP_IS_POSITIVE(state) && !(test > 0))
{
- fprintf(stderr, "%g[%d] -> '%s' but postive value not so reported\n",
+ fprintf(stderr, "%g[%d] -> '%s' but positive value not so reported\n",
test, precision, buffer);
failed = 1;
assert(!PNG_FP_IS_NEGATIVE(state));
/* Check the result against the original. */
double out = atof(buffer);
double change = fabs((out - test)/test);
- double allow = .5/pow(10,
+ double allow = .5 / pow(10,
(precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
/* NOTE: if you hit this error case are you compiling with gcc
}
while (--count);
- printf("Tested %d finite values, %d non-finite, %d OK (%d failed) %d minor "
- "arithmetic errors\n", finite, nonfinite, ok, failcount, minorarith);
+ printf("Tested %d finite values, %d non-finite, %d OK (%d failed) "
+ "%d minor arithmetic errors\n",
+ finite, nonfinite, ok, failcount, minorarith);
printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
{
/* Test this character (ch) to ensure the parser does the correct thing.
*/
- png_size_t index = 0;
+ size_t index = 0;
const char test = (char)ch;
- const int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
- const int character_accepted = (index == 1);
+ int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
+ int character_accepted = (index == 1);
if (c.check_state != exponent && isdigit(ch) && ch != '0')
c.is_zero = 0;
/* This should never fail (it's a serious bug if it does): */
if (index != 0 && index != 1)
{
- fprintf(stderr, "%s: read beyond end of string (%lu)\n", co->number,
- (unsigned long)index);
+ fprintf(stderr, "%s: read beyond end of string (%lu)\n",
+ co->number, (unsigned long)index);
return 0;
}
if (number_is_valid != c.number_was_valid)
{
fprintf(stderr,
- "%s: character '%c' [0x%.2x] changed number validity\n", co->number,
- ch, ch);
+ "%s: character '%c' [0x%.2x] changed number validity\n",
+ co->number, ch, ch);
return 0;
}
{
int ch;
- if (c.cnumber+4 < sizeof co->number) for (ch=0; ch<256; ++ch)
+ if (c.cnumber+4 < sizeof co->number)
{
- if (!check_one_character(co, c, ch))
- return 0;
+ for (ch=0; ch<256; ++ch)
+ {
+ if (!check_one_character(co, c, ch))
+ return 0;
+ }
}
return 1;
if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
{
- if (c.limit > 0) for (i=0; tests[i]; ++i)
+ if (c.limit > 0)
{
- if (!check_one_character(co, c, tests[i]))
- return 0;
+ for (i=0; tests[i]; ++i)
+ {
+ if (!check_one_character(co, c, tests[i]))
+ return 0;
+ }
}
/* At the end check all the characters. */
png_int_32 times, div;
while (--argc > 0)
- {
- fprintf(stderr, "unknown argument %s\n", *++argv);
- return 1;
- }
+ {
+ fprintf(stderr, "unknown argument %s\n", *++argv);
+ return 1;
+ }
/* Find out about the random number generator. */
randbuffer = RAND_MAX;
{
png_fixed_point result;
/* NOTE: your mileage may vary, a type is required below that can
- * hold 64 bits or more, if floating point is used a 64 bit or
+ * hold 64 bits or more, if floating point is used a 64-bit or
* better mantissa is required.
*/
long long int fp, fpround;
ok = 0, ++overflow, fpround = fp/*misleading*/;
if (verbose)
- fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n", a, times, div,
- fp, ok ? "ok" : "overflow");
+ fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n",
+ a, times, div, fp, ok ? "ok" : "overflow");
++tested;
if (png_muldiv(&result, a, times, div) != ok)
{
++error;
if (ok)
- fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n", a,
- times, div, fp);
+ fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n",
+ a, times, div, fp);
else
- fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n", a,
- times, div, result, fp);
+ fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n",
+ a, times, div, result, fp);
}
else if (ok && result != fpround)
{
++error;
- fprintf(stderr, "%d * %d / %d -> %d not %lld\n", a, times, div, result,
- fp);
+ fprintf(stderr, "%d * %d / %d -> %d not %lld\n",
+ a, times, div, result, fp);
}
else
++passed;
}
while (--count > 0);
- printf("%d tests including %d overflows, %d passed, %d failed (%d 64 bit "
- "errors)\n", tested, overflow, passed, error, error64);
+ printf("%d tests including %d overflows, %d passed, %d failed "
+ "(%d 64-bit errors)\n", tested, overflow, passed, error, error64);
return 0;
}
if (i == 0 && png_log8bit(i) != 0xffffffff ||
i != 0 && png_log8bit(i) != floor(correct+.5))
{
- fprintf(stderr, "8 bit log error: %d: got %u, expected %f\n",
+ fprintf(stderr, "8-bit log error: %d: got %u, expected %f\n",
i, png_log8bit(i), correct);
}
}
if (!silent)
- printf("maximum 8 bit log error = %f\n", maxerr);
+ printf("maximum 8-bit log error = %f\n", maxerr);
maxerr = 0;
for (i=0; i<65536; ++i)
{
if (error > .68) /* By experiment error is less than .68 */
{
- fprintf(stderr, "16 bit log error: %d: got %u, expected %f"
- " error: %f\n", i, png_log16bit(i), correct, error);
+ fprintf(stderr,
+ "16-bit log error: %d: got %u, expected %f error: %f\n",
+ i, png_log16bit(i), correct, error);
}
}
}
if (!silent)
- printf("maximum 16 bit log error = %f\n", maxerr);
+ printf("maximum 16-bit log error = %f\n", maxerr);
/* Now exponentiations. */
maxerr = 0;
maxerr = fabs(error);
if (fabs(error) > 1883) /* By experiment. */
{
- fprintf(stderr, "32 bit exp error: %d: got %u, expected %f"
- " error: %f\n", i, png_exp(i), correct, error);
+ fprintf(stderr,
+ "32-bit exp error: %d: got %u, expected %f error: %f\n",
+ i, png_exp(i), correct, error);
}
}
if (!silent)
- printf("maximum 32 bit exp error = %f\n", maxerr);
+ printf("maximum 32-bit exp error = %f\n", maxerr);
maxerr = 0;
for (i=0; i<=0xfffff; ++i)
maxerr = fabs(error);
if (fabs(error) > .50002) /* By experiment */
{
- fprintf(stderr, "8 bit exp error: %d: got %u, expected %f"
- " error: %f\n", i, png_exp8bit(i), correct, error);
+ fprintf(stderr,
+ "8-bit exp error: %d: got %u, expected %f error: %f\n",
+ i, png_exp8bit(i), correct, error);
}
}
if (!silent)
- printf("maximum 8 bit exp error = %f\n", maxerr);
+ printf("maximum 8-bit exp error = %f\n", maxerr);
maxerr = 0;
for (i=0; i<=0xfffff; ++i)
maxerr = fabs(error);
if (fabs(error) > .524) /* By experiment */
{
- fprintf(stderr, "16 bit exp error: %d: got %u, expected %f"
- " error: %f\n", i, png_exp16bit(i), correct, error);
+ fprintf(stderr,
+ "16-bit exp error: %d: got %u, expected %f error: %f\n",
+ i, png_exp16bit(i), correct, error);
}
}
if (!silent)
- printf("maximum 16 bit exp error = %f\n", maxerr);
+ printf("maximum 16-bit exp error = %f\n", maxerr);
} /* !onlygamma */
/* Test the overall gamma correction. */
}
if (!silent)
- printf("gamma %f: maximum 8 bit error %f\n", g, maxerr);
+ printf("gamma %f: maximum 8-bit error %f\n", g, maxerr);
maxerr = 0;
for (j=0; j<65536; ++j)
}
if (!silent)
- printf("gamma %f: maximum 16 bit error %f\n", g, maxerr);
+ printf("gamma %f: maximum 16-bit error %f\n", g, maxerr);
}
return 0;
/**************************** VALIDATION TESTS ********************************/
/* Various validation routines are included herein, they require some
- * definition for png_warning and png_error, seetings of VALIDATION:
+ * definition for png_warning and png_error, settings of VALIDATION:
*
* 1: validates the ASCII to floating point conversions
* 2: validates png_muldiv