2006-05-28 11:18:15 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/strings/vsnprintf.cpp
|
|
|
|
// Purpose: wxVsnprintf unit test
|
|
|
|
// Author: Francesco Montorsi
|
|
|
|
// (part of this file was taken from CMP.c of TRIO package
|
|
|
|
// written by Bjorn Reese and Daniel Stenberg)
|
|
|
|
// Created: 2006-04-01
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2008-06-01 12:58:17 -04:00
|
|
|
#include "wx/crt.h"
|
|
|
|
|
2008-05-30 18:11:51 -04:00
|
|
|
#if wxUSE_WXVSNPRINTF
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#include "wx/wxchar.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
|
2007-02-10 21:23:19 -05:00
|
|
|
// 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
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
|
2007-02-10 21:23:19 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// global utilities for testing
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define MAX_TEST_LEN 1024
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
// temporary buffers
|
2006-08-28 08:30:17 -04:00
|
|
|
static wxChar buf[MAX_TEST_LEN];
|
2007-01-07 11:17:28 -05:00
|
|
|
int r;
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2007-02-12 10:32:52 -05:00
|
|
|
// these macros makes it possible to write all tests without repeating a lot
|
2007-02-10 21:23:19 -05:00
|
|
|
// of times the wxT() macro
|
2007-02-12 10:32:52 -05:00
|
|
|
// NOTE: you should use expected strings with these macros which do not exceed
|
2007-02-10 21:23:19 -05:00
|
|
|
// MAX_TEST_LEN as these macro do check if the return value is == (int)wxStrlen(buf)
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2006-08-28 08:30:17 -04:00
|
|
|
#define ASSERT_STR_EQUAL( a, b ) \
|
2006-08-30 04:39:06 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
|
2006-08-28 08:30:17 -04:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
#define CMP6(expected, fmt, y, z, w, t) \
|
|
|
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w, t); \
|
2009-04-12 19:37:28 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
|
2009-02-18 09:57:42 -05:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
|
|
|
|
|
|
|
#define CMP5(expected, fmt, y, z, w) \
|
|
|
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w); \
|
2009-04-12 19:37:28 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
#define CMP4(expected, fmt, y, z) \
|
|
|
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z); \
|
2009-04-12 19:37:28 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
#define CMP3(expected, fmt, y) \
|
|
|
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
|
2009-04-12 19:37:28 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2010-10-03 13:15:10 -04:00
|
|
|
#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 );
|
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
#define CMP2(expected, fmt) \
|
|
|
|
r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt)); \
|
2009-04-12 19:37:28 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
|
|
|
|
2007-02-10 21:23:19 -05:00
|
|
|
// 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)
|
2009-02-18 09:57:42 -05:00
|
|
|
#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))
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2007-02-12 10:32:52 -05:00
|
|
|
// this is the same as wxSnprintf() but it passes the format string to
|
2009-03-11 03:51:38 -04:00
|
|
|
// wxVsnprintf() without using WX_ATTRIBUTE_PRINTF and thus suppresses the gcc
|
2007-02-12 10:32:52 -05:00
|
|
|
// checks (and resulting warnings) for the format string
|
|
|
|
//
|
|
|
|
// use with extreme care and only when you're really sure the warnings must be
|
|
|
|
// suppressed!
|
2007-06-29 13:30:56 -04:00
|
|
|
template<typename T>
|
2007-02-12 10:32:52 -05:00
|
|
|
static int
|
2007-06-29 13:30:56 -04:00
|
|
|
wxUnsafeSnprintf(T *buf, size_t len, const wxChar *fmt, ...)
|
2007-02-12 10:32:52 -05:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
int rc = wxVsnprintf(buf, len, fmt, args);
|
|
|
|
|
|
|
|
va_end(args);
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2007-02-12 10:32:52 -05:00
|
|
|
return rc;
|
|
|
|
}
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class VsnprintfTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
2009-03-23 12:25:44 -04:00
|
|
|
VsnprintfTestCase() {}
|
|
|
|
|
|
|
|
virtual void setUp();
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( VsnprintfTestCase );
|
2007-02-10 21:23:19 -05:00
|
|
|
CPPUNIT_TEST( C );
|
2006-10-22 07:41:44 -04:00
|
|
|
CPPUNIT_TEST( D );
|
|
|
|
CPPUNIT_TEST( X );
|
|
|
|
CPPUNIT_TEST( O );
|
|
|
|
CPPUNIT_TEST( P );
|
|
|
|
CPPUNIT_TEST( N );
|
2006-05-28 11:18:15 -04:00
|
|
|
CPPUNIT_TEST( E );
|
|
|
|
CPPUNIT_TEST( F );
|
|
|
|
CPPUNIT_TEST( G );
|
|
|
|
CPPUNIT_TEST( S );
|
2006-08-12 21:08:35 -04:00
|
|
|
CPPUNIT_TEST( Asterisk );
|
2006-09-05 17:14:38 -04:00
|
|
|
CPPUNIT_TEST( Percent );
|
2006-11-08 20:25:23 -05:00
|
|
|
#ifdef wxLongLong_t
|
2006-09-05 17:14:38 -04:00
|
|
|
CPPUNIT_TEST( LongLong );
|
2006-11-08 20:25:23 -05:00
|
|
|
#endif
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
CPPUNIT_TEST( BigToSmallBuffer );
|
2007-01-07 11:17:28 -05:00
|
|
|
CPPUNIT_TEST( WrongFormatStrings );
|
2006-11-10 17:49:45 -05:00
|
|
|
CPPUNIT_TEST( Miscellaneous );
|
2010-06-02 10:42:26 -04:00
|
|
|
CPPUNIT_TEST( GlibcMisc1 );
|
|
|
|
CPPUNIT_TEST( GlibcMisc2 );
|
2006-05-28 11:18:15 -04:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2007-02-10 21:23:19 -05:00
|
|
|
void C();
|
2006-10-22 07:41:44 -04:00
|
|
|
void D();
|
|
|
|
void X();
|
|
|
|
void O();
|
|
|
|
void P();
|
|
|
|
void N();
|
2006-05-28 11:18:15 -04:00
|
|
|
void E();
|
|
|
|
void F();
|
|
|
|
void G();
|
|
|
|
void S();
|
2006-08-12 21:08:35 -04:00
|
|
|
void Asterisk();
|
2006-09-05 17:14:38 -04:00
|
|
|
void Percent();
|
2006-11-08 20:25:23 -05:00
|
|
|
#ifdef wxLongLong_t
|
2006-09-05 17:14:38 -04:00
|
|
|
void LongLong();
|
2006-11-08 20:25:23 -05:00
|
|
|
#endif
|
2006-09-05 17:14:38 -04:00
|
|
|
void Unicode();
|
2006-05-28 11:18:15 -04:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
template<typename T>
|
|
|
|
void DoBigToSmallBuffer(T *buffer, int size);
|
2006-05-28 11:18:15 -04:00
|
|
|
void BigToSmallBuffer();
|
2009-02-18 09:57:42 -05:00
|
|
|
|
2007-01-07 11:17:28 -05:00
|
|
|
void WrongFormatStrings();
|
2006-05-28 11:18:15 -04:00
|
|
|
|
2007-02-10 21:23:19 -05:00
|
|
|
// 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, ...);
|
2009-02-18 09:57:42 -05:00
|
|
|
void Miscellaneous();
|
2007-02-10 21:23:19 -05:00
|
|
|
|
2010-06-02 10:42:26 -04:00
|
|
|
void GlibcMisc1();
|
|
|
|
void GlibcMisc2();
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
|
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2006-05-28 11:18:15 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
|
|
|
|
|
2009-03-23 12:25:44 -04:00
|
|
|
void VsnprintfTestCase::setUp()
|
2006-05-28 11:18:15 -04:00
|
|
|
{
|
2009-02-17 06:11:50 -05:00
|
|
|
// this call is required to avoid check failures when running on machines
|
|
|
|
// with a locale where the decimal point is not '.'
|
2009-03-23 12:23:44 -04:00
|
|
|
wxSetlocale(LC_ALL, "C");
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
2007-02-10 21:23:19 -05:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2006-10-22 07:41:44 -04:00
|
|
|
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()
|
|
|
|
{
|
2010-10-03 13:16:00 -04:00
|
|
|
// 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.
|
2006-10-22 07:41:44 -04:00
|
|
|
#if SIZEOF_VOID_P == 4
|
2010-10-03 13:15:10 -04:00
|
|
|
CMP3i("00ABCDEF", "%p", (void*)0xABCDEF);
|
2006-10-22 07:41:44 -04:00
|
|
|
CMP3("00000000", "%p", (void*)NULL);
|
|
|
|
#elif SIZEOF_VOID_P == 8
|
2010-10-03 13:15:10 -04:00
|
|
|
CMP3i("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
|
2006-10-22 07:41:44 -04:00
|
|
|
CMP3("0000000000000000", "%p", (void*)NULL);
|
|
|
|
#endif
|
2009-04-04 12:02:23 -04:00
|
|
|
#elif defined(__MINGW32__)
|
2010-10-03 13:16:00 -04:00
|
|
|
// 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.
|
2009-04-04 12:02:23 -04:00
|
|
|
CMP3("0xabcdef", "%p", (void*)0xABCDEF);
|
|
|
|
CMP3("0", "%p", (void*)NULL);
|
2006-10-22 07:41:44 -04:00
|
|
|
#elif defined(__GNUG__)
|
2010-10-03 13:16:00 -04:00
|
|
|
// glibc prints pointers as %#x except for NULL pointers which are printed
|
|
|
|
// as '(nil)'.
|
2006-10-22 07:41:44 -04:00
|
|
|
CMP3("0xabcdef", "%p", (void*)0xABCDEF);
|
|
|
|
CMP3("(nil)", "%p", (void*)NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::N()
|
|
|
|
{
|
|
|
|
int nchar;
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSnprintf(buf, MAX_TEST_LEN, wxT("%d %s%n\n"), 3, wxT("bears"), &nchar);
|
2006-10-22 07:41:44 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 7, nchar );
|
|
|
|
}
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
void VsnprintfTestCase::E()
|
|
|
|
{
|
2006-05-30 13:22:04 -04:00
|
|
|
// NB: there are no standards about the minimum exponent width
|
|
|
|
// (and the width of the %e conversion specifier refers to the
|
|
|
|
// mantissa, not to the exponent).
|
|
|
|
// Since newer MSVC versions use 3 digits as minimum exponent
|
|
|
|
// width while GNU libc uses 2 digits as minimum width, here we
|
|
|
|
// workaround this problem using for the exponent values with at
|
|
|
|
// least three digits.
|
|
|
|
// Some examples:
|
|
|
|
// printf("%e",2.342E+02);
|
|
|
|
// -> under MSVC7.1 prints: 2.342000e+002
|
|
|
|
// -> under GNU libc 2.4 prints: 2.342000e+02
|
2006-08-12 21:08:35 -04:00
|
|
|
CMP3("2.342000e+112", "%e",2.342E+112);
|
|
|
|
CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
|
|
|
|
CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
|
|
|
|
CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
|
|
|
|
|
|
|
|
CMP3("-0.02342", "%G",-2.342E-02);
|
|
|
|
CMP3("3.1415E-116", "%G",3.1415e-116);
|
|
|
|
CMP3("0003.141500e+103", "%016e", 3141.5e100);
|
|
|
|
CMP3(" 3.141500e+103", "%16e", 3141.5e100);
|
|
|
|
CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
|
|
|
|
CMP3("3.142e+103", "%010.3e", 3141.5e100);
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::F()
|
|
|
|
{
|
2006-08-12 21:08:35 -04:00
|
|
|
CMP3("3.300000", "%5f", 3.3);
|
|
|
|
CMP3("3.000000", "%5f", 3.0);
|
|
|
|
CMP3("0.000100", "%5f", .999999E-4);
|
|
|
|
CMP3("0.000990", "%5f", .99E-3);
|
|
|
|
CMP3("3333.000000", "%5f", 3333.0);
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::G()
|
|
|
|
{
|
2006-05-30 13:22:04 -04:00
|
|
|
// NOTE: the same about E() testcase applies here...
|
|
|
|
|
2006-08-12 21:08:35 -04:00
|
|
|
CMP3(" 3.3", "%5g", 3.3);
|
|
|
|
CMP3(" 3", "%5g", 3.0);
|
|
|
|
CMP3("9.99999e-115", "%5g", .999999E-114);
|
|
|
|
CMP3("0.00099", "%5g", .99E-3);
|
|
|
|
CMP3(" 3333", "%5g", 3333.0);
|
|
|
|
CMP3(" 0.01", "%5g", 0.01);
|
|
|
|
|
|
|
|
CMP3(" 3", "%5.g", 3.3);
|
|
|
|
CMP3(" 3", "%5.g", 3.0);
|
|
|
|
CMP3("1e-114", "%5.g", .999999E-114);
|
|
|
|
CMP3("0.0001", "%5.g", 1.0E-4);
|
|
|
|
CMP3("0.001", "%5.g", .99E-3);
|
|
|
|
CMP3("3e+103", "%5.g", 3333.0E100);
|
|
|
|
CMP3(" 0.01", "%5.g", 0.01);
|
|
|
|
|
|
|
|
CMP3(" 3.3", "%5.2g", 3.3);
|
|
|
|
CMP3(" 3", "%5.2g", 3.0);
|
|
|
|
CMP3("1e-114", "%5.2g", .999999E-114);
|
|
|
|
CMP3("0.00099", "%5.2g", .99E-3);
|
|
|
|
CMP3("3.3e+103", "%5.2g", 3333.0E100);
|
|
|
|
CMP3(" 0.01", "%5.2g", 0.01);
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::S()
|
|
|
|
{
|
2006-08-12 21:08:35 -04:00
|
|
|
CMP3(" abc", "%5s", wxT("abc"));
|
|
|
|
CMP3(" a", "%5s", wxT("a"));
|
|
|
|
CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
|
|
|
|
CMP3("abc ", "%-5s", wxT("abc"));
|
|
|
|
CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
|
|
|
|
|
|
|
|
CMP3("abcde", "%.5s", wxT("abcdefghi"));
|
|
|
|
|
2006-09-05 17:14:38 -04:00
|
|
|
// do the same tests but with Unicode characters:
|
2009-02-18 09:57:42 -05:00
|
|
|
#if wxUSE_UNICODE
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2009-04-04 12:45:23 -04:00
|
|
|
// 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
|
2007-02-10 21:23:19 -05:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
// test a string which has a NULL character after "ab";
|
2007-02-10 21:23:19 -05:00
|
|
|
// obviously it should be handled exactly like just as "ab"
|
|
|
|
CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
|
2006-09-05 17:14:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2009-02-18 09:57:42 -05:00
|
|
|
|
|
|
|
CMP4(" a", "%*c", 8, 'a');
|
|
|
|
CMP4(" four", "%*s", 8, "four");
|
|
|
|
CMP6(" four four", "%*s %*s", 8, "four", 6, "four");
|
2006-09-05 17:14:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::Percent()
|
|
|
|
{
|
2006-08-12 21:08:35 -04:00
|
|
|
// some tests without any argument passed through ...
|
2006-08-28 08:30:17 -04:00
|
|
|
CMP2("%", "%%");
|
|
|
|
CMP2("%%%", "%%%%%%");
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2006-09-05 17:14:38 -04:00
|
|
|
CMP3("% abc", "%%%5s", wxT("abc"));
|
|
|
|
CMP3("% abc%", "%%%5s%%", wxT("abc"));
|
|
|
|
|
2006-08-12 21:08:35 -04:00
|
|
|
// do not test odd number of '%' symbols as different implementations
|
|
|
|
// of snprintf() give different outputs as this situation is not considered
|
|
|
|
// by any standard (in fact, GCC will also warn you about a spurious % if
|
|
|
|
// you write %%% as argument of some *printf function !)
|
|
|
|
// Compare(wxT("%"), wxT("%%%"));
|
|
|
|
}
|
|
|
|
|
2006-11-08 20:25:23 -05:00
|
|
|
#ifdef wxLongLong_t
|
2006-09-05 17:14:38 -04:00
|
|
|
void VsnprintfTestCase::LongLong()
|
2006-08-12 21:08:35 -04:00
|
|
|
{
|
2006-11-08 20:25:23 -05:00
|
|
|
CMP3("123456789", "%lld", (wxLongLong_t)123456789);
|
|
|
|
CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2006-11-08 20:25:23 -05:00
|
|
|
CMP3("123456789", "%llu", (wxULongLong_t)123456789);
|
2007-01-07 11:58:17 -05:00
|
|
|
|
2012-03-04 15:31:42 -05:00
|
|
|
#ifdef __WINDOWS__
|
2007-01-07 11:58:17 -05:00
|
|
|
CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
|
2008-02-20 17:28:02 -05:00
|
|
|
CMP3("123456789abcdef", "%I64x", wxLL(0x123456789abcdef));
|
2007-01-07 11:58:17 -05:00
|
|
|
#endif
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
2006-11-08 20:25:23 -05:00
|
|
|
#endif
|
2006-05-28 11:18:15 -04:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
void VsnprintfTestCase::WrongFormatStrings()
|
|
|
|
{
|
|
|
|
// test how wxVsnprintf() behaves with wrong format string:
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// NB: the next 2 tests currently return an error but it would be nice
|
|
|
|
// if they didn't (see ticket #9367)
|
|
|
|
|
|
|
|
// two positionals with the same index:
|
|
|
|
r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$s %1$s"), "hello");
|
|
|
|
CPPUNIT_ASSERT(r != -1);
|
|
|
|
|
|
|
|
// three positionals with the same index mixed with other pos args:
|
|
|
|
r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
|
|
|
|
CPPUNIT_ASSERT(r != -1);
|
|
|
|
#endif
|
|
|
|
|
2010-09-30 10:30:23 -04:00
|
|
|
// 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) );
|
2009-02-18 09:57:42 -05:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigToSmallBuffer() test case helper:
|
2007-06-29 13:30:56 -04:00
|
|
|
template<typename T>
|
2009-02-18 09:57:42 -05:00
|
|
|
void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
|
2006-05-28 11:18:15 -04:00
|
|
|
{
|
2007-02-10 21:23:19 -05:00
|
|
|
// 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.
|
2007-01-07 11:17:28 -05:00
|
|
|
//
|
2007-02-10 21:23:19 -05:00
|
|
|
// 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
|
2007-02-12 10:32:52 -05:00
|
|
|
// output or
|
2007-02-10 21:23:19 -05:00
|
|
|
// -1 if there was an error in the format string
|
|
|
|
// maxSize+1 if the output buffer is too small
|
2006-05-28 11:18:15 -04:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
wxString errStr;
|
|
|
|
errStr << "The size of the buffer was " << size;
|
|
|
|
std::string errMsg(errStr.mb_str());
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
// test without positionals
|
2009-02-18 09:57:42 -05:00
|
|
|
CMPTOSIZE(buffer, size, errMsg,
|
|
|
|
"123456789012 - test - 123 -4.567",
|
|
|
|
"%i%li - test - %d %.3f",
|
|
|
|
123, (long int)456789012, 123, -4.567);
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
#if wxUSE_PRINTF_POS_PARAMS
|
|
|
|
// test with positional
|
2009-02-18 09:57:42 -05:00
|
|
|
CMPTOSIZE(buffer, size, errMsg,
|
|
|
|
"-4.567 123 - test - 456789012 123",
|
2006-08-28 08:30:17 -04:00
|
|
|
"%4$.3f %1$i - test - %2$li %3$d",
|
2009-02-18 09:57:42 -05:00
|
|
|
123, (long int)456789012, 123, -4.567);
|
2006-08-12 21:08:35 -04:00
|
|
|
#endif
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
// test unicode/ansi conversion specifiers
|
2007-02-12 10:32:52 -05:00
|
|
|
//
|
|
|
|
// NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
|
|
|
|
// format and gcc would warn about this otherwise
|
|
|
|
|
|
|
|
r = wxUnsafeSnprintf(buffer, size,
|
2009-07-23 16:30:22 -04:00
|
|
|
wxT("unicode string/char: %ls/%lc -- ansi string/char: %hs/%hc"),
|
2009-02-18 09:57:42 -05:00
|
|
|
L"unicode", L'U', "ansi", 'A');
|
2009-02-17 06:11:50 -05:00
|
|
|
wxString expected =
|
2009-02-18 09:57:42 -05:00
|
|
|
wxString(wxT("unicode string/char: unicode/U -- ansi string/char: ansi/A")).Left(size - 1);
|
2009-02-17 06:11:50 -05:00
|
|
|
|
2007-02-12 10:32:52 -05:00
|
|
|
CPPUNIT_ASSERT( r != -1 );
|
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
2009-02-17 06:11:50 -05:00
|
|
|
expected,
|
2007-02-12 10:32:52 -05:00
|
|
|
wxString(buffer)
|
|
|
|
);
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::BigToSmallBuffer()
|
|
|
|
{
|
2008-01-12 20:13:03 -05:00
|
|
|
// VC6 can't compile this code
|
|
|
|
#if !defined(__VISUALC__) || (__VISUALC__ >= 1310)
|
2007-07-04 16:35:42 -04:00
|
|
|
#if wxUSE_UNICODE
|
2007-07-23 18:34:29 -04:00
|
|
|
wchar_t bufw[1024], bufw2[16], bufw3[4], bufw4;
|
2009-02-18 09:57:42 -05:00
|
|
|
DoBigToSmallBuffer(bufw, 1024);
|
|
|
|
DoBigToSmallBuffer(bufw2, 16);
|
|
|
|
DoBigToSmallBuffer(bufw3, 4);
|
|
|
|
DoBigToSmallBuffer(&bufw4, 1);
|
2007-07-04 16:35:42 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2007-06-29 13:30:56 -04:00
|
|
|
|
2008-01-12 20:13:03 -05:00
|
|
|
char bufa[1024], bufa2[16], bufa3[4], bufa4;
|
2009-02-18 09:57:42 -05:00
|
|
|
DoBigToSmallBuffer(bufa, 1024);
|
|
|
|
DoBigToSmallBuffer(bufa2, 16);
|
|
|
|
DoBigToSmallBuffer(bufa3, 4);
|
|
|
|
DoBigToSmallBuffer(&bufa4, 1);
|
2008-01-12 20:13:03 -05:00
|
|
|
#endif // !VC6
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
2006-11-08 20:25:23 -05:00
|
|
|
|
2009-02-18 09:57:42 -05:00
|
|
|
// Miscellaneous() test case helper:
|
2007-02-10 21:23:19 -05:00
|
|
|
void VsnprintfTestCase::DoMisc(
|
2006-11-08 20:25:23 -05:00
|
|
|
int expectedLen,
|
|
|
|
const wxString& expectedString,
|
|
|
|
size_t max,
|
|
|
|
const wxChar *format, ...)
|
|
|
|
{
|
2007-02-10 21:23:19 -05:00
|
|
|
const size_t BUFSIZE = MAX_TEST_LEN - 1;
|
2006-11-08 20:25:23 -05:00
|
|
|
size_t i;
|
|
|
|
static int count = 0;
|
|
|
|
|
|
|
|
wxASSERT(max <= BUFSIZE);
|
|
|
|
|
|
|
|
for (i = 0; i < BUFSIZE; i++)
|
|
|
|
buf[i] = '*';
|
|
|
|
buf[BUFSIZE] = 0;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2007-02-12 10:32:52 -05:00
|
|
|
|
2006-11-08 20:25:23 -05:00
|
|
|
int n = wxVsnprintf(buf, max, format, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
// Prepare messages so that it is possible to see from the error which
|
|
|
|
// test was running.
|
|
|
|
wxString errStr, overflowStr;
|
2009-07-23 16:30:22 -04:00
|
|
|
errStr << wxT("No.: ") << ++count << wxT(", expected: ") << expectedLen
|
|
|
|
<< wxT(" '") << expectedString << wxT("', result: ");
|
|
|
|
overflowStr << errStr << wxT("buffer overflow");
|
|
|
|
errStr << n << wxT(" '") << buf << wxT("'");
|
2006-11-08 20:25:23 -05:00
|
|
|
|
|
|
|
// turn them into std::strings
|
|
|
|
std::string errMsg(errStr.mb_str());
|
|
|
|
std::string overflowMsg(overflowStr.mb_str());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(errMsg,
|
|
|
|
(expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
|
|
|
|
|
|
|
|
for (i = max; i < BUFSIZE; i++)
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
|
|
|
|
}
|
|
|
|
|
2006-11-10 17:49:45 -05:00
|
|
|
void VsnprintfTestCase::Miscellaneous()
|
2006-11-08 20:25:23 -05:00
|
|
|
{
|
|
|
|
// expectedLen, expectedString, max, format, ...
|
2009-02-18 09:57:42 -05:00
|
|
|
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("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(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
|
|
|
|
DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
|
2006-11-08 20:25:23 -05:00
|
|
|
}
|
2008-05-30 18:11:51 -04:00
|
|
|
|
2010-06-02 10:42:26 -04:00
|
|
|
|
|
|
|
/* (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);
|
2010-10-03 13:16:00 -04:00
|
|
|
#ifdef wxUSING_VC_CRT_IO
|
2010-06-02 10:42:26 -04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:11:51 -04:00
|
|
|
#endif // wxUSE_WXVSNPRINTF
|
2010-06-02 10:42:26 -04:00
|
|
|
|