Applied patch [ 853850 ] Fixes for wxFormatConverter

(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
This commit is contained in:
Julian Smart
2004-01-11 14:16:32 +00:00
parent 5a410e4453
commit cfee166d4e
5 changed files with 156 additions and 14 deletions

View 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
// ----------------------------------------------------------------------------

View File

@@ -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";
}
}
}
}
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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.