tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sprintf.c (12103B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * File:    sprintf.c
      8 * Description:
      9 *     This is a test program for the PR_snprintf() functions defined
     10 *     in prprf.c.  This test program is based on ns/nspr/tests/sprintf.c,
     11 *     revision 1.10.
     12 * Modification History:
     13 *  20-May-1997 AGarcia replaced printf statment to return PASS\n. This is to be
     14 *used by the regress tool parsing routine.
     15 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
     16 *updated to recognize the return code from tha main program.
     17 */
     18 
     19 #include "prinit.h"
     20 #include "prprf.h"
     21 #include "prlog.h"
     22 #include "prlong.h"
     23 #include <string.h>
     24 #include <stdio.h>
     25 #include <stdlib.h>
     26 
     27 static char sbuf[20000];
     28 
     29 /*
     30 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
     31 ** Make sure the results are identical
     32 */
     33 static void test_i(char* pattern, int i) {
     34  char* s;
     35  char buf[200];
     36  int n;
     37 
     38  /* try all three routines */
     39  s = PR_smprintf(pattern, i);
     40  PR_ASSERT(s != 0);
     41  n = PR_snprintf(buf, sizeof(buf), pattern, i);
     42  PR_ASSERT(n <= sizeof(buf));
     43  sprintf(sbuf, pattern, i);
     44 
     45  /* compare results */
     46  if ((strncmp(s, buf, sizeof(buf)) != 0) ||
     47      (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
     48    fprintf(stderr,
     49            "pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n    "
     50            "sprintf='%s'\n",
     51            pattern, i, s, buf, sbuf);
     52    PR_smprintf_free(s);
     53    exit(-1);
     54  }
     55  PR_smprintf_free(s);
     56 }
     57 
     58 static void TestI(void) {
     59  static int nums[] = {
     60      0, 1, -1, 10, -10, 32767, -32768,
     61  };
     62  static char* signs[] = {
     63      "",     "0",    "-",    "+",    " ",    "0-",   "0+",   "0 ",   "-0",
     64      "-+",   "- ",   "+0",   "+-",   "+ ",   " 0",   " -",   " +",   "0-+",
     65      "0- ",  "0+-",  "0+ ",  "0 -",  "0 +",  "-0+",  "-0 ",  "-+0",  "-+ ",
     66      "- 0",  "- +",  "+0-",  "+0 ",  "+-0",  "+- ",  "+ 0",  "+ -",  " 0-",
     67      " 0+",  " -0",  " -+",  " +0",  " +-",  "0-+ ", "0- +", "0+- ", "0+ -",
     68      "0 -+", "0 +-", "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", "+0- ",
     69      "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", " 0-+", " 0+-", " -0+", " -+0",
     70      " +0-", " +-0",
     71  };
     72  static char* precs[] = {
     73      "", "3", "5", "43", "7.3", "7.5", "7.11", "7.43",
     74  };
     75  static char* formats[] = {"d", "o", "x", "u", "hd", "ho", "hx", "hu"};
     76  int f, s, n, p;
     77  char fmt[20];
     78 
     79  for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
     80    for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
     81      for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
     82        fmt[0] = '%';
     83        fmt[1] = 0;
     84        if (signs[s]) {
     85          strcat(fmt, signs[s]);
     86        }
     87        if (precs[p]) {
     88          strcat(fmt, precs[p]);
     89        }
     90        if (formats[f]) {
     91          strcat(fmt, formats[f]);
     92        }
     93        for (n = 0; n < PR_ARRAY_SIZE(nums); n++) {
     94          test_i(fmt, nums[n]);
     95        }
     96      }
     97    }
     98  }
     99 }
    100 
    101 /************************************************************************/
    102 
    103 /*
    104 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
    105 ** Make sure the results are identical
    106 */
    107 static void test_l(char* pattern, char* spattern, PRInt32 l) {
    108  char* s;
    109  char buf[200];
    110  int n;
    111 
    112  /* try all three routines */
    113  s = PR_smprintf(pattern, l);
    114  PR_ASSERT(s != 0);
    115  n = PR_snprintf(buf, sizeof(buf), pattern, l);
    116  PR_ASSERT(n <= sizeof(buf));
    117  sprintf(sbuf, spattern, l);
    118 
    119  /* compare results */
    120  if ((strncmp(s, buf, sizeof(buf)) != 0) ||
    121      (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
    122    fprintf(stderr,
    123            "pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n    "
    124            "sprintf='%s'\n",
    125            pattern, l, s, buf, sbuf);
    126    PR_smprintf_free(s);
    127    exit(-1);
    128  }
    129  PR_smprintf_free(s);
    130 }
    131 
    132 static void TestL(void) {
    133  static PRInt32 nums[] = {
    134      0,
    135      1,
    136      -1,
    137      10,
    138      -10,
    139      32767,
    140      -32768,
    141      PR_INT32(0x7fffffff),     /* 2147483647L */
    142      -1 - PR_INT32(0x7fffffff) /* -2147483648L */
    143  };
    144  static char* signs[] = {
    145      "",     "0",    "-",    "+",    " ",    "0-",   "0+",   "0 ",   "-0",
    146      "-+",   "- ",   "+0",   "+-",   "+ ",   " 0",   " -",   " +",   "0-+",
    147      "0- ",  "0+-",  "0+ ",  "0 -",  "0 +",  "-0+",  "-0 ",  "-+0",  "-+ ",
    148      "- 0",  "- +",  "+0-",  "+0 ",  "+-0",  "+- ",  "+ 0",  "+ -",  " 0-",
    149      " 0+",  " -0",  " -+",  " +0",  " +-",  "0-+ ", "0- +", "0+- ", "0+ -",
    150      "0 -+", "0 +-", "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", "+0- ",
    151      "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", " 0-+", " 0+-", " -0+", " -+0",
    152      " +0-", " +-0",
    153  };
    154  static char* precs[] = {
    155      "", "3", "5", "43", ".3", ".43", "7.3", "7.5", "7.11", "7.43",
    156  };
    157  static char* formats[] = {"ld", "lo", "lx", "lu"};
    158 
    159 #if PR_BYTES_PER_INT == 4
    160  static char* sformats[] = {"d", "o", "x", "u"};
    161 #elif PR_BYTES_PER_LONG == 4
    162  static char* sformats[] = {"ld", "lo", "lx", "lu"};
    163 #else
    164 #  error Neither int nor long is 4 bytes on this platform
    165 #endif
    166 
    167  int f, s, n, p;
    168  char fmt[40], sfmt[40];
    169 
    170  for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
    171    for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
    172      for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
    173        fmt[0] = '%';
    174        fmt[1] = 0;
    175        if (signs[s]) {
    176          strcat(fmt, signs[s]);
    177        }
    178        if (precs[p]) {
    179          strcat(fmt, precs[p]);
    180        }
    181        strcpy(sfmt, fmt);
    182        if (formats[f]) {
    183          strcat(fmt, formats[f]);
    184        }
    185        if (sformats[f]) {
    186          strcat(sfmt, sformats[f]);
    187        }
    188        for (n = 0; n < PR_ARRAY_SIZE(nums); n++) {
    189          test_l(fmt, sfmt, nums[n]);
    190        }
    191      }
    192    }
    193  }
    194 }
    195 
    196 /************************************************************************/
    197 
    198 /*
    199 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
    200 ** Make sure the results are identical
    201 */
    202 static void test_ll(char* pattern, char* spattern, PRInt64 l) {
    203  char* s;
    204  char buf[200];
    205  int n;
    206 
    207  /* try all three routines */
    208  s = PR_smprintf(pattern, l);
    209  PR_ASSERT(s != 0);
    210  n = PR_snprintf(buf, sizeof(buf), pattern, l);
    211  PR_ASSERT(n <= sizeof(buf));
    212 #if defined(HAVE_LONG_LONG)
    213  sprintf(sbuf, spattern, l);
    214 
    215  /* compare results */
    216  if ((strncmp(s, buf, sizeof(buf)) != 0) ||
    217      (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
    218 #  if PR_BYTES_PER_LONG == 8
    219 #    define FORMAT_SPEC "%ld"
    220 #  elif defined(WIN32)
    221 #    define FORMAT_SPEC "%I64d"
    222 #  else
    223 #    define FORMAT_SPEC "%lld"
    224 #  endif
    225    fprintf(stderr,
    226            "pattern='%s' ll=" FORMAT_SPEC
    227            "\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
    228            pattern, l, s, buf, sbuf);
    229    printf("FAIL\n");
    230    PR_smprintf_free(s);
    231    exit(-1);
    232  }
    233  PR_smprintf_free(s);
    234 #else
    235  /* compare results */
    236  if ((strncmp(s, buf, sizeof(buf)) != 0)) {
    237    fprintf(
    238        stderr,
    239        "pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
    240        pattern, s, buf, sbuf);
    241    printf("FAIL\n");
    242    PR_smprintf_free(s);
    243    exit(-1);
    244  }
    245  PR_smprintf_free(s);
    246 #endif
    247 }
    248 
    249 static void TestLL(void) {
    250  static PRInt64 nums[] = {
    251      LL_INIT(0, 0),
    252      LL_INIT(0, 1),
    253      LL_INIT(0xffffffff, 0xffffffff), /* -1 */
    254      LL_INIT(0, 10),
    255      LL_INIT(0xffffffff, 0xfffffff6), /* -10 */
    256      LL_INIT(0, 32767),
    257      LL_INIT(0xffffffff, 0xffff8000), /* -32768 */
    258      LL_INIT(0, 0x7fffffff),          /* 2147483647 */
    259      LL_INIT(0xffffffff, 0x80000000), /* -2147483648 */
    260      LL_INIT(0x7fffffff, 0xffffffff), /* 9223372036854775807 */
    261      LL_INIT(0x80000000, 0),          /* -9223372036854775808 */
    262      PR_INT64(0),
    263      PR_INT64(1),
    264      PR_INT64(-1),
    265      PR_INT64(10),
    266      PR_INT64(-10),
    267      PR_INT64(32767),
    268      PR_INT64(-32768),
    269      PR_INT64(2147483647),
    270      PR_INT64(-2147483648),
    271      PR_INT64(9223372036854775807),
    272      PR_INT64(-9223372036854775808)};
    273 
    274  static char* signs[] = {
    275      "",     "0",    "-",    "+",    " ",    "0-",   "0+",   "0 ",   "-0",
    276      "-+",   "- ",   "+0",   "+-",   "+ ",   " 0",   " -",   " +",   "0-+",
    277      "0- ",  "0+-",  "0+ ",  "0 -",  "0 +",  "-0+",  "-0 ",  "-+0",  "-+ ",
    278      "- 0",  "- +",  "+0-",  "+0 ",  "+-0",  "+- ",  "+ 0",  "+ -",  " 0-",
    279      " 0+",  " -0",  " -+",  " +0",  " +-",  "0-+ ", "0- +", "0+- ", "0+ -",
    280      "0 -+", "0 +-", "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", "+0- ",
    281      "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", " 0-+", " 0+-", " -0+", " -+0",
    282      " +0-", " +-0",
    283  };
    284  static char* precs[] = {
    285      "", "3", "5", "43", ".3", ".43", "7.3", "7.5", "7.11", "7.43",
    286  };
    287  static char* formats[] = {"lld", "llo", "llx", "llu"};
    288 
    289 #if PR_BYTES_PER_LONG == 8
    290  static char* sformats[] = {"ld", "lo", "lx", "lu"};
    291 #elif defined(WIN32)
    292  static char* sformats[] = {"I64d", "I64o", "I64x", "I64u"};
    293 #else
    294  static char* sformats[] = {"lld", "llo", "llx", "llu"};
    295 #endif
    296 
    297  int f, s, n, p;
    298  char fmt[40], sfmt[40];
    299 
    300  for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
    301    for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
    302      for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
    303        fmt[0] = '%';
    304        fmt[1] = 0;
    305        if (signs[s]) {
    306          strcat(fmt, signs[s]);
    307        }
    308        if (precs[p]) {
    309          strcat(fmt, precs[p]);
    310        }
    311        strcpy(sfmt, fmt);
    312        if (formats[f]) {
    313          strcat(fmt, formats[f]);
    314        }
    315        if (sformats[f]) {
    316          strcat(sfmt, sformats[f]);
    317        }
    318        for (n = 0; n < PR_ARRAY_SIZE(nums); n++) {
    319          test_ll(fmt, sfmt, nums[n]);
    320        }
    321      }
    322    }
    323  }
    324 }
    325 
    326 /************************************************************************/
    327 
    328 /*
    329 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
    330 ** Make sure the results are identical
    331 */
    332 static void test_s(char* pattern, char* ss) {
    333  char* s;
    334  unsigned char before[8];
    335  char buf[200];
    336  unsigned char after[8];
    337  int n;
    338 
    339  memset(before, 0xBB, 8);
    340  memset(after, 0xAA, 8);
    341 
    342  /* try all three routines */
    343  s = PR_smprintf(pattern, ss);
    344  PR_ASSERT(s != 0);
    345  n = PR_snprintf(buf, sizeof(buf), pattern, ss);
    346  PR_ASSERT(n <= sizeof(buf));
    347  sprintf(sbuf, pattern, ss);
    348 
    349  for (n = 0; n < 8; n++) {
    350    PR_ASSERT(before[n] == 0xBB);
    351    PR_ASSERT(after[n] == 0xAA);
    352  }
    353 
    354  /* compare results */
    355  if ((strncmp(s, buf, sizeof(buf)) != 0) ||
    356      (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
    357    fprintf(stderr,
    358            "pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n    "
    359            "sprintf='%s'\n",
    360            pattern, ss, s, buf, sbuf);
    361    printf("FAIL\n");
    362    PR_smprintf_free(s);
    363    exit(-1);
    364  }
    365  PR_smprintf_free(s);
    366 }
    367 
    368 static void TestS(void) {
    369  static char* strs[] = {
    370      "",
    371      "a",
    372      "abc",
    373      "abcde",
    374      "abcdefABCDEF",
    375      "abcdefghijklmnopqrstuvwxyz0123456789!@#$"
    376      "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$"
    377      "abcdefghijklmnopqrstuvwxyz0123456789!@#$",
    378  };
    379  /* '0' is not relevant to printing strings */
    380  static char* signs[] = {
    381      "",   "-",  "+",   " ",   "-+",  "- ",  "+-",  "+ ",
    382      " -", " +", "-+ ", "- +", "+- ", "+ -", " -+", " +-",
    383  };
    384  static char* precs[] = {
    385      "", "3", "5", "43", ".3", ".43", "7.3", "7.5", "7.11", "7.43",
    386  };
    387  static char* formats[] = {"s"};
    388  int f, s, n, p;
    389  char fmt[40];
    390 
    391  for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
    392    for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
    393      for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
    394        fmt[0] = '%';
    395        fmt[1] = 0;
    396        if (signs[s]) {
    397          strcat(fmt + strlen(fmt), signs[s]);
    398        }
    399        if (precs[p]) {
    400          strcat(fmt + strlen(fmt), precs[p]);
    401        }
    402        if (formats[f]) {
    403          strcat(fmt + strlen(fmt), formats[f]);
    404        }
    405        for (n = 0; n < PR_ARRAY_SIZE(strs); n++) {
    406          test_s(fmt, strs[n]);
    407        }
    408      }
    409    }
    410  }
    411 }
    412 
    413 /************************************************************************/
    414 
    415 int main(int argc, char** argv) {
    416  TestI();
    417  TestL();
    418  TestLL();
    419  TestS();
    420  printf("PASS\n");
    421  return 0;
    422 }