Imported Upstream version 1.6.40
[platform/upstream/libpng.git] / contrib / libtests / tarith.c
index cdb00db..e35b7ab 100644 (file)
@@ -1,10 +1,9 @@
 
 /* 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
@@ -88,6 +87,7 @@ int validation_ascii_to_fp(int count, int argc, char **argv)
    int    minorarith = 0;
 
    while (--argc > 0)
+   {
       if (strcmp(*++argv, "-a") == 0)
          showall = 1;
       else if (strcmp(*argv, "-e") == 0 && argc > 0)
@@ -105,10 +105,11 @@ int validation_ascii_to_fp(int count, int argc, char **argv)
          fprintf(stderr, "unknown argument %s\n", *argv);
          return 1;
       }
+   }
 
    do
    {
-      png_size_t index;
+      size_t index;
       int state, failed = 0;
       char buffer[64];
 
@@ -130,8 +131,8 @@ int validation_ascii_to_fp(int count, int argc, char **argv)
        */
       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;
       }
 
@@ -146,16 +147,16 @@ int validation_ascii_to_fp(int count, int argc, char **argv)
          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))
@@ -176,7 +177,7 @@ int validation_ascii_to_fp(int count, int argc, char **argv)
       }
       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));
@@ -187,7 +188,7 @@ int validation_ascii_to_fp(int count, int argc, char **argv)
          /* 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
@@ -257,8 +258,9 @@ skip:
    }
    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);
 
@@ -329,10 +331,10 @@ static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
 {
    /* 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;
@@ -371,8 +373,8 @@ static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
    /* 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;
    }
 
@@ -503,8 +505,8 @@ static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
       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;
       }
 
@@ -521,10 +523,13 @@ static int check_all_characters(checkfp_command *co, checkfp_control c)
 {
    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;
@@ -539,10 +544,13 @@ static int check_some_characters(checkfp_command *co, checkfp_control c,
 
    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. */
@@ -616,10 +624,10 @@ int validation_muldiv(int count, int argc, char **argv)
    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;
@@ -634,7 +642,7 @@ int validation_muldiv(int count, int argc, char **argv)
    {
       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;
@@ -687,25 +695,25 @@ int validation_muldiv(int count, int argc, char **argv)
         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;
@@ -721,8 +729,8 @@ int validation_muldiv(int count, int argc, char **argv)
    }
    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;
 }
 
@@ -799,13 +807,13 @@ int validation_gamma(int argc, char **argv)
          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)
@@ -821,14 +829,15 @@ int validation_gamma(int argc, char **argv)
          {
             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;
@@ -841,13 +850,14 @@ int validation_gamma(int argc, char **argv)
             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)
@@ -859,13 +869,14 @@ int validation_gamma(int argc, char **argv)
             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)
@@ -877,13 +888,14 @@ int validation_gamma(int argc, char **argv)
             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. */
@@ -913,7 +925,7 @@ int validation_gamma(int argc, char **argv)
       }
 
       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)
@@ -932,7 +944,7 @@ int validation_gamma(int argc, char **argv)
       }
 
       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;
@@ -940,7 +952,7 @@ int validation_gamma(int argc, char **argv)
 
 /**************************** 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