- wxPutchar(_T('\n'));
-
- wxDateTime dt2(1, wxDateTime::Jan, d.year);
- dt2.SetToTheWeek(wn.week, dt.GetWeekDay());
- if ( dt2 != dt )
- {
- Date d2;
- d2.Init(dt2.GetTm());
- wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
- d2.FormatDate().c_str());
- }
- }
-}
-
-// test DST calculations
-static void TestTimeDST()
-{
- wxPuts(_T("\n*** wxDateTime DST test ***"));
-
- wxPrintf(_T("DST is%s in effect now.\n\n"),
- wxDateTime::Now().IsDST() ? _T("") : _T(" not"));
-
- // taken from http://www.energy.ca.gov/daylightsaving.html
- static const Date datesDST[2][2004 - 1900 + 1] =
- {
- {
- { 1, wxDateTime::Apr, 1990 },
- { 7, wxDateTime::Apr, 1991 },
- { 5, wxDateTime::Apr, 1992 },
- { 4, wxDateTime::Apr, 1993 },
- { 3, wxDateTime::Apr, 1994 },
- { 2, wxDateTime::Apr, 1995 },
- { 7, wxDateTime::Apr, 1996 },
- { 6, wxDateTime::Apr, 1997 },
- { 5, wxDateTime::Apr, 1998 },
- { 4, wxDateTime::Apr, 1999 },
- { 2, wxDateTime::Apr, 2000 },
- { 1, wxDateTime::Apr, 2001 },
- { 7, wxDateTime::Apr, 2002 },
- { 6, wxDateTime::Apr, 2003 },
- { 4, wxDateTime::Apr, 2004 },
- },
- {
- { 28, wxDateTime::Oct, 1990 },
- { 27, wxDateTime::Oct, 1991 },
- { 25, wxDateTime::Oct, 1992 },
- { 31, wxDateTime::Oct, 1993 },
- { 30, wxDateTime::Oct, 1994 },
- { 29, wxDateTime::Oct, 1995 },
- { 27, wxDateTime::Oct, 1996 },
- { 26, wxDateTime::Oct, 1997 },
- { 25, wxDateTime::Oct, 1998 },
- { 31, wxDateTime::Oct, 1999 },
- { 29, wxDateTime::Oct, 2000 },
- { 28, wxDateTime::Oct, 2001 },
- { 27, wxDateTime::Oct, 2002 },
- { 26, wxDateTime::Oct, 2003 },
- { 31, wxDateTime::Oct, 2004 },
- }
- };
-
- int year;
- for ( year = 1990; year < 2005; year++ )
- {
- wxDateTime dtBegin = wxDateTime::GetBeginDST(year, wxDateTime::USA),
- dtEnd = wxDateTime::GetEndDST(year, wxDateTime::USA);
-
- wxPrintf(_T("DST period in the US for year %d: from %s to %s"),
- year, dtBegin.Format().c_str(), dtEnd.Format().c_str());
-
- size_t n = year - 1990;
- const Date& dBegin = datesDST[0][n];
- const Date& dEnd = datesDST[1][n];
-
- if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) )
- {
- wxPuts(_T(" (ok)"));
- }
- else
- {
- wxPrintf(_T(" (ERROR: should be %s %d to %s %d)\n"),
- wxDateTime::GetMonthName(dBegin.month).c_str(), dBegin.day,
- wxDateTime::GetMonthName(dEnd.month).c_str(), dEnd.day);
- }
- }
-
- wxPuts(_T(""));
-
- for ( year = 1990; year < 2005; year++ )
- {
- wxPrintf(_T("DST period in Europe for year %d: from %s to %s\n"),
- year,
- wxDateTime::GetBeginDST(year, wxDateTime::Country_EEC).Format().c_str(),
- wxDateTime::GetEndDST(year, wxDateTime::Country_EEC).Format().c_str());
- }
-}
-
-// test wxDateTime -> text conversion
-static void TestTimeFormat()
-{
- wxPuts(_T("\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 wxChar *format;
- } formatTestFormats[] =
- {
- { CompareBoth, _T("---> %c") },
- { CompareDate, _T("Date is %A, %d of %B, in year %Y") },
- { CompareBoth, _T("Date is %x, time is %X") },
- { CompareTime, _T("Time is %H:%M:%S or %I:%M:%S %p") },
- { CompareNone, _T("The day of year: %j, the week of year: %W") },
- { CompareDate, _T("ISO date without separators: %Y%m%d") },
- };
-
- 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)
- wxPrintf(_T("%s\n"), wxDateTime::Now().Format(_T("Our timezone is %Z")).c_str());
-
- for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ )
- {
- wxPuts(_T(""));
-
- 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);
- wxPrintf(_T("%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 )
- wxPuts(_T(" (ok)"));
- else
- wxPuts(_T(" (ERROR: conversion back failed)"));
- }
- else if ( *result )
- {
- // should have parsed the entire string
- wxPuts(_T(" (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 )
- {
- wxPrintf(_T(" (ERROR: got back '%s' instead of '%s')\n"),
- dt2.Format().c_str(), dt.Format().c_str());
- }
- else
- {
- wxPuts(_T(" (ok)"));
- }
- }
- }
- }
-}
-
-// test text -> wxDateTime conversion
-static void TestTimeParse()
-{
- wxPuts(_T("\n*** wxDateTime parse test ***"));
-
- struct ParseTestData
- {
- const wxChar *format;
- Date date;
- bool good;
- };
-
- static const ParseTestData parseTestDates[] =
- {
- { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, true },
- { _T("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++ )
- {
- const wxChar *format = parseTestDates[n].format;
-
- wxPrintf(_T("%s => "), format);
-
- wxDateTime dt;
- if ( dt.ParseRfc822Date(format) )
- {
- wxPrintf(_T("%s "), dt.Format().c_str());
-
- if ( parseTestDates[n].good )
- {
- wxDateTime dtReal = parseTestDates[n].date.DT();
- if ( dt == dtReal )
- {
- wxPuts(_T("(ok)"));
- }
- else
- {
- wxPrintf(_T("(ERROR: should be %s)\n"), dtReal.Format().c_str());
- }
- }
- else
- {
- wxPuts(_T("(ERROR: bad format)"));
- }
- }
- else
- {
- wxPrintf(_T("bad format (%s)\n"),
- parseTestDates[n].good ? "ERROR" : "ok");
- }
- }
-}
-
-static void TestDateTimeInteractive()
-{
- wxPuts(_T("\n*** interactive wxDateTime tests ***"));
-
- wxChar buf[128];
-
- for ( ;; )
- {
- wxPrintf(_T("Enter a date: "));
- if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
- break;
-
- // kill the last '\n'
- buf[wxStrlen(buf) - 1] = 0;
-
- wxDateTime dt;
- const wxChar *p = dt.ParseDate(buf);
- if ( !p )
- {
- wxPrintf(_T("ERROR: failed to parse the date '%s'.\n"), buf);
-
- continue;
- }
- else if ( *p )
- {
- wxPrintf(_T("WARNING: parsed only first %u characters.\n"), p - buf);
- }
-
- wxPrintf(_T("%s: day %u, week of month %u/%u, week of year %u\n"),
- dt.Format(_T("%b %d, %Y")).c_str(),
- dt.GetDayOfYear(),
- dt.GetWeekOfMonth(wxDateTime::Monday_First),
- dt.GetWeekOfMonth(wxDateTime::Sunday_First),
- dt.GetWeekOfYear(wxDateTime::Monday_First));
- }
-
- wxPuts(_T("\n*** done ***"));
-}
-
-static void TestTimeMS()
-{
- wxPuts(_T("*** testing millisecond-resolution support in wxDateTime ***"));
-
- wxDateTime dt1 = wxDateTime::Now(),
- dt2 = wxDateTime::UNow();
-
- wxPrintf(_T("Now = %s\n"), dt1.Format(_T("%H:%M:%S:%l")).c_str());
- wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
- wxPrintf(_T("Dummy loop: "));
- for ( int i = 0; i < 6000; i++ )
- {
- //for ( int j = 0; j < 10; j++ )
- {
- wxString s;
- s.Printf(_T("%g"), sqrt(i));
- }
-
- if ( !(i % 100) )
- putchar('.');
- }
- wxPuts(_T(", done"));
-
- dt1 = dt2;
- dt2 = wxDateTime::UNow();
- wxPrintf(_T("UNow = %s\n"), dt2.Format(_T("%H:%M:%S:%l")).c_str());
-
- wxPrintf(_T("Loop executed in %s ms\n"), (dt2 - dt1).Format(_T("%l")).c_str());
-
- wxPuts(_T("\n*** done ***"));
-}
-
-static void TestTimeArithmetics()
-{
- wxPuts(_T("\n*** testing arithmetic operations on wxDateTime ***"));
-
- static const struct ArithmData
- {
- ArithmData(const wxDateSpan& sp, const wxChar *nam)
- : span(sp), name(nam) { }
-
- wxDateSpan span;
- const wxChar *name;
- } testArithmData[] =
- {
- ArithmData(wxDateSpan::Day(), _T("day")),
- ArithmData(wxDateSpan::Week(), _T("week")),
- ArithmData(wxDateSpan::Month(), _T("month")),
- ArithmData(wxDateSpan::Year(), _T("year")),
- ArithmData(wxDateSpan(1, 2, 3, 4), _T("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 wxChar *name = testArithmData[n].name;
- wxPrintf(_T("%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());
-
- wxPrintf(_T("Going back: %s"), (dt1 - span).FormatISODate().c_str());
- if ( dt1 - span == dt )
- {
- wxPuts(_T(" (ok)"));
- }
- else
- {
- wxPrintf(_T(" (ERROR: should be %s)\n"), dt.FormatISODate().c_str());
- }
-
- wxPrintf(_T("Going forward: %s"), (dt2 + span).FormatISODate().c_str());
- if ( dt2 + span == dt )
- {
- wxPuts(_T(" (ok)"));
- }
- else
- {
- wxPrintf(_T(" (ERROR: should be %s)\n"), dt.FormatISODate().c_str());
- }
-
- wxPrintf(_T("Double increment: %s"), (dt2 + 2*span).FormatISODate().c_str());
- if ( dt2 + 2*span == dt1 )
- {
- wxPuts(_T(" (ok)"));
- }
- else
- {
- wxPrintf(_T(" (ERROR: should be %s)\n"), dt2.FormatISODate().c_str());
- }
-
- wxPuts(_T(""));
- }
-}
-
-static void TestTimeHolidays()
-{
- wxPuts(_T("\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 = _T("%d-%b-%Y (%a)");
-
- wxPrintf(_T("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++ )
- {
- wxPrintf(_T("\t%s\n"), hol[n].Format(format).c_str());
- }
-
- wxPuts(_T(""));
-}
-
-static void TestTimeZoneBug()
-{
- wxPuts(_T("\n*** testing for DST/timezone bug ***\n"));
-
- wxDateTime date = wxDateTime(1, wxDateTime::Mar, 2000);
- for ( int i = 0; i < 31; i++ )
- {
- wxPrintf(_T("Date %s: week day %s.\n"),
- date.Format(_T("%d-%m-%Y")).c_str(),
- date.GetWeekDayName(date.GetWeekDay()).c_str());
-
- date += wxDateSpan::Day();
- }
-
- wxPuts(_T(""));
-}
-
-static void TestTimeSpanFormat()
-{
- wxPuts(_T("\n*** wxTimeSpan tests ***"));
-
- static const wxChar *formats[] =
- {
- _T("(default) %H:%M:%S"),
- _T("%E weeks and %D days"),
- _T("%l milliseconds"),
- _T("(with ms) %H:%M:%S:%l"),
- _T("100%% of minutes is %M"), // test "%%"
- _T("%D days and %H hours"),
- _T("or also %S seconds"),
- };
-
- wxTimeSpan ts1(1, 2, 3, 4),
- ts2(111, 222, 333);
- for ( size_t n = 0; n < WXSIZEOF(formats); n++ )
- {
- wxPrintf(_T("ts1 = %s\tts2 = %s\n"),
- ts1.Format(formats[n]).c_str(),
- ts2.Format(formats[n]).c_str());
- }
-
- wxPuts(_T(""));
-}
-
-#endif // TEST_DATETIME
-
-// ----------------------------------------------------------------------------
-// wxTextInput/OutputStream
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_TEXTSTREAM
-
-#include "wx/txtstrm.h"
-#include "wx/wfstream.h"
-
-static void TestTextInputStream()
-{
- wxPuts(_T("\n*** wxTextInputStream test ***"));
-
- wxFileInputStream fsIn(_T("testdata.fc"));
- if ( !fsIn.Ok() )
- {
- wxPuts(_T("ERROR: couldn't open file."));
- }
- else
- {
- wxTextInputStream tis(fsIn);
-
- size_t line = 1;
- for ( ;; )
- {
- const wxString s = tis.ReadLine();
-
- // line could be non empty if the last line of the file isn't
- // terminated with EOL
- if ( fsIn.Eof() && s.empty() )
- break;
-
- wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
- }
- }
-}
-
-#endif // TEST_TEXTSTREAM
-
-// ----------------------------------------------------------------------------
-// threads
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_THREADS
-
-#include "wx/thread.h"
-
-static size_t gs_counter = (size_t)-1;
-static wxCriticalSection gs_critsect;
-static wxSemaphore gs_cond;
-
-class MyJoinableThread : public wxThread
-{
-public:
- MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
- { m_n = n; Create(); }
-
- // thread execution starts here
- virtual ExitCode Entry();
-
-private:
- size_t m_n;
-};
-
-wxThread::ExitCode MyJoinableThread::Entry()
-{
- unsigned long res = 1;
- for ( size_t n = 1; n < m_n; n++ )
- {
- res *= n;
-
- // it's a loooong calculation :-)
- Sleep(100);
- }
-
- return (ExitCode)res;
-}
-
-class MyDetachedThread : public wxThread
-{
-public:
- MyDetachedThread(size_t n, wxChar ch)
- {
- m_n = n;
- m_ch = ch;
- m_cancelled = false;
-
- Create();
- }
-
- // thread execution starts here
- virtual ExitCode Entry();
-
- // and stops here
- virtual void OnExit();
-
-private:
- size_t m_n; // number of characters to write
- wxChar m_ch; // character to write
-
- bool m_cancelled; // false if we exit normally
-};
-
-wxThread::ExitCode MyDetachedThread::Entry()
-{
- {
- wxCriticalSectionLocker lock(gs_critsect);
- if ( gs_counter == (size_t)-1 )
- gs_counter = 1;
- else
- gs_counter++;
- }
-
- for ( size_t n = 0; n < m_n; n++ )
- {
- if ( TestDestroy() )
- {
- m_cancelled = true;
-
- break;
- }
-
- putchar(m_ch);
- fflush(stdout);
-
- wxThread::Sleep(100);
- }
-
- return 0;
-}
-
-void MyDetachedThread::OnExit()
-{
- wxLogTrace(_T("thread"), _T("Thread %ld is in OnExit"), GetId());
-
- wxCriticalSectionLocker lock(gs_critsect);
- if ( !--gs_counter && !m_cancelled )
- gs_cond.Post();
-}
-
-static void TestDetachedThreads()
-{
- wxPuts(_T("\n*** Testing detached threads ***"));
-
- static const size_t nThreads = 3;
- MyDetachedThread *threads[nThreads];
- size_t n;
- for ( n = 0; n < nThreads; n++ )
- {
- threads[n] = new MyDetachedThread(10, 'A' + n);
- }
-
- threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
- threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
-
- for ( n = 0; n < nThreads; n++ )
- {
- threads[n]->Run();
- }
-
- // wait until all threads terminate
- gs_cond.Wait();
-
- wxPuts(_T(""));
-}
-
-static void TestJoinableThreads()
-{
- wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***"));
-
- // calc 10! in the background
- MyJoinableThread thread(10);
- thread.Run();
-
- wxPrintf(_T("\nThread terminated with exit code %lu.\n"),
- (unsigned long)thread.Wait());
-}
-
-static void TestThreadSuspend()
-{
- wxPuts(_T("\n*** Testing thread suspend/resume functions ***"));
-
- MyDetachedThread *thread = new MyDetachedThread(15, 'X');
-
- thread->Run();
-
- // this is for this demo only, in a real life program we'd use another
- // condition variable which would be signaled from wxThread::Entry() to
- // tell us that the thread really started running - but here just wait a
- // bit and hope that it will be enough (the problem is, of course, that
- // the thread might still not run when we call Pause() which will result
- // in an error)
- wxThread::Sleep(300);
-
- for ( size_t n = 0; n < 3; n++ )
- {
- thread->Pause();
-
- wxPuts(_T("\nThread suspended"));
- if ( n > 0 )
- {
- // don't sleep but resume immediately the first time
- wxThread::Sleep(300);
- }
- wxPuts(_T("Going to resume the thread"));
-
- thread->Resume();
- }
-
- wxPuts(_T("Waiting until it terminates now"));
-
- // wait until the thread terminates
- gs_cond.Wait();
-
- wxPuts(_T(""));
-}
-
-static void TestThreadDelete()
-{
- // As above, using Sleep() is only for testing here - we must use some
- // synchronisation object instead to ensure that the thread is still
- // running when we delete it - deleting a detached thread which already
- // terminated will lead to a crash!
-
- wxPuts(_T("\n*** Testing thread delete function ***"));
-
- MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
-
- thread0->Delete();
-
- wxPuts(_T("\nDeleted a thread which didn't start to run yet."));
-
- MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
-
- thread1->Run();
-
- wxThread::Sleep(300);
-
- thread1->Delete();
-
- wxPuts(_T("\nDeleted a running thread."));
-
- MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
-
- thread2->Run();
-
- wxThread::Sleep(300);
-
- thread2->Pause();
-
- thread2->Delete();
-
- wxPuts(_T("\nDeleted a sleeping thread."));
-
- MyJoinableThread thread3(20);
- thread3.Run();
-
- thread3.Delete();
-
- wxPuts(_T("\nDeleted a joinable thread."));
-
- MyJoinableThread thread4(2);
- thread4.Run();
-
- wxThread::Sleep(300);
-
- thread4.Delete();
-
- wxPuts(_T("\nDeleted a joinable thread which already terminated."));
-
- wxPuts(_T(""));
-}
-
-class MyWaitingThread : public wxThread
-{
-public:
- MyWaitingThread( wxMutex *mutex, wxCondition *condition )
- {
- m_mutex = mutex;
- m_condition = condition;
-
- Create();
- }
-
- virtual ExitCode Entry()
- {
- wxPrintf(_T("Thread %lu has started running.\n"), GetId());
- fflush(stdout);
-
- gs_cond.Post();
-
- wxPrintf(_T("Thread %lu starts to wait...\n"), GetId());
- fflush(stdout);
-
- m_mutex->Lock();
- m_condition->Wait();
- m_mutex->Unlock();
-
- wxPrintf(_T("Thread %lu finished to wait, exiting.\n"), GetId());
- fflush(stdout);
-
- return 0;
- }
-
-private:
- wxMutex *m_mutex;
- wxCondition *m_condition;
-};