wxFormatConverter test rewritten for CppUnit (patch #910051)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26116 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík 2004-03-06 11:23:49 +00:00
parent 4d2962b127
commit 7d5ab151a5
12 changed files with 315 additions and 140 deletions

View File

@ -38,6 +38,7 @@ TEST_CXXFLAGS = -D__WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p) -I$(srcdir) \
TEST_OBJECTS = \
test_test.o \
test_main.o \
test_formatconverter.o \
test_regex.o
### Conditionally set variables: ###
@ -107,6 +108,9 @@ test_test.o: $(srcdir)/test.cpp
test_main.o: $(srcdir)/mbconv/main.cpp
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $<
test_formatconverter.o: $(srcdir)/formatconverter/formatconverter.cpp
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $<
test_regex.o: $(srcdir)/regex/regex.cpp
$(CXXC) -c -o $@ $(TEST_CXXFLAGS) $<

View File

@ -0,0 +1,289 @@
///////////////////////////////////////////////////////////////////////////////
// Name: tests/formatconverter/formatconverter.cpp
// Purpose: Test wxFormatConverter
// Author: Mike Wetherell
// RCS-ID: $Id$
// Copyright: (c) 2004 Mike Wetherell
// Licence: wxWidgets licence
///////////////////////////////////////////////////////////////////////////////
//
// Notes:
//
// The conversions wxFormatConverter currently does are as follows:
//
// %s, %lS -> %ls
// %S, %hS -> %s
// %c, %lC -> %lc
// %C, %hC -> %c
//
// %hs and %hc stay the same.
//
// %S and %C aren't actually in the ISO C or C++ standards, but they can be
// avoided when writing portable code.
//
// Nor are %hs or %hc in the standards, which means wxWidgets currently doesn't
// have a specifier for 'char' types that is ok for all builds and platforms.
//
// The effect of using %hs/%hc is undefined, though RTLs are quite likely
// to just ignore the 'h', so maybe it works as required even though it's
// not legal.
//
// I've put in some checks, such as this which will flag up any platforms
// where this is not the case:
//
// CPPUNIT_ASSERT(wxString::Format(_T("%hs"), "test") == _T("test"));
//
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma implementation
#pragma interface
#endif
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/cppunit.h"
// wxFormatConverter can only be tested in a Unicode non-Windows debug build
//
#if defined(wxNEED_PRINTF_CONVERSION) && defined(__WXDEBUG__)
#define CAN_TEST
extern wxString wxConvertFormat(const wxChar *format);
#endif
using namespace std;
using namespace CppUnit;
///////////////////////////////////////////////////////////////////////////////
// The test case
//
// wxFormatConverter only changes %s, %c, %S and %C, all others are treated
// equally, therefore it is enough to choose just one other for testing, %d
// will do.
class FormatConverterTestCase : public TestCase
{
CPPUNIT_TEST_SUITE(FormatConverterTestCase);
CPPUNIT_TEST(format_d);
CPPUNIT_TEST(format_hd);
CPPUNIT_TEST(format_ld);
CPPUNIT_TEST(format_s);
CPPUNIT_TEST(format_hs);
CPPUNIT_TEST(format_ls);
CPPUNIT_TEST(format_c);
CPPUNIT_TEST(format_hc);
CPPUNIT_TEST(format_lc);
#ifdef CAN_TEST
CPPUNIT_TEST(format_S);
CPPUNIT_TEST(format_hS);
CPPUNIT_TEST(format_lS);
CPPUNIT_TEST(format_C);
CPPUNIT_TEST(format_hC);
CPPUNIT_TEST(format_lC);
CPPUNIT_TEST(testLonger);
#endif
CPPUNIT_TEST_SUITE_END();
private:
void format_d();
void format_hd();
void format_ld();
void format_s();
void format_hs();
void format_ls();
void format_c();
void format_hc();
void format_lc();
#ifdef CAN_TEST
void format_S();
void format_hS();
void format_lS();
void format_C();
void format_hC();
void format_lC();
void testLonger();
void doTest(const wxChar *input, const wxChar *expected);
void check(const wxString& input, const wxString& expected);
#endif
};
void FormatConverterTestCase::format_d()
{
#ifdef CAN_TEST
doTest(_T("d"), _T("d"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%d"), 255) == _T("255"));
}
void FormatConverterTestCase::format_hd()
{
#ifdef CAN_TEST
doTest(_T("hd"), _T("hd"));
#endif
short s = 32767;
CPPUNIT_ASSERT(wxString::Format(_T("%hd"), s) == _T("32767"));
}
void FormatConverterTestCase::format_ld()
{
#ifdef CAN_TEST
doTest(_T("ld"), _T("ld"));
#endif
long l = 2147483647L;
CPPUNIT_ASSERT(wxString::Format(_T("%ld"), l) == _T("2147483647"));
}
void FormatConverterTestCase::format_s()
{
#ifdef CAN_TEST
doTest(_T("s"), _T("ls"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%s"), _T("test")) == _T("test"));
}
void FormatConverterTestCase::format_hs()
{
#ifdef CAN_TEST
doTest(_T("hs"), _T("hs"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%hs"), "test") == _T("test"));
}
void FormatConverterTestCase::format_ls()
{
#ifdef CAN_TEST
doTest(_T("ls"), _T("ls"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%ls"), L"test") == _T("test"));
}
void FormatConverterTestCase::format_c()
{
#ifdef CAN_TEST
doTest(_T("c"), _T("lc"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%c"), _T('x')) == _T("x"));
}
void FormatConverterTestCase::format_hc()
{
#ifdef CAN_TEST
doTest(_T("hc"), _T("hc"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%hc"), 'x') == _T("x"));
}
void FormatConverterTestCase::format_lc()
{
#ifdef CAN_TEST
doTest(_T("lc"), _T("lc"));
#endif
CPPUNIT_ASSERT(wxString::Format(_T("%lc"), L'x') == _T("x"));
}
#ifdef CAN_TEST
void FormatConverterTestCase::format_S() { doTest(_T("S"), _T("s")); }
void FormatConverterTestCase::format_hS() { doTest(_T("hS"), _T("s")); }
void FormatConverterTestCase::format_lS() { doTest(_T("lS"), _T("ls")); }
void FormatConverterTestCase::format_C() { doTest(_T("C"), _T("c")); }
void FormatConverterTestCase::format_hC() { doTest(_T("hC"), _T("c")); }
void FormatConverterTestCase::format_lC() { doTest(_T("lC"), _T("lc")); }
// It's possible that although a format converts correctly alone, it leaves
// the converter in a bad state that will affect subsequent formats, so
// check with a selection of longer patterns.
//
void FormatConverterTestCase::testLonger()
{
struct {
const wxChar *input;
const wxChar *expected;
} formats[] = {
{ _T("%d"), _T("%d"), },
{ _T("%*hd"), _T("%*hd") },
{ _T("%.4ld"), _T("%.4ld") },
{ _T("%-.*s"), _T("%-.*ls") },
{ _T("%.*hs"), _T("%.*hs"), },
{ _T("%-.9ls"), _T("%-.9ls") },
{ _T("%-*c"), _T("%-*lc") },
{ _T("%3hc"), _T("%3hc") },
{ _T("%-5lc"), _T("%-5lc") }
};
size_t i, j;
// exclude patterns that don't translate correctly alone from the test
for (i = 0; i < WXSIZEOF(formats); i++)
if (wxConvertFormat(formats[i].input) != formats[i].expected)
formats[i].input = NULL;
// test all possible pairs of the above patterns
for (i = 0; i < WXSIZEOF(formats); i++) {
if (formats[i].input) {
wxString input(formats[i].input);
wxString expected(formats[i].expected);
for (j = 0; j < WXSIZEOF(formats); j++)
if (formats[j].input)
check(input + formats[j].input,
expected + formats[j].expected);
}
}
}
void FormatConverterTestCase::doTest(const wxChar *input,
const wxChar *expected)
{
static const wxChar *flag_width[] =
{ _T(""), _T("*"), _T("10"), _T("-*"), _T("-10"), NULL };
static const wxChar *precision[] =
{ _T(""), _T(".*"), _T(".10"), NULL };
static const wxChar *empty[] =
{ _T(""), NULL };
// no precision for %c or %C
const wxChar **precs = wxTolower(input[wxStrlen(input)-1]) == _T('c') ?
empty : precision;
wxString fmt(_T("%"));
// try the test for a variety of combinations of flag, width and precision
for (const wxChar **prec = precs; *prec; prec++)
for (const wxChar **width = flag_width; *width; width++)
check(fmt + *width + *prec + input,
fmt + *width + *prec + expected);
}
void FormatConverterTestCase::check(const wxString& input,
const wxString& expected)
{
wxString result = wxConvertFormat(input);
wxString msg = _T("input: '") + input +
_T("', result: '") + result +
_T("', expected: '") + expected + _T("'");
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expected);
}
#endif // CAN_TEST
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase);
// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase,
"FormatConverterTestCase");

View File

@ -1,47 +0,0 @@
#!/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

@ -1,26 +0,0 @@
#!/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

@ -1,58 +0,0 @@
/*
* 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

@ -1,8 +0,0 @@
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.

View File

@ -32,6 +32,7 @@ TEST_CXXFLAGS = $(__RUNTIME_LIBS_6) -I$(BCCDIR)\include $(__DEBUGINFO) \
TEST_OBJECTS = \
$(OBJS)\test_test.obj \
$(OBJS)\test_main.obj \
$(OBJS)\test_formatconverter.obj \
$(OBJS)\test_regex.obj
### Conditionally set variables: ###
@ -158,5 +159,8 @@ $(OBJS)\test_test.obj: .\test.cpp
$(OBJS)\test_main.obj: .\mbconv\main.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $**
$(OBJS)\test_formatconverter.obj: .\formatconverter\formatconverter.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $**
$(OBJS)\test_regex.obj: .\regex\regex.cpp
$(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) $**

View File

@ -23,6 +23,7 @@ TEST_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG_2) $(GCCFLAGS) -DHAVE_W32API_H \
TEST_OBJECTS = \
$(OBJS)\test_test.o \
$(OBJS)\test_main.o \
$(OBJS)\test_formatconverter.o \
$(OBJS)\test_regex.o
### Conditionally set variables: ###
@ -152,6 +153,9 @@ $(OBJS)\test_test.o: ./test.cpp
$(OBJS)\test_main.o: ./mbconv/main.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $<
$(OBJS)\test_formatconverter.o: ./formatconverter/formatconverter.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $<
$(OBJS)\test_regex.o: ./regex/regex.cpp
$(CXX) -c -o $@ $(TEST_CXXFLAGS) $<

View File

@ -25,6 +25,7 @@ TEST_CXXFLAGS = /M$(__RUNTIME_LIBS_7)$(__DEBUGRUNTIME_3) /DWIN32 \
TEST_OBJECTS = \
$(OBJS)\test_test.obj \
$(OBJS)\test_main.obj \
$(OBJS)\test_formatconverter.obj \
$(OBJS)\test_regex.obj
### Conditionally set variables: ###
@ -214,5 +215,8 @@ $(OBJS)\test_test.obj: .\test.cpp
$(OBJS)\test_main.obj: .\mbconv\main.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $**
$(OBJS)\test_formatconverter.obj: .\formatconverter\formatconverter.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $**
$(OBJS)\test_regex.obj: .\regex\regex.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) $**

View File

@ -173,6 +173,7 @@ TEST_CXXFLAGS = $(__DEBUGINFO_0) $(__OPTIMIZEFLAG_2) -bm $(__RUNTIME_LIBS_5) &
TEST_OBJECTS = &
$(OBJS)\test_test.obj &
$(OBJS)\test_main.obj &
$(OBJS)\test_formatconverter.obj &
$(OBJS)\test_regex.obj
@ -208,5 +209,8 @@ $(OBJS)\test_test.obj : .AUTODEPEND .\test.cpp
$(OBJS)\test_main.obj : .AUTODEPEND .\mbconv\main.cpp
$(CXX) -zq -fo=$^@ $(TEST_CXXFLAGS) $<
$(OBJS)\test_formatconverter.obj : .AUTODEPEND .\formatconverter\formatconverter.cpp
$(CXX) -zq -fo=$^@ $(TEST_CXXFLAGS) $<
$(OBJS)\test_regex.obj : .AUTODEPEND .\regex\regex.cpp
$(CXX) -zq -fo=$^@ $(TEST_CXXFLAGS) $<

View File

@ -1,5 +1,5 @@
<?xml version="1.0" ?>
<!-- $id$ -->
<!-- $Id$ -->
<makefile>
@ -10,6 +10,7 @@
<sources>
test.cpp
mbconv/main.cpp
formatconverter/formatconverter.cpp
regex/regex.cpp
</sources>
<wx-lib>base</wx-lib>

View File

@ -435,6 +435,10 @@ LINK32=link.exe
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\formatconverter\formatconverter.cpp
# End Source File
# Begin Source File
SOURCE=.\mbconv\main.cpp
# End Source File
# Begin Source File