]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/console/console.cpp
Updates to fix EMX specific stuff that broke VisualAge specific stuff
[wxWidgets.git] / samples / console / console.cpp
index cd100faafb0b842d73f63bb3656de0d39ad8394a..80d7eb5be058f252bd2a154889ed667ac28ca4d1 100644 (file)
 // what to test?
 
 //#define TEST_ARRAYS
+//#define TEST_CMDLINE
 //#define TEST_DIR
+#define TEST_EXECUTE
 //#define TEST_LOG
 //#define TEST_LONGLONG
 //#define TEST_MIME
 //#define TEST_STRINGS
 //#define TEST_THREADS
-#define TEST_TIME
+//#define TEST_TIME
 
 // ============================================================================
 // implementation
 // ============================================================================
 
+#ifdef TEST_CMDLINE
+
+// ----------------------------------------------------------------------------
+// wxCmdLineParser
+// ----------------------------------------------------------------------------
+
+#include <wx/cmdline.h>
+#include <wx/datetime.h>
+
+static void ShowCmdLine(const wxCmdLineParser& parser)
+{
+    wxString s = "Input files: ";
+
+    size_t count = parser.GetParamCount();
+    for ( size_t param = 0; param < count; param++ )
+    {
+        s << parser.GetParam(param) << ' ';
+    }
+
+    s << '\n'
+      << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n'
+      << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n';
+
+    wxString strVal;
+    long lVal;
+    wxDateTime dt;
+    if ( parser.Found("o", &strVal) )
+        s << "Output file:\t" << strVal << '\n';
+    if ( parser.Found("i", &strVal) )
+        s << "Input dir:\t" << strVal << '\n';
+    if ( parser.Found("s", &lVal) )
+        s << "Size:\t" << lVal << '\n';
+    if ( parser.Found("d", &dt) )
+        s << "Date:\t" << dt.FormatISODate() << '\n';
+
+    wxLogMessage(s);
+}
+
+#endif // TEST_CMDLINE
+
 // ----------------------------------------------------------------------------
 // wxDir
 // ----------------------------------------------------------------------------
@@ -119,6 +161,34 @@ static void TestDirEnum()
 
 #endif // TEST_DIR
 
+// ----------------------------------------------------------------------------
+// wxExecute
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_EXECUTE
+
+#include <wx/utils.h>
+
+static void TestExecute()
+{
+    puts("*** testing wxExecute ***");
+
+#ifdef __UNIX__
+    #define COMMAND "echo hi"
+#elif defined(__WXMSW__)
+    #define COMMAND "command.com -c 'echo hi'"
+#else
+    #error "no command to exec"
+#endif // OS
+
+    if ( wxExecute(COMMAND) == 0 )
+        puts("\nOk.");
+    else
+        puts("\nError.");
+}
+
+#endif // TEST_EXECUTE
+
 // ----------------------------------------------------------------------------
 // MIME types
 // ----------------------------------------------------------------------------
@@ -178,6 +248,19 @@ static void TestMimeEnum()
 #include <wx/longlong.h>
 #include <wx/timer.h>
 
