]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
6e6af43806f186a4141f7d316e1f2847af284c97
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <stdio.h>
21
22 #include <wx/string.h>
23 #include <wx/file.h>
24 #include <wx/app.h>
25
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
29
30 // what to test?
31
32 //#define TEST_ARRAYS
33 //#define TEST_LOG
34 //#define TEST_THREADS
35 //#define TEST_TIME
36 #define TEST_LONGLONG
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 // ----------------------------------------------------------------------------
43 // long long
44 // ----------------------------------------------------------------------------
45
46 #ifdef TEST_LONGLONG
47
48 #include <wx/longlong.h>
49 #include <wx/timer.h>
50
51 static void TestSpeed()
52 {
53 static const long max = 100000000;
54 long n;
55
56 {
57 wxStopWatch sw;
58
59 long l = 0;
60 for ( n = 0; n < max; n++ )
61 {
62 l += n;
63 }
64
65 printf("Summing longs took %ld milliseconds.\n", sw.Time());
66 }
67
68 {
69 wxStopWatch sw;
70
71 __int64 l = 0;
72 for ( n = 0; n < max; n++ )
73 {
74 l += n;
75 }
76
77 printf("Summing __int64s took %ld milliseconds.\n", sw.Time());
78 }
79
80 {
81 wxStopWatch sw;
82
83 wxLongLong l;
84 for ( n = 0; n < max; n++ )
85 {
86 l += n;
87 }
88
89 printf("Summing wxLongLongs took %ld milliseconds.\n", sw.Time());
90 }
91 }
92
93 static void TestDivision()
94 {
95 wxLongLong ll = 0x38417388; // some number < LONG_MAX
96
97 wxASSERT( (ll / 1000l)*1000l == ll );
98 }
99
100 #endif // TEST_LONGLONG
101
102 // ----------------------------------------------------------------------------
103 // date time
104 // ----------------------------------------------------------------------------
105
106 #ifdef TEST_TIME
107
108 #include <wx/datetime.h>
109
110 #endif // TEST_TIME
111
112 // ----------------------------------------------------------------------------
113 // threads
114 // ----------------------------------------------------------------------------
115
116 #ifdef TEST_THREADS
117
118 #include <wx/thread.h>
119
120 static size_t gs_counter = (size_t)-1;
121 static wxCriticalSection gs_critsect;
122 static wxCondition gs_cond;
123
124 class MyJoinableThread : public wxThread
125 {
126 public:
127 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
128 { m_n = n; Create(); }
129
130 // thread execution starts here
131 virtual ExitCode Entry();
132
133 private:
134 size_t m_n;
135 };
136
137 wxThread::ExitCode MyJoinableThread::Entry()
138 {
139 unsigned long res = 1;
140 for ( size_t n = 1; n < m_n; n++ )
141 {
142 res *= n;
143
144 // it's a loooong calculation :-)
145 Sleep(100);
146 }
147
148 return (ExitCode)res;
149 }
150
151 class MyDetachedThread : public wxThread
152 {
153 public:
154 MyDetachedThread(char ch) { m_ch = ch; Create(); }
155
156 // thread execution starts here
157 virtual ExitCode Entry();
158
159 // and stops here
160 virtual void OnExit();
161
162 private:
163 char m_ch;
164 };
165
166 wxThread::ExitCode MyDetachedThread::Entry()
167 {
168 {
169 wxCriticalSectionLocker lock(gs_critsect);
170 if ( gs_counter == (size_t)-1 )
171 gs_counter = 1;
172 else
173 gs_counter++;
174 }
175
176 static const size_t nIter = 10;
177 for ( size_t n = 0; n < nIter; n++ )
178 {
179 if ( TestDestroy() )
180 break;
181
182 putchar(m_ch);
183 fflush(stdout);
184
185 wxThread::Sleep(100);
186 }
187
188 return 0;
189 }
190
191 void MyDetachedThread::OnExit()
192 {
193 wxCriticalSectionLocker lock(gs_critsect);
194 if ( !--gs_counter )
195 gs_cond.Signal();
196 }
197
198 #endif // TEST_THREADS
199
200 // ----------------------------------------------------------------------------
201 // arrays
202 // ----------------------------------------------------------------------------
203
204 #ifdef TEST_ARRAYS
205
206 void PrintArray(const char* name, const wxArrayString& array)
207 {
208 printf("Dump of the array '%s'\n", name);
209
210 size_t nCount = array.GetCount();
211 for ( size_t n = 0; n < nCount; n++ )
212 {
213 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
214 }
215 }
216
217 #endif // TEST_ARRAYS
218
219 // ----------------------------------------------------------------------------
220 // entry point
221 // ----------------------------------------------------------------------------
222
223 int main(int argc, char **argv)
224 {
225 if ( !wxInitialize() )
226 {
227 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
228 }
229
230 #ifdef TEST_ARRAYS
231 wxArrayString a1;
232 a1.Add("tiger");
233 a1.Add("cat");
234 a1.Add("lion");
235 a1.Add("dog");
236 a1.Add("human");
237 a1.Add("ape");
238
239 puts("*** Initially:");
240
241 PrintArray("a1", a1);
242
243 wxArrayString a2(a1);
244 PrintArray("a2", a2);
245
246 wxSortedArrayString a3(a1);
247 PrintArray("a3", a3);
248
249 puts("*** After deleting a string from a1");
250 a1.Remove(2);
251
252 PrintArray("a1", a1);
253 PrintArray("a2", a2);
254 PrintArray("a3", a3);
255
256 puts("*** After reassigning a1 to a2 and a3");
257 a3 = a2 = a1;
258 PrintArray("a2", a2);
259 PrintArray("a3", a3);
260 #endif // TEST_ARRAYS
261
262 #ifdef TEST_LOG
263 wxString s;
264 for ( size_t n = 0; n < 8000; n++ )
265 {
266 s << (char)('A' + (n % 26));
267 }
268
269 wxString msg;
270 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
271
272 // this one shouldn't be truncated
273 printf(msg);
274
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
277 // by wxLog anyhow)
278 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
279 #endif // TEST_LOG
280
281 #ifdef TEST_THREADS
282 puts("Testing detached threads...");
283
284 static const size_t nThreads = 3;
285 MyDetachedThread *threads[nThreads];
286 size_t n;
287 for ( n = 0; n < nThreads; n++ )
288 {
289 threads[n] = new MyDetachedThread('A' + n);
290 }
291
292 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
293 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
294
295 for ( n = 0; n < nThreads; n++ )
296 {
297 threads[n]->Run();
298 }
299
300 // wait until all threads terminate
301 wxMutex mutex;
302 mutex.Lock();
303 gs_cond.Wait(mutex);
304 mutex.Unlock();
305
306 puts("\n\nTesting a joinable thread used for a loooong calculation...");
307
308 // calc 10! in the background
309 MyJoinableThread thread(10);
310 thread.Run();
311
312 printf("\nThread terminated with exit code %lu.\n",
313 (unsigned long)thread.Wait());
314 #endif // TEST_THREADS
315
316 #ifdef TEST_LONGLONG
317 if ( 0 )
318 TestSpeed();
319 if ( 1 )
320 TestDivision();
321 #endif // TEST_LONGLONG
322
323 #ifdef TEST_TIME
324 wxDateTime time = wxDateTime::Now();
325 printf("Current time: '%s', current year %u is %sa leap one",
326 time.Format().c_str(),
327 time.GetYear(),
328 wxDateTime::IsLeapYear(time.GetYear()) ? "" : "not");
329 #endif // TEST_TIME
330
331 wxUninitialize();
332
333 return 0;
334 }