-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;
-};
-
-static void TestThreadConditions()
-{
- wxMutex mutex;
- wxCondition condition(mutex);
-
- // otherwise its difficult to understand which log messages pertain to
- // which condition
- //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"),
- // condition.GetId(), gs_cond.GetId());
-
- // create and launch threads
- MyWaitingThread *threads[10];
-
- size_t n;
- for ( n = 0; n < WXSIZEOF(threads); n++ )
- {
- threads[n] = new MyWaitingThread( &mutex, &condition );
- }
-
- for ( n = 0; n < WXSIZEOF(threads); n++ )
- {
- threads[n]->Run();
- }
-
- // wait until all threads run
- wxPuts(_T("Main thread is waiting for the other threads to start"));
- fflush(stdout);
-
- size_t nRunning = 0;
- while ( nRunning < WXSIZEOF(threads) )
- {
- gs_cond.Wait();
-
- nRunning++;
-
- wxPrintf(_T("Main thread: %u already running\n"), nRunning);
- fflush(stdout);
- }
-
- wxPuts(_T("Main thread: all threads started up."));
- fflush(stdout);
-
- wxThread::Sleep(500);
-
-#if 1
- // now wake one of them up
- wxPrintf(_T("Main thread: about to signal the condition.\n"));
- fflush(stdout);
- condition.Signal();
-#endif
-
- wxThread::Sleep(200);
-
- // wake all the (remaining) threads up, so that they can exit
- wxPrintf(_T("Main thread: about to broadcast the condition.\n"));
- fflush(stdout);
- condition.Broadcast();
-
- // give them time to terminate (dirty!)
- wxThread::Sleep(500);
-}
-
-#include "wx/utils.h"
-
-class MyExecThread : public wxThread
-{
-public:
- MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE),
- m_command(command)
- {
- Create();
- }
-
- virtual ExitCode Entry()
- {
- return (ExitCode)wxExecute(m_command, wxEXEC_SYNC);
- }
-
-private:
- wxString m_command;
-};
-
-static void TestThreadExec()
-{
- wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
-
- MyExecThread thread(_T("true"));
- thread.Run();
-
- wxPrintf(_T("Main program exit code: %ld.\n"),
- wxExecute(_T("false"), wxEXEC_SYNC));
-
- wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait());
-}
-
-// semaphore tests
-#include "wx/datetime.h"
-
-class MySemaphoreThread : public wxThread