stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r372301 - in /incubator/stdcxx/trunk/tests: self/0.printf.cpp src/printf.cpp
Date Wed, 25 Jan 2006 20:07:03 GMT
Author: sebor
Date: Wed Jan 25 12:06:59 2006
New Revision: 372301

URL: http://svn.apache.org/viewcvs?rev=372301&view=rev
Log:
2006-01-25  Martin Sebor  <sebor@roguewave.com>

	* printf.cpp (_rw_fmtchr, _rw_fmwtchr): Added a noesc argument
	for callers to control whether to escape non-printable characters
	independently of quoting.
	(_rw_vasnprintf_c99, _rw_vasnprintf_ext): Disabled and enabled,
	respectively, the use of escape sequences when formatting narrow
	and wide characters using the %c, %{c}, %lc, and %{lc} directives.
	* 0.printf.cpp: Exercised the above.

Modified:
    incubator/stdcxx/trunk/tests/self/0.printf.cpp
    incubator/stdcxx/trunk/tests/src/printf.cpp

Modified: incubator/stdcxx/trunk/tests/self/0.printf.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/self/0.printf.cpp?rev=372301&r1=372300&r2=372301&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/self/0.printf.cpp (original)
+++ incubator/stdcxx/trunk/tests/self/0.printf.cpp Wed Jan 25 12:06:59 2006
@@ -37,7 +37,9 @@
 
 /***********************************************************************/
 
