- // 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 (%ld) starting to wait for semaphore...\n"),
- wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
-
- m_sem->Wait();
-
- wxPrintf(_T("%s: Thread #%d (%ld) acquired the semaphore.\n"),
- wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
-
- Sleep(1000);
-
- wxPrintf(_T("%s: Thread #%d (%ld) releasing the semaphore.\n"),
- wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
-
- 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];