]> git.saurik.com Git - wxWidgets.git/commitdiff
Applied patch [ 853850 ] Fixes for wxFormatConverter
authorJulian Smart <julian@anthemion.co.uk>
Sun, 11 Jan 2004 14:16:32 +0000 (14:16 +0000)
committerJulian Smart <julian@anthemion.co.uk>
Sun, 11 Jan 2004 14:16:32 +0000 (14:16 +0000)
(M.J.Wetherell)
Added wxConvertFormat function in debug mode to allow for
unit testing
Added tests/formatconverter

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25116 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/wxchar.cpp
tests/formatconverter/formats.pl [new file with mode: 0644]
tests/formatconverter/formats2.pl [new file with mode: 0644]
tests/formatconverter/formattest.cpp [new file with mode: 0644]
tests/formatconverter/readme.txt [new file with mode: 0644]

index 06c69bd0cd0319719a899c40ca10bfd4883c1f97..be0dc8d53d8b3057b7d0518656921eb81a73a71e 100644 (file)
@@ -752,7 +752,11 @@ wxFormatConverter::wxFormatConverter(const wxChar *format)
             // precision?
             if ( *format == _T('.') )
             {
-                SkipDigits(&format);
+                CopyFmtChar(*format++);
+                if ( *format == _T('*') )
+                    CopyFmtChar(*format++);
+                else
+                    SkipDigits(&format);
             }
 
             // next we can have a size modifier
@@ -799,23 +803,14 @@ wxFormatConverter::wxFormatConverter(const wxChar *format)
                 case _T('c'):
                 case _T('s'):
                     // %c -> %lc but %hc stays %hc and %lc is still %lc
-                    switch ( size )
-                    {
-                        case Default:
-                            InsertFmtChar(_T('l'));
-                            break;
-
-                        case Short:
-                            CopyFmtChar(_T('h'));
-                            break;
-
-                        case Long:
-                            ;
-                    }
+                    if ( size == Default)
+                        InsertFmtChar(_T('l'));
                     // fall through
 
                 default:
                     // nothing special to do
+                    if ( size != Default )
+                        CopyFmtChar(*(format - 1));
                     CopyFmtChar(*format++);
             }
         }
@@ -827,6 +822,14 @@ wxFormatConverter::wxFormatConverter(const wxChar *format)
     #define wxFormatConverter(x) (x)
 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
 
+#ifdef __WXDEBUG__
+// For testing the format converter
+wxString wxConvertFormat(const wxChar *format)
+{
+    return wxString(wxFormatConverter(format));
+}
+#endif
+
 // ----------------------------------------------------------------------------
 // wxPrintf(), wxScanf() and relatives
 // ----------------------------------------------------------------------------
diff --git a/tests/formatconverter/formats.pl b/tests/formatconverter/formats.pl
new file mode 100644 (file)
index 0000000..e3f37b8
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/perl -w
+#
+# Prints test formats for wxFormatConverter. The output is in two columns (tab
+# separated), the first is the test input and the second is the expected
+# output.
+#
+# run the output thought formattest like this:
+#   ./formats.pl | ./formattest
+#
+use strict;
+
+my %transform = (
+    s  => [ 'l', 's' ],
+    S  => [ '',  's' ],
+    hS => [ '',  's' ],
+    lS => [ 'l', 's' ],
+    c  => [ 'l', 'c' ],
+    C  => [ '',  'c' ],
+    hC => [ '',  'c' ],
+    lC => [ 'l', 'c' ]
+);
+
+print "%%\t%%\n";
+
+for my $type ('d', 's', 'S', 'c', 'C')
+{
+    for my $mod ('', 'h', 'l', 'hh', 'll', 'j') #, 'z', 't', 'L')
+    {
+        for my $prec ('', '.*', '.10')
+        {
+            for my $width ('', '*', '10')
+            {
+                for my $flag ('', '#') #, '0', '-', ' ', '+' )
+                {
+                    my ($newmod, $newtype) = ($mod, $type);
+
+                    if ($transform{$mod.$type}) {
+                        ($newmod, $newtype) = @{$transform{$mod.$type}};
+                    }
+
+                    print "%$flag$width$prec$mod$type\t".
+                          "%$flag$width$prec$newmod$newtype\n";
+                }
+            }
+        }
+    }
+}
diff --git a/tests/formatconverter/formats2.pl b/tests/formatconverter/formats2.pl
new file mode 100644 (file)
index 0000000..f7f2c5d
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/perl -w
+#
+# Creates longer test formats for wxFormatConverter, containing two '%'s. The
+# output is basically the cross product of the output of two './formats.pl's.
+#
+#   ./formats2.pl | ./formattest
+#
+use strict;
+
+open IN, './formats.pl |';
+
+while (<IN>) {
+    chomp;
+    my ($format, $expected) = split "\t";
+    open IN2, './formats.pl |';
+
+    while (<IN2>) {
+        chomp;
+        my ($format2, $expected2) = split "\t";
+        print "$format $format2\t$expected $expected2\n";
+    }
+
+    close IN2;
+}
+
+close IN;
diff --git a/tests/formatconverter/formattest.cpp b/tests/formatconverter/formattest.cpp
new file mode 100644 (file)
index 0000000..e0bd9bf
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Tester for wxFormatConverter, by M. J. Wetherell 
+ *
+ * Reads stdin, expects two column input (as produced by formats.pl or
+ * formats2.pl), the first column contains the test pattern, the second
+ * the expected result.
+ *
+ * The output is any patterns that wxFormatConveter doesn't transform to the
+ * expected value, in three columns: input, expected, output.
+ *
+ *  ./formats.pl | ./formattest
+ *  ./formats2.pl | ./formattest
+ */
+
+#include <wx/wx.h>
+#include <wx/wfstream.h>
+#include <wx/txtstrm.h>
+#include <iostream>
+
+/* This function is in wxchar.cpp to give access to wxFormatConverter,
+ * but only in debug mode.
+ */
+
+#ifdef __WXDEBUG__ 
+extern wxString wxConvertFormat(const wxChar *format);
+#endif
+
+class TestApp : public wxAppConsole
+{
+public:
+    int OnRun();
+};
+
+IMPLEMENT_APP_CONSOLE(TestApp)
+
+int TestApp::OnRun()
+{
+#ifdef __WXDEBUG__ 
+    wxFFileInputStream in(stdin);
+    wxTextInputStream txt(in, _T("\t"));
+
+    for (;;) {
+        wxString format = txt.ReadWord();
+        wxString expected = txt.ReadWord();
+        if (!in) break;
+        wxString converted = wxConvertFormat(format);
+
+        if (converted != expected)
+            std::cout << "'" << format.mb_str() << "'\t"
+                      << "'" << expected.mb_str() << "'\t"
+                      << "'" << converted.mb_str() << "'\n";
+    }
+#else
+    std::cout << "Please compile this test program in debug mode.\n";
+#endif
+    return 0;
+}
+
diff --git a/tests/formatconverter/readme.txt b/tests/formatconverter/readme.txt
new file mode 100644 (file)
index 0000000..75d6ecb
--- /dev/null
@@ -0,0 +1,8 @@
+
+Tests for wxFormatConverter by M. J. Wetherell 
+
+The Perl scripts generate test patterns;
+formattest.cpp reads this file and checks
+the converted pattern matches the expected
+pattern.
+