1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
22 #include <wx/string.h>
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
34 //#define TEST_THREADS
38 // ============================================================================
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
48 #include <wx/longlong.h>
51 static void TestSpeed()
53 static const long max
= 100000000;
60 for ( n
= 0; n
< max
; n
++ )
65 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
72 for ( n
= 0; n
< max
; n
++ )
77 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
84 for ( n
= 0; n
< max
; n
++ )
89 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
93 static void TestDivision()
95 wxLongLong ll
= 0x38417388; // some number < LONG_MAX
97 wxASSERT( (ll
/ 1000l)*1000l == ll
);
100 #endif // TEST_LONGLONG
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
108 #include <wx/datetime.h>
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
118 #include <wx/thread.h>
120 static size_t gs_counter
= (size_t)-1;
121 static wxCriticalSection gs_critsect
;
122 static wxCondition gs_cond
;
124 class MyJoinableThread
: public wxThread
127 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
128 { m_n
= n
; Create(); }
130 // thread execution starts here
131 virtual ExitCode
Entry();
137 wxThread::ExitCode
MyJoinableThread::Entry()
139 unsigned long res
= 1;
140 for ( size_t n
= 1; n
< m_n
; n
++ )
144 // it's a loooong calculation :-)
148 return (ExitCode
)res
;
151 class MyDetachedThread
: public wxThread
154 MyDetachedThread(char ch
) { m_ch
= ch
; Create(); }
156 // thread execution starts here
157 virtual ExitCode
Entry();
160 virtual void OnExit();
166 wxThread::ExitCode
MyDetachedThread::Entry()
169 wxCriticalSectionLocker
lock(gs_critsect
);
170 if ( gs_counter
== (size_t)-1 )
176 static const size_t nIter
= 10;
177 for ( size_t n
= 0; n
< nIter
; n
++ )
185 wxThread::Sleep(100);
191 void MyDetachedThread::OnExit()
193 wxCriticalSectionLocker
lock(gs_critsect
);
198 #endif // TEST_THREADS
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
206 void PrintArray(const char* name
, const wxArrayString
& array
)
208 printf("Dump of the array '%s'\n", name
);
210 size_t nCount
= array
.GetCount();
211 for ( size_t n
= 0; n
< nCount
; n
++ )
213 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
217 #endif // TEST_ARRAYS
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 int main(int argc
, char **argv
)
225 if ( !wxInitialize() )
227 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
239 puts("*** Initially:");
241 PrintArray("a1", a1
);
243 wxArrayString
a2(a1
);
244 PrintArray("a2", a2
);
246 wxSortedArrayString
a3(a1
);
247 PrintArray("a3", a3
);
249 puts("*** After deleting a string from a1");
252 PrintArray("a1", a1
);
253 PrintArray("a2", a2
);
254 PrintArray("a3", a3
);
256 puts("*** After reassigning a1 to a2 and a3");
258 PrintArray("a2", a2
);
259 PrintArray("a3", a3
);
260 #endif // TEST_ARRAYS
264 for ( size_t n
= 0; n
< 8000; n
++ )
266 s
<< (char)('A' + (n
% 26));
270 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
272 // this one shouldn't be truncated
275 // but this one will because log functions use fixed size buffer
276 // (note that it doesn't need '\n' at the end neither - will be added
278 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
282 puts("Testing detached threads...");
284 static const size_t nThreads
= 3;
285 MyDetachedThread
*threads
[nThreads
];
287 for ( n
= 0; n
< nThreads
; n
++ )
289 threads
[n
] = new MyDetachedThread('A' + n
);
292 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
293 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
295 for ( n
= 0; n
< nThreads
; n
++ )
300 // wait until all threads terminate
306 puts("\n\nTesting a joinable thread used for a loooong calculation...");
308 // calc 10! in the background
309 MyJoinableThread
thread(10);
312 printf("\nThread terminated with exit code %lu.\n",
313 (unsigned long)thread
.Wait());
314 #endif // TEST_THREADS
321 #endif // TEST_LONGLONG
324 wxDateTime time
= wxDateTime::Now();
325 printf("Current time: '%s', current year %u is %sa leap one",
326 time
.Format().c_str(),
328 wxDateTime::IsLeapYear(time
.GetYear()) ? "" : "not");