make the testcase more readable; add a few checks to Asterisk() test; make unicode part of S() test compile with MSVC

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58989 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Francesco Montorsi 2009-02-18 14:57:42 +00:00
parent 5b5939956d
commit 35d3edcd53

View File

@ -53,34 +53,41 @@ int r;
#define ASSERT_STR_EQUAL( a, b ) \
CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
#define CMP5(expected, x, y, z, w) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
#define CMP6(expected, fmt, y, z, w, t) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w, t); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP5(expected, fmt, y, z, w) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP4(expected, fmt, y, z) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP4(expected, x, y, z) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
#define CMP3(expected, fmt, y) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP3(expected, x, y) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP2(expected, x) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
#define CMP2(expected, fmt) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt)); \
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
// NOTE: this macro is used also with too-small buffers (see Miscellaneous())
// test function, thus the return value can be > size and thus we
// cannot check if r == (int)wxStrlen(buf)
#define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
CPPUNIT_ASSERT( r > 0 ); \
CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
wxString(buffer) )
#define CMPTOSIZE(buffer, size, failuremsg, expected, fmt, x, y, z, w) \
r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
CPPUNIT_ASSERT( r > 0 ); \
CPPUNIT_ASSERT_EQUAL_MESSAGE( \
failuremsg, \
wxString(wxT(expected)).Left(size - 1), \
wxString(buffer))
// this is the same as wxSnprintf() but it passes the format string to
// wxVsnprintf() without using ATTRIBUTE_PRINTF and thus suppresses the gcc
@ -153,18 +160,20 @@ private:
#endif
void Unicode();
template<typename T>
void DoBigToSmallBuffer(T *buffer, int size);
void BigToSmallBuffer();
#if wxUSE_WXVSNPRINTF
void WrongFormatStrings();
#endif // wxUSE_WXVSNPRINTF
void Miscellaneous();
template<typename T> void Misc(T *buffer, int size);
// compares the expectedString and the result of wxVsnprintf() char by char
// for all its lenght (not only for first expectedLen chars) and also
// checks the return value
void DoMisc(int expectedLen, const wxString& expectedString,
size_t max, const wxChar *format, ...);
void Miscellaneous();
DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
};
@ -319,31 +328,37 @@ void VsnprintfTestCase::S()
CMP3("abcde", "%.5s", wxT("abcdefghi"));
// do the same tests but with Unicode characters:
#if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
#define ALPHA "\x3B1"
#define BETA "\x3B2"
#define GAMMA "\x3B3"
#define DELTA "\x3B4"
#define EPSILON "\x3B5"
#define ZETA "\x3B6"
#define ETA "\x3B7"
#define THETA "\x3B8"
#define IOTA "\x3B9"
#if wxUSE_UNICODE
#define ABC ALPHA BETA GAMMA
#define ABCDE ALPHA BETA GAMMA DELTA EPSILON
#define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
// Unicode code points from U+03B1 to U+03B9 are the greek letters alpha-iota;
// UTF8 encoding of such code points is 0xCEB1 to 0xCEB9
CMP3(" " ABC, "%5s", wxT(ABC));
CMP3(" " ALPHA, "%5s", wxT(ALPHA));
CMP3(ABCDEFGHI, "%5s", wxT(ABCDEFGHI));
CMP3(ABC " ", "%-5s", wxT(ABC));
CMP3(ABCDEFGHI, "%-5s", wxT(ABCDEFGHI));
#define ALPHA "\xCE\xB1"
// alpha
#define ABC "\xCE\xB1\xCE\xB2\xCE\xB3"
// alpha+beta+gamma
#define ABCDE "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
// alpha+beta+gamma+delta+epsilon
#define ABCDEFGHI "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9"
// alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
#define ALPHA_w wxT(ALPHA)
#define ABC_w wxT(ABC)
#define ABCDE_w wxT(ABCDE)
#define ABCDEFGHI_w wxT(ABCDEFGHI)
// CMP3 uses wxT() on the first argument so we need to be careful
// when using string concatenation that all parts of the string after
// the first explicitely use wxT():
CMP3(" " ABC_w, "%5s", ABC);
CMP3(" " ALPHA_w, "%5s", ALPHA);
CMP3(ABCDEFGHI, "%5s", ABCDEFGHI);
CMP3(ABC L" ", "%-5s", ABC);
CMP3(ABCDEFGHI, "%-5s", ABCDEFGHI);
CMP3(ABCDE, "%.5s", ABCDEFGHI);
#endif
// test a string which have the NULL character after "ab";
// test a string which has a NULL character after "ab";
// obviously it should be handled exactly like just as "ab"
CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
}
@ -355,6 +370,10 @@ void VsnprintfTestCase::Asterisk()
CMP5("0.1", "%*.*f", 3, 1, 0.123);
CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
CMP4(" a", "%*c", 8, 'a');
CMP4(" four", "%*s", 8, "four");
CMP6(" four four", "%*s %*s", 8, "four", 6, "four");
}
void VsnprintfTestCase::Percent()
@ -388,60 +407,10 @@ void VsnprintfTestCase::LongLong()
}
#endif
template<typename T>
void VsnprintfTestCase::Misc(T *buffer, int size)
{
// Remember that wx*printf could be mapped either to system
// implementation or to wx implementation.
// In the first case, when the output buffer is too small, the returned
// value can be the number of characters required for the output buffer
// (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
// just a negative number, usually -1; (this is how e.g. MSVC's
// *printf() behaves). Luckily, in all implementations, when the
// output buffer is too small, it's nonetheless filled up to its max size.
//
// Note that in the second case (i.e. when we're using our own implementation),
// wxVsnprintf() will return the number of characters written in the standard
// output or
// -1 if there was an error in the format string
// maxSize+1 if the output buffer is too small
// test without positionals
CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
"%i %li - test - %d %.3f",
123, (long int)444444444, 555, -0.666);
#if wxUSE_PRINTF_POS_PARAMS
// test with positional
CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555",
"%4$.3f %1$i - test - %2$li %3$d",
123, (long int)444444444, 555, -0.666);
#endif
// test unicode/ansi conversion specifiers
//
// NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
// format and gcc would warn about this otherwise
r = wxUnsafeSnprintf(buffer, size,
_T("unicode string: %ls %lc - ansi string: %hs %hc\n\n"),
L"unicode!!", L'W', "ansi!!", 'w');
wxString expected =
wxString(wxT("unicode string: unicode!! W - ansi string: ansi!! w\n\n")).Left(size - 1);
CPPUNIT_ASSERT( r != -1 );
CPPUNIT_ASSERT_EQUAL(
expected,
wxString(buffer)
);
}
// this test is only for our own implementation, the system implementation
// doesn't always give errors for invalid format strings (e.g. glibc doesn't)
// and as it's not required too (the behaviour is "undefined" according to the
// spec), there is really no sense in testing for it (and the first 2 formats
// in this test are not invalid at all in fact)
// spec), there is really no sense in testing for it
#if wxUSE_WXVSNPRINTF
void VsnprintfTestCase::WrongFormatStrings()
@ -473,26 +442,82 @@ void VsnprintfTestCase::WrongFormatStrings()
#endif // wxUSE_WXVSNPRINTF
// BigToSmallBuffer() test case helper:
template<typename T>
void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
{
// Remember that wx*printf could be mapped either to system
// implementation or to wx implementation.
// In the first case, when the output buffer is too small, the returned
// value can be the number of characters required for the output buffer
// (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
// just a negative number, usually -1; (this is how e.g. MSVC's
// *printf() behaves). Luckily, in all implementations, when the
// output buffer is too small, it's nonetheless filled up to its max size.
//
// Note that in the second case (i.e. when we're using our own implementation),
// wxVsnprintf() will return the number of characters written in the standard
// output or
// -1 if there was an error in the format string
// maxSize+1 if the output buffer is too small
wxString errStr;
errStr << "The size of the buffer was " << size;
std::string errMsg(errStr.mb_str());
// test without positionals
CMPTOSIZE(buffer, size, errMsg,
"123456789012 - test - 123 -4.567",
"%i%li - test - %d %.3f",
123, (long int)456789012, 123, -4.567);
#if wxUSE_PRINTF_POS_PARAMS
// test with positional
CMPTOSIZE(buffer, size, errMsg,
"-4.567 123 - test - 456789012 123",
"%4$.3f %1$i - test - %2$li %3$d",
123, (long int)456789012, 123, -4.567);
#endif
// test unicode/ansi conversion specifiers
//
// NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
// format and gcc would warn about this otherwise
r = wxUnsafeSnprintf(buffer, size,
_T("unicode string/char: %ls/%lc -- ansi string/char: %hs/%hc"),
L"unicode", L'U', "ansi", 'A');
wxString expected =
wxString(wxT("unicode string/char: unicode/U -- ansi string/char: ansi/A")).Left(size - 1);
CPPUNIT_ASSERT( r != -1 );
CPPUNIT_ASSERT_EQUAL(
expected,
wxString(buffer)
);
}
void VsnprintfTestCase::BigToSmallBuffer()
{
// VC6 can't compile this code
#if !defined(__VISUALC__) || (__VISUALC__ >= 1310)
#if wxUSE_UNICODE
wchar_t bufw[1024], bufw2[16], bufw3[4], bufw4;
Misc(bufw, 1024);
Misc(bufw2, 16);
Misc(bufw3, 4);
Misc(&bufw4, 1);
DoBigToSmallBuffer(bufw, 1024);
DoBigToSmallBuffer(bufw2, 16);
DoBigToSmallBuffer(bufw3, 4);
DoBigToSmallBuffer(&bufw4, 1);
#endif // wxUSE_UNICODE
char bufa[1024], bufa2[16], bufa3[4], bufa4;
Misc(bufa, 1024);
Misc(bufa2, 16);
Misc(bufa3, 4);
Misc(&bufa4, 1);
DoBigToSmallBuffer(bufa, 1024);
DoBigToSmallBuffer(bufa2, 16);
DoBigToSmallBuffer(bufa3, 4);
DoBigToSmallBuffer(&bufa4, 1);
#endif // !VC6
}
// Miscellaneous() test case helper:
void VsnprintfTestCase::DoMisc(
int expectedLen,
const wxString& expectedString,
@ -540,26 +565,26 @@ void VsnprintfTestCase::DoMisc(
void VsnprintfTestCase::Miscellaneous()
{
// expectedLen, expectedString, max, format, ...
DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
DoMisc(6, wxT("123456"), 8, wxT("123456"));
DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
DoMisc(6, wxT("123456"), 8, wxT("123456"));
DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
}
#endif // wxUSE_WXVSNPRINTF