+// make a 64 bit number from 4 16 bit ones
+#define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
+
+// get a random 64 bit number
+#define RAND_LL()   MAKE_LL(rand(), rand(), rand(), rand())
+
+#if wxUSE_LONGLONG_NATIVE
+inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
+    { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
+inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
+    { return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
+#endif // wxUSE_LONGLONG_NATIVE
+
 static void TestSpeed()
 {
     static const long max = 100000000;
@@ -222,14 +305,75 @@ static void TestSpeed()
     }
 }
 
-static void TestDivision()
+static void TestLongLongConversion()
 {
-    puts("*** Testing wxLongLong division ***\n");
+    puts("*** Testing wxLongLong conversions ***\n");
 
-    #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
+    wxLongLong a;
+    size_t nTested = 0;
+    for ( size_t n = 0; n < 100000; n++ )
+    {
+        a = RAND_LL();
 
-    // seed pseudo random generator
-    srand((unsigned)time(NULL));
+#if wxUSE_LONGLONG_NATIVE
+        wxLongLongNative b(a.GetHi(), a.GetLo());
+
+        wxASSERT_MSG( a == b, "conversions failure" );
+#else
+        puts("Can't do it without native long long type, test skipped.");
+
+        return;
+#endif // wxUSE_LONGLONG_NATIVE
+
+        if ( !(nTested % 1000) )
+        {
+            putchar('.');
+            fflush(stdout);
+        }
+
+        nTested++;
+    }
+
+    puts(" done!");
+}
+
+static void TestMultiplication()
+{
+    puts("*** Testing wxLongLong multiplication ***\n");
+
+    wxLongLong a, b;
+    size_t nTested = 0;
+    for ( size_t n = 0; n < 100000; n++ )
+    {
+        a = RAND_LL();
+        b = RAND_LL();
+
+#if wxUSE_LONGLONG_NATIVE
+        wxLongLongNative aa(a.GetHi(), a.GetLo());
+        wxLongLongNative bb(b.GetHi(), b.GetLo());
+
+        wxASSERT_MSG( a*b == aa*bb, "multiplication failure" );
+#else // !wxUSE_LONGLONG_NATIVE
+        puts("Can't do it without native long long type, test skipped.");
+
+        return;
+#endif // wxUSE_LONGLONG_NATIVE
+
+        if ( !(nTested % 1000) )
+        {
+            putchar('.');
+            fflush(stdout);
+        }
+
+        nTested++;
+    }
+
+    puts(" done!");
+}
+
+static void TestDivision()
+{
+    puts("*** Testing wxLongLong division ***\n");
 
     wxLongLong q, r;
     size_t nTested = 0;
@@ -244,8 +388,15 @@ static void TestDivision()
         q = ll / l;
         r = ll % l;
 
+#if wxUSE_LONGLONG_NATIVE
+        wxLongLongNative m(ll.GetHi(), ll.GetLo());
+
+        wxLongLongNative p = m / l, s = m % l;
+        wxASSERT_MSG( q == p && r == s, "division failure" );
+#else // !wxUSE_LONGLONG_NATIVE
         // verify the result
         wxASSERT_MSG( ll == q*l + r, "division failure" );
+#endif // wxUSE_LONGLONG_NATIVE
 
         if ( !(nTested % 1000) )
         {
@@ -257,10 +408,87 @@ static void TestDivision()
     }
 
     puts(" done!");
+}
+
+static void TestAddition()
+{
+    puts("*** Testing wxLongLong addition ***\n");
+
+    wxLongLong a, b, c;
+    size_t nTested = 0;
+    for ( size_t n = 0; n < 100000; n++ )
+    {
+        a = RAND_LL();
+        b = RAND_LL();
+        c = a + b;
+
+#if wxUSE_LONGLONG_NATIVE
+        wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
+                           wxLongLongNative(b.GetHi(), b.GetLo()),
+                      "addition failure" ); 
+#else // !wxUSE_LONGLONG_NATIVE
+        wxASSERT_MSG( c - b == a, "addition failure" );
+#endif // wxUSE_LONGLONG_NATIVE
+
+        if ( !(nTested % 1000) )
+        {
+            putchar('.');
+            fflush(stdout);
+        }
+
+        nTested++;
+    }
+
+    puts(" done!");
+}
+
+static void TestBitOperations()
+{
+    puts("*** Testing wxLongLong bit operation ***\n");
+
+    wxLongLong a, c;
+    size_t nTested = 0;
+    for ( size_t n = 0; n < 100000; n++ )
+    {
+        a = RAND_LL();
+
+#if wxUSE_LONGLONG_NATIVE
+        for ( size_t n = 0; n < 33; n++ )
+        {
+            wxLongLongNative b(a.GetHi(), a.GetLo());
+
+            b >>= n;
+            c = a >> n;
+
+            wxASSERT_MSG( b == c, "bit shift failure" );
+
+            b = wxLongLongNative(a.GetHi(), a.GetLo()) << n;
+            c = a << n;
+
+            wxASSERT_MSG( b == c, "bit shift failure" );
+        }
+
+#else // !wxUSE_LONGLONG_NATIVE
+        puts("Can't do it without native long long type, test skipped.");
+
+        return;
+#endif // wxUSE_LONGLONG_NATIVE
+
+        if ( !(nTested % 1000) )
+        {
+            putchar('.');
+            fflush(stdout);
+        }
+
+        nTested++;
+    }
 
-    #undef MAKE_LL
+    puts(" done!");
 }
 
+#undef MAKE_LL
+#undef RAND_LL
+
 #endif // TEST_LONGLONG
 
 // ----------------------------------------------------------------------------
@@ -1150,6 +1378,31 @@ static void TestTimeArithmetics()
     }
 }
 
+static void TestTimeHolidays()
+{
+    puts("\n*** testing wxDateTimeHolidayAuthority ***\n");
+
+    wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm();
+    wxDateTime dtStart(1, tm.mon, tm.year),
+               dtEnd = dtStart.GetLastMonthDay();
+
+    wxDateTimeArray hol;
+    wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
+
+    const wxChar *format = "%d-%b-%Y (%a)";
+
+    printf("All holidays between %s and %s:\n",
+           dtStart.Format(format).c_str(), dtEnd.Format(format).c_str());
+
+    size_t count = hol.GetCount();
+    for ( size_t n = 0; n < count; n++ )
+    {
+        printf("\t%s\n", hol[n].Format(format).c_str());
+    }
+
+    puts("");
+}
+
 #if 0
 
 // test compatibility with the old wxDate/wxTime classes
@@ -1538,6 +1791,46 @@ int main(int argc, char **argv)
         fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
     }
 
+#ifdef TEST_USLEEP
+    puts("Sleeping for 3 seconds... z-z-z-z-z...");
+    wxUsleep(3000);
+#endif // TEST_USLEEP
+
+#ifdef TEST_CMDLINE
+    static const wxCmdLineEntryDesc cmdLineDesc[] =
+    {
+        { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
+        { wxCMD_LINE_SWITCH, "q", "quiet",   "be quiet" },
+
+        { wxCMD_LINE_OPTION, "o", "output",  "output file" },
+        { wxCMD_LINE_OPTION, "i", "input",   "input dir" },
+        { wxCMD_LINE_OPTION, "s", "size",    "output block size", wxCMD_LINE_VAL_NUMBER },
+        { wxCMD_LINE_OPTION, "d", "date",    "output file date", wxCMD_LINE_VAL_DATE },
+
+        { wxCMD_LINE_PARAM,  NULL, NULL, "input file",
+            wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
+
+        { wxCMD_LINE_NONE }
+    };
+
+    wxCmdLineParser parser(cmdLineDesc, argc, argv);
+
+    switch ( parser.Parse() )
+    {
+        case -1:
+            wxLogMessage("Help was given, terminating.");
+            break;
+
+        case 0:
+            ShowCmdLine(parser);
+            break;
+
+        default:
+            wxLogMessage("Syntax error detected, aborting.");
+            break;
+    }
+#endif // TEST_CMDLINE
+
 #ifdef TEST_STRINGS
     if ( 0 )
     {
@@ -1587,6 +1880,10 @@ int main(int argc, char **argv)
     TestDirEnum();
 #endif // TEST_DIR
 
+#ifdef TEST_EXECUTE
+    TestExecute();
+#endif // TEST_EXECUTE
+
 #ifdef TEST_LOG
     wxString s;
     for ( size_t n = 0; n < 8000; n++ )
@@ -1627,10 +1924,21 @@ int main(int argc, char **argv)
 #endif // TEST_THREADS
 
 #ifdef TEST_LONGLONG
+    // seed pseudo random generator
+    srand((unsigned)time(NULL));
+
     if ( 0 )
+    {
         TestSpeed();
-    if ( 1 )
+    }
+    TestMultiplication();
+    if ( 0 )
+    {
         TestDivision();
+        TestAddition();
+        TestLongLongConversion();
+        TestBitOperations();
+    }
 #endif // TEST_LONGLONG
 
 #ifdef TEST_MIME
@@ -1653,6 +1961,7 @@ int main(int argc, char **argv)
         TestTimeFormat();
         TestTimeArithmetics();
     }
+    TestTimeHolidays();
     if ( 0 )
         TestInteractive();
 #endif // TEST_TIME