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
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#include "wx/wxchar.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-12 21:08:35 -04:00
|
|
|
#define MAX_TEST_LEN 1024
|
|
|
|
|
|
|
|
|
|
|
|
// temporary buffers
|
2006-08-28 08:30:17 -04:00
|
|
|
static wxChar buf[MAX_TEST_LEN];
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
// these macros makes it possible to write all tests without repeating a lot of times wxT() macro
|
|
|
|
|
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
|
|
|
|
2006-08-12 21:08:35 -04:00
|
|
|
#define CMP5(expected, x, y, z, w) \
|
|
|
|
wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
|
|
|
|
\
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
#define CMP4(expected, x, y, z) \
|
|
|
|
wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
|
|
|
|
\
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
#define CMP3(expected, x, y) \
|
|
|
|
wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
|
|
|
|
\
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
#define CMP2(expected, x) \
|
|
|
|
wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
|
|
|
|
\
|
2006-08-28 08:30:17 -04:00
|
|
|
ASSERT_STR_EQUAL( wxT(expected), buf );
|
|
|
|
|
2006-08-30 04:39:06 -04:00
|
|
|
#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) )
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class VsnprintfTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VsnprintfTestCase();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( VsnprintfTestCase );
|
|
|
|
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 );
|
|
|
|
CPPUNIT_TEST( LongLong );
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
CPPUNIT_TEST( BigToSmallBuffer );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
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();
|
|
|
|
void LongLong();
|
|
|
|
void Unicode();
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
void BigToSmallBuffer();
|
|
|
|
void Misc(wxChar *buffer, int size);
|
|
|
|
|
|
|
|
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
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
|
|
|
|
|
|
|
|
VsnprintfTestCase::VsnprintfTestCase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
#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()
|
|
|
|
{
|
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-09-05 17:14:38 -04:00
|
|
|
void VsnprintfTestCase::LongLong()
|
2006-08-12 21:08:35 -04:00
|
|
|
{
|
2006-09-05 17:14:38 -04:00
|
|
|
CMP3("123456789", "%lld", (long long int)123456789);
|
|
|
|
CMP3("-123456789", "%lld", (long long int)-123456789);
|
2006-08-12 21:08:35 -04:00
|
|
|
|
2006-09-05 17:14:38 -04:00
|
|
|
CMP3("123456789", "%llu", (unsigned long long int)123456789);
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::Misc(wxChar *buffer, int size)
|
|
|
|
{
|
2006-08-28 08:30:17 -04:00
|
|
|
// 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.
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
// test without positionals
|
2006-08-28 08:30:17 -04:00
|
|
|
CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
|
|
|
|
"%i %li - test - %d %.3f",
|
2006-08-12 21:08:35 -04:00
|
|
|
123, (long int)444444444, 555, -0.666);
|
|
|
|
|
|
|
|
#if wxUSE_PRINTF_POS_PARAMS
|
|
|
|
// test with positional
|
2006-08-28 08:30:17 -04:00
|
|
|
CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555",
|
|
|
|
"%4$.3f %1$i - test - %2$li %3$d",
|
2006-08-12 21:08:35 -04:00
|
|
|
123, (long int)444444444, 555, -0.666);
|
|
|
|
#endif
|
2006-05-28 11:18:15 -04:00
|
|
|
|
|
|
|
// test unicode/ansi conversion specifiers
|
2006-05-30 13:22:04 -04:00
|
|
|
// NB: this line will output two warnings like these, on GCC:
|
2006-08-28 08:30:17 -04:00
|
|
|
// 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
|
2006-08-12 21:08:35 -04:00
|
|
|
|
|
|
|
CMPTOSIZE(buffer, size,
|
2006-08-28 08:30:17 -04:00
|
|
|
"unicode string: unicode!! W - ansi string: ansi!! w\n\n",
|
|
|
|
"unicode string: %ls %lc - ansi string: %hs %hc\n\n",
|
2006-08-12 21:08:35 -04:00
|
|
|
L"unicode!!", L'W', "ansi!!", 'w');
|
2006-05-28 11:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VsnprintfTestCase::BigToSmallBuffer()
|
|
|
|
{
|
|
|
|
wxChar buf[1024], buf2[16], buf3[4], buf4;
|
|
|
|
|
|
|
|
Misc(buf, 1024);
|
|
|
|
Misc(buf2, 16);
|
|
|
|
Misc(buf3, 4);
|
|
|
|
Misc(&buf4, 1);
|
|
|
|
}
|