// http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html
-
// ----------------------------------------------------------------------------
// global utilities for testing
// ----------------------------------------------------------------------------
#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) ); \
+ CPPUNIT_ASSERT_EQUAL( r, 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) ); \
+ CPPUNIT_ASSERT_EQUAL( r, 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) ); \
+ CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP3(expected, fmt, y) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
- CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
+ CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
+#define CMP3i(expected, fmt, y) \
+ r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
+ CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
+ WX_ASSERT_MESSAGE( ("Expected \"%s\", got \"%s\"", expected, buf), \
+ wxStricmp(expected, buf) == 0 );
+
#define CMP2(expected, fmt) \
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt)); \
- CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
+ CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
ASSERT_STR_EQUAL( wxT(expected), buf );
// NOTE: this macro is used also with too-small buffers (see Miscellaneous())
class VsnprintfTestCase : public CppUnit::TestCase
{
public:
- VsnprintfTestCase();
+ VsnprintfTestCase() {}
+
+ virtual void setUp();
private:
CPPUNIT_TEST_SUITE( VsnprintfTestCase );
#endif
CPPUNIT_TEST( BigToSmallBuffer );
-#if wxUSE_WXVSNPRINTF
CPPUNIT_TEST( WrongFormatStrings );
-#endif // wxUSE_WXVSNPRINTF
CPPUNIT_TEST( Miscellaneous );
+ CPPUNIT_TEST( GlibcMisc1 );
+ CPPUNIT_TEST( GlibcMisc2 );
CPPUNIT_TEST_SUITE_END();
void C();
void DoBigToSmallBuffer(T *buffer, int size);
void BigToSmallBuffer();
-#if wxUSE_WXVSNPRINTF
void WrongFormatStrings();
-#endif // wxUSE_WXVSNPRINTF
// compares the expectedString and the result of wxVsnprintf() char by char
// for all its lenght (not only for first expectedLen chars) and also
size_t max, const wxChar *format, ...);
void Miscellaneous();
+ void GlibcMisc1();
+ void GlibcMisc2();
+
DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
-// also include in it's own registry so that these tests can be run alone
+// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
-VsnprintfTestCase::VsnprintfTestCase()
+void VsnprintfTestCase::setUp()
{
// this call is required to avoid check failures when running on machines
// with a locale where the decimal point is not '.'
- wxSetlocale(LC_NUMERIC, "English");
+ wxSetlocale(LC_ALL, "C");
}
void VsnprintfTestCase::C()
void VsnprintfTestCase::P()
{
- // WARNING: printing of pointers is not fully standard.
- // GNU prints them as %#x except for NULL pointers which are
- // printed as '(nil)'.
- // MSVC always print them as %8X on 32 bit systems and as %16X
- // on 64 bit systems
-#ifdef __VISUALC__
+ // The exact format used for "%p" is not specified by the standard and so
+ // varies among different platforms, so we need to expect different results
+ // here (remember that while we test our own wxPrintf() code here, it uses
+ // the system sprintf() for actual formatting so the results are still
+ // different under different systems).
+
+#ifdef wxUSING_VC_CRT_IO
+ // MSVC always prints pointers as %8X on 32 bit systems and as %16X on 64
+ // bit systems.
#if SIZEOF_VOID_P == 4
- CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
+ CMP3i("00ABCDEF", "%p", (void*)0xABCDEF);
CMP3("00000000", "%p", (void*)NULL);
#elif SIZEOF_VOID_P == 8
- CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
+ CMP3i("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
CMP3("0000000000000000", "%p", (void*)NULL);
#endif
+#elif defined(__MINGW32__)
+ // mingw32 uses MSVC CRT in old versions but is own implementation now
+ // which is somewhere in the middle as it uses %8x, so to catch both cases
+ // we use case-insensitive comparison here.
+ CMP3("0xabcdef", "%p", (void*)0xABCDEF);
+ CMP3("0", "%p", (void*)NULL);
#elif defined(__GNUG__)
+ // glibc prints pointers as %#x except for NULL pointers which are printed
+ // as '(nil)'.
CMP3("0xabcdef", "%p", (void*)0xABCDEF);
CMP3("(nil)", "%p", (void*)NULL);
#endif
{
int nchar;
- wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
+ wxSnprintf(buf, MAX_TEST_LEN, wxT("%d %s%n\n"), 3, wxT("bears"), &nchar);
CPPUNIT_ASSERT_EQUAL( 7, nchar );
}
#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
-#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
+ // the 'expected' and 'arg' parameters of this macro are supposed to be
+ // UTF-8 strings
+#define CMP3_UTF8(expected, fmt, arg) \
+ CPPUNIT_ASSERT_EQUAL \
+ ( \
+ wxString::FromUTF8(expected).length(), \
+ wxSnprintf(buf, MAX_TEST_LEN, fmt, wxString::FromUTF8(arg)) \
+ ); \
+ CPPUNIT_ASSERT_EQUAL \
+ ( \
+ wxString::FromUTF8(expected), \
+ buf \
+ )
+
+ CMP3_UTF8(" " ABC, "%5s", ABC);
+ CMP3_UTF8(" " ALPHA, "%5s", ALPHA);
+ CMP3_UTF8(ABCDEFGHI, "%5s", ABCDEFGHI);
+ CMP3_UTF8(ABC " ", "%-5s", ABC);
+ CMP3_UTF8(ABCDEFGHI, "%-5s", ABCDEFGHI);
+ CMP3_UTF8(ABCDE, "%.5s", ABCDEFGHI);
+#endif // wxUSE_UNICODE
// test a string which has a NULL character after "ab";
// obviously it should be handled exactly like just as "ab"
}
#endif
-// 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
-#if wxUSE_WXVSNPRINTF
-
void VsnprintfTestCase::WrongFormatStrings()
{
// test how wxVsnprintf() behaves with wrong format string:
CPPUNIT_ASSERT(r != -1);
#endif
- // a missing positional arg: this should result in an error but not all
- // implementations detect it (e.g. glibc doesn't)
- r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3);
- CPPUNIT_ASSERT_EQUAL(-1, r);
+ // a missing positional arg should result in an assert
+ WX_ASSERT_FAILS_WITH_ASSERT(
+ wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3) );
// positional and non-positionals in the same format string:
r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %d %3$d"), 1, 2, 3);
CPPUNIT_ASSERT_EQUAL(-1, r);
}
-#endif // wxUSE_WXVSNPRINTF
-
// BigToSmallBuffer() test case helper:
template<typename T>
void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
// format and gcc would warn about this otherwise
r = wxUnsafeSnprintf(buffer, size,
- _T("unicode string/char: %ls/%lc -- ansi string/char: %hs/%hc"),
+ wxT("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);
// Prepare messages so that it is possible to see from the error which
// test was running.
wxString errStr, overflowStr;
- errStr << _T("No.: ") << ++count << _T(", expected: ") << expectedLen
- << _T(" '") << expectedString << _T("', result: ");
- overflowStr << errStr << _T("buffer overflow");
- errStr << n << _T(" '") << buf << _T("'");
+ errStr << wxT("No.: ") << ++count << wxT(", expected: ") << expectedLen
+ << wxT(" '") << expectedString << wxT("', result: ");
+ overflowStr << errStr << wxT("buffer overflow");
+ errStr << n << wxT(" '") << buf << wxT("'");
// turn them into std::strings
std::string errMsg(errStr.mb_str());
DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
}
+
+/* (C) Copyright C E Chew
+*
+* Feel free to copy, use and distribute this software provided:
+*
+* 1. you do not pretend that you wrote it
+* 2. you leave this copyright notice intact.
+*/
+
+void VsnprintfTestCase::GlibcMisc1()
+{
+ CMP3(" ", "%5.s", "xyz");
+ CMP3(" 33", "%5.f", 33.3);
+#ifdef wxUSING_VC_CRT_IO
+ // see the previous notes about the minimum width of mantissa:
+ CMP3(" 3e+008", "%8.e", 33.3e7);
+ CMP3(" 3E+008", "%8.E", 33.3e7);
+ CMP3("3e+001", "%.g", 33.3);
+ CMP3("3E+001", "%.G", 33.3);
+#else
+ CMP3(" 3e+08", "%8.e", 33.3e7);
+ CMP3(" 3E+08", "%8.E", 33.3e7);
+ CMP3("3e+01", "%.g", 33.3);
+ CMP3("3E+01", "%.G", 33.3);
+#endif
+}
+
+void VsnprintfTestCase::GlibcMisc2()
+{
+ int prec;
+ wxString test_format;
+
+ prec = 0;
+ CMP4("3", "%.*g", prec, 3.3);
+
+ prec = 0;
+ CMP4("3", "%.*G", prec, 3.3);
+
+ prec = 0;
+ CMP4(" 3", "%7.*G", prec, 3.33);
+
+ prec = 3;
+ CMP4(" 041", "%04.*o", prec, 33);
+
+ prec = 7;
+ CMP4(" 0000033", "%09.*u", prec, 33);
+
+ prec = 3;
+ CMP4(" 021", "%04.*x", prec, 33);
+
+ prec = 3;
+ CMP4(" 021", "%04.*X", prec, 33);
+}
+
#endif // wxUSE_WXVSNPRINTF
+