home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / sprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.6 KB  |  436 lines

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