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 // ----------------------------------------------------------------------------
36 //#define TEST_STRINGS
37 //#define TEST_THREADS
39 //#define TEST_LONGLONG
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
53 static void TestDirEnumHelper(wxDir
& dir
,
54 int flags
= wxDIR_DEFAULT
,
55 const wxString
& filespec
= wxEmptyString
)
59 if ( !dir
.IsOpened() )
62 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
65 printf("\t%s\n", filename
.c_str());
67 cont
= dir
.GetNext(&filename
);
73 static void TestDirEnum()
75 wxDir
dir(wxGetCwd());
77 puts("Enumerating everything in current directory:");
78 TestDirEnumHelper(dir
);
80 puts("Enumerating really everything in current directory:");
81 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
83 puts("Enumerating object files in current directory:");
84 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o");
86 puts("Enumerating directories in current directory:");
87 TestDirEnumHelper(dir
, wxDIR_DIRS
);
89 puts("Enumerating files in current directory:");
90 TestDirEnumHelper(dir
, wxDIR_FILES
);
92 puts("Enumerating files including hidden in current directory:");
93 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
97 #elif defined(__WXMSW__)
100 #error "don't know where the root directory is"
103 puts("Enumerating everything in root directory:");
104 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
106 puts("Enumerating directories in root directory:");
107 TestDirEnumHelper(dir
, wxDIR_DIRS
);
109 puts("Enumerating files in root directory:");
110 TestDirEnumHelper(dir
, wxDIR_FILES
);
112 puts("Enumerating files including hidden in root directory:");
113 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
115 puts("Enumerating files in non existing directory:");
116 wxDir
dirNo("nosuchdir");
117 TestDirEnumHelper(dirNo
);
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
128 #include <wx/mimetype.h>
130 static void TestMimeEnum()
132 wxMimeTypesManager mimeTM
;
133 wxArrayString mimetypes
;
135 size_t count
= mimeTM
.EnumAllFileTypes(mimetypes
);
137 printf("*** All %u known filetypes: ***\n", count
);
142 for ( size_t n
= 0; n
< count
; n
++ )
144 wxFileType
*filetype
= mimeTM
.GetFileTypeFromMimeType(mimetypes
[n
]);
147 printf("nothing known about the filetype '%s'!\n",
148 mimetypes
[n
].c_str());
152 filetype
->GetDescription(&desc
);
153 filetype
->GetExtensions(exts
);
155 filetype
->GetIcon(NULL
);
158 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
165 printf("\t%s: %s (%s)\n",
166 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
178 #include <wx/longlong.h>
179 #include <wx/timer.h>
181 static void TestSpeed()
183 static const long max
= 100000000;
190 for ( n
= 0; n
< max
; n
++ )
195 printf("Summing longs took %ld milliseconds.\n", sw
.Time());
202 for ( n
= 0; n
< max
; n
++ )
207 printf("Summing __int64s took %ld milliseconds.\n", sw
.Time());
214 for ( n
= 0; n
< max
; n
++ )
219 printf("Summing wxLongLongs took %ld milliseconds.\n", sw
.Time());
223 static void TestDivision()
225 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
227 // seed pseudo random generator
228 //srand((unsigned)time(NULL));
231 for ( size_t n
= 0; n
< 10000; n
++ )
233 // get a random wxLongLong (shifting by 12 the MSB ensures that the
234 // multiplication will not overflow)
235 wxLongLong ll
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
237 wxASSERT( (ll
* 1000l)/1000l == ll
);
242 printf("\n*** Tested %u divisions/multiplications: ok\n", nTested
);
247 #endif // TEST_LONGLONG
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
255 #include <wx/datetime.h>
260 wxDateTime::wxDateTime_t day
;
261 wxDateTime::Month month
;
263 wxDateTime::wxDateTime_t hour
, min
, sec
;
265 wxDateTime::WeekDay wday
;
266 time_t gmticks
, ticks
;
268 void Init(const wxDateTime::Tm
& tm
)
277 gmticks
= ticks
= -1;
280 wxDateTime
DT() const
281 { return wxDateTime(day
, month
, year
, hour
, min
, sec
); }
283 wxString
Format() const
286 s
.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
288 wxDateTime::GetMonthName(month
).c_str(),
290 abs(wxDateTime::ConvertYearToBC(year
)),
291 year
> 0 ? "AD" : "BC");
296 static const Date testDates
[] =
298 { 1, wxDateTime::Jan
, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu
, 0, -3600 },
299 { 21, wxDateTime::Jan
, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon
, -1, -1 },
300 { 29, wxDateTime::May
, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat
, 202219200, 202212000 },
301 { 29, wxDateTime::Feb
, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun
, 194400000, 194396400 },
302 { 1, wxDateTime::Jan
, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon
, -1, -1 },
303 { 1, wxDateTime::Jan
, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon
, -1, -1 },
304 { 15, wxDateTime::Oct
, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri
, -1, -1 },
305 { 4, wxDateTime::Oct
, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon
, -1, -1 },
306 { 1, wxDateTime::Mar
, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu
, -1, -1 },
307 { 1, wxDateTime::Jan
, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon
, -1, -1 },
308 { 31, wxDateTime::Dec
, 0, 00, 00, 00, 1721424.5, wxDateTime::Tue
, -1, -1 },
309 { 1, wxDateTime::Jan
, 0, 00, 00, 00, 1721059.5, wxDateTime::Wed
, -1, -1 },
310 { 12, wxDateTime::Aug
, -1234, 00, 00, 00, 1270573.5, wxDateTime::Thu
, -1, -1 },
311 { 12, wxDateTime::Aug
, -4000, 00, 00, 00, 260313.5, wxDateTime::Wed
, -1, -1 },
312 { 24, wxDateTime::Nov
, -4713, 00, 00, 00, -0.5, wxDateTime::Mon
, -1, -1 },
315 // this test miscellaneous static wxDateTime functions
316 static void TestTimeStatic()
318 puts("\n*** wxDateTime static methods test ***");
320 // some info about the current date
321 int year
= wxDateTime::GetCurrentYear();
322 printf("Current year %d is %sa leap one and has %d days.\n",
324 wxDateTime::IsLeapYear(year
) ? "" : "not ",
325 wxDateTime::GetNumberOfDays(year
));
327 wxDateTime::Month month
= wxDateTime::GetCurrentMonth();
328 printf("Current month is '%s' ('%s') and it has %d days\n",
329 wxDateTime::GetMonthName(month
, TRUE
).c_str(),
330 wxDateTime::GetMonthName(month
).c_str(),
331 wxDateTime::GetNumberOfDays(month
));
334 static const size_t nYears
= 5;
335 static const size_t years
[2][nYears
] =
337 // first line: the years to test
338 { 1990, 1976, 2000, 2030, 1984, },
340 // second line: TRUE if leap, FALSE otherwise
341 { FALSE
, TRUE
, TRUE
, FALSE
, TRUE
}
344 for ( size_t n
= 0; n
< nYears
; n
++ )
346 int year
= years
[0][n
];
347 bool should
= years
[1][n
] != 0;
349 printf("Year %d is %sa leap year (should be: %s)\n",
351 wxDateTime::IsLeapYear(year
) ? "" : "not ",
352 should
? "yes" : "no");
354 wxASSERT( should
== wxDateTime::IsLeapYear(year
) );
358 // test constructing wxDateTime objects
359 static void TestTimeSet()
361 puts("\n*** wxDateTime construction test ***");
363 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
365 const Date
& d1
= testDates
[n
];
366 wxDateTime dt
= d1
.DT();
371 wxString s1
= d1
.Format(),
374 printf("Date: %s == %s (%s)\n",
375 s1
.c_str(), s2
.c_str(),
376 s1
== s2
? "ok" : "ERROR");
380 // test time zones stuff
381 static void TestTimeZones()
383 puts("\n*** wxDateTime timezone test ***");
385 wxDateTime now
= wxDateTime::Now();
387 printf("Current GMT time:\t%s\n", now
.Format("%c", wxDateTime::GMT0
).c_str());
388 printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0
).c_str());
389 printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST
).c_str());
390 printf("Current time in Paris:\t%s\n", now
.Format("%c", wxDateTime::CET
).c_str());
391 printf(" Moscow:\t%s\n", now
.Format("%c", wxDateTime::MSK
).c_str());
392 printf(" New York:\t%s\n", now
.Format("%c", wxDateTime::EST
).c_str());
395 // test some minimal support for the dates outside the standard range
396 static void TestTimeRange()
398 puts("\n*** wxDateTime out-of-standard-range dates test ***");
400 static const char *fmt
= "%d-%b-%Y %H:%M:%S";
402 printf("Unix epoch:\t%s\n",
403 wxDateTime(2440587.5).Format(fmt
).c_str());
404 printf("Feb 29, 0: \t%s\n",
405 wxDateTime(29, wxDateTime::Feb
, 0).Format(fmt
).c_str());
406 printf("JDN 0: \t%s\n",
407 wxDateTime(0.0).Format(fmt
).c_str());
408 printf("Jan 1, 1AD:\t%s\n",
409 wxDateTime(1, wxDateTime::Jan
, 1).Format(fmt
).c_str());
410 printf("May 29, 2099:\t%s\n",
411 wxDateTime(29, wxDateTime::May
, 2099).Format(fmt
).c_str());
414 static void TestTimeTicks()
416 puts("\n*** wxDateTime ticks test ***");
418 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
420 const Date
& d
= testDates
[n
];
424 wxDateTime dt
= d
.DT();
425 long ticks
= (dt
.GetValue() / 1000).ToLong();
426 printf("Ticks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
427 if ( ticks
== d
.ticks
)
433 printf(" (ERROR: should be %ld, delta = %ld)\n",
434 d
.ticks
, ticks
- d
.ticks
);
437 dt
= d
.DT().ToTimezone(wxDateTime::GMT0
);
438 ticks
= (dt
.GetValue() / 1000).ToLong();
439 printf("GMtks of %s:\t% 10ld", d
.Format().c_str(), ticks
);
440 if ( ticks
== d
.gmticks
)
446 printf(" (ERROR: should be %ld, delta = %ld)\n",
447 d
.gmticks
, ticks
- d
.gmticks
);
454 // test conversions to JDN &c
455 static void TestTimeJDN()
457 puts("\n*** wxDateTime to JDN test ***");
459 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
461 const Date
& d
= testDates
[n
];
462 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
463 double jdn
= dt
.GetJulianDayNumber();
465 printf("JDN of %s is:\t% 15.6f", d
.Format().c_str(), jdn
);
472 printf(" (ERROR: should be %f, delta = %f)\n",
478 // test week days computation
479 static void TestTimeWDays()
481 puts("\n*** wxDateTime weekday test ***");
483 for ( size_t n
= 0; n
< WXSIZEOF(testDates
); n
++ )
485 const Date
& d
= testDates
[n
];
486 wxDateTime
dt(d
.day
, d
.month
, d
.year
, d
.hour
, d
.min
, d
.sec
);
488 wxDateTime::WeekDay wday
= dt
.GetWeekDay();
491 wxDateTime::GetWeekDayName(wday
));
492 if ( wday
== d
.wday
)
498 printf(" (ERROR: should be %s)\n",
499 wxDateTime::GetWeekDayName(d
.wday
));
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
512 #include <wx/thread.h>
514 static size_t gs_counter
= (size_t)-1;
515 static wxCriticalSection gs_critsect
;
516 static wxCondition gs_cond
;
518 class MyJoinableThread
: public wxThread
521 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
522 { m_n
= n
; Create(); }
524 // thread execution starts here
525 virtual ExitCode
Entry();
531 wxThread::ExitCode
MyJoinableThread::Entry()
533 unsigned long res
= 1;
534 for ( size_t n
= 1; n
< m_n
; n
++ )
538 // it's a loooong calculation :-)
542 return (ExitCode
)res
;
545 class MyDetachedThread
: public wxThread
548 MyDetachedThread(size_t n
, char ch
)
557 // thread execution starts here
558 virtual ExitCode
Entry();
561 virtual void OnExit();
564 size_t m_n
; // number of characters to write
565 char m_ch
; // character to write
567 bool m_cancelled
; // FALSE if we exit normally
570 wxThread::ExitCode
MyDetachedThread::Entry()
573 wxCriticalSectionLocker
lock(gs_critsect
);
574 if ( gs_counter
== (size_t)-1 )
580 for ( size_t n
= 0; n
< m_n
; n
++ )
592 wxThread::Sleep(100);
598 void MyDetachedThread::OnExit()
600 wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
602 wxCriticalSectionLocker
lock(gs_critsect
);
603 if ( !--gs_counter
&& !m_cancelled
)
607 void TestDetachedThreads()
609 puts("\n*** Testing detached threads ***");
611 static const size_t nThreads
= 3;
612 MyDetachedThread
*threads
[nThreads
];
614 for ( n
= 0; n
< nThreads
; n
++ )
616 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
619 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
620 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
622 for ( n
= 0; n
< nThreads
; n
++ )
627 // wait until all threads terminate
633 void TestJoinableThreads()
635 puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
637 // calc 10! in the background
638 MyJoinableThread
thread(10);
641 printf("\nThread terminated with exit code %lu.\n",
642 (unsigned long)thread
.Wait());
645 void TestThreadSuspend()
647 puts("\n*** Testing thread suspend/resume functions ***");
649 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
653 // this is for this demo only, in a real life program we'd use another
654 // condition variable which would be signaled from wxThread::Entry() to
655 // tell us that the thread really started running - but here just wait a
656 // bit and hope that it will be enough (the problem is, of course, that
657 // the thread might still not run when we call Pause() which will result
659 wxThread::Sleep(300);
661 for ( size_t n
= 0; n
< 3; n
++ )
665 puts("\nThread suspended");
668 // don't sleep but resume immediately the first time
669 wxThread::Sleep(300);
671 puts("Going to resume the thread");
676 puts("Waiting until it terminates now");
678 // wait until the thread terminates
684 void TestThreadDelete()
686 // As above, using Sleep() is only for testing here - we must use some
687 // synchronisation object instead to ensure that the thread is still
688 // running when we delete it - deleting a detached thread which already
689 // terminated will lead to a crash!
691 puts("\n*** Testing thread delete function ***");
693 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
697 puts("\nDeleted a thread which didn't start to run yet.");
699 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
703 wxThread::Sleep(300);
707 puts("\nDeleted a running thread.");
709 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
713 wxThread::Sleep(300);
719 puts("\nDeleted a sleeping thread.");
721 MyJoinableThread
thread3(20);
726 puts("\nDeleted a joinable thread.");
728 MyJoinableThread
thread4(2);
731 wxThread::Sleep(300);
735 puts("\nDeleted a joinable thread which already terminated.");
740 #endif // TEST_THREADS
742 // ----------------------------------------------------------------------------
744 // ----------------------------------------------------------------------------
748 void PrintArray(const char* name
, const wxArrayString
& array
)
750 printf("Dump of the array '%s'\n", name
);
752 size_t nCount
= array
.GetCount();
753 for ( size_t n
= 0; n
< nCount
; n
++ )
755 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
759 #endif // TEST_ARRAYS
761 // ----------------------------------------------------------------------------
763 // ----------------------------------------------------------------------------
767 #include "wx/timer.h"
769 static void TestString()
779 for (int i
= 0; i
< 1000000; ++i
)
783 c
= "! How'ya doin'?";
786 c
= "Hello world! What's up?";
791 printf ("TestString elapsed time: %ld\n", sw
.Time());
794 static void TestPChar()
802 for (int i
= 0; i
< 1000000; ++i
)
805 strcpy (b
, " world");
806 strcpy (c
, "! How'ya doin'?");
809 strcpy (c
, "Hello world! What's up?");
810 if (strcmp (c
, a
) == 0)
814 printf ("TestPChar elapsed time: %ld\n", sw
.Time());
817 static void TestStringSub()
819 wxString
s("Hello, world!");
821 puts("*** Testing wxString substring extraction ***");
823 printf("String = '%s'\n", s
.c_str());
824 printf("Left(5) = '%s'\n", s
.Left(5).c_str());
825 printf("Right(6) = '%s'\n", s
.Right(6).c_str());
826 printf("Mid(3, 5) = '%s'\n", s(3, 5).c_str());
827 printf("Mid(3) = '%s'\n", s
.Mid(3).c_str());
828 printf("substr(3, 5) = '%s'\n", s
.substr(3, 5).c_str());
829 printf("substr(3) = '%s'\n", s
.substr(3).c_str());
834 #endif // TEST_STRINGS
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 int main(int argc
, char **argv
)
842 if ( !wxInitialize() )
844 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
854 #endif // TEST_STRINGS
865 puts("*** Initially:");
867 PrintArray("a1", a1
);
869 wxArrayString
a2(a1
);
870 PrintArray("a2", a2
);
872 wxSortedArrayString
a3(a1
);
873 PrintArray("a3", a3
);
875 puts("*** After deleting a string from a1");
878 PrintArray("a1", a1
);
879 PrintArray("a2", a2
);
880 PrintArray("a3", a3
);
882 puts("*** After reassigning a1 to a2 and a3");
884 PrintArray("a2", a2
);
885 PrintArray("a3", a3
);
886 #endif // TEST_ARRAYS
894 for ( size_t n
= 0; n
< 8000; n
++ )
896 s
<< (char)('A' + (n
% 26));
900 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
902 // this one shouldn't be truncated
905 // but this one will because log functions use fixed size buffer
906 // (note that it doesn't need '\n' at the end neither - will be added
908 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
912 int nCPUs
= wxThread::GetCPUCount();
913 printf("This system has %d CPUs\n", nCPUs
);
915 wxThread::SetConcurrency(nCPUs
);
917 if ( argc
> 1 && argv
[1][0] == 't' )
918 wxLog::AddTraceMask("thread");
921 TestDetachedThreads();
923 TestJoinableThreads();
929 #endif // TEST_THREADS
936 #endif // TEST_LONGLONG