// temporary buffers
-static wxChar buf[MAX_TEST_LEN], buf2[MAX_TEST_LEN];
-
+static wxChar buf[MAX_TEST_LEN];
// these macros makes it possible to write all tests without repeating a lot of times wxT() macro
+#define ASSERT_STR_EQUAL( a, b ) \
+ CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
+
#define CMP5(expected, x, y, z, w) \
wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
- snprintf(buf2, MAX_TEST_LEN, wxT(x), y, z, w); \
\
- CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
- CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
+ ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP4(expected, x, y, z) \
wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
- snprintf(buf2, MAX_TEST_LEN, wxT(x), y, z); \
\
- CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
- CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
+ ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP3(expected, x, y) \
wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
- snprintf(buf2, MAX_TEST_LEN, wxT(x), y); \
\
- CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
- CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
+ ASSERT_STR_EQUAL( wxT(expected), buf );
#define CMP2(expected, x) \
wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
- snprintf(buf2, MAX_TEST_LEN, wxT(x)); \
\
- CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
- CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
+ ASSERT_STR_EQUAL( wxT(expected), buf );
+
+#define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
+ wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
+ \
+ CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
+ wxString(buffer) )
-#define CMPTOSIZE(buffer, size, fmt, x, y, z, w) \
- wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), x, y, z, w); \
- snprintf(buf2, MAX_TEST_LEN, wxT(fmt), x, y, z, w); \
- \
- CPPUNIT_ASSERT_EQUAL( wxString(buf2, size), wxString(buf, size) );
// ----------------------------------------------------------------------------
private:
CPPUNIT_TEST_SUITE( VsnprintfTestCase );
+ CPPUNIT_TEST( D );
+ CPPUNIT_TEST( X );
+ CPPUNIT_TEST( O );
+ CPPUNIT_TEST( P );
+ CPPUNIT_TEST( N );
CPPUNIT_TEST( E );
CPPUNIT_TEST( F );
CPPUNIT_TEST( G );
CPPUNIT_TEST( S );
CPPUNIT_TEST( Asterisk );
+ CPPUNIT_TEST( Percent );
+ CPPUNIT_TEST( LongLong );
CPPUNIT_TEST( BigToSmallBuffer );
CPPUNIT_TEST_SUITE_END();
+ void D();
+ void X();
+ void O();
+ void P();
+ void N();
void E();
void F();
void G();
void S();
void Asterisk();
+ void Percent();
+ void LongLong();
+ void Unicode();
void BigToSmallBuffer();
void Misc(wxChar *buffer, int size);
{
}
+void VsnprintfTestCase::D()
+{
+ CMP3("+123456", "%+d", 123456);
+ CMP3("-123456", "%d", -123456);
+ CMP3(" 123456", "% d", 123456);
+ CMP3(" 123456", "%10d", 123456);
+ CMP3("0000123456", "%010d", 123456);
+ CMP3("-123456 ", "%-10d", -123456);
+}
+
+void VsnprintfTestCase::X()
+{
+ CMP3("ABCD", "%X", 0xABCD);
+ CMP3("0XABCD", "%#X", 0xABCD);
+ CMP3("0xabcd", "%#x", 0xABCD);
+}
+
+void VsnprintfTestCase::O()
+{
+ CMP3("1234567", "%o", 01234567);
+ CMP3("01234567", "%#o", 01234567);
+}
+
+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__
+ #if SIZEOF_VOID_P == 4
+ CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
+ CMP3("00000000", "%p", (void*)NULL);
+ #elif SIZEOF_VOID_P == 8
+ CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
+ CMP3("0000000000000000", "%p", (void*)NULL);
+ #endif
+#elif defined(__GNUG__)
+ CMP3("0xabcdef", "%p", (void*)0xABCDEF);
+ CMP3("(nil)", "%p", (void*)NULL);
+#endif
+}
+
+void VsnprintfTestCase::N()
+{
+ int nchar;
+
+ wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
+ CPPUNIT_ASSERT_EQUAL( 7, nchar );
+}
+
void VsnprintfTestCase::E()
{
// NB: there are no standards about the minimum exponent width
CMP3("abcde", "%.5s", wxT("abcdefghi"));
+ // do the same tests but with Unicode characters:
+#if wxUSE_UNICODE
+ #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"
+
+ #define ABC ALPHA BETA GAMMA
+ #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
+ #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
+
+ 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));
+
+ CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
+#endif
+}
+
+void VsnprintfTestCase::Asterisk()
+{
+ CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
+ CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
+ CMP5("0.1", "%*.*f", 3, 1, 0.123);
+
+ CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
+}
+
+void VsnprintfTestCase::Percent()
+{
// some tests without any argument passed through ...
- CMP2(wxT("%"), wxT("%%"));
- CMP2(wxT("%%%"), wxT("%%%%%%"));
+ CMP2("%", "%%");
+ CMP2("%%%", "%%%%%%");
+
+ CMP3("% abc", "%%%5s", wxT("abc"));
+ CMP3("% abc%", "%%%5s%%", wxT("abc"));
// do not test odd number of '%' symbols as different implementations
// of snprintf() give different outputs as this situation is not considered
// Compare(wxT("%"), wxT("%%%"));
}
-void VsnprintfTestCase::Asterisk()
+void VsnprintfTestCase::LongLong()
{
- CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
- CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
- CMP5("0.1", "%*.*f", 3, 1, 0.123);
+ CMP3("123456789", "%lld", (long long int)123456789);
+ CMP3("-123456789", "%lld", (long long int)-123456789);
- CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
+ CMP3("123456789", "%llu", (unsigned long long int)123456789);
}
void VsnprintfTestCase::Misc(wxChar *buffer, int size)
{
- // NB: 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).
- // Fortunately, in all implementations, when the output buffer is too small, it's
- // nonetheless filled up to its max size.
+ // NB: 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). Fortunately, in all implementations, when the
+ // output buffer is too small, it's nonetheless filled up to its max
+ // size.
// test without positionals
- CMPTOSIZE(buffer, size,
- wxT("%i %li - test - %d %.3f"),
+ 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,
- wxT("%4$.3f %1$i - test - %2$li %3$d"),
+ 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: this line will output two warnings like these, on GCC:
- // warning: use of ‘h’ length modifier with ‘s’ type character
- // (i.e. GCC warns you that 'h' is not legal on 's' conv spec) but they must be ignored
- // as here we explicitely want to test the wxSnprintf() behaviour in such case
+ // warning: use of 'h' length modifier with 's' type character (i.e.
+ // GCC warns you that 'h' is not legal on 's' conv spec) but they must
+ // be ignored as here we explicitely want to test the wxSnprintf()
+ // behaviour in such case
CMPTOSIZE(buffer, size,
- wxT("unicode string: %ls %lc - ansi string: %hs %hc\n\n"),
+ "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
+ "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
L"unicode!!", L'W', "ansi!!", 'w');
}