+// 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)
+ );