- puts("\nDeleted a joinable thread.");
-
- MyJoinableThread thread4(2);
- thread4.Run();
-
- wxThread::Sleep(300);
-
- thread4.Delete();
-
- puts("\nDeleted a joinable thread which already terminated.");
-
- puts("");
-}
-
-class MyWaitingThread : public wxThread
-{
-public:
- MyWaitingThread( wxMutex *mutex, wxCondition *condition )
- {
- m_mutex = mutex;
- m_condition = condition;
-
- Create();
- }
-
- virtual ExitCode Entry()
- {
- printf("Thread %lu has started running.\n", GetId());
- fflush(stdout);
-
- gs_cond.Post();
-
- printf("Thread %lu starts to wait...\n", GetId());
- fflush(stdout);
-
- m_mutex->Lock();
- m_condition->Wait();
- m_mutex->Unlock();
-
- printf("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("thread", "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
- puts("Main thread is waiting for the other threads to start");
- fflush(stdout);
-
- size_t nRunning = 0;
- while ( nRunning < WXSIZEOF(threads) )
- {
- gs_cond.Wait();
-
- nRunning++;
-
- printf("Main thread: %u already running\n", nRunning);
- fflush(stdout);
- }
-
- puts("Main thread: all threads started up.");
- fflush(stdout);
-
- wxThread::Sleep(500);
-
-#if 1
- // now wake one of them up
- printf("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
- printf("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
-{
-public:
- MySemaphoreThread(int i, wxSemaphore *sem)
- : wxThread(wxTHREAD_JOINABLE),
- m_sem(sem),
- m_i(i)
- {
- Create();
- }
-
- virtual ExitCode Entry()
- {
- wxPrintf(_T("%s: Thread %d starting to wait for semaphore...\n"),
- wxDateTime::Now().FormatTime().c_str(), m_i);
-
- m_sem->Wait();
-
- wxPrintf(_T("%s: Thread %d acquired the semaphore.\n"),
- wxDateTime::Now().FormatTime().c_str(), m_i);
-
- Sleep(1000);
-
- wxPrintf(_T("%s: Thread %d releasing the semaphore.\n"),
- wxDateTime::Now().FormatTime().c_str(), m_i);
-
- m_sem->Post();
-
- return 0;
- }
-
-private:
- wxSemaphore *m_sem;
- int m_i;
-};
-
-WX_DEFINE_ARRAY(wxThread *, ArrayThreads);
-
-static void TestSemaphore()
-{
- wxPuts(_T("*** Testing wxSemaphore class. ***"));
-
- static const int SEM_LIMIT = 3;
-
- wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
- ArrayThreads threads;
-
- for ( int i = 0; i < 3*SEM_LIMIT; i++ )
- {
- threads.Add(new MySemaphoreThread(i, &sem));
- threads.Last()->Run();
- }
-
- for ( size_t n = 0; n < threads.GetCount(); n++ )
- {
- threads[n]->Wait();
- delete threads[n];
- }
-}
-
-#endif // TEST_THREADS
-
-// ----------------------------------------------------------------------------
-// arrays
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_ARRAYS
-
-#include "wx/dynarray.h"
-
-typedef unsigned short ushort;
-
-#define DefineCompare(name, T) \
- \
-int wxCMPFUNC_CONV name ## CompareValues(T first, T second) \
-{ \
- return first - second; \
-} \
- \
-int wxCMPFUNC_CONV name ## Compare(T* first, T* second) \
-{ \
- return *first - *second; \
-} \
- \
-int wxCMPFUNC_CONV name ## RevCompare(T* first, T* second) \
-{ \
- return *second - *first; \
-} \
-
-DefineCompare(UShort, ushort);
-DefineCompare(Int, int);
-
-// test compilation of all macros
-WX_DEFINE_ARRAY_SHORT(ushort, wxArrayUShort);
-WX_DEFINE_SORTED_ARRAY_SHORT(ushort, wxSortedArrayUShortNoCmp);
-WX_DEFINE_SORTED_ARRAY_CMP_SHORT(ushort, UShortCompareValues, wxSortedArrayUShort);
-WX_DEFINE_SORTED_ARRAY_CMP_INT(int, IntCompareValues, wxSortedArrayInt);
-
-WX_DECLARE_OBJARRAY(Bar, ArrayBars);
-#include "wx/arrimpl.cpp"
-WX_DEFINE_OBJARRAY(ArrayBars);
-
-static void PrintArray(const char* name, const wxArrayString& array)
-{
- printf("Dump of the array '%s'\n", name);
-
- size_t nCount = array.GetCount();
- for ( size_t n = 0; n < nCount; n++ )
- {
- printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
- }
-}
-
-int wxCMPFUNC_CONV StringLenCompare(const wxString& first,
- const wxString& second)
-{
- return first.length() - second.length();
-}
-
-#define TestArrayOf(name) \
- \
-static void PrintArray(const char* name, const wxSortedArray##name & array) \
-{ \
- printf("Dump of the array '%s'\n", name); \
- \
- size_t nCount = array.GetCount(); \
- for ( size_t n = 0; n < nCount; n++ ) \
- { \
- printf("\t%s[%u] = %d\n", name, n, array[n]); \
- } \
-} \
- \
-static void PrintArray(const char* name, const wxArray##name & array) \
-{ \
- printf("Dump of the array '%s'\n", name); \
- \
- size_t nCount = array.GetCount(); \
- for ( size_t n = 0; n < nCount; n++ ) \
- { \
- printf("\t%s[%u] = %d\n", name, n, array[n]); \
- } \
-} \
- \
-static void TestArrayOf ## name ## s() \
-{ \
- printf("*** Testing wxArray%s ***\n", #name); \
- \
- wxArray##name a; \
- a.Add(1); \
- a.Add(17,2); \
- a.Add(5,3); \
- a.Add(3,4); \
- \
- puts("Initially:"); \
- PrintArray("a", a); \
- \
- puts("After sort:"); \
- a.Sort(name ## Compare); \
- PrintArray("a", a); \
- \
- puts("After reverse sort:"); \
- a.Sort(name ## RevCompare); \
- PrintArray("a", a); \
- \
- wxSortedArray##name b; \
- b.Add(1); \
- b.Add(17); \
- b.Add(5); \
- b.Add(3); \
- \
- puts("Sorted array initially:"); \
- PrintArray("b", b); \
-}
-
-TestArrayOf(UShort);
-TestArrayOf(Int);
-
-static void TestArrayOfObjects()
-{
- puts("*** Testing wxObjArray ***\n");
-
- {
- ArrayBars bars;
- Bar bar("second bar (two copies!)");
-
- printf("Initially: %u objects in the array, %u objects total.\n",
- bars.GetCount(), Bar::GetNumber());
-
- bars.Add(new Bar("first bar"));
- bars.Add(bar,2);
-
- printf("Now: %u objects in the array, %u objects total.\n",
- bars.GetCount(), Bar::GetNumber());
-
- bars.RemoveAt(1, bars.GetCount() - 1);
-
- printf("After removing all but first element: %u objects in the "
- "array, %u objects total.\n",
- bars.GetCount(), Bar::GetNumber());
-
- bars.Empty();
-
- printf("After Empty(): %u objects in the array, %u objects total.\n",
- bars.GetCount(), Bar::GetNumber());
- }
-
- printf("Finally: no more objects in the array, %u objects total.\n",
- Bar::GetNumber());
-}
-
-#endif // TEST_ARRAYS
-
-// ----------------------------------------------------------------------------
-// strings
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_STRINGS
-
-#include "wx/timer.h"
-#include "wx/tokenzr.h"
-
-static void TestStringConstruction()
-{
- puts("*** Testing wxString constructores ***");
-
- #define TEST_CTOR(args, res) \
- { \
- wxString s args ; \
- printf("wxString%s = %s ", #args, s.c_str()); \
- if ( s == res ) \
- { \
- puts("(ok)"); \
- } \
- else \
- { \
- printf("(ERROR: should be %s)\n", res); \
- } \
- }
-
- TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
- TEST_CTOR((_T("Hello"), 4), _T("Hell"));
- TEST_CTOR((_T("Hello"), 5), _T("Hello"));
- // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure
-
- static const wxChar *s = _T("?really!");
- const wxChar *start = wxStrchr(s, _T('r'));
- const wxChar *end = wxStrchr(s, _T('!'));
- TEST_CTOR((start, end), _T("really"));
-
- puts("");
-}
-
-static void TestString()
-{
- wxStopWatch sw;
-
- wxString a, b, c;
-
- a.reserve (128);
- b.reserve (128);
- c.reserve (128);
-
- for (int i = 0; i < 1000000; ++i)
- {
- a = "Hello";
- b = " world";
- c = "! How'ya doin'?";
- a += b;
- a += c;
- c = "Hello world! What's up?";
- if (c != a)
- c = "Doh!";
- }
-
- printf ("TestString elapsed time: %ld\n", sw.Time());
-}
-
-static void TestPChar()
-{
- wxStopWatch sw;
-
- char a [128];
- char b [128];
- char c [128];
-
- for (int i = 0; i < 1000000; ++i)
- {
- strcpy (a, "Hello");
- strcpy (b, " world");
- strcpy (c, "! How'ya doin'?");
- strcat (a, b);
- strcat (a, c);
- strcpy (c, "Hello world! What's up?");
- if (strcmp (c, a) == 0)
- strcpy (c, "Doh!");
- }
-
- printf ("TestPChar elapsed time: %ld\n", sw.Time());
-}
-
-static void TestStringSub()
-{
- wxString s("Hello, world!");
-
- puts("*** Testing wxString substring extraction ***");
-
- printf("String = '%s'\n", s.c_str());
- printf("Left(5) = '%s'\n", s.Left(5).c_str());
- printf("Right(6) = '%s'\n", s.Right(6).c_str());
- printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
- printf("Mid(3) = '%s'\n", s.Mid(3).c_str());
- printf("substr(3, 5) = '%s'\n", s.substr(3, 5).c_str());
- printf("substr(3) = '%s'\n", s.substr(3).c_str());
-
- static const wxChar *prefixes[] =
- {
- _T("Hello"),
- _T("Hello, "),
- _T("Hello, world!"),
- _T("Hello, world!!!"),
- _T(""),
- _T("Goodbye"),
- _T("Hi"),
- };
-
- for ( size_t n = 0; n < WXSIZEOF(prefixes); n++ )
- {
- wxString prefix = prefixes[n], rest;
- bool rc = s.StartsWith(prefix, &rest);
- printf("StartsWith('%s') = %s", prefix.c_str(), rc ? "TRUE" : "FALSE");
- if ( rc )
- {
- printf(" (the rest is '%s')\n", rest.c_str());
- }
- else
- {
- putchar('\n');
- }
- }