more wxDateTime tests

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5062 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-12-22 00:16:35 +00:00
parent be4017f89b
commit b38e2f7dc3

View File

@@ -820,58 +820,101 @@ static void TestTimeFormat()
{ {
puts("\n*** wxDateTime formatting test ***"); puts("\n*** wxDateTime formatting test ***");
static const char *formatTestFormats[] = // some information may be lost during conversion, so store what kind
// of info should we recover after a round trip
enum CompareKind
{ {
"---> %c", CompareNone, // don't try comparing
"Date is %A, %d of %B, in year %Y", CompareBoth, // dates and times should be identical
"Date is %x, time is %X", CompareDate, // dates only
"Time is %H:%M:%S or %I:%M:%S %p", CompareTime // time only
"The day of year: %j, the week of year: %W", };
static const struct
{
CompareKind compareKind;
const char *format;
} formatTestFormats[] =
{
{ CompareBoth, "---> %c" },
{ CompareDate, "Date is %A, %d of %B, in year %Y" },
{ CompareBoth, "Date is %x, time is %X" },
{ CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
{ CompareNone, "The day of year: %j, the week of year: %W" },
}; };
static const Date formatTestDates[] = static const Date formatTestDates[] =
{ {
{ }, // unused
{ 29, wxDateTime::May, 1976, 18, 30, 00 }, { 29, wxDateTime::May, 1976, 18, 30, 00 },
{ 31, wxDateTime::Dec, 1999, 23, 30, 00 }, { 31, wxDateTime::Dec, 1999, 23, 30, 00 },
#if 0
// this test can't work for other centuries because it uses two digit
// years in formats, so don't even try it
{ 29, wxDateTime::May, 2076, 18, 30, 00 }, { 29, wxDateTime::May, 2076, 18, 30, 00 },
{ 29, wxDateTime::Feb, 2400, 02, 15, 25 }, { 29, wxDateTime::Feb, 2400, 02, 15, 25 },
{ 01, wxDateTime::Jan, -52, 03, 16, 47 }, { 01, wxDateTime::Jan, -52, 03, 16, 47 },
#endif
}; };
// an extra test (as it doesn't depend on date, don't do it in the loop) // an extra test (as it doesn't depend on date, don't do it in the loop)
printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str()); printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
for ( size_t d = 0; d < WXSIZEOF(formatTestDates); d++ ) for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
{ {
puts(""); puts("");
wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d].DT(); wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ ) for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
{ {
wxString s = dt.Format(formatTestFormats[n]); wxString s = dt.Format(formatTestFormats[n].format);
printf("%s", s.c_str()); printf("%s", s.c_str());
// what can we recover?
int kind = formatTestFormats[n].compareKind;
// convert back // convert back
wxDateTime dt2; wxDateTime dt2;
const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n]); const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
if ( !result ) if ( !result )
{ {
puts(" (ERROR: conversion back failed)"); // converion failed - should it have?
if ( kind == CompareNone )
puts(" (ok)");
else
puts(" (ERROR: conversion back failed)");
} }
else if ( *result ) else if ( *result )
{ {
// should have parsed the entire string // should have parsed the entire string
puts(" (ERROR: conversion back stopped too soon)"); puts(" (ERROR: conversion back stopped too soon)");
} }
else if ( dt2 != dt )
{
printf(" (ERROR: got back '%s' instead of '%s')\n",
dt2.Format().c_str(), dt.Format().c_str());
}
else else
{ {
puts(" (ok)"); bool equal = FALSE; // suppress compilaer warning
switch ( kind )
{
case CompareBoth:
equal = dt2 == dt;
break;
case CompareDate:
equal = dt.IsSameDate(dt2);
break;
case CompareTime:
equal = dt.IsSameTime(dt2);
break;
}
if ( !equal )
{
printf(" (ERROR: got back '%s' instead of '%s')\n",
dt2.Format().c_str(), dt.Format().c_str());
}
else
{
puts(" (ok)");
}
} }
} }
} }