From cfee166d4e4c1739c9a00cf8128db5260f154a43 Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Sun, 11 Jan 2004 14:16:32 +0000 Subject: [PATCH] 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 --- src/common/wxchar.cpp | 31 ++++++++------- tests/formatconverter/formats.pl | 47 ++++++++++++++++++++++ tests/formatconverter/formats2.pl | 26 +++++++++++++ tests/formatconverter/formattest.cpp | 58 ++++++++++++++++++++++++++++ tests/formatconverter/readme.txt | 8 ++++ 5 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 tests/formatconverter/formats.pl create mode 100644 tests/formatconverter/formats2.pl create mode 100644 tests/formatconverter/formattest.cpp create mode 100644 tests/formatconverter/readme.txt diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index 06c69bd0cd..be0dc8d53d 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -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 index 0000000000..e3f37b8866 --- /dev/null +++ b/tests/formatconverter/formats.pl @@ -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 index 0000000000..f7f2c5db4f --- /dev/null +++ b/tests/formatconverter/formats2.pl @@ -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 () { + chomp; + my ($format, $expected) = split "\t"; + open IN2, './formats.pl |'; + + while () { + 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 index 0000000000..e0bd9bfc70 --- /dev/null +++ b/tests/formatconverter/formattest.cpp @@ -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 +#include +#include +#include + +/* 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 index 0000000000..75d6ecbaf9 --- /dev/null +++ b/tests/formatconverter/readme.txt @@ -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. + -- 2.45.2