added test for %c and other minor changes (patch 1643843)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44453 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -25,39 +25,53 @@
|
|||||||
#endif // WX_PRECOMP
|
#endif // WX_PRECOMP
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE: for more info about the specification of wxVsnprintf() behaviour you can
|
||||||
|
// refer to the following page of the GNU libc manual:
|
||||||
|
// http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// global utilities for testing
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
#define MAX_TEST_LEN 1024
|
#define MAX_TEST_LEN 1024
|
||||||
|
|
||||||
|
|
||||||
// temporary buffers
|
// temporary buffers
|
||||||
static wxChar buf[MAX_TEST_LEN];
|
static wxChar buf[MAX_TEST_LEN];
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
// these macros makes it possible to write all tests without repeating a lot of times wxT() macro
|
// these macros makes it possible to write all tests without repeating a lot
|
||||||
|
// of times the wxT() macro
|
||||||
|
// NOTE: you should use expected strings with these macros which do not exceed
|
||||||
|
// MAX_TEST_LEN as these macro do check if the return value is == (int)wxStrlen(buf)
|
||||||
|
|
||||||
#define ASSERT_STR_EQUAL( a, b ) \
|
#define ASSERT_STR_EQUAL( a, b ) \
|
||||||
CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
|
CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
|
||||||
|
|
||||||
#define CMP5(expected, x, y, z, w) \
|
#define CMP5(expected, x, y, z, w) \
|
||||||
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
|
||||||
CPPUNIT_ASSERT( r > 0 ); \
|
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
|
||||||
ASSERT_STR_EQUAL( wxT(expected), buf );
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
||||||
|
|
||||||
#define CMP4(expected, x, y, z) \
|
#define CMP4(expected, x, y, z) \
|
||||||
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
|
||||||
CPPUNIT_ASSERT( r > 0 ); \
|
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
|
||||||
ASSERT_STR_EQUAL( wxT(expected), buf );
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
||||||
|
|
||||||
#define CMP3(expected, x, y) \
|
#define CMP3(expected, x, y) \
|
||||||
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
|
||||||
CPPUNIT_ASSERT( r > 0 ); \
|
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
|
||||||
ASSERT_STR_EQUAL( wxT(expected), buf );
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
||||||
|
|
||||||
#define CMP2(expected, x) \
|
#define CMP2(expected, x) \
|
||||||
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
|
||||||
CPPUNIT_ASSERT( r > 0 ); \
|
CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
|
||||||
ASSERT_STR_EQUAL( wxT(expected), 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) \
|
#define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
|
||||||
r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
|
r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
|
||||||
CPPUNIT_ASSERT( r > 0 ); \
|
CPPUNIT_ASSERT( r > 0 ); \
|
||||||
@@ -77,6 +91,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
CPPUNIT_TEST_SUITE( VsnprintfTestCase );
|
CPPUNIT_TEST_SUITE( VsnprintfTestCase );
|
||||||
|
CPPUNIT_TEST( C );
|
||||||
CPPUNIT_TEST( D );
|
CPPUNIT_TEST( D );
|
||||||
CPPUNIT_TEST( X );
|
CPPUNIT_TEST( X );
|
||||||
CPPUNIT_TEST( O );
|
CPPUNIT_TEST( O );
|
||||||
@@ -97,6 +112,7 @@ private:
|
|||||||
CPPUNIT_TEST( Miscellaneous );
|
CPPUNIT_TEST( Miscellaneous );
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
|
void C();
|
||||||
void D();
|
void D();
|
||||||
void X();
|
void X();
|
||||||
void O();
|
void O();
|
||||||
@@ -118,6 +134,12 @@ private:
|
|||||||
void Miscellaneous();
|
void Miscellaneous();
|
||||||
void Misc(wxChar *buffer, int size);
|
void Misc(wxChar *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, ...);
|
||||||
|
|
||||||
DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
|
DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -131,6 +153,18 @@ VsnprintfTestCase::VsnprintfTestCase()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VsnprintfTestCase::C()
|
||||||
|
{
|
||||||
|
CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
|
||||||
|
|
||||||
|
// NOTE:
|
||||||
|
// the NULL characters _can_ be passed to %c to e.g. create strings
|
||||||
|
// with embedded NULs (because strings are not always supposed to be
|
||||||
|
// NUL-terminated).
|
||||||
|
|
||||||
|
DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
|
||||||
|
}
|
||||||
|
|
||||||
void VsnprintfTestCase::D()
|
void VsnprintfTestCase::D()
|
||||||
{
|
{
|
||||||
CMP3("+123456", "%+d", 123456);
|
CMP3("+123456", "%+d", 123456);
|
||||||
@@ -279,6 +313,10 @@ void VsnprintfTestCase::S()
|
|||||||
|
|
||||||
CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
|
CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// test a string which have the NULL character after "ab";
|
||||||
|
// obviously it should be handled exactly like just as "ab"
|
||||||
|
CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void VsnprintfTestCase::Asterisk()
|
void VsnprintfTestCase::Asterisk()
|
||||||
@@ -323,18 +361,20 @@ void VsnprintfTestCase::LongLong()
|
|||||||
|
|
||||||
void VsnprintfTestCase::Misc(wxChar *buffer, int size)
|
void VsnprintfTestCase::Misc(wxChar *buffer, int size)
|
||||||
{
|
{
|
||||||
// NB: remember that wx*printf could be mapped either to system
|
// Remember that wx*printf could be mapped either to system
|
||||||
// implementation or to wx implementation.
|
// implementation or to wx implementation.
|
||||||
// In the first case, when the output buffer is too small, the returned
|
// 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
|
// 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
|
// (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
|
// just a negative number, usually -1; (this is how e.g. MSVC's
|
||||||
// *printf() behaves). Luckily, in all implementations, when the
|
// *printf() behaves). Luckily, in all implementations, when the
|
||||||
// output buffer is too small, it's nonetheless filled up to its max
|
// output buffer is too small, it's nonetheless filled up to its max size.
|
||||||
// size.
|
|
||||||
//
|
//
|
||||||
// Note that in the second case (i.e. when we're using our own implementation),
|
// Note that in the second case (i.e. when we're using our own implementation),
|
||||||
// wxVsnprintf() will always return the number of characters which
|
// 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
|
// test without positionals
|
||||||
CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
|
CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
|
||||||
@@ -396,14 +436,13 @@ void VsnprintfTestCase::BigToSmallBuffer()
|
|||||||
Misc(&buf4, 1);
|
Misc(&buf4, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DoMisc(
|
void VsnprintfTestCase::DoMisc(
|
||||||
int expectedLen,
|
int expectedLen,
|
||||||
const wxString& expectedString,
|
const wxString& expectedString,
|
||||||
size_t max,
|
size_t max,
|
||||||
const wxChar *format, ...)
|
const wxChar *format, ...)
|
||||||
{
|
{
|
||||||
const size_t BUFSIZE = 16;
|
const size_t BUFSIZE = MAX_TEST_LEN - 1;
|
||||||
wxChar buf[BUFSIZE + 1];
|
|
||||||
size_t i;
|
size_t i;
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user