- int lang = gs_localeDefault.GetSystemLanguage();
- wxPrintf(_T("Locale for '%s' is %s.\n"),
- langStr ? langStr : _T("system default"), GetLangName(lang));
- }
-}
-
-#endif // TEST_LOCALE
-
-// ----------------------------------------------------------------------------
-// MIME types
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_MIME
-
-#include "wx/mimetype.h"
-
-static void TestMimeEnum()
-{
- wxPuts(_T("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
-
- wxArrayString mimetypes;
-
- size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
-
- wxPrintf(_T("*** All %u known filetypes: ***\n"), count);
-
- wxArrayString exts;
- wxString desc;
-
- for ( size_t n = 0; n < count; n++ )
- {
- wxFileType *filetype =
- wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
- if ( !filetype )
- {
- wxPrintf(_T("nothing known about the filetype '%s'!\n"),
- mimetypes[n].c_str());
- continue;
- }
-
- filetype->GetDescription(&desc);
- filetype->GetExtensions(exts);
-
- filetype->GetIcon(NULL);
-
- wxString extsAll;
- for ( size_t e = 0; e < exts.GetCount(); e++ )
- {
- if ( e > 0 )
- extsAll << _T(", ");
- extsAll += exts[e];
- }
-
- wxPrintf(_T("\t%s: %s (%s)\n"),
- mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
- }
-
- wxPuts(_T(""));
-}
-
-static void TestMimeOverride()
-{
- wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
-
- static const wxChar *mailcap = _T("/tmp/mailcap");
- static const wxChar *mimetypes = _T("/tmp/mime.types");
-
- if ( wxFile::Exists(mailcap) )
- wxPrintf(_T("Loading mailcap from '%s': %s\n"),
- mailcap,
- wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
- else
- wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
- mailcap);
-
- if ( wxFile::Exists(mimetypes) )
- wxPrintf(_T("Loading mime.types from '%s': %s\n"),
- mimetypes,
- wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
- else
- wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
- mimetypes);
-
- wxPuts(_T(""));
-}
-
-static void TestMimeFilename()
-{
- wxPuts(_T("*** Testing MIME type from filename query ***\n"));
-
- static const wxChar *filenames[] =
- {
- _T("readme.txt"),
- _T("document.pdf"),
- _T("image.gif"),
- _T("picture.jpeg"),
- };
-
- for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
- {
- const wxString fname = filenames[n];
- wxString ext = fname.AfterLast(_T('.'));
- wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
- if ( !ft )
- {
- wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
- }
- else
- {
- wxString desc;
- if ( !ft->GetDescription(&desc) )
- desc = _T("<no description>");
-
- wxString cmd;
- if ( !ft->GetOpenCommand(&cmd,
- wxFileType::MessageParameters(fname, _T(""))) )
- cmd = _T("<no command available>");
- else
- cmd = wxString(_T('"')) + cmd + _T('"');
-
- wxPrintf(_T("To open %s (%s) do %s.\n"),
- fname.c_str(), desc.c_str(), cmd.c_str());
-
- delete ft;
- }
- }
-
- wxPuts(_T(""));
-}
-
-static void TestMimeAssociate()
-{
- wxPuts(_T("*** Testing creation of filetype association ***\n"));
-
- wxFileTypeInfo ftInfo(
- _T("application/x-xyz"),
- _T("xyzview '%s'"), // open cmd
- _T(""), // print cmd
- _T("XYZ File"), // description
- _T(".xyz"), // extensions
- NULL // end of extensions
- );
- ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only
-
- wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
- if ( !ft )
- {
- wxPuts(_T("ERROR: failed to create association!"));
- }
- else
- {
- // TODO: read it back
- delete ft;
- }
-
- wxPuts(_T(""));
-}
-
-#endif // TEST_MIME
-
-// ----------------------------------------------------------------------------
-// misc information functions
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_INFO_FUNCTIONS
-
-#include "wx/utils.h"
-
-static void TestDiskInfo()
-{
- wxPuts(_T("*** Testing wxGetDiskSpace() ***"));
-
- for ( ;; )
- {
- wxChar pathname[128];
- wxPrintf(_T("\nEnter a directory name: "));
- if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
- break;
-
- // kill the last '\n'
- pathname[wxStrlen(pathname) - 1] = 0;
-
- wxLongLong total, free;
- if ( !wxGetDiskSpace(pathname, &total, &free) )
- {
- wxPuts(_T("ERROR: wxGetDiskSpace failed."));
- }
- else
- {
- wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
- (total / 1024).ToString().c_str(),
- (free / 1024).ToString().c_str(),
- pathname);
- }
- }
-}
-
-static void TestOsInfo()
-{
- wxPuts(_T("*** Testing OS info functions ***\n"));
-
- int major, minor;
- wxGetOsVersion(&major, &minor);
- wxPrintf(_T("Running under: %s, version %d.%d\n"),
- wxGetOsDescription().c_str(), major, minor);
-
- wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory());
-
- wxPrintf(_T("Host name is %s (%s).\n"),
- wxGetHostName().c_str(), wxGetFullHostName().c_str());
-
- wxPuts(_T(""));
-}
-
-static void TestUserInfo()
-{
- wxPuts(_T("*** Testing user info functions ***\n"));
-
- wxPrintf(_T("User id is:\t%s\n"), wxGetUserId().c_str());
- wxPrintf(_T("User name is:\t%s\n"), wxGetUserName().c_str());
- wxPrintf(_T("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
- wxPrintf(_T("Email address:\t%s\n"), wxGetEmailAddress().c_str());
-
- wxPuts(_T(""));
-}
-
-#endif // TEST_INFO_FUNCTIONS
-
-// ----------------------------------------------------------------------------
-// long long
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_LONGLONG
-
-#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())
-
-static const long testLongs[] =
-{
- 0,
- 1,
- -1,
- LONG_MAX,
- LONG_MIN,
- 0x1234,
- -0x1234
-};
-
-#if wxUSE_LONGLONG_WX
-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_WX
-
-static void TestSpeed()
-{
- static const long max = 100000000;
- long n;
-
- {
- wxStopWatch sw;
-
- long l = 0;
- for ( n = 0; n < max; n++ )
- {
- l += n;
- }
-
- wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw.Time());
- }
-
-#if wxUSE_LONGLONG_NATIVE
- {
- wxStopWatch sw;
-
- wxLongLong_t l = 0;
- for ( n = 0; n < max; n++ )
- {
- l += n;
- }
-
- wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw.Time());
- }
-#endif // wxUSE_LONGLONG_NATIVE
-
- {
- wxStopWatch sw;
-
- wxLongLong l;
- for ( n = 0; n < max; n++ )
- {
- l += n;
- }
-
- wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw.Time());
- }
-}
-
-static void TestLongLongConversion()
-{
- wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
-
- wxLongLong a;
- size_t nTested = 0;
- for ( size_t n = 0; n < 100000; n++ )
- {
- a = RAND_LL();
-
-#if wxUSE_LONGLONG_NATIVE
- wxLongLongNative b(a.GetHi(), a.GetLo());
-
- wxASSERT_MSG( a == b, _T("conversions failure") );
-#else
- wxPuts(_T("Can't do it without native long long type, test skipped."));
-
- return;
-#endif // wxUSE_LONGLONG_NATIVE
-
- if ( !(nTested % 1000) )
- {
- putchar('.');
- fflush(stdout);
- }
-
- nTested++;
- }
-
- wxPuts(_T(" done!"));
-}
-
-static void TestMultiplication()
-{
- wxPuts(_T("*** 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, _T("multiplication failure") );
-#else // !wxUSE_LONGLONG_NATIVE
- wxPuts(_T("Can't do it without native long long type, test skipped."));
-
- return;
-#endif // wxUSE_LONGLONG_NATIVE
-
- if ( !(nTested % 1000) )
- {
- putchar('.');
- fflush(stdout);
- }
-
- nTested++;
- }
-
- wxPuts(_T(" done!"));
-}
-
-static void TestDivision()
-{
- wxPuts(_T("*** Testing wxLongLong division ***\n"));
-
- wxLongLong q, r;
- size_t nTested = 0;
- for ( size_t n = 0; n < 100000; n++ )
- {
- // get a random wxLongLong (shifting by 12 the MSB ensures that the
- // multiplication will not overflow)
- wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
-
- // get a random (but non null) long (not wxLongLong for now) to divide
- // it with
- long l;
- do
- {
- l = rand();
- }
- while ( !l );
-
- 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, _T("division failure") );
-#else // !wxUSE_LONGLONG_NATIVE
- // verify the result
- wxASSERT_MSG( ll == q*l + r, "division failure" );
-#endif // wxUSE_LONGLONG_NATIVE
-
- if ( !(nTested % 1000) )
- {
- putchar('.');
- fflush(stdout);
- }
-
- nTested++;
- }
-
- wxPuts(_T(" done!"));
-}
-
-static void TestAddition()
-{
- wxPuts(_T("*** 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()),
- _T("addition failure") );
-#else // !wxUSE_LONGLONG_NATIVE
- wxASSERT_MSG( c - b == a, "addition failure" );
-#endif // wxUSE_LONGLONG_NATIVE
-
- if ( !(nTested % 1000) )
- {
- putchar('.');
- fflush(stdout);
- }
-
- nTested++;
- }
-
- wxPuts(_T(" done!"));
-}
-
-static void TestBitOperations()
-{
- wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
-
- wxLongLong ll;
- size_t nTested = 0;
- for ( size_t n = 0; n < 100000; n++ )
- {
- ll = RAND_LL();
-
-#if wxUSE_LONGLONG_NATIVE
- for ( size_t n = 0; n < 33; n++ )
- {
- }
-#else // !wxUSE_LONGLONG_NATIVE
- wxPuts(_T("Can't do it without native long long type, test skipped."));
-
- return;
-#endif // wxUSE_LONGLONG_NATIVE
-
- if ( !(nTested % 1000) )
- {
- putchar('.');
- fflush(stdout);
- }
-
- nTested++;
- }
-
- wxPuts(_T(" done!"));
-}
-
-static void TestLongLongComparison()
-{
-#if wxUSE_LONGLONG_WX
- wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
-
- static const long ls[2] =
- {
- 0x1234,
- -0x1234,
- };
-
- wxLongLongWx lls[2];
- lls[0] = ls[0];
- lls[1] = ls[1];
-
- for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
- {
- bool res;
-
- for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
- {
- res = lls[m] > testLongs[n];
- wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
- ls[m], testLongs[n], res ? "true" : "false",
- res == (ls[m] > testLongs[n]) ? "ok" : "ERROR");
-
- res = lls[m] < testLongs[n];
- wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
- ls[m], testLongs[n], res ? "true" : "false",
- res == (ls[m] < testLongs[n]) ? "ok" : "ERROR");
-
- res = lls[m] == testLongs[n];
- wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
- ls[m], testLongs[n], res ? "true" : "false",
- res == (ls[m] == testLongs[n]) ? "ok" : "ERROR");
- }
- }
-#endif // wxUSE_LONGLONG_WX
-}
-
-static void TestLongLongToString()
-{
- wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
-
- for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
- {
- wxLongLong ll = testLongs[n];
- wxPrintf(_T("%ld == %s\n"), testLongs[n], ll.ToString().c_str());
- }
-
- wxLongLong ll(0x12345678, 0x87654321);
- wxPrintf(_T("0x1234567887654321 = %s\n"), ll.ToString().c_str());
-
- ll.Negate();
- 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
-
-#endif // TEST_LONGLONG
-
-// ----------------------------------------------------------------------------
-// path list
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_PATHLIST
-
-#ifdef __UNIX__
- #define CMD_IN_PATH _T("ls")
-#else
- #define CMD_IN_PATH _T("command.com")
-#endif
-
-static void TestPathList()
-{
- wxPuts(_T("*** Testing wxPathList ***\n"));
-
- wxPathList pathlist;
- pathlist.AddEnvList(_T("PATH"));
- wxString path = pathlist.FindValidPath(CMD_IN_PATH);
- if ( path.empty() )
- {
- wxPrintf(_T("ERROR: command not found in the path.\n"));
- }
- else
- {
- wxPrintf(_T("Command found in the path as '%s'.\n"), path.c_str());
- }
-}
-
-#endif // TEST_PATHLIST
-
-// ----------------------------------------------------------------------------
-// regular expressions
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_REGEX
-
-#include "wx/regex.h"
-
-static void TestRegExCompile()
-{
- wxPuts(_T("*** Testing RE compilation ***\n"));
-
- static struct RegExCompTestData
- {
- const wxChar *pattern;
- bool correct;
- } regExCompTestData[] =
- {
- { _T("foo"), true },
- { _T("foo("), false },
- { _T("foo(bar"), false },
- { _T("foo(bar)"), true },
- { _T("foo["), false },
- { _T("foo[bar"), false },
- { _T("foo[bar]"), true },
- { _T("foo{"), true },
- { _T("foo{1"), false },
- { _T("foo{bar"), true },
- { _T("foo{1}"), true },
- { _T("foo{1,2}"), true },
- { _T("foo{bar}"), true },
- { _T("foo*"), true },
- { _T("foo**"), false },
- { _T("foo+"), true },
- { _T("foo++"), false },
- { _T("foo?"), true },
- { _T("foo??"), false },
- { _T("foo?+"), false },
- };
-
- wxRegEx re;
- for ( size_t n = 0; n < WXSIZEOF(regExCompTestData); n++ )
- {
- const RegExCompTestData& data = regExCompTestData[n];
- bool ok = re.Compile(data.pattern);
-
- wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
- data.pattern,
- ok ? _T("") : _T("not "),
- ok == data.correct ? _T("ok") : _T("ERROR"));
- }
-}
-
-static void TestRegExMatch()
-{
- wxPuts(_T("*** Testing RE matching ***\n"));
-
- static struct RegExMatchTestData
- {
- const wxChar *pattern;
- const wxChar *text;
- bool correct;
- } regExMatchTestData[] =
- {
- { _T("foo"), _T("bar"), false },
- { _T("foo"), _T("foobar"), true },
- { _T("^foo"), _T("foobar"), true },
- { _T("^foo"), _T("barfoo"), false },
- { _T("bar$"), _T("barbar"), true },
- { _T("bar$"), _T("barbar "), false },
- };
-
- for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ )
- {
- const RegExMatchTestData& data = regExMatchTestData[n];
-
- wxRegEx re(data.pattern);
- bool ok = re.Matches(data.text);
-
- wxPrintf(_T("'%s' %s %s (%s)\n"),
- data.pattern,
- ok ? _T("matches") : _T("doesn't match"),
- data.text,
- ok == data.correct ? _T("ok") : _T("ERROR"));
- }
-}
-
-static void TestRegExSubmatch()
-{
- wxPuts(_T("*** Testing RE subexpressions ***\n"));
-
- wxRegEx re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
- if ( !re.IsValid() )
- {
- wxPuts(_T("ERROR: compilation failed."));
- return;
- }
-
- wxString text = _T("Fri Jul 13 18:37:52 CEST 2001");
-
- if ( !re.Matches(text) )
- {
- wxPuts(_T("ERROR: match expected."));
- }
- else
- {
- wxPrintf(_T("Entire match: %s\n"), re.GetMatch(text).c_str());
-
- wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
- re.GetMatch(text, 3).c_str(),
- re.GetMatch(text, 2).c_str(),
- re.GetMatch(text, 4).c_str(),
- re.GetMatch(text, 1).c_str());
- }
-}
-
-static void TestRegExReplacement()
-{
- wxPuts(_T("*** Testing RE replacement ***"));
-
- static struct RegExReplTestData
- {
- const wxChar *text;
- const wxChar *repl;
- const wxChar *result;
- size_t count;
- } regExReplTestData[] =
- {
- { _T("foo123"), _T("bar"), _T("bar"), 1 },
- { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
- { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
- { _T("123foo"), _T("bar"), _T("123foo"), 0 },
- { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
- { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
- { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
- };
-
- const wxChar *pattern = _T("([a-z]+)[^0-9]*([0-9]+)");
- wxRegEx re(pattern);
-
- wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern);
-
- for ( size_t n = 0; n < WXSIZEOF(regExReplTestData); n++ )
- {
- const RegExReplTestData& data = regExReplTestData[n];
-
- wxString text = data.text;
- size_t nRepl = re.Replace(&text, data.repl);
-
- wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
- data.text, data.repl,
- nRepl, nRepl == 1 ? _T("") : _T("es"),
- text.c_str());
- if ( text == data.result && nRepl == data.count )
- {
- wxPuts(_T("ok)"));
- }
- else
- {
- wxPrintf(_T("ERROR: should be %u and '%s')\n"),
- data.count, data.result);
- }
- }
-}
-
-static void TestRegExInteractive()
-{
- wxPuts(_T("*** Testing RE interactively ***"));
-
- for ( ;; )
- {
- wxChar pattern[128];
- wxPrintf(_T("\nEnter a pattern: "));
- if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
- break;
-
- // kill the last '\n'
- pattern[wxStrlen(pattern) - 1] = 0;
-
- wxRegEx re;
- if ( !re.Compile(pattern) )
- {
- continue;
- }
-
- wxChar text[128];
- for ( ;; )
- {
- wxPrintf(_T("Enter text to match: "));
- if ( !wxFgets(text, WXSIZEOF(text), stdin) )
- break;
-
- // kill the last '\n'
- text[wxStrlen(text) - 1] = 0;
-
- if ( !re.Matches(text) )
- {
- wxPrintf(_T("No match.\n"));
- }
- else
- {
- wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
-
- size_t start, len;
- for ( size_t n = 1; ; n++ )
- {
- if ( !re.GetMatch(&start, &len, n) )
- {
- break;
- }
-
- wxPrintf(_T("Subexpr %u matched '%s'\n"),
- n, wxString(text + start, len).c_str());
- }
- }
- }
- }
-}
-
-#endif // TEST_REGEX
-
-// ----------------------------------------------------------------------------
-// database
-// ----------------------------------------------------------------------------
-
-#if !wxUSE_ODBC
- #undef TEST_ODBC
-#endif
-
-#ifdef TEST_ODBC
-
-#include <wx/db.h>
-
-static void TestDbOpen()
-{
- HENV henv;
- wxDb db(henv);
-}
-
-#endif // TEST_ODBC
-
-// ----------------------------------------------------------------------------
-// printf() tests
-// ----------------------------------------------------------------------------
-
-/*
- NB: this stuff was taken from the glibc test suite and modified to build
- in wxWindows: if I read the copyright below properly, this shouldn't
- be a problem
- */
-
-#ifdef TEST_PRINTF
-
-#ifdef wxTEST_PRINTF
- // use our functions from wxchar.cpp
- #undef wxPrintf
- #undef wxSprintf
-
- // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
- // in the tests below
- int wxPrintf( const wxChar *format, ... );
- int wxSprintf( wxChar *str, const wxChar *format, ... );
-#endif
-
-#include "wx/longlong.h"
-
-#include <float.h>
-
-static void rfg1 (void);
-static void rfg2 (void);
-
-
-static void
-fmtchk (const wxChar *fmt)
-{
- (void) wxPrintf(_T("%s:\t`"), fmt);
- (void) wxPrintf(fmt, 0x12);
- (void) wxPrintf(_T("'\n"));
-}
-
-static void
-fmtst1chk (const wxChar *fmt)
-{
- (void) wxPrintf(_T("%s:\t`"), fmt);
- (void) wxPrintf(fmt, 4, 0x12);
- (void) wxPrintf(_T("'\n"));
-}
-
-static void
-fmtst2chk (const wxChar *fmt)
-{
- (void) wxPrintf(_T("%s:\t`"), fmt);
- (void) wxPrintf(fmt, 4, 4, 0x12);
- (void) wxPrintf(_T("'\n"));
-}
-
-/* This page is covered by the following copyright: */
-
-/* (C) Copyright C E Chew
- *
- * Feel free to copy, use and distribute this software provided:
- *
- * 1. you do not pretend that you wrote it
- * 2. you leave this copyright notice intact.
- */
-
-/*
- * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
- */
-
-#define DEC -123
-#define INT 255
-#define UNS (~0)
-
-/* Formatted Output Test
- *
- * This exercises the output formatting code.
- */
-
-static void
-fp_test (void)
-{
- int i, j, k, l;
- wxChar buf[7];
- wxChar *prefix = buf;
- wxChar tp[20];
-
- wxPuts(_T("\nFormatted output test"));
- wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
- wxStrcpy(prefix, _T("%"));
- for (i = 0; i < 2; i++) {
- for (j = 0; j < 2; j++) {
- for (k = 0; k < 2; k++) {
- for (l = 0; l < 2; l++) {
- wxStrcpy(prefix, _T("%"));
- if (i == 0) wxStrcat(prefix, _T("-"));
- if (j == 0) wxStrcat(prefix, _T("+"));
- if (k == 0) wxStrcat(prefix, _T("#"));
- if (l == 0) wxStrcat(prefix, _T("0"));
- wxPrintf(_T("%5s |"), prefix);
- wxStrcpy(tp, prefix);
- wxStrcat(tp, _T("6d |"));
- wxPrintf(tp, DEC);
- wxStrcpy(tp, prefix);
- wxStrcat(tp, _T("6o |"));
- wxPrintf(tp, INT);
- wxStrcpy(tp, prefix);
- wxStrcat(tp, _T("6x |"));
- wxPrintf(tp, INT);
- wxStrcpy(tp, prefix);
- wxStrcat(tp, _T("6X |"));
- wxPrintf(tp, INT);
- wxStrcpy(tp, prefix);
- wxStrcat(tp, _T("6u |"));
- wxPrintf(tp, UNS);
- wxPrintf(_T("\n"));
- }
- }
- }
- }
- wxPrintf(_T("%10s\n"), (wxChar *) NULL);
- wxPrintf(_T("%-10s\n"), (wxChar *) NULL);
-}
-
-static void TestPrintf()
-{
- static wxChar shortstr[] = _T("Hi, Z.");
- 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"));
- fmtchk(_T("%04x"));
- fmtchk(_T("%4.4x"));
- fmtchk(_T("%04.4x"));
- fmtchk(_T("%4.3x"));
- fmtchk(_T("%04.3x"));
-
- fmtst1chk(_T("%.*x"));
- fmtst1chk(_T("%0*x"));
- fmtst2chk(_T("%*.*x"));
- fmtst2chk(_T("%0*.*x"));
-
- wxPrintf(_T("bad format:\t\"%b\"\n"));
- wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
-
- wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
- wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
- wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
- wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
- wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
- wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
- wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
- wxPrintf(_T("left-adjusted ZLDN:\t\"%-010ld\"\n"), -123456);
- wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
- wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
-
- wxPrintf(_T("zero-padded string:\t\"%010s\"\n"), shortstr);
- wxPrintf(_T("left-adjusted Z string:\t\"%-010s\"\n"), shortstr);
- wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
- wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
- wxPrintf(_T("null string:\t\"%s\"\n"), (wxChar *)NULL);
- wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
-
- wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
- wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
- wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
- wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
- wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
- wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
- wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
- wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
- wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
- wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
- wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
- wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
-
- wxPrintf (_T(" %6.5f\n"), .099999999860301614);
- wxPrintf (_T(" %6.5f\n"), .1);
- wxPrintf (_T("x%5.4fx\n"), .5);
-
- wxPrintf (_T("%#03x\n"), 1);
-
- //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
-
- {
- double d = FLT_MIN;
- int niter = 17;
-
- while (niter-- != 0)
- wxPrintf (_T("%.17e\n"), d / 2);
- fflush (stdout);
- }
-
- wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
-
-#define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
- wxPrintf (FORMAT, 0.0, 0.0, 0.0);
- wxPrintf (FORMAT, 1.0, 1.0, 1.0);
- wxPrintf (FORMAT, -1.0, -1.0, -1.0);
- wxPrintf (FORMAT, 100.0, 100.0, 100.0);
- wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
- wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
- wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
- wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
- wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
-#undef FORMAT
-
- {
- wxChar buf[20];
- int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
-
- wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
- rc, WXSIZEOF(buf), buf);
-#if 0
- wxChar buf2[512];
- wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
- wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
-#endif
- }
-
- fp_test ();
-
- wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
- wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
- wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
- wxPrintf (_T("%g should be 123.456\n"), 123.456);
- wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
- wxPrintf (_T("%g should be 10\n"), 10.0);
- wxPrintf (_T("%g should be 0.02\n"), 0.02);
-
- {
- double x=1.0;
- wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
- }
-
- {
- wxChar buf[200];
-
- wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
-
- result |= wxStrcmp (buf,
- _T("onetwo three "));
-
- wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
- }
-
-#ifdef wxLongLong_t
- {
- wxChar buf[200];
-
- wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
- #if 0
- // for some reason below line fails under Borland
- wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
- #endif
-
- if (wxStrcmp (buf, _T("40000000000")) != 0)
- {
- result = 1;
- wxPuts (_T("\tFAILED"));
- }
- 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);
-
- wxPuts (_T("--- Should be no further output. ---"));
- rfg1 ();
- rfg2 ();
-
-#if 0
- {
- wxChar bytes[7];
- wxChar buf[20];
-
- memset (bytes, '\xff', sizeof bytes);
- wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
- if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
- || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
- {
- wxPuts (_T("%hhn overwrite more bytes"));
- result = 1;
- }
- if (bytes[3] != 3)
- {
- wxPuts (_T("%hhn wrote incorrect value"));
- result = 1;
- }
- }
-#endif
-}
-
-static void
-rfg1 (void)
-{
- wxChar buf[100];
-
- wxSprintf (buf, _T("%5.s"), _T("xyz"));
- if (wxStrcmp (buf, _T(" ")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
- wxSprintf (buf, _T("%5.f"), 33.3);
- if (wxStrcmp (buf, _T(" 33")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
- wxSprintf (buf, _T("%8.e"), 33.3e7);
- if (wxStrcmp (buf, _T(" 3e+08")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
- wxSprintf (buf, _T("%8.E"), 33.3e7);
- if (wxStrcmp (buf, _T(" 3E+08")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
- wxSprintf (buf, _T("%.g"), 33.3);
- if (wxStrcmp (buf, _T("3e+01")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
- wxSprintf (buf, _T("%.G"), 33.3);
- if (wxStrcmp (buf, _T("3E+01")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
-}
-
-static void
-rfg2 (void)
-{
- int prec;
- wxChar buf[100];
-
- prec = 0;
- wxSprintf (buf, _T("%.*g"), prec, 3.3);
- if (wxStrcmp (buf, _T("3")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
- prec = 0;
- wxSprintf (buf, _T("%.*G"), prec, 3.3);
- if (wxStrcmp (buf, _T("3")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
- prec = 0;
- wxSprintf (buf, _T("%7.*G"), prec, 3.33);
- if (wxStrcmp (buf, _T(" 3")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
- prec = 3;
- wxSprintf (buf, _T("%04.*o"), prec, 33);
- if (wxStrcmp (buf, _T(" 041")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
- prec = 7;
- wxSprintf (buf, _T("%09.*u"), prec, 33);
- if (wxStrcmp (buf, _T(" 0000033")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
- prec = 3;
- wxSprintf (buf, _T("%04.*x"), prec, 33);
- if (wxStrcmp (buf, _T(" 021")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
- prec = 3;
- wxSprintf (buf, _T("%04.*X"), prec, 33);
- if (wxStrcmp (buf, _T(" 021")) != 0)
- wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
-}
-
-#endif // TEST_PRINTF
-
-// ----------------------------------------------------------------------------
-// registry and related stuff
-// ----------------------------------------------------------------------------
-
-// this is for MSW only
-#ifndef __WXMSW__
- #undef TEST_REGCONF
- #undef TEST_REGISTRY
-#endif
-
-#ifdef TEST_REGCONF
-
-#include "wx/confbase.h"
-#include "wx/msw/regconf.h"
-
-static void TestRegConfWrite()
-{
- wxConfig *config = new wxConfig("myapp");
- config->SetPath("/group1");
- config->Write("entry1", "foo");
- config->SetPath("/group2");
- config->Write("entry1", "bar");
-}
-
-static void TestRegConfRead()
-{
- wxConfig *config = new wxConfig("myapp");
-
- wxString str;
- long dummy;
- config->SetPath("/");
- puts("Enumerating / subgroups:");
- bool bCont = config->GetFirstGroup(str, dummy);
- while(bCont)
- {
- puts(str);
- bCont = config->GetNextGroup(str, dummy);
- }
-}
-
-#endif // TEST_REGCONF
-
-#ifdef TEST_REGISTRY
-
-#include "wx/msw/registry.h"
-
-// I chose this one because I liked its name, but it probably only exists under
-// NT
-static const wxChar *TESTKEY =
- _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
-
-static void TestRegistryRead()
-{
- wxPuts(_T("*** testing registry reading ***"));
-
- wxRegKey key(TESTKEY);
- wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
- if ( !key.Open() )
- {
- wxPuts(_T("ERROR: test key can't be opened, aborting test."));
-
- return;
- }
-
- size_t nSubKeys, nValues;
- if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
- {
- wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
- }
-
- wxPrintf(_T("Enumerating values:\n"));
-
- long dummy;
- wxString value;
- bool cont = key.GetFirstValue(value, dummy);
- while ( cont )
- {
- wxPrintf(_T("Value '%s': type "), value.c_str());
- switch ( key.GetValueType(value) )
- {
- case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
- case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
- case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
- case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
- case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
- case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
- default: wxPrintf(_T("other (unknown)")); break;
- }
-
- wxPrintf(_T(", value = "));
- if ( key.IsNumericValue(value) )
- {
- long val;
- key.QueryValue(value, &val);
- wxPrintf(_T("%ld"), val);
- }
- else // string
- {
- wxString val;
- key.QueryValue(value, val);
- wxPrintf(_T("'%s'"), val.c_str());
-
- key.QueryRawValue(value, val);
- wxPrintf(_T(" (raw value '%s')"), val.c_str());
- }
-
- putchar('\n');
-
- cont = key.GetNextValue(value, dummy);
- }
-}
-
-static void TestRegistryAssociation()
-{
- /*
- The second call to deleteself genertaes an error message, with a
- messagebox saying .flo is crucial to system operation, while the .ddf
- call also fails, but with no error message
- */
-
- wxRegKey key;
-
- key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
- key.Create();
- key = _T("ddxf_auto_file") ;
- key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
- key.Create();
- key = _T("ddxf_auto_file") ;
- key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
- key.Create();
- key = _T("program,0") ;
- key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
- key.Create();
- key = _T("program \"%1\"") ;
-
- key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
- key.DeleteSelf();
- key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
- key.DeleteSelf();
- key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
- key.DeleteSelf();
- key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
- key.DeleteSelf();
-}
-
-#endif // TEST_REGISTRY
-
-// ----------------------------------------------------------------------------
-// scope guard
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_SCOPEGUARD
-
-#include "wx/scopeguard.h"
-
-static void function0() { puts("function0()"); }
-static void function1(int n) { printf("function1(%d)\n", n); }
-static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
-
-struct Object
-{
- void method0() { printf("method0()\n"); }
- void method1(int n) { printf("method1(%d)\n", n); }
- void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
-};
-
-static void TestScopeGuard()
-{
- ON_BLOCK_EXIT0(function0);
- ON_BLOCK_EXIT1(function1, 17);
- ON_BLOCK_EXIT2(function2, 3.14, 'p');
-
- Object obj;
- ON_BLOCK_EXIT_OBJ0(obj, &Object::method0);
- ON_BLOCK_EXIT_OBJ1(obj, &Object::method1, 7);
- ON_BLOCK_EXIT_OBJ2(obj, &Object::method2, 2.71, 'e');
-
- wxScopeGuard dismissed = wxMakeGuard(function0);
- dismissed.Dismiss();
-}
-
-#endif
-
-// ----------------------------------------------------------------------------
-// sockets
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_SOCKETS
-
-#include "wx/socket.h"
-#include "wx/protocol/protocol.h"
-#include "wx/protocol/http.h"
-
-static void TestSocketServer()
-{
- wxPuts(_T("*** Testing wxSocketServer ***\n"));
-
- static const int PORT = 3000;
-
- wxIPV4address addr;
- addr.Service(PORT);
-
- wxSocketServer *server = new wxSocketServer(addr);
- if ( !server->Ok() )
- {
- wxPuts(_T("ERROR: failed to bind"));
-
- return;
- }
-
- bool quit = false;
- while ( !quit )
- {
- wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
-
- wxSocketBase *socket = server->Accept();
- if ( !socket )
- {
- wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
- break;
- }
-
- wxPuts(_T("Server: got a client."));
-
- server->SetTimeout(60); // 1 min
-
- bool close = false;
- while ( !close && socket->IsConnected() )
- {
- wxString s;
- wxChar ch = _T('\0');
- for ( ;; )
- {
- if ( socket->Read(&ch, sizeof(ch)).Error() )
- {
- // don't log error if the client just close the connection
- if ( socket->IsConnected() )
- {
- wxPuts(_T("ERROR: in wxSocket::Read."));
- }
-
- break;
- }
-
- if ( ch == '\r' )
- continue;
-
- if ( ch == '\n' )
- break;
-
- s += ch;
- }
-
- if ( ch != '\n' )
- {
- break;
- }
-
- wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
- if ( s == _T("close") )
- {
- wxPuts(_T("Closing connection"));
-
- close = true;
- }
- else if ( s == _T("quit") )
- {
- close =
- quit = true;
-
- wxPuts(_T("Shutting down the server"));
- }
- else // not a special command
- {
- socket->Write(s.MakeUpper().c_str(), s.length());
- socket->Write("\r\n", 2);
- wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
- }
- }
-
- if ( !close )
- {
- wxPuts(_T("Server: lost a client unexpectedly."));
- }
-
- socket->Destroy();
- }
-
- // same as "delete server" but is consistent with GUI programs
- server->Destroy();
-}
-
-static void TestSocketClient()
-{
- wxPuts(_T("*** Testing wxSocketClient ***\n"));
-
- static const wxChar *hostname = _T("www.wxwindows.org");
-
- wxIPV4address addr;
- addr.Hostname(hostname);
- addr.Service(80);
-
- wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
-
- wxSocketClient client;
- if ( !client.Connect(addr) )
- {
- wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
- }
- else
- {
- wxPrintf(_T("--- Connected to %s:%u...\n"),
- addr.Hostname().c_str(), addr.Service());
-
- wxChar buf[8192];
-
- // could use simply "GET" here I suppose
- wxString cmdGet =
- wxString::Format(_T("GET http://%s/\r\n"), hostname);
- client.Write(cmdGet, cmdGet.length());
- wxPrintf(_T("--- Sent command '%s' to the server\n"),
- MakePrintable(cmdGet).c_str());
- client.Read(buf, WXSIZEOF(buf));
- wxPrintf(_T("--- Server replied:\n%s"), buf);
- }
-}
-
-#endif // TEST_SOCKETS
-
-// ----------------------------------------------------------------------------
-// FTP
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_FTP
-
-#include "wx/protocol/ftp.h"
-
-static wxFTP ftp;
-
-#define FTP_ANONYMOUS
-
-#ifdef FTP_ANONYMOUS
- static const wxChar *directory = _T("/pub");
- static const wxChar *filename = _T("welcome.msg");
-#else
- static const wxChar *directory = _T("/etc");
- static const wxChar *filename = _T("issue");
-#endif
-
-static bool TestFtpConnect()
-{
- wxPuts(_T("*** Testing FTP connect ***"));
-
-#ifdef FTP_ANONYMOUS
- static const wxChar *hostname = _T("ftp.wxwindows.org");
-
- wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
-#else // !FTP_ANONYMOUS
- static const wxChar *hostname = "localhost";
-
- wxChar user[256];
- wxFgets(user, WXSIZEOF(user), stdin);
- user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
- ftp.SetUser(user);
-
- wxChar password[256];
- wxPrintf(_T("Password for %s: "), password);
- wxFgets(password, WXSIZEOF(password), stdin);
- password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
- ftp.SetPassword(password);
-
- wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
-#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
-
- if ( !ftp.Connect(hostname) )
- {
- wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
-
- return false;
- }
- else
- {
- wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
- hostname, ftp.Pwd().c_str());
- }
-
- return true;
-}
-
-// test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
-static void TestFtpWuFtpd()
-{
- wxFTP ftp;
- static const wxChar *hostname = _T("ftp.eudora.com");
- if ( !ftp.Connect(hostname) )
- {
- wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
- }
- else
- {
- static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
- wxInputStream *in = ftp.GetInputStream(filename);
- if ( !in )
- {
- wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
- }
- else
- {
- size_t size = in->GetSize();
- wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
-
- wxChar *data = new wxChar[size];
- if ( !in->Read(data, size) )
- {
- wxPuts(_T("ERROR: read error"));
- }
- else
- {
- wxPrintf(_T("Successfully retrieved the file.\n"));
- }
-
- delete [] data;
- delete in;
- }
- }
-}
-
-static void TestFtpList()
-{
- wxPuts(_T("*** Testing wxFTP file listing ***\n"));
-
- // test CWD
- if ( !ftp.ChDir(directory) )
- {
- wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
- }
-
- wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
-
- // test NLIST and LIST
- wxArrayString files;
- if ( !ftp.GetFilesList(files) )
- {
- wxPuts(_T("ERROR: failed to get NLIST of files"));
- }
- else
- {
- wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
- size_t count = files.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- wxPrintf(_T("\t%s\n"), files[n].c_str());
- }
- wxPuts(_T("End of the file list"));
- }
-
- if ( !ftp.GetDirList(files) )
- {
- wxPuts(_T("ERROR: failed to get LIST of files"));
- }
- else
- {
- wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
- size_t count = files.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- wxPrintf(_T("\t%s\n"), files[n].c_str());
- }
- wxPuts(_T("End of the file list"));
- }
-
- if ( !ftp.ChDir(_T("..")) )
- {
- wxPuts(_T("ERROR: failed to cd to .."));
- }
-
- wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
-}
-
-static void TestFtpDownload()
-{
- wxPuts(_T("*** Testing wxFTP download ***\n"));
-
- // test RETR
- wxInputStream *in = ftp.GetInputStream(filename);
- if ( !in )
- {
- wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
- }
- else
- {
- size_t size = in->GetSize();
- wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
- fflush(stdout);
-
- wxChar *data = new wxChar[size];
- if ( !in->Read(data, size) )
- {
- wxPuts(_T("ERROR: read error"));
- }
- else
- {
- wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
- }
-
- delete [] data;
- delete in;
- }
-}
-
-static void TestFtpFileSize()
-{
- wxPuts(_T("*** Testing FTP SIZE command ***"));
-
- if ( !ftp.ChDir(directory) )
- {
- wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
- }
-
- wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
-
- if ( ftp.FileExists(filename) )
- {
- int size = ftp.GetFileSize(filename);
- if ( size == -1 )
- wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
- else
- wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
- }
- else
- {
- wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
- }
-}
-
-static void TestFtpMisc()
-{
- wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
-
- if ( ftp.SendCommand(_T("STAT")) != '2' )
- {
- wxPuts(_T("ERROR: STAT failed"));
- }
- else
- {
- wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
- }
-
- if ( ftp.SendCommand(_T("HELP SITE")) != '2' )
- {
- wxPuts(_T("ERROR: HELP SITE failed"));
- }
- else
- {
- wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
- ftp.GetLastResult().c_str());
- }
-}
-
-static void TestFtpInteractive()