// what to test?
//#define TEST_ARRAYS
+//#define TEST_CMDLINE
//#define TEST_DIR
+//#define TEST_EXECUTE
//#define TEST_LOG
+//#define TEST_LONGLONG
//#define TEST_MIME
-//#define TEST_STRINGS
+#define TEST_STRINGS
//#define TEST_THREADS
-#define TEST_TIME
-//#define TEST_LONGLONG
+//#define TEST_TIME
// ============================================================================
// implementation
// ============================================================================
+#ifdef TEST_CMDLINE
+
+// ----------------------------------------------------------------------------
+// wxCmdLineParser
+// ----------------------------------------------------------------------------
+
+#include <wx/cmdline.h>
+#include <wx/datetime.h>
+
+static void ShowCmdLine(const wxCmdLineParser& parser)
+{
+ wxString s = "Input files: ";
+
+ size_t count = parser.GetParamCount();
+ for ( size_t param = 0; param < count; param++ )
+ {
+ s << parser.GetParam(param) << ' ';
+ }
+
+ s << '\n'
+ << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n'
+ << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n';
+
+ wxString strVal;
+ long lVal;
+ wxDateTime dt;
+ if ( parser.Found("o", &strVal) )
+ s << "Output file:\t" << strVal << '\n';
+ if ( parser.Found("i", &strVal) )
+ s << "Input dir:\t" << strVal << '\n';
+ if ( parser.Found("s", &lVal) )
+ s << "Size:\t" << lVal << '\n';
+ if ( parser.Found("d", &dt) )
+ s << "Date:\t" << dt.FormatISODate() << '\n';
+
+ wxLogMessage(s);
+}
+
+#endif // TEST_CMDLINE
+
// ----------------------------------------------------------------------------
// wxDir
// ----------------------------------------------------------------------------
#endif // TEST_DIR
+// ----------------------------------------------------------------------------
+// wxExecute
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_EXECUTE
+
+#include <wx/utils.h>
+
+static void TestExecute()
+{
+ puts("*** testing wxExecute ***");
+
+#ifdef __UNIX__
+ #define COMMAND "echo hi"
+#elif defined(__WXMSW__)
+ #define COMMAND "command.com -c 'echo hi'"
+#else
+ #error "no command to exec"
+#endif // OS
+
+ if ( wxExecute(COMMAND) == 0 )
+ puts("\nOk.");
+ else
+ puts("\nError.");
+}
+
+#endif // TEST_EXECUTE
+
// ----------------------------------------------------------------------------
// MIME types
// ----------------------------------------------------------------------------
#include <wx/longlong.h>
#include <wx/timer.h>
+// make a 64 bit number from 4 16 bit ones
+#define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
+
+// get a random 64 bit number
+#define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
+
+#if wxUSE_LONGLONG_WX
+inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
+ { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
+inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
+ { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
+#endif // wxUSE_LONGLONG_WX
+
static void TestSpeed()
{
static const long max = 100000000;
printf("Summing longs took %ld milliseconds.\n", sw.Time());
}
+#if wxUSE_LONGLONG_NATIVE
{
wxStopWatch sw;
- __int64 l = 0;
+ wxLongLong_t l = 0;
for ( n = 0; n < max; n++ )
{
l += n;
}
- printf("Summing __int64s took %ld milliseconds.\n", sw.Time());
+ printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time());
}
+#endif // wxUSE_LONGLONG_NATIVE
{
wxStopWatch sw;
}
}
-static void TestDivision()
+static void TestLongLongConversion()
{
- #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
+ puts("*** Testing wxLongLong conversions ***\n");
- // seed pseudo random generator
- //srand((unsigned)time(NULL));
+ wxLongLong a;
+ size_t nTested = 0;
+ for ( size_t n = 0; n < 100000; n++ )
+ {
+ a = RAND_LL();
+
+#if wxUSE_LONGLONG_NATIVE
+ wxLongLongNative b(a.GetHi(), a.GetLo());
+
+ wxASSERT_MSG( a == b, "conversions failure" );
+#else
+ puts("Can't do it without native long long type, test skipped.");
+
+ return;
+#endif // wxUSE_LONGLONG_NATIVE
+
+ if ( !(nTested % 1000) )
+ {
+ putchar('.');
+ fflush(stdout);
+ }
+
+ nTested++;
+ }
+
+ puts(" done!");
+}
+static void TestMultiplication()
+{
+ puts("*** Testing wxLongLong multiplication ***\n");
+
+ wxLongLong a, b;
size_t nTested = 0;
- for ( size_t n = 0; n < 10000; n++ )
+ for ( size_t n = 0; n < 100000; n++ )
+ {
+ a = RAND_LL();
+ b = RAND_LL();
+
+#if wxUSE_LONGLONG_NATIVE
+ wxLongLongNative aa(a.GetHi(), a.GetLo());
+ wxLongLongNative bb(b.GetHi(), b.GetLo());
+
+ wxASSERT_MSG( a*b == aa*bb, "multiplication failure" );
+#else // !wxUSE_LONGLONG_NATIVE
+ puts("Can't do it without native long long type, test skipped.");
+
+ return;
+#endif // wxUSE_LONGLONG_NATIVE
+
+ if ( !(nTested % 1000) )
+ {
+ putchar('.');
+ fflush(stdout);
+ }
+
+ nTested++;
+ }
+
+ puts(" done!");
+}
+
+static void TestDivision()
+{
+ puts("*** Testing wxLongLong division ***\n");
+
+ wxLongLong q, r;
+ size_t nTested = 0;
+ for ( size_t n = 0; n < 100000; n++ )
{
// get a random wxLongLong (shifting by 12 the MSB ensures that the
// multiplication will not overflow)
wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
- wxASSERT( (ll * 1000l)/1000l == ll );
+ // get a random long (not wxLongLong for now) to divide it with
+ long l = rand();
+ q = ll / l;
+ r = ll % l;
+
+#if wxUSE_LONGLONG_NATIVE
+ wxLongLongNative m(ll.GetHi(), ll.GetLo());
+
+ wxLongLongNative p = m / l, s = m % l;
+ wxASSERT_MSG( q == p && r == s, "division failure" );
+#else // !wxUSE_LONGLONG_NATIVE
+ // verify the result
+ wxASSERT_MSG( ll == q*l + r, "division failure" );
+#endif // wxUSE_LONGLONG_NATIVE
+
+ if ( !(nTested % 1000) )
+ {
+ putchar('.');
+ fflush(stdout);
+ }
+
+ nTested++;
+ }
+
+ puts(" done!");
+}
+
+static void TestAddition()
+{
+ puts("*** Testing wxLongLong addition ***\n");
+
+ wxLongLong a, b, c;
+ size_t nTested = 0;
+ for ( size_t n = 0; n < 100000; n++ )
+ {
+ a = RAND_LL();
+ b = RAND_LL();
+ c = a + b;
+
+#if wxUSE_LONGLONG_NATIVE
+ wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
+ wxLongLongNative(b.GetHi(), b.GetLo()),
+ "addition failure" );
+#else // !wxUSE_LONGLONG_NATIVE
+ wxASSERT_MSG( c - b == a, "addition failure" );
+#endif // wxUSE_LONGLONG_NATIVE
+
+ if ( !(nTested % 1000) )
+ {
+ putchar('.');
+ fflush(stdout);
+ }
nTested++;
}
- printf("\n*** Tested %u divisions/multiplications: ok\n", nTested);
+ puts(" done!");
+}
+
+static void TestBitOperations()
+{
+ puts("*** Testing wxLongLong bit operation ***\n");
+
+ wxLongLong a, c;
+ size_t nTested = 0;
+ for ( size_t n = 0; n < 100000; n++ )
+ {
+ a = RAND_LL();
+
+#if wxUSE_LONGLONG_NATIVE
+ for ( size_t n = 0; n < 33; n++ )
+ {
+ wxLongLongNative b(a.GetHi(), a.GetLo());
+
+ b >>= n;
+ c = a >> n;
+
+ wxASSERT_MSG( b == c, "bit shift failure" );
+
+ b = wxLongLongNative(a.GetHi(), a.GetLo()) << n;
+ c = a << n;
+
+ wxASSERT_MSG( b == c, "bit shift failure" );
+ }
+
+#else // !wxUSE_LONGLONG_NATIVE
+ puts("Can't do it without native long long type, test skipped.");
- #undef MAKE_LL
+ return;
+#endif // wxUSE_LONGLONG_NATIVE
+
+ if ( !(nTested % 1000) )
+ {
+ putchar('.');
+ fflush(stdout);
+ }
+
+ nTested++;
+ }
+
+ puts(" done!");
}
+#undef MAKE_LL
+#undef RAND_LL
+
#endif // TEST_LONGLONG
// ----------------------------------------------------------------------------
wxString s;
s.Printf("%02d-%s-%4d%s",
day,
- wxDateTime::GetMonthName(month, TRUE).c_str(),
+ wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
abs(wxDateTime::ConvertYearToBC(year)),
year > 0 ? "AD" : "BC");
return s;
wxDateTime::Month month = wxDateTime::GetCurrentMonth();
printf("Current month is '%s' ('%s') and it has %d days\n",
- wxDateTime::GetMonthName(month, TRUE).c_str(),
+ wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
wxDateTime::GetMonthName(month).c_str(),
wxDateTime::GetNumberOfDays(month));
printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
+
+ wxDateTime::Tm tm = now.GetTm();
+ if ( wxDateTime(tm) != now )
+ {
+ printf("ERROR: got %s instead of %s\n",
+ wxDateTime(tm).Format().c_str(), now.Format().c_str());
+ }
}
// test some minimal support for the dates outside the standard range
struct WeekNumberTestData
{
Date date; // the date
- wxDateTime::wxDateTime_t week; // the week number
+ wxDateTime::wxDateTime_t week; // the week number in the year
+ wxDateTime::wxDateTime_t wmon; // the week number in the month
+ wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun
wxDateTime::wxDateTime_t dnum; // day number in the year
};
monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
+def GetMonthWeek(dt):
+ weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1
+ if weekNumMonth < 0:
+ weekNumMonth = weekNumMonth + 53
+ return weekNumMonth
+
+def GetLastSundayBefore(dt):
+ if dt.iso_week[2] == 7:
+ return dt
+ else:
+ return dt - DateTimeDelta(dt.iso_week[2])
+
for n in range(20):
year = randint(1900, 2100)
month = randint(1, 12)
dt = DateTime(year, month, day)
dayNum = dt.day_of_year
weekNum = dt.iso_week[1]
-
- data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'dayNum': rjust(`dayNum`, 3) }
-
- print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)s, "\
+ weekNumMonth = GetMonthWeek(dt)
+
+ weekNumMonth2 = 0
+ dtSunday = GetLastSundayBefore(dt)
+
+ while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)):
+ weekNumMonth2 = weekNumMonth2 + 1
+ dtSunday = dtSunday - DateTimeDelta(7)
+
+ data = { 'day': rjust(`day`, 2), \
+ 'month': monthNames[month - 1], \
+ 'year': year, \
+ 'weekNum': rjust(`weekNum`, 2), \
+ 'weekNumMonth': weekNumMonth, \
+ 'weekNumMonth2': weekNumMonth2, \
+ 'dayNum': rjust(`dayNum`, 3) }
+
+ print " { { %(day)s, "\
+ "wxDateTime::%(month)s, "\
+ "%(year)d }, "\
+ "%(weekNum)s, "\
+ "%(weekNumMonth)s, "\
+ "%(weekNumMonth2)s, "\
"%(dayNum)s }," % data
+
*/
static const WeekNumberTestData weekNumberTestDates[] =
{
- { { 2, wxDateTime::Jul, 2093 }, 27, 183 },
- { { 25, wxDateTime::Jun, 1986 }, 26, 176 },
- { { 15, wxDateTime::Jun, 2014 }, 24, 166 },
- { { 20, wxDateTime::Jul, 2018 }, 29, 201 },
- { { 3, wxDateTime::Aug, 2074 }, 31, 215 },
- { { 26, wxDateTime::Jul, 2012 }, 30, 208 },
- { { 4, wxDateTime::Nov, 1915 }, 44, 308 },
- { { 11, wxDateTime::Feb, 2035 }, 6, 42 },
- { { 15, wxDateTime::Feb, 1942 }, 7, 46 },
- { { 5, wxDateTime::Jan, 2087 }, 1, 5 },
- { { 6, wxDateTime::Nov, 2016 }, 44, 311 },
- { { 6, wxDateTime::Jun, 2057 }, 23, 157 },
- { { 25, wxDateTime::Feb, 1976 }, 9, 56 },
- { { 12, wxDateTime::Jan, 2073 }, 2, 12 },
- { { 12, wxDateTime::Sep, 2040 }, 37, 256 },
- { { 15, wxDateTime::Jul, 1931 }, 29, 196 },
- { { 23, wxDateTime::Mar, 2084 }, 12, 83 },
- { { 12, wxDateTime::Dec, 1970 }, 50, 346 },
- { { 6, wxDateTime::Sep, 1996 }, 36, 250 },
- { { 7, wxDateTime::Jan, 2076 }, 2, 7 },
+ { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 },
+ { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 },
+ { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 },
+ { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 },
+ { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 },
+ { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 },
+ { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 },
+ { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 },
+ { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 },
+ { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 },
+ { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 },
+ { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 },
+ { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 },
+ { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 },
+ { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 },
+ { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 },
+ { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 },
+ { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 },
+ { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 },
+ { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 },
};
for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )
wxDateTime dt = d.DT();
- wxDateTime::wxDateTime_t week = dt.GetWeekOfYear(),
- dnum = dt.GetDayOfYear();
+ wxDateTime::wxDateTime_t
+ week = dt.GetWeekOfYear(wxDateTime::Monday_First),
+ wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First),
+ wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First),
+ dnum = dt.GetDayOfYear();
printf("%s: the day number is %d",
d.FormatDate().c_str(), dnum);
printf(" (ERROR: should be %d)", wn.dnum);
}
- printf(", week number is %d", week);
+ printf(", week in month is %d", wmon);
+ if ( wmon == wn.wmon )
+ {
+ printf(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %d)", wn.wmon);
+ }
+
+ printf(" or %d", wmon2);
+ if ( wmon2 == wn.wmon2 )
+ {
+ printf(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %d)", wn.wmon2);
+ }
+
+ printf(", week in year is %d", week);
if ( week == wn.week )
{
puts(" (ok)");
}
}
+// test wxDateTime -> text conversion
+static void TestTimeFormat()
+{
+ puts("\n*** wxDateTime formatting test ***");
+
+ // some information may be lost during conversion, so store what kind
+ // of info should we recover after a round trip
+ enum CompareKind
+ {
+ CompareNone, // don't try comparing
+ CompareBoth, // dates and times should be identical
+ CompareDate, // dates only
+ CompareTime // time only
+ };
+
+ 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[] =
+ {
+ { 29, wxDateTime::May, 1976, 18, 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::Feb, 2400, 02, 15, 25 },
+ { 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)
+ printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str());
+
+ for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
+ {
+ puts("");
+
+ wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT();
+ for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ )
+ {
+ wxString s = dt.Format(formatTestFormats[n].format);
+ printf("%s", s.c_str());
+
+ // what can we recover?
+ int kind = formatTestFormats[n].compareKind;
+
+ // convert back
+ wxDateTime dt2;
+ const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format);
+ if ( !result )
+ {
+ // converion failed - should it have?
+ if ( kind == CompareNone )
+ puts(" (ok)");
+ else
+ puts(" (ERROR: conversion back failed)");
+ }
+ else if ( *result )
+ {
+ // should have parsed the entire string
+ puts(" (ERROR: conversion back stopped too soon)");
+ }
+ else
+ {
+ 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)");
+ }
+ }
+ }
+ }
+}
+
// test text -> wxDateTime conversion
static void TestTimeParse()
{
static const ParseTestData parseTestDates[] =
{
- "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE,
+ { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE },
+ { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE },
};
for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
}
}
+static void TestInteractive()
+{
+ puts("\n*** interactive wxDateTime tests ***");
+
+ char buf[128];
+
+ for ( ;; )
+ {
+ printf("Enter a date: ");
+ if ( !fgets(buf, WXSIZEOF(buf), stdin) )
+ break;
+
+ wxDateTime dt;
+ if ( !dt.ParseDate(buf) )
+ {
+ puts("failed to parse the date");
+
+ continue;
+ }
+
+ printf("%s: day %u, week of month %u/%u, week of year %u\n",
+ dt.FormatISODate().c_str(),
+ dt.GetDayOfYear(),
+ dt.GetWeekOfMonth(wxDateTime::Monday_First),
+ dt.GetWeekOfMonth(wxDateTime::Sunday_First),
+ dt.GetWeekOfYear(wxDateTime::Monday_First));
+ }
+
+ puts("\n*** done ***");
+}
+
+static void TestTimeArithmetics()
+{
+ puts("\n*** testing arithmetic operations on wxDateTime ***");
+
+ static const struct
+ {
+ wxDateSpan span;
+ const char *name;
+ } testArithmData[] =
+ {
+ { wxDateSpan::Day(), "day" },
+ { wxDateSpan::Week(), "week" },
+ { wxDateSpan::Month(), "month" },
+ { wxDateSpan::Year(), "year" },
+ { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" },
+ };
+
+ wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2;
+
+ for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ )
+ {
+ wxDateSpan span = testArithmData[n].span;
+ dt1 = dt + span;
+ dt2 = dt - span;
+
+ const char *name = testArithmData[n].name;
+ printf("%s + %s = %s, %s - %s = %s\n",
+ dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(),
+ dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str());
+
+ printf("Going back: %s", (dt1 - span).FormatISODate().c_str());
+ if ( dt1 - span == dt )
+ {
+ puts(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
+ }
+
+ printf("Going forward: %s", (dt2 + span).FormatISODate().c_str());
+ if ( dt2 + span == dt )
+ {
+ puts(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str());
+ }
+
+ printf("Double increment: %s", (dt2 + 2*span).FormatISODate().c_str());
+ if ( dt2 + 2*span == dt1 )
+ {
+ puts(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %s)\n", dt2.FormatISODate().c_str());
+ }
+
+ puts("");
+ }
+}
+
+static void TestTimeHolidays()
+{
+ puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
+
+ wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
+ wxDateTime dtStart(1, tm.mon, tm.year),
+ dtEnd = dtStart.GetLastMonthDay();
+
+ wxDateTimeArray hol;
+ wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
+
+ const wxChar *format = "%d-%b-%Y (%a)";
+
+ printf("All holidays between %s and %s:\n",
+ dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
+
+ size_t count = hol.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ printf("\t%s\n", hol[n].Format(format).c_str());
+ }
+
+ puts("");
+}
+
+#if 0
+
// test compatibility with the old wxDate/wxTime classes
static void TestTimeCompatibility()
{
}
}
+#endif // 0
+
#endif // TEST_TIME
// ----------------------------------------------------------------------------
#ifdef TEST_STRINGS
#include "wx/timer.h"
+#include "wx/tokenzr.h"
static void TestString()
{
puts("");
}
+static void TestStringFormat()
+{
+ puts("*** Testing wxString formatting ***");
+
+ wxString s;
+ s.Printf("%03d", 18);
+
+ printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str());
+ printf("Number 18: %s\n", s.c_str());
+
+ puts("");
+}
+
+// returns "not found" for npos, value for all others
+static wxString PosToString(size_t res)
+{
+ wxString s = res == wxString::npos ? wxString(_T("not found"))
+ : wxString::Format(_T("%u"), res);
+ return s;
+}
+
+static void TestStringFind()
+{
+ puts("*** Testing wxString find() functions ***");
+
+ static const wxChar *strToFind = _T("ell");
+ static const struct StringFindTest
+ {
+ const wxChar *str;
+ size_t start,
+ result; // of searching "ell" in str
+ } findTestData[] =
+ {
+ { _T("Well, hello world"), 0, 1 },
+ { _T("Well, hello world"), 6, 7 },
+ { _T("Well, hello world"), 9, wxString::npos },
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ )
+ {
+ const StringFindTest& ft = findTestData[n];
+ size_t res = wxString(ft.str).find(strToFind, ft.start);
+
+ printf(_T("Index of '%s' in '%s' starting from %u is %s "),
+ strToFind, ft.str, ft.start, PosToString(res).c_str());
+
+ size_t resTrue = ft.result;
+ if ( res == resTrue )
+ {
+ puts(_T("(ok)"));
+ }
+ else
+ {
+ printf(_T("(ERROR: should be %s)\n"),
+ PosToString(resTrue).c_str());
+ }
+ }
+
+ puts("");
+}
+
+// replace TABs with \t and CRs with \n
+static wxString MakePrintable(const wxChar *s)
+{
+ wxString str(s);
+ (void)str.Replace(_T("\t"), _T("\\t"));
+ (void)str.Replace(_T("\n"), _T("\\n"));
+ (void)str.Replace(_T("\r"), _T("\\r"));
+
+ return str;
+}
+
+static void TestStringTokenizer()
+{
+ puts("*** Testing wxStringTokenizer ***");
+
+ static const struct StringTokenizerTest
+ {
+ const wxChar *str; // string to tokenize
+ const wxChar *delims; // delimiters to use
+ size_t count; // count of token
+ bool with; // return tokens with delimiters?
+ } tokenizerTestData[] =
+ {
+ { _T(""), _T(" "), 0, FALSE },
+ { _T("Hello, world"), _T(" "), 2, FALSE },
+ { _T("Hello, world"), _T(","), 2, FALSE },
+ { _T("Hello, world!"), _T(",!"), 3, TRUE },
+ { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7, FALSE },
+ { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, TRUE },
+ { _T("01/02/99"), _T("/-"), 3, FALSE },
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ )
+ {
+ const StringTokenizerTest& tt = tokenizerTestData[n];
+ wxStringTokenizer tkz(tt.str, tt.delims, tt.with);
+
+ size_t count = tkz.CountTokens();
+ printf(_T("String '%s' has %u tokens delimited by '%s' "),
+ tt.str,
+ count,
+ MakePrintable(tt.delims).c_str());
+ if ( count == tt.count )
+ {
+ puts(_T("(ok)"));
+ }
+ else
+ {
+ printf(_T("(ERROR: should be %u)\n"), tt.count);
+
+ continue;
+ }
+
+ // now show the tokens themselves
+ size_t count2 = 0;
+ while ( tkz.HasMoreTokens() )
+ {
+ printf(_T("\ttoken %u: '%s'\n"),
+ ++count2,
+ MakePrintable(tkz.GetNextToken()).c_str());
+ }
+
+ if ( count2 != count )
+ {
+ puts(_T("ERROR: token count mismatch"));
+ }
+ }
+
+ puts("");
+}
+
#endif // TEST_STRINGS
// ----------------------------------------------------------------------------
fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
}
+#ifdef TEST_USLEEP
+ puts("Sleeping for 3 seconds... z-z-z-z-z...");
+ wxUsleep(3000);
+#endif // TEST_USLEEP
+
+#ifdef TEST_CMDLINE
+ static const wxCmdLineEntryDesc cmdLineDesc[] =
+ {
+ { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
+ { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
+
+ { wxCMD_LINE_OPTION, "o", "output", "output file" },
+ { wxCMD_LINE_OPTION, "i", "input", "input dir" },
+ { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
+ { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
+
+ { wxCMD_LINE_PARAM, NULL, NULL, "input file",
+ wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
+
+ { wxCMD_LINE_NONE }
+ };
+
+ wxCmdLineParser parser(cmdLineDesc, argc, argv);
+
+ switch ( parser.Parse() )
+ {
+ case -1:
+ wxLogMessage("Help was given, terminating.");
+ break;
+
+ case 0:
+ ShowCmdLine(parser);
+ break;
+
+ default:
+ wxLogMessage("Syntax error detected, aborting.");
+ break;
+ }
+#endif // TEST_CMDLINE
+
#ifdef TEST_STRINGS
if ( 0 )
{
TestPChar();
TestString();
}
- TestStringSub();
+ if ( 0 )
+ {
+ TestStringSub();
+ TestStringFormat();
+ TestStringFind();
+ }
+ TestStringTokenizer();
#endif // TEST_STRINGS
#ifdef TEST_ARRAYS
TestDirEnum();
#endif // TEST_DIR
+#ifdef TEST_EXECUTE
+ TestExecute();
+#endif // TEST_EXECUTE
+
#ifdef TEST_LOG
wxString s;
for ( size_t n = 0; n < 8000; n++ )
#endif // TEST_THREADS
#ifdef TEST_LONGLONG
+ // seed pseudo random generator
+ srand((unsigned)time(NULL));
+
if ( 0 )
+ {
TestSpeed();
- if ( 1 )
+ }
+ TestMultiplication();
+ if ( 0 )
+ {
TestDivision();
+ TestAddition();
+ TestLongLongConversion();
+ TestBitOperations();
+ }
#endif // TEST_LONGLONG
#ifdef TEST_MIME
#ifdef TEST_TIME
if ( 0 )
{
- TestTimeSet();
- TestTimeStatic();
- TestTimeZones();
- TestTimeRange();
- TestTimeTicks();
- TestTimeJDN();
- TestTimeDST();
- TestTimeWDays();
- TestTimeWNumber();
- TestTimeParse();
+ TestTimeSet();
+ TestTimeStatic();
+ TestTimeRange();
+ TestTimeZones();
+ TestTimeTicks();
+ TestTimeJDN();
+ TestTimeDST();
+ TestTimeWDays();
+ TestTimeWNumber();
+ TestTimeParse();
+ TestTimeFormat();
+ TestTimeArithmetics();
}
- TestTimeCompatibility();
+ TestTimeHolidays();
+ if ( 0 )
+ TestInteractive();
#endif // TEST_TIME
wxUninitialize();