]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/console/console.cpp
tried to clean strdup() mess: we now have wxStrdup[AW] which should be always available
[wxWidgets.git] / samples / console / console.cpp
index c71f9c738166cc3258969008cbe747fabec66892..f2c47855720982993ad82b7899ca14ec760f60fb 100644 (file)
@@ -48,7 +48,7 @@
  */
 
 // what to test (in alphabetic order)? uncomment the line below to do all tests
-#define TEST_ALL
+//#define TEST_ALL
 #ifdef TEST_ALL
     #define TEST_ARRAYS
     #define TEST_CHARSET
@@ -81,6 +81,7 @@
     #define TEST_SOCKETS
     #define TEST_STREAMS
     #define TEST_STRINGS
+    #define TEST_TEXTSTREAM
     #define TEST_THREADS
     #define TEST_TIMER
     #define TEST_UNICODE
@@ -93,7 +94,7 @@
     #undef TEST_ALL
     static const bool TEST_ALL = TRUE;
 #else
-    #define TEST_PRINTF
+    #define TEST_TEXTSTREAM
 
     static const bool TEST_ALL = FALSE;
 #endif
@@ -1766,7 +1767,7 @@ static void TestMimeFilename()
                                      wxFileType::MessageParameters(fname, _T(""))) )
                 cmd = _T("<no command available>");
             else
-                cmd = wxString('"') + cmd + '"';
+                cmd = wxString(_T('"')) + cmd + _T('"');
 
             wxPrintf(_T("To open %s (%s) do %s.\n"),
                      fname.c_str(), desc.c_str(), cmd.c_str());
@@ -2169,9 +2170,9 @@ static void TestLongLongComparison()
 #endif // wxUSE_LONGLONG_WX
 }
 
-static void TestLongLongPrint()
+static void TestLongLongToString()
 {
-    wxPuts(_T("*** Testing wxLongLong printing ***\n"));
+    wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
 
     for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
     {
@@ -2186,6 +2187,20 @@ static void TestLongLongPrint()
     wxPrintf(_T("-0x1234567887654321 = %s\n"), ll.ToString().c_str());
 }
 
+static void TestLongLongPrintf()
+{
+    wxPuts(_T("*** Testing wxLongLong printing ***\n"));
+
+#ifdef wxLongLongFmtSpec
+    wxLongLong ll = wxLL(0x1234567890abcdef);
+    wxString s = wxString::Format(_T("%") wxLongLongFmtSpec _T("x"), ll);
+    wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
+             s.c_str(), s == _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
+#else // !wxLongLongFmtSpec
+    #error "wxLongLongFmtSpec not defined for this compiler/platform"
+#endif
+}
+
 #undef MAKE_LL
 #undef RAND_LL
 
@@ -2486,6 +2501,8 @@ static void TestDbOpen()
     int wxSprintf( wxChar *str, const wxChar *format, ... );
 #endif
 
+#include "wx/longlong.h"
+
 #include <float.h>
 
 static void rfg1 (void);
@@ -2587,8 +2604,8 @@ fp_test (void)
 static void TestPrintf()
 {
   static wxChar shortstr[] = _T("Hi, Z.");
-  static wxChar longstr[] = "Good morning, Doctor Chandra.  This is Hal.  \
-I am ready for my first lesson today.";
+  static wxChar longstr[] = _T("Good morning, Doctor Chandra.  This is Hal.  \
+I am ready for my first lesson today.");
   int result = 0;
 
   fmtchk(_T("%.4x"));
@@ -2707,19 +2724,21 @@ I am ready for my first lesson today.";
     wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
   }
 
+#ifdef wxLongLong_t
   {
-    wxChar buf[200];
+      wxChar buf[200];
 
-    wxSprintf (buf, _T("%07Lo"), 040000000000ll);
-    wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
+      wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
+      wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
 
-    if (wxStrcmp (buf, _T("40000000000")) != 0)
+      if (wxStrcmp (buf, _T("40000000000")) != 0)
       {
-        result = 1;
-        wxPuts (_T("\tFAILED"));
+          result = 1;
+          wxPuts (_T("\tFAILED"));
       }
-    wxPuts ("");
+      wxPuts (_T(""));
   }
+#endif // wxLongLong_t
 
   wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
   wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
@@ -3152,7 +3171,7 @@ static void TestFtpWuFtpd()
         }
         else
         {
-            size_t size = in->StreamSize();
+            size_t size = in->GetSize();
             wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
 
             wxChar *data = new wxChar[size];
@@ -3235,7 +3254,7 @@ static void TestFtpDownload()
     }
     else
     {
-        size_t size = in->StreamSize();
+        size_t size = in->GetSize();
         wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
         fflush(stdout);
 
@@ -3854,7 +3873,7 @@ static void TestUnicodeToFromAscii()
 #include "wx/encconv.h"
 #include "wx/buffer.h"
 
-static const unsigned char textInUtf8_[] =
+static const unsigned char utf8koi8r[] =
 {
     208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
     208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
@@ -3865,7 +3884,36 @@ static const unsigned char textInUtf8_[] =
     178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
 };
 
-#define textInUtf8 ((const char *)textInUtf8_)
+static const unsigned char utf8iso8859_1[] =
+{
+    0x53, 0x79, 0x73, 0x74, 0xc3, 0xa8, 0x6d, 0x65, 0x73, 0x20, 0x49, 0x6e,
+    0x74, 0xc3, 0xa9, 0x67, 0x72, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x65,
+    0x6e, 0x20, 0x4d, 0xc3, 0xa9, 0x63, 0x61, 0x6e, 0x69, 0x71, 0x75, 0x65,
+    0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x71, 0x75, 0x65, 0x20, 0x65,
+    0x74, 0x20, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x71, 0x75, 0x65, 0
+};
+
+static const unsigned char utf8Invalid[] =
+{
+    0x3c, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x3e, 0x32, 0x30, 0x30,
+    0x32, 0xe5, 0xb9, 0xb4, 0x30, 0x39, 0xe6, 0x9c, 0x88, 0x32, 0x35, 0xe6,
+    0x97, 0xa5, 0x20, 0x30, 0x37, 0xe6, 0x99, 0x82, 0x33, 0x39, 0xe5, 0x88,
+    0x86, 0x35, 0x37, 0xe7, 0xa7, 0x92, 0x3c, 0x2f, 0x64, 0x69, 0x73, 0x70,
+    0x6c, 0x61, 0x79, 0
+};
+
+static const struct Utf8Data
+{
+    const unsigned char *text;
+    size_t len;
+    const wxChar *charset;
+    wxFontEncoding encoding;
+} utf8data[] =
+{
+    { utf8Invalid, WXSIZEOF(utf8Invalid), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
+    { utf8koi8r, WXSIZEOF(utf8koi8r), _T("koi8-r"), wxFONTENCODING_KOI8 },
+    { utf8iso8859_1, WXSIZEOF(utf8iso8859_1), _T("iso8859-1"), wxFONTENCODING_ISO8859_1 },
+};
 
 static void TestUtf8()
 {
@@ -3873,30 +3921,33 @@ static void TestUtf8()
 
     char buf[1024];
     wchar_t wbuf[1024];
-    if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
-    {
-        wxPuts(_T("ERROR: UTF-8 decoding failed."));
-    }
-    else
+
+    for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
     {
-        wxCSConv conv(_T("koi8-r"));
-        if ( conv.WC2MB(buf, wbuf, 0 /* not needed wcslen(wbuf) */) <= 0 )
+        const Utf8Data& u8d = utf8data[n];
+        if ( wxConvUTF8.MB2WC(wbuf, (const char *)u8d.text,
+                              WXSIZEOF(wbuf)) == (size_t)-1 )
         {
-            wxPuts(_T("ERROR: conversion to KOI8-R failed."));
+            wxPuts(_T("ERROR: UTF-8 decoding failed."));
         }
         else
         {
-            wxPrintf(_T("The resulting string (in KOI8-R): %s\n"), buf);
+            wxCSConv conv(u8d.charset);
+            if ( conv.WC2MB(buf, wbuf, WXSIZEOF(buf)) == (size_t)-1 )
+            {
+                wxPrintf(_T("ERROR: conversion to %s failed.\n"), u8d.charset);
+            }
+            else
+            {
+                wxPrintf(_T("String in %s: %s\n"), u8d.charset, buf);
+            }
         }
-    }
 
-    if ( wxConvUTF8.WC2MB(buf, L"à la", WXSIZEOF(buf)) <= 0 )
-    {
-        wxPuts(_T("ERROR: conversion to UTF-8 failed."));
-    }
-    else
-    {
-        wxPrintf(_T("The string in UTF-8: %s\n"), buf);
+        wxString s(wxConvUTF8.cMB2WC((const char *)u8d.text), *wxConvCurrent);
+        if ( s.empty() )
+            s = _T("<< conversion failed >>");
+        wxPrintf(_T("String in current cset: %s\n"), s.c_str());
+
     }
 
     wxPuts(_T(""));
@@ -3909,7 +3960,8 @@ static void TestEncodingConverter()
     // using wxEncodingConverter should give the same result as above
     char buf[1024];
     wchar_t wbuf[1024];
-    if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
+    if ( wxConvUTF8.MB2WC(wbuf, (const char *)utf8koi8r,
+                          WXSIZEOF(utf8koi8r)) == (size_t)-1 )
     {
         wxPuts(_T("ERROR: UTF-8 decoding failed."));
     }
@@ -3918,7 +3970,7 @@ static void TestEncodingConverter()
         wxEncodingConverter ec;
         ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
         ec.Convert(wbuf, buf);
-        wxPrintf(_T("The same string obtained using wxEC: %s\n"), buf);
+        wxPrintf(_T("The same KOI8-R string using wxEC: %s\n"), buf);
     }
 
     wxPuts(_T(""));
@@ -4585,12 +4637,8 @@ for n in range(20):
             wxPrintf(_T(" (ERROR: should be %d)"), wn.dnum);
         }
 
-        wxPrintf(_T(", week in month is %d"), wmon);
-        if ( wmon == wn.wmon )
-        {
-            wxPrintf(_T(" (ok)"));
-        }
-        else
+        wxPrintf(_T(", week in month = %d"), wmon);
+        if ( wmon != wn.wmon )
         {
             wxPrintf(_T(" (ERROR: should be %d)"), wn.wmon);
         }
@@ -4605,14 +4653,22 @@ for n in range(20):
             wxPrintf(_T(" (ERROR: should be %d)"), wn.wmon2);
         }
 
-        wxPrintf(_T(", week in year is %d"), week);
-        if ( week == wn.week )
+        wxPrintf(_T(", week in year = %d"), week);
+        if ( week != wn.week )
         {
-            wxPuts(_T(" (ok)"));
+            wxPrintf(_T(" (ERROR: should be %d)"), wn.week);
         }
-        else
+
+        wxPutchar(_T('\n'));
+
+        wxDateTime dt2(1, wxDateTime::Jan, d.year);
+        dt2.SetToTheWeek(wn.week, dt.GetWeekDay());
+        if ( dt2 != dt )
         {
-            wxPrintf(_T(" (ERROR: should be %d)\n"), wn.week);
+            Date d2;
+            d2.Init(dt2.GetTm());
+            wxPrintf(_T("ERROR: SetToTheWeek() returned %s\n"),
+                     d2.FormatDate().c_str());
         }
     }
 }
@@ -5101,6 +5157,45 @@ static void TestTimeCompatibility()
 
 #endif // TEST_DATETIME
 
+// ----------------------------------------------------------------------------
+// wxTextInput/OutputStream
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_TEXTSTREAM
+
+#include "wx/txtstrm.h"
+#include "wx/wfstream.h"
+
+static void TestTextInputStream()
+{
+    wxPuts(_T("\n*** wxTextInputStream test ***"));
+
+    wxFileInputStream fsIn(_T("testdata.fc"));
+    if ( !fsIn.Ok() )
+    {
+        wxPuts(_T("ERROR: couldn't open file."));
+    }
+    else
+    {
+        wxTextInputStream tis(fsIn);
+
+        size_t line = 1;
+        for ( ;; )
+        {
+            const wxString s = tis.ReadLine();
+
+            // line could be non empty if the last line of the file isn't
+            // terminated with EOL
+            if ( fsIn.Eof() && s.empty() )
+                break;
+
+            wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
+        }
+    }
+}
+
+#endif // TEST_TEXTSTREAM
+
 // ----------------------------------------------------------------------------
 // threads
 // ----------------------------------------------------------------------------
@@ -6387,7 +6482,8 @@ int main(int argc, char **argv)
         TestLongLongConversion();
         TestBitOperations();
         TestLongLongComparison();
-        TestLongLongPrint();
+        TestLongLongToString();
+        TestLongLongPrintf();
     }
 #endif // TEST_LONGLONG
 
@@ -6469,6 +6565,10 @@ int main(int argc, char **argv)
         TestMemoryStream();
 #endif // TEST_STREAMS
 
+#ifdef TEST_TEXTSTREAM
+    TestTextInputStream();
+#endif // TEST_TEXTSTREAM
+
 #ifdef TEST_THREADS
     int nCPUs = wxThread::GetCPUCount();
     wxPrintf(_T("This system has %d CPUs\n"), nCPUs);
@@ -6512,7 +6612,8 @@ int main(int argc, char **argv)
 
         TestTimeZoneBug();
     }
-        TestTimeFormat();
+
+    TestTimeWNumber();
 
     if ( TEST_INTERACTIVE )
         TestDateTimeInteractive();