Move some wxPrintf() tests (taken from glibc) to VsnprintfTestCase. Other tests are more difficult to convert in CppUnit style and not worth the effort (since now we don't use wx's own vsnprintf implementation anymore typically).
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64468 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -119,8 +119,6 @@
|
||||
#define TEST_MIME
|
||||
#define TEST_MODULE
|
||||
#define TEST_PATHLIST
|
||||
#define TEST_PRINTF
|
||||
#define TEST_REGEX
|
||||
#else // #if TEST_ALL
|
||||
#define TEST_DATETIME
|
||||
#define TEST_VOLUME
|
||||
@@ -128,6 +126,7 @@
|
||||
#define TEST_STACKWALKER
|
||||
#define TEST_FTP
|
||||
#define TEST_SNGLINST
|
||||
#define TEST_REGEX
|
||||
#endif
|
||||
|
||||
// some tests are interactive, define this to run them
|
||||
@@ -1551,380 +1550,6 @@ static void TestRegExInteractive()
|
||||
|
||||
#endif // TEST_REGEX
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// printf() tests
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
NB: this stuff was taken from the glibc test suite and modified to build
|
||||
in wxWidgets: if I read the copyright below properly, this shouldn't
|
||||
be a problem
|
||||
*/
|
||||
|
||||
#ifdef TEST_PRINTF
|
||||
|
||||
#ifdef wxTEST_PRINTF
|
||||
// use our functions from wxchar.cpp
|
||||
#undef wxPrintf
|
||||
#undef wxSprintf
|
||||
|
||||
// NB: do _not_ use WX_ATTRIBUTE_PRINTF here, we have some invalid formats
|
||||
// in the tests below
|
||||
int wxPrintf( const wxChar *format, ... );
|
||||
int wxSprintf( wxChar *str, const wxChar *format, ... );
|
||||
#endif
|
||||
|
||||
#include "wx/longlong.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
static void rfg1 (void);
|
||||
static void rfg2 (void);
|
||||
|
||||
|
||||
static void
|
||||
fmtchk (const wxChar *fmt)
|
||||
{
|
||||
(void) wxPrintf(wxT("%s:\t`"), fmt);
|
||||
(void) wxPrintf(fmt, 0x12);
|
||||
(void) wxPrintf(wxT("'\n"));
|
||||
}
|
||||
|
||||
static void
|
||||
fmtst1chk (const wxChar *fmt)
|
||||
{
|
||||
(void) wxPrintf(wxT("%s:\t`"), fmt);
|
||||
(void) wxPrintf(fmt, 4, 0x12);
|
||||
(void) wxPrintf(wxT("'\n"));
|
||||
}
|
||||
|
||||
static void
|
||||
fmtst2chk (const wxChar *fmt)
|
||||
{
|
||||
(void) wxPrintf(wxT("%s:\t`"), fmt);
|
||||
(void) wxPrintf(fmt, 4, 4, 0x12);
|
||||
(void) wxPrintf(wxT("'\n"));
|
||||
}
|
||||
|
||||
/* This page is covered by the following copyright: */
|
||||
|
||||
/* (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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
|
||||
*/
|
||||
|
||||
#define DEC -123
|
||||
#define INT 255
|
||||
#define UNS (~0)
|
||||
|
||||
/* Formatted Output Test
|
||||
*
|
||||
* This exercises the output formatting code.
|
||||
*/
|
||||
|
||||
wxChar *PointerNull = NULL;
|
||||
|
||||
static void
|
||||
fp_test (void)
|
||||
{
|
||||
int i, j, k, l;
|
||||
wxChar buf[7];
|
||||
wxChar *prefix = buf;
|
||||
wxChar tp[20];
|
||||
|
||||
wxPuts(wxT("\nFormatted output test"));
|
||||
wxPrintf(wxT("prefix 6d 6o 6x 6X 6u\n"));
|
||||
wxStrcpy(prefix, wxT("%"));
|
||||
for (i = 0; i < 2; i++) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
for (k = 0; k < 2; k++) {
|
||||
for (l = 0; l < 2; l++) {
|
||||
wxStrcpy(prefix, wxT("%"));
|
||||
if (i == 0) wxStrcat(prefix, wxT("-"));
|
||||
if (j == 0) wxStrcat(prefix, wxT("+"));
|
||||
if (k == 0) wxStrcat(prefix, wxT("#"));
|
||||
if (l == 0) wxStrcat(prefix, wxT("0"));
|
||||
wxPrintf(wxT("%5s |"), prefix);
|
||||
wxStrcpy(tp, prefix);
|
||||
wxStrcat(tp, wxT("6d |"));
|
||||
wxPrintf(tp, DEC);
|
||||
wxStrcpy(tp, prefix);
|
||||
wxStrcat(tp, wxT("6o |"));
|
||||
wxPrintf(tp, INT);
|
||||
wxStrcpy(tp, prefix);
|
||||
wxStrcat(tp, wxT("6x |"));
|
||||
wxPrintf(tp, INT);
|
||||
wxStrcpy(tp, prefix);
|
||||
wxStrcat(tp, wxT("6X |"));
|
||||
wxPrintf(tp, INT);
|
||||
wxStrcpy(tp, prefix);
|
||||
wxStrcat(tp, wxT("6u |"));
|
||||
wxPrintf(tp, UNS);
|
||||
wxPrintf(wxT("\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
wxPrintf(wxT("%10s\n"), PointerNull);
|
||||
wxPrintf(wxT("%-10s\n"), PointerNull);
|
||||
}
|
||||
|
||||
static void TestPrintf()
|
||||
{
|
||||
static wxChar shortstr[] = wxT("Hi, Z.");
|
||||
static wxChar longstr[] = wxT("Good morning, Doctor Chandra. This is Hal. \
|
||||
I am ready for my first lesson today.");
|
||||
int result = 0;
|
||||
wxString test_format;
|
||||
|
||||
fmtchk(wxT("%.4x"));
|
||||
fmtchk(wxT("%04x"));
|
||||
fmtchk(wxT("%4.4x"));
|
||||
fmtchk(wxT("%04.4x"));
|
||||
fmtchk(wxT("%4.3x"));
|
||||
fmtchk(wxT("%04.3x"));
|
||||
|
||||
fmtst1chk(wxT("%.*x"));
|
||||
fmtst1chk(wxT("%0*x"));
|
||||
fmtst2chk(wxT("%*.*x"));
|
||||
fmtst2chk(wxT("%0*.*x"));
|
||||
|
||||
wxString bad_format = wxT("bad format:\t\"%b\"\n");
|
||||
wxPrintf(bad_format.c_str());
|
||||
wxPrintf(wxT("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
|
||||
|
||||
wxPrintf(wxT("decimal negative:\t\"%d\"\n"), -2345);
|
||||
wxPrintf(wxT("octal negative:\t\"%o\"\n"), -2345);
|
||||
wxPrintf(wxT("hex negative:\t\"%x\"\n"), -2345);
|
||||
wxPrintf(wxT("long decimal number:\t\"%ld\"\n"), -123456L);
|
||||
wxPrintf(wxT("long octal negative:\t\"%lo\"\n"), -2345L);
|
||||
wxPrintf(wxT("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
|
||||
wxPrintf(wxT("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
|
||||
test_format = wxT("left-adjusted ZLDN:\t\"%-010ld\"\n");
|
||||
wxPrintf(test_format.c_str(), -123456);
|
||||
wxPrintf(wxT("space-padded LDN:\t\"%10ld\"\n"), -123456L);
|
||||
wxPrintf(wxT("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
|
||||
|
||||
test_format = wxT("zero-padded string:\t\"%010s\"\n");
|
||||
wxPrintf(test_format.c_str(), shortstr);
|
||||
test_format = wxT("left-adjusted Z string:\t\"%-010s\"\n");
|
||||
wxPrintf(test_format.c_str(), shortstr);
|
||||
wxPrintf(wxT("space-padded string:\t\"%10s\"\n"), shortstr);
|
||||
wxPrintf(wxT("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
|
||||
wxPrintf(wxT("null string:\t\"%s\"\n"), PointerNull);
|
||||
wxPrintf(wxT("limited string:\t\"%.22s\"\n"), longstr);
|
||||
|
||||
wxPrintf(wxT("e-style >= 1:\t\"%e\"\n"), 12.34);
|
||||
wxPrintf(wxT("e-style >= .1:\t\"%e\"\n"), 0.1234);
|
||||
wxPrintf(wxT("e-style < .1:\t\"%e\"\n"), 0.001234);
|
||||
wxPrintf(wxT("e-style big:\t\"%.60e\"\n"), 1e20);
|
||||
wxPrintf(wxT("e-style == .1:\t\"%e\"\n"), 0.1);
|
||||
wxPrintf(wxT("f-style >= 1:\t\"%f\"\n"), 12.34);
|
||||
wxPrintf(wxT("f-style >= .1:\t\"%f\"\n"), 0.1234);
|
||||
wxPrintf(wxT("f-style < .1:\t\"%f\"\n"), 0.001234);
|
||||
wxPrintf(wxT("g-style >= 1:\t\"%g\"\n"), 12.34);
|
||||
wxPrintf(wxT("g-style >= .1:\t\"%g\"\n"), 0.1234);
|
||||
wxPrintf(wxT("g-style < .1:\t\"%g\"\n"), 0.001234);
|
||||
wxPrintf(wxT("g-style big:\t\"%.60g\"\n"), 1e20);
|
||||
|
||||
wxPrintf (wxT(" %6.5f\n"), .099999999860301614);
|
||||
wxPrintf (wxT(" %6.5f\n"), .1);
|
||||
wxPrintf (wxT("x%5.4fx\n"), .5);
|
||||
|
||||
wxPrintf (wxT("%#03x\n"), 1);
|
||||
|
||||
//wxPrintf (wxT("something really insane: %.10000f\n"), 1.0);
|
||||
|
||||
{
|
||||
double d = FLT_MIN;
|
||||
int niter = 17;
|
||||
|
||||
while (niter-- != 0)
|
||||
wxPrintf (wxT("%.17e\n"), d / 2);
|
||||
fflush (stdout);
|
||||
}
|
||||
|
||||
#ifndef __WATCOMC__
|
||||
// Open Watcom cause compiler error here
|
||||
// Error! E173: col(24) floating-point constant too small to represent
|
||||
wxPrintf (wxT("%15.5e\n"), 4.9406564584124654e-324);
|
||||
#endif
|
||||
|
||||
#define FORMAT wxT("|%12.4f|%12.4e|%12.4g|\n")
|
||||
wxPrintf (FORMAT, 0.0, 0.0, 0.0);
|
||||
wxPrintf (FORMAT, 1.0, 1.0, 1.0);
|
||||
wxPrintf (FORMAT, -1.0, -1.0, -1.0);
|
||||
wxPrintf (FORMAT, 100.0, 100.0, 100.0);
|
||||
wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
|
||||
wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
|
||||
wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
|
||||
wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
|
||||
wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
|
||||
#undef FORMAT
|
||||
|
||||
{
|
||||
wxChar buf[20];
|
||||
int rc = wxSnprintf (buf, WXSIZEOF(buf), wxT("%30s"), wxT("foo"));
|
||||
|
||||
wxPrintf(wxT("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
|
||||
rc, WXSIZEOF(buf), buf);
|
||||
#if 0
|
||||
wxChar buf2[512];
|
||||
wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
|
||||
wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
|
||||
#endif
|
||||
}
|
||||
|
||||
fp_test ();
|
||||
|
||||
wxPrintf (wxT("%e should be 1.234568e+06\n"), 1234567.8);
|
||||
wxPrintf (wxT("%f should be 1234567.800000\n"), 1234567.8);
|
||||
wxPrintf (wxT("%g should be 1.23457e+06\n"), 1234567.8);
|
||||
wxPrintf (wxT("%g should be 123.456\n"), 123.456);
|
||||
wxPrintf (wxT("%g should be 1e+06\n"), 1000000.0);
|
||||
wxPrintf (wxT("%g should be 10\n"), 10.0);
|
||||
wxPrintf (wxT("%g should be 0.02\n"), 0.02);
|
||||
|
||||
{
|
||||
double x=1.0;
|
||||
wxPrintf(wxT("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
|
||||
}
|
||||
|
||||
{
|
||||
wxChar buf[200];
|
||||
|
||||
wxSprintf(buf,wxT("%*s%*s%*s"),-1,wxT("one"),-20,wxT("two"),-30,wxT("three"));
|
||||
|
||||
result |= wxStrcmp (buf,
|
||||
wxT("onetwo three "));
|
||||
|
||||
wxPuts (result != 0 ? wxT("Test failed!") : wxT("Test ok."));
|
||||
}
|
||||
|
||||
#ifdef wxLongLong_t
|
||||
{
|
||||
wxChar buf[200];
|
||||
|
||||
wxSprintf(buf, "%07" wxLongLongFmtSpec "o", wxLL(040000000000));
|
||||
#if 0
|
||||
// for some reason below line fails under Borland
|
||||
wxPrintf (wxT("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
|
||||
#endif
|
||||
|
||||
if (wxStrcmp (buf, wxT("40000000000")) != 0)
|
||||
{
|
||||
result = 1;
|
||||
wxPuts (wxT("\tFAILED"));
|
||||
}
|
||||
wxUnusedVar(result);
|
||||
wxPuts (wxEmptyString);
|
||||
}
|
||||
#endif // wxLongLong_t
|
||||
|
||||
wxPrintf (wxT("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
|
||||
wxPrintf (wxT("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
|
||||
|
||||
wxPuts (wxT("--- Should be no further output. ---"));
|
||||
rfg1 ();
|
||||
rfg2 ();
|
||||
|
||||
#if 0
|
||||
{
|
||||
wxChar bytes[7];
|
||||
wxChar buf[20];
|
||||
|
||||
memset (bytes, '\xff', sizeof bytes);
|
||||
wxSprintf (buf, wxT("foo%hhn\n"), &bytes[3]);
|
||||
if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
|
||||
|| bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
|
||||
{
|
||||
wxPuts (wxT("%hhn overwrite more bytes"));
|
||||
result = 1;
|
||||
}
|
||||
if (bytes[3] != 3)
|
||||
{
|
||||
wxPuts (wxT("%hhn wrote incorrect value"));
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
rfg1 (void)
|
||||
{
|
||||
wxChar buf[100];
|
||||
|
||||
wxSprintf (buf, wxT("%5.s"), wxT("xyz"));
|
||||
if (wxStrcmp (buf, wxT(" ")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" "));
|
||||
wxSprintf (buf, wxT("%5.f"), 33.3);
|
||||
if (wxStrcmp (buf, wxT(" 33")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 33"));
|
||||
wxSprintf (buf, wxT("%8.e"), 33.3e7);
|
||||
if (wxStrcmp (buf, wxT(" 3e+08")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 3e+08"));
|
||||
wxSprintf (buf, wxT("%8.E"), 33.3e7);
|
||||
if (wxStrcmp (buf, wxT(" 3E+08")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 3E+08"));
|
||||
wxSprintf (buf, wxT("%.g"), 33.3);
|
||||
if (wxStrcmp (buf, wxT("3e+01")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3e+01"));
|
||||
wxSprintf (buf, wxT("%.G"), 33.3);
|
||||
if (wxStrcmp (buf, wxT("3E+01")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3E+01"));
|
||||
}
|
||||
|
||||
static void
|
||||
rfg2 (void)
|
||||
{
|
||||
int prec;
|
||||
wxChar buf[100];
|
||||
wxString test_format;
|
||||
|
||||
prec = 0;
|
||||
wxSprintf (buf, wxT("%.*g"), prec, 3.3);
|
||||
if (wxStrcmp (buf, wxT("3")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3"));
|
||||
prec = 0;
|
||||
wxSprintf (buf, wxT("%.*G"), prec, 3.3);
|
||||
if (wxStrcmp (buf, wxT("3")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3"));
|
||||
prec = 0;
|
||||
wxSprintf (buf, wxT("%7.*G"), prec, 3.33);
|
||||
if (wxStrcmp (buf, wxT(" 3")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 3"));
|
||||
prec = 3;
|
||||
test_format = wxT("%04.*o");
|
||||
wxSprintf (buf, test_format.c_str(), prec, 33);
|
||||
if (wxStrcmp (buf, wxT(" 041")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 041"));
|
||||
prec = 7;
|
||||
test_format = wxT("%09.*u");
|
||||
wxSprintf (buf, test_format.c_str(), prec, 33);
|
||||
if (wxStrcmp (buf, wxT(" 0000033")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 0000033"));
|
||||
prec = 3;
|
||||
test_format = wxT("%04.*x");
|
||||
wxSprintf (buf, test_format.c_str(), prec, 33);
|
||||
if (wxStrcmp (buf, wxT(" 021")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 021"));
|
||||
prec = 3;
|
||||
test_format = wxT("%04.*X");
|
||||
wxSprintf (buf, test_format.c_str(), prec, 33);
|
||||
if (wxStrcmp (buf, wxT(" 021")) != 0)
|
||||
wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 021"));
|
||||
}
|
||||
|
||||
#endif // TEST_PRINTF
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// FTP
|
||||
// ----------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user