2004-03-06 06:23:49 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/formatconverter/formatconverter.cpp
|
|
|
|
// Purpose: Test wxFormatConverter
|
|
|
|
// Author: Mike Wetherell
|
|
|
|
// Copyright: (c) 2004 Mike Wetherell
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2004-03-06 06:23:49 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
2009-07-23 16:30:22 -04:00
|
|
|
// CPPUNIT_ASSERT(wxString::Format(wxT("%hs"), "test") == wxT("test"));
|
2004-03-06 06:23:49 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
2004-11-22 00:00:19 -05:00
|
|
|
#include "testprec.h"
|
2004-03-06 06:23:49 -05:00
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-11-10 16:29:08 -05:00
|
|
|
using CppUnit::TestCase;
|
|
|
|
using std::string;
|
2004-03-06 06:23:49 -05:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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);
|
|
|
|
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);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
void format_S();
|
|
|
|
void format_hS();
|
|
|
|
void format_lS();
|
|
|
|
void format_C();
|
|
|
|
void format_hC();
|
|
|
|
void format_lC();
|
|
|
|
void testLonger();
|
|
|
|
|
2007-06-22 07:10:36 -04:00
|
|
|
void doTest(const char *input, const char *expectedScanf,
|
|
|
|
const char *expectedUtf8,
|
|
|
|
const char *expectedWcharUnix,
|
|
|
|
const char *expectedWcharWindows);
|
|
|
|
void check(const wxString& input, const wxString& expectedScanf,
|
|
|
|
const wxString& expectedUtf8,
|
|
|
|
const wxString& expectedWcharUnix,
|
|
|
|
const wxString& expectedWcharWindows);
|
2004-03-06 06:23:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_d()
|
|
|
|
{
|
2007-06-22 07:10:36 -04:00
|
|
|
doTest("d", "d", "d", "d", "d");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%d"), 255) == wxT("255"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%05d"), 255) == wxT("00255"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), 255) == wxT(" 255"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), -255) == wxT(" -255"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%-5d"), -255) == wxT("-255 "));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%+5d"), 255) == wxT(" +255"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%*d"), 5, 255) == wxT(" 255"));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_hd()
|
|
|
|
{
|
2007-06-22 07:10:36 -04:00
|
|
|
doTest("hd", "hd", "hd", "hd", "hd");
|
2004-03-06 06:23:49 -05:00
|
|
|
short s = 32767;
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%hd"), s) == wxT("32767"));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_ld()
|
|
|
|
{
|
2007-06-22 07:10:36 -04:00
|
|
|
doTest("ld", "ld", "ld", "ld", "ld");
|
2004-03-06 06:23:49 -05:00
|
|
|
long l = 2147483647L;
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%ld"), l) == wxT("2147483647"));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_s()
|
|
|
|
{
|
2007-06-22 07:10:36 -04:00
|
|
|
doTest("s", "ls", "s", "ls", "s");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%s!"), wxT("test")) == wxT("test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%6s!"), wxT("test")) == wxT(" test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%-6s!"), wxT("test")) == wxT("test !"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%.6s!"), wxT("test")) == wxT("test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%6.4s!"), wxT("testing")) == wxT(" test!"));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_hs()
|
|
|
|
{
|
2007-06-22 07:10:36 -04:00
|
|
|
doTest("hs", "hs", "s", "ls", "s");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hs!")), "test") == wxT("test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6hs!")), "test") == wxT(" test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-6hs!")), "test") == wxT("test !"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%.6hs!")), "test") == wxT("test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6.4hs!")), "testing") == wxT(" test!"));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_ls()
|
|
|
|
{
|
2007-06-22 07:10:36 -04:00
|
|
|
doTest("ls", "ls", "s", "ls", "s");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%ls!"), L"test") == wxT("test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%6ls!"), L"test") == wxT(" test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%-6ls!"), L"test") == wxT("test !"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%.6ls!"), L"test") == wxT("test!"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%6.4ls!"), L"testing") == wxT(" test!"));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_c()
|
|
|
|
{
|
2008-05-30 19:38:13 -04:00
|
|
|
doTest("c", "lc", "lc", "lc", "c");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%c"), wxT('x')) == wxT("x"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%2c"), wxT('x')) == wxT(" x"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%-2c"), wxT('x')) == wxT("x "));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_hc()
|
|
|
|
{
|
2008-05-30 19:38:13 -04:00
|
|
|
doTest("hc", "hc", "lc", "lc", "c");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hc")), 'x') == wxT("x"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%2hc")), 'x') == wxT(" x"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-2hc")), 'x') == wxT("x "));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::format_lc()
|
|
|
|
{
|
2008-05-30 19:38:13 -04:00
|
|
|
doTest("lc", "lc", "lc", "lc", "c");
|
2009-07-23 16:30:22 -04:00
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%lc"), L'x') == wxT("x"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%2lc"), L'x') == wxT(" x"));
|
|
|
|
CPPUNIT_ASSERT(wxString::Format(wxT("%-2lc"), L'x') == wxT("x "));
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-22 07:10:36 -04:00
|
|
|
void FormatConverterTestCase::format_S()
|
|
|
|
{ doTest("S", "s", "s", "ls", "s"); }
|
|
|
|
void FormatConverterTestCase::format_hS()
|
|
|
|
{ doTest("hS", "s", "s", "ls", "s"); }
|
|
|
|
void FormatConverterTestCase::format_lS()
|
|
|
|
{ doTest("lS", "ls", "s", "ls", "s"); }
|
2004-03-06 06:23:49 -05:00
|
|
|
|
2007-06-22 07:10:36 -04:00
|
|
|
void FormatConverterTestCase::format_C()
|
2008-05-30 19:38:13 -04:00
|
|
|
{ doTest("C", "c", "lc", "lc", "c"); }
|
2007-06-22 07:10:36 -04:00
|
|
|
void FormatConverterTestCase::format_hC()
|
2008-05-30 19:38:13 -04:00
|
|
|
{ doTest("hC", "c", "lc", "lc", "c"); }
|
2007-06-22 07:10:36 -04:00
|
|
|
void FormatConverterTestCase::format_lC()
|
2008-05-30 19:38:13 -04:00
|
|
|
{ doTest("lC", "lc", "lc", "lc", "c"); }
|
2004-03-06 06:23:49 -05:00
|
|
|
|
|
|
|
// 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 {
|
2007-06-22 07:10:36 -04:00
|
|
|
const char *input;
|
|
|
|
const char *expectedScanf;
|
|
|
|
const char *expectedWcharUnix;
|
|
|
|
const char *expectedWcharWindows;
|
|
|
|
const char *expectedUtf8;
|
2004-03-06 06:23:49 -05:00
|
|
|
} formats[] = {
|
2007-06-22 07:10:36 -04:00
|
|
|
{ "%d", "%d", "%d", "%d", "%d" },
|
|
|
|
{ "%*hd", "%*hd", "%*hd", "%*hd", "%*hd" },
|
|
|
|
{ "%.4ld", "%.4ld", "%.4ld", "%.4ld", "%.4ld" },
|
|
|
|
{ "%-.*s", "%-.*ls", "%-.*ls", "%-.*s", "%-.*s" },
|
|
|
|
{ "%.*hs", "%.*hs", "%.*ls", "%.*s", "%.*s" },
|
|
|
|
{ "%-.9ls", "%-.9ls", "%-.9ls", "%-.9s", "%-.9s" },
|
2008-05-30 19:38:13 -04:00
|
|
|
{ "%-*c", "%-*lc", "%-*lc", "%-*c", "%-*lc" },
|
|
|
|
{ "%3hc", "%3hc", "%3lc", "%3c", "%3lc" },
|
|
|
|
{ "%-5lc", "%-5lc", "%-5lc", "%-5c", "%-5lc" }
|
2004-03-06 06:23:49 -05:00
|
|
|
};
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
// test all possible pairs of the above patterns
|
|
|
|
for (i = 0; i < WXSIZEOF(formats); i++) {
|
|
|
|
if (formats[i].input) {
|
|
|
|
wxString input(formats[i].input);
|
2007-06-22 07:10:36 -04:00
|
|
|
wxString expectedScanf(formats[i].expectedScanf);
|
|
|
|
wxString expectedUtf8(formats[i].expectedUtf8);
|
|
|
|
wxString expectedWcharUnix(formats[i].expectedWcharUnix);
|
|
|
|
wxString expectedWcharWindows(formats[i].expectedWcharWindows);
|
2004-03-06 06:23:49 -05:00
|
|
|
|
|
|
|
for (j = 0; j < WXSIZEOF(formats); j++)
|
|
|
|
if (formats[j].input)
|
|
|
|
check(input + formats[j].input,
|
2007-06-22 07:10:36 -04:00
|
|
|
expectedScanf + formats[j].expectedScanf,
|
|
|
|
expectedUtf8 + formats[j].expectedUtf8,
|
|
|
|
expectedWcharUnix + formats[j].expectedWcharUnix,
|
|
|
|
expectedWcharWindows + formats[j].expectedWcharWindows);
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-22 07:10:36 -04:00
|
|
|
void FormatConverterTestCase::doTest(const char *input,
|
|
|
|
const char *expectedScanf,
|
|
|
|
const char *expectedUtf8,
|
|
|
|
const char *expectedWcharUnix,
|
|
|
|
const char *expectedWcharWindows)
|
2004-03-06 06:23:49 -05:00
|
|
|
{
|
|
|
|
static const wxChar *flag_width[] =
|
2009-07-23 16:30:22 -04:00
|
|
|
{ wxT(""), wxT("*"), wxT("10"), wxT("-*"), wxT("-10"), NULL };
|
2004-03-06 06:23:49 -05:00
|
|
|
static const wxChar *precision[] =
|
2009-07-23 16:30:22 -04:00
|
|
|
{ wxT(""), wxT(".*"), wxT(".10"), NULL };
|
2004-03-06 06:23:49 -05:00
|
|
|
static const wxChar *empty[] =
|
2009-07-23 16:30:22 -04:00
|
|
|
{ wxT(""), NULL };
|
2004-03-06 06:23:49 -05:00
|
|
|
|
|
|
|
// no precision for %c or %C
|
2009-07-23 16:30:22 -04:00
|
|
|
const wxChar **precs = wxTolower(input[wxStrlen(input)-1]) == wxT('c') ?
|
2004-03-06 06:23:49 -05:00
|
|
|
empty : precision;
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxString fmt(wxT("%"));
|
2004-03-06 06:23:49 -05:00
|
|
|
|
|
|
|
// 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,
|
2007-06-22 07:10:36 -04:00
|
|
|
fmt + *width + *prec + expectedScanf,
|
|
|
|
fmt + *width + *prec + expectedUtf8,
|
|
|
|
fmt + *width + *prec + expectedWcharUnix,
|
|
|
|
fmt + *width + *prec + expectedWcharWindows);
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormatConverterTestCase::check(const wxString& input,
|
2007-06-22 07:10:36 -04:00
|
|
|
const wxString& expectedScanf,
|
|
|
|
const wxString& expectedUtf8,
|
|
|
|
const wxString& expectedWcharUnix,
|
|
|
|
const wxString& expectedWcharWindows)
|
2004-03-06 06:23:49 -05:00
|
|
|
{
|
2007-09-14 19:33:46 -04:00
|
|
|
// all of them are unused in some build configurations
|
|
|
|
wxUnusedVar(expectedScanf);
|
|
|
|
wxUnusedVar(expectedUtf8);
|
|
|
|
wxUnusedVar(expectedWcharUnix);
|
|
|
|
wxUnusedVar(expectedWcharWindows);
|
|
|
|
|
2007-06-22 07:10:36 -04:00
|
|
|
wxString result, msg;
|
|
|
|
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
// on windows, wxScanf() string needs no modifications
|
|
|
|
result = wxScanfConvertFormatW(input.wc_str());
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
msg = wxT("input: '") + input +
|
|
|
|
wxT("', result (scanf): '") + result +
|
|
|
|
wxT("', expected: '") + expectedScanf + wxT("'");
|
2007-06-22 07:10:36 -04:00
|
|
|
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expectedScanf);
|
|
|
|
#endif // !__WINDOWS__
|
|
|
|
|
|
|
|
#if wxUSE_UNICODE_UTF8
|
|
|
|
result = (const char*)wxFormatString(input);
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
msg = wxT("input: '") + input +
|
|
|
|
wxT("', result (UTF-8): '") + result +
|
|
|
|
wxT("', expected: '") + expectedUtf8 + wxT("'");
|
2007-06-22 07:10:36 -04:00
|
|
|
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expectedUtf8);
|
|
|
|
#endif // wxUSE_UNICODE_UTF8
|
|
|
|
|
|
|
|
#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
|
|
|
|
result = (const wchar_t*)wxFormatString(input);
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
wxString expectedWchar(expectedWcharWindows);
|
|
|
|
#else
|
|
|
|
wxString expectedWchar(expectedWcharUnix);
|
|
|
|
#endif
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
msg = wxT("input: '") + input +
|
|
|
|
wxT("', result (wchar_t): '") + result +
|
|
|
|
wxT("', expected: '") + expectedWchar + wxT("'");
|
2007-06-22 07:10:36 -04:00
|
|
|
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expectedWchar);
|
|
|
|
#endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
|
2004-03-06 06:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase);
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2004-03-06 06:23:49 -05:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase,
|
|
|
|
"FormatConverterTestCase");
|
|
|
|
|