-int exit_status /* = 0 */;
+int ntests;      // number of tests performed
+int nfailures;   // number of failures
+
 
 static void
 do_test (int         line,     // line number of the test case
@@ -45,6 +47,8 @@
          const char *expect,   // expected output or 0 on error
          const char *result)   // actual result (0 on error)
 {
+    ++ntests;
+
     const char* q_fmt = "\"\"";
 
     if (0 == fmt) {
@@ -57,7 +61,7 @@
         const int cmp = memcmp (expect, result, strlen (expect) + 1);
 
         if (cmp) {
-            exit_status = 2;
+            ++nfailures;
             fprintf (stderr,
                      "Assertion failed on line %d: "
                      "rw_sprintf(%c%s%c, ...) == \"%s\", got \"%s\"\n",
@@ -65,7 +69,7 @@
         }
     }
     else if (result || expect) {
-        exit_status = 2;
+        ++nfailures;
 
         const char* q_expect = "\"\"";
         const char* q_result = "\"\"";
@@ -101,6 +105,7 @@
 #undef TEST_SPEC
 #define TEST_SPEC(pfx, a1, a2, a3, expect)                              \
     do {                                                                \
+        ++ntests;                                                       \
         char fmt [64];                                                  \
         sprintf (fmt, "%s%c", pfx, spec);                               \
         char* const s0 = rw_sprintfa (fmt, a1, a2, a3);                 \
@@ -111,7 +116,7 @@
             strcpy (buf, (expect) ? (expect) : "");                     \
         const int result = memcmp (buf, s0, strlen (buf) + 1);          \
         if (result) {                                                   \
-           exit_status = 2;                                             \
+           ++nfailures;                                                 \
            fprintf (stderr,                                             \
                     "Assertion failed on line %d: "                     \
                     "rw_sprintf(\"%s\", %ld, %ld, %ld) "                \
@@ -204,20 +209,22 @@
     TEST_SPEC ("%#",  'a', 0,  0, "'a'");
 
     //////////////////////////////////////////////////////////////////
-    printf ("%s\n", "extension: \"%{c}\": quoted character");
+    printf ("%s\n", "extension: \"%{c}\": escaped character");
 
-    TEST ("%{c}", '\0',   0, 0, "\0");
-    TEST ("%{c}", '\2',   0, 0, "\2");
-    TEST ("%{c}", '\a',   0, 0, "\a");
-    TEST ("%{c}", '\n',   0, 0, "\n");
-    TEST ("%{c}", '\r',   0, 0, "\r");
-    TEST ("%{c}", '\t',   0, 0, "\t");
+    TEST ("%{c}", '\0',   0, 0, "\\0");
+    TEST ("%{c}", '\2',   0, 0, "\\x02");
+    TEST ("%{c}", '\a',   0, 0, "\\a");
+    TEST ("%{c}", '\n',   0, 0, "\\n");
+    TEST ("%{c}", '\r',   0, 0, "\\r");
+    TEST ("%{c}", '\t',   0, 0, "\\t");
     TEST ("%{c}", '0',    0, 0, "0");
     TEST ("%{c}", '2',    0, 0, "2");
     TEST ("%{c}", 'A',    0, 0, "A");
     TEST ("%{c}", 'Z',    0, 0, "Z");
-    TEST ("%{c}", '\xff', 0, 0, "\xff");
+    TEST ("%{c}", '\xff', 0, 0, "\\xff");
 
+    //////////////////////////////////////////////////////////////////
+    printf ("%s\n", "extension: \"%{#c}\": quoted escaped character");
     TEST ("%{#c}", '\0',   0, 0, "'\\0'");
     TEST ("%{#c}", '\3',   0, 0, "'\\x03'");
     TEST ("%{#c}", '\a',   0, 0, "'\\a'");
@@ -244,21 +251,25 @@
     TEST_SPEC ("%", L'Z',    0, 0, "Z");
 
     //////////////////////////////////////////////////////////////////
-    printf ("%s\n", "extension: \"%{lc}\": quoted wide character");
+    printf ("%s\n", "extension: \"%{lc}\": escaped wide character");
 
-    TEST ("%{lc}", L'\0',   0, 0, "\0");
-    TEST ("%{lc}", L'\a',   0, 0, "\a");
-    TEST ("%{lc}", L'\n',   0, 0, "\n");
-    TEST ("%{lc}", L'\r',   0, 0, "\r");
-    TEST ("%{lc}", L'\t',   0, 0, "\t");
+    TEST ("%{lc}", L'\0',   0, 0, "\\0");
+    TEST ("%{lc}", L'\4',   0, 0, "\\x04");
+    TEST ("%{lc}", L'\a',   0, 0, "\\a");
+    TEST ("%{lc}", L'\n',   0, 0, "\\n");
+    TEST ("%{lc}", L'\r',   0, 0, "\\r");
+    TEST ("%{lc}", L'\t',   0, 0, "\\t");
     TEST ("%{lc}", L'0',    0, 0, "0");
     TEST ("%{lc}", L'1',    0, 0, "1");
     TEST ("%{lc}", L'A',    0, 0, "A");
     TEST ("%{lc}", L'Z',    0, 0, "Z");
-    TEST ("%{lc}", L'\xff', 0, 0, "\xff");
+    TEST ("%{lc}", L'\xff', 0, 0, "\\xff");
+
+    //////////////////////////////////////////////////////////////////
+    printf ("%s\n", "extension: \"%{#lc}\": quoted escaped wide character");
 
     TEST ("%{#lc}", L'\0',   0, 0, "L'\\0'");
-    TEST ("%{#lc}", L'\1',   0, 0, "L'\\x01'");
+    TEST ("%{#lc}", L'\5',   0, 0, "L'\\x05'");
     TEST ("%{#lc}", L'\a',   0, 0, "L'\\a'");
     TEST ("%{#lc}", L'\n',   0, 0, "L'\\n'");
     TEST ("%{#lc}", L'\r',   0, 0, "L'\\r'");
@@ -1534,11 +1545,11 @@
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{#m}\": errno");
 
-    int ntests = 0;
+    int count = 0;
 
 #ifdef EDOM
 
-    ++ntests;
+    ++count;
 
     errno = EDOM;
     TEST ("%{#m}", 0, 0, 0, "EDOM");
@@ -1550,7 +1561,7 @@
 
 #ifdef ERANGE
 
-    ++ntests;
+    ++count;
 
     errno = ERANGE;
     TEST ("%{#m}", 0, 0, 0, "ERANGE");
@@ -1562,7 +1573,7 @@
 
 #ifdef EILSEQ
 
-    ++ntests;
+    ++count;
 
     errno = EILSEQ;
     TEST ("%{#m}", 0, 0, 0, "EILSEQ");
@@ -1572,7 +1583,7 @@
 
 #endif   // EILSEQ
 
-    if (0 == ntests)
+    if (0 == count)
         fprintf (stderr, "%s\n", "%{#m}: could not test");
 
     errno = 0;
@@ -1914,19 +1925,19 @@
 {
     if (0 == pbuf) {
         fprintf (stderr, "pbuf: unexpected null argument #1");
-        exit_status = 1;
+        ++nfailures;
         return -1;
     }
 
     if (0 == pbufsize) {
         fprintf (stderr, "pbufsize: unexpected null argument #2");
-        exit_status = 1;
+        ++nfailures;
         return -1;
     }
 
     if (0 == fmt) {
         fprintf (stderr, "fmt: unexpected null argument #3");
-        exit_status = 1;
+        ++nfailures;
         return -1;
     }
 
@@ -2118,5 +2129,13 @@
 
     test_user_defined_formatting ();
 
-    return exit_status;
+    if (nfailures) {
+        fprintf (stderr, "\nFailed %d out of %d assertions.\n",
+                 nfailures, ntests);
+        return 1;
+    }
+
+    printf ("\nPassed all %d assertions.\n", ntests);
+
+    return 0;
 }

Modified: incubator/stdcxx/trunk/tests/src/printf.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/printf.cpp?rev=372301&r1=372300&r2=372301&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/printf.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/printf.cpp Wed Jan 25 12:06:59 2006
@@ -118,10 +118,10 @@
 _rw_fmtstr (const FmtSpec&, char**, size_t*, const char*, size_t);
 
 static int
-_rw_fmtchr (const FmtSpec&, char**, size_t*, int);
+_rw_fmtchr (const FmtSpec&, char**, size_t*, int, int);
 
 static int
-_rw_fmtwchr (const FmtSpec&, char**, size_t*, wint_t);
+_rw_fmtwchr (const FmtSpec&, char**, size_t*, wint_t, int);
 
 static int
 _rw_fmtwstr (const FmtSpec&, char**, size_t*, const wchar_t*, size_t);
@@ -654,12 +654,14 @@
         // the lc conversion specification and the second a null wide
         // character.
         if (spec.mod == spec.mod_l) {
+            // wint_t argument formatted as wchar_t (no escapes)
             spec.param.wint_ = PARAM (wint_);
-            len = _rw_fmtwchr (spec, pbuf, pbufsize, spec.param.wint_);
+            len = _rw_fmtwchr (spec, pbuf, pbufsize, spec.param.wint_, 1);
         }
         else {
+            // int argument formatted as char (no escapes)
             spec.param.int_ = PARAM (int_);
-            len = _rw_fmtchr (spec, pbuf, pbufsize, spec.param.int_);
+            len = _rw_fmtchr (spec, pbuf, pbufsize, spec.param.int_, 1);
         }
         break;
 
@@ -2960,14 +2962,17 @@
 
 /********************************************************************/
 
+// implements %c and %{c} (narrow character formatting, both without
+// and with escape sequences, respectively)
 static int
-_rw_fmtchr (const FmtSpec &spec, char **pbuf, size_t *pbufsize, int val)
+_rw_fmtchr (const FmtSpec &spec, char **pbuf, size_t *pbufsize,
+            int val, int noesc)
 {
     typedef unsigned char UChar;
     const UChar uc = UChar (val);
 
     char buffer [8];
-    int len = rw_quotechar (buffer + spec.fl_pound, uc, !spec.fl_pound);
+    int len = rw_quotechar (buffer + spec.fl_pound, uc, noesc);
     if (spec.fl_pound) {
         buffer [0] = buffer [len + 1] = '\'';
         buffer [len + 2] = '\0';
@@ -2981,13 +2986,16 @@
 
 /********************************************************************/
 
+// implements %lc and %{lc} (wide character formatting, both without
+// and with escape sequences, respectively)
 static int
-_rw_fmtwchr (const FmtSpec &spec, char **pbuf, size_t *pbufsize, wint_t val)
+_rw_fmtwchr (const FmtSpec &spec, char **pbuf, size_t *pbufsize,
+             wint_t val, int noesc)
 {
     const wchar_t wc = wchar_t (val);
 
     char buffer [16];
-    int len = rw_quotechar (buffer + 2 * spec.fl_pound, wc, !spec.fl_pound);
+    int len = rw_quotechar (buffer + 2 * spec.fl_pound, wc, noesc);
     if (spec.fl_pound) {
         buffer [0] = 'L';
         buffer [1] = buffer [len + 2] = '\'';
@@ -3519,13 +3527,19 @@
             spec.param.int_ = PARAM (int_);
             len = rw_fmtlc (spec, pbuf, pbufsize, spec.param.int_);
         }
-        else if (spec.mod == spec.mod_l) {   // wchar_t
+        else if (spec.mod == spec.mod_l) {
+            // wint_t argument formatted as wchar_t with non-printable
+            // characters represented using traditional C escape
+            // sequences
             spec.param.wint_ = PARAM (wint_);
-            return _rw_fmtwchr (spec, pbuf, pbufsize, spec.param.wint_);
+            return _rw_fmtwchr (spec, pbuf, pbufsize, spec.param.wint_, 0);
         }
-        else {   // char
+        else {
+            // int argument formatted as char with non-printable
+            // characters represented using traditional C escape
+            // sequences
             spec.param.int_ = PARAM (int_);
-            return _rw_fmtchr (spec, pbuf, pbufsize, spec.param.int_);
+            return _rw_fmtchr (spec, pbuf, pbufsize, spec.param.int_, 0);
         }
         break;
 



Mime
View raw message