-static void TestUtf8()
-{
- puts("*** Testing UTF8 support ***\n");
-
- char buf[1024];
- wchar_t wbuf[1024];
- if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
- {
- puts("ERROR: UTF-8 decoding failed.");
- }
- else
- {
- wxCSConv conv(_T("koi8-r"));
- if ( conv.WC2MB(buf, wbuf, 0 /* not needed wcslen(wbuf) */) <= 0 )
- {
- puts("ERROR: conversion to KOI8-R failed.");
- }
- else
- {
- printf("The resulting string (in KOI8-R): %s\n", buf);
- }
- }
-
- if ( wxConvUTF8.WC2MB(buf, L"Ã la", WXSIZEOF(buf)) <= 0 )
- {
- puts("ERROR: conversion to UTF-8 failed.");
- }
- else
- {
- printf("The string in UTF-8: %s\n", buf);
- }
-
- puts("");
-}
-
-static void TestEncodingConverter()
-{
- wxPuts(_T("*** Testing wxEncodingConverter ***\n"));
-
- // using wxEncodingConverter should give the same result as above
- char buf[1024];
- wchar_t wbuf[1024];
- if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
- {
- puts("ERROR: UTF-8 decoding failed.");
- }
- else
- {
- wxEncodingConverter ec;
- ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
- ec.Convert(wbuf, buf);
- printf("The same string obtained using wxEC: %s\n", buf);
- }
-
- puts("");
-}
-
-#endif // TEST_WCHAR
-
-// ----------------------------------------------------------------------------
-// ZIP stream
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_ZIP
-
-#include "wx/filesys.h"
-#include "wx/fs_zip.h"
-#include "wx/zipstrm.h"
-
-static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
-
-static void TestZipStreamRead()
-{
- puts("*** Testing ZIP reading ***\n");
-
- static const wxChar *filename = _T("foo");
- wxZipInputStream istr(TESTFILE_ZIP, filename);
- printf("Archive size: %u\n", istr.GetSize());
-
- printf("Dumping the file '%s':\n", filename);
- while ( !istr.Eof() )
- {
- putchar(istr.GetC());
- fflush(stdout);
- }
-
- puts("\n----- done ------");
-}
-
-static void DumpZipDirectory(wxFileSystem& fs,
- const wxString& dir,
- const wxString& indent)
-{
- wxString prefix = wxString::Format(_T("%s#zip:%s"),
- TESTFILE_ZIP, dir.c_str());
- wxString wildcard = prefix + _T("/*");
-
- wxString dirname = fs.FindFirst(wildcard, wxDIR);
- while ( !dirname.empty() )
- {
- if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
- {
- wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
-
- break;
- }
-
- wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
-
- DumpZipDirectory(fs, dirname,
- indent + wxString(_T(' '), 4));
-
- dirname = fs.FindNext();
- }
-
- wxString filename = fs.FindFirst(wildcard, wxFILE);
- while ( !filename.empty() )
- {
- if ( !filename.StartsWith(prefix, &filename) )
- {
- wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
-
- break;
- }
-
- wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
-
- filename = fs.FindNext();
- }
-}
-
-static void TestZipFileSystem()
-{
- puts("*** Testing ZIP file system ***\n");
-
- wxFileSystem::AddHandler(new wxZipFSHandler);
- wxFileSystem fs;
- wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
-
- DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
-}
-
-#endif // TEST_ZIP
-
-// ----------------------------------------------------------------------------
-// ZLIB stream
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_ZLIB
-
-#include "wx/zstream.h"
-#include "wx/wfstream.h"
-
-static const wxChar *FILENAME_GZ = _T("test.gz");
-static const char *TEST_DATA = "hello and hello again";
-
-static void TestZlibStreamWrite()
-{
- puts("*** Testing Zlib stream reading ***\n");
-
- wxFileOutputStream fileOutStream(FILENAME_GZ);
- wxZlibOutputStream ostr(fileOutStream, 0);
- printf("Compressing the test string... ");
- ostr.Write(TEST_DATA, sizeof(TEST_DATA));
- if ( !ostr )
- {
- puts("(ERROR: failed)");
- }
- else
- {
- puts("(ok)");
- }
-
- puts("\n----- done ------");
-}
-
-static void TestZlibStreamRead()
-{
- puts("*** Testing Zlib stream reading ***\n");
-
- wxFileInputStream fileInStream(FILENAME_GZ);
- wxZlibInputStream istr(fileInStream);
- printf("Archive size: %u\n", istr.GetSize());
-
- puts("Dumping the file:");
- while ( !istr.Eof() )
- {
- putchar(istr.GetC());
- fflush(stdout);
- }
-
- puts("\n----- done ------");
-}
-
-#endif // TEST_ZLIB
-
-// ----------------------------------------------------------------------------
-// date time
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_DATETIME
-
-#include <math.h>
-
-#include "wx/date.h"
-#include "wx/datetime.h"
-
-// the test data
-struct Date
-{
- wxDateTime::wxDateTime_t day;
- wxDateTime::Month month;
- int year;
- wxDateTime::wxDateTime_t hour, min, sec;
- double jdn;
- wxDateTime::WeekDay wday;
- time_t gmticks, ticks;
-
- void Init(const wxDateTime::Tm& tm)
- {
- day = tm.mday;
- month = tm.mon;
- year = tm.year;
- hour = tm.hour;
- min = tm.min;
- sec = tm.sec;
- jdn = 0.0;
- gmticks = ticks = -1;
- }
-
- wxDateTime DT() const
- { return wxDateTime(day, month, year, hour, min, sec); }
-
- bool SameDay(const wxDateTime::Tm& tm) const
- {
- return day == tm.mday && month == tm.mon && year == tm.year;
- }
-
- wxString Format() const
- {
- wxString s;
- s.Printf("%02d:%02d:%02d %10s %02d, %4d%s",
- hour, min, sec,
- wxDateTime::GetMonthName(month).c_str(),
- day,
- abs(wxDateTime::ConvertYearToBC(year)),
- year > 0 ? "AD" : "BC");
- return s;
- }
-
- wxString FormatDate() const
- {
- wxString s;
- s.Printf("%02d-%s-%4d%s",
- day,
- wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
- abs(wxDateTime::ConvertYearToBC(year)),
- year > 0 ? "AD" : "BC");
- return s;
- }
-};
-
-static const Date testDates[] =
-{
- { 1, wxDateTime::Jan, 1970, 00, 00, 00, 2440587.5, wxDateTime::Thu, 0, -3600 },
- { 21, wxDateTime::Jan, 2222, 00, 00, 00, 2532648.5, wxDateTime::Mon, -1, -1 },
- { 29, wxDateTime::May, 1976, 12, 00, 00, 2442928.0, wxDateTime::Sat, 202219200, 202212000 },
- { 29, wxDateTime::Feb, 1976, 00, 00, 00, 2442837.5, wxDateTime::Sun, 194400000, 194396400 },
- { 1, wxDateTime::Jan, 1900, 12, 00, 00, 2415021.0, wxDateTime::Mon, -1, -1 },
- { 1, wxDateTime::Jan, 1900, 00, 00, 00, 2415020.5, wxDateTime::Mon, -1, -1 },
- { 15, wxDateTime::Oct, 1582, 00, 00, 00, 2299160.5, wxDateTime::Fri, -1, -1 },
- { 4, wxDateTime::Oct, 1582, 00, 00, 00, 2299149.5, wxDateTime::Mon, -1, -1 },
- { 1, wxDateTime::Mar, 1, 00, 00, 00, 1721484.5, wxDateTime::Thu, -1, -1 },
- { 1, wxDateTime::Jan, 1, 00, 00, 00, 1721425.5, wxDateTime::Mon, -1, -1 },
- { 31, wxDateTime::Dec, 0, 00, 00, 00, 1721424.5, wxDateTime::Sun, -1, -1 },
- { 1, wxDateTime::Jan, 0, 00, 00, 00, 1721059.5, wxDateTime::Sat, -1, -1 },
- { 12, wxDateTime::Aug, -1234, 00, 00, 00, 1270573.5, wxDateTime::Fri, -1, -1 },
- { 12, wxDateTime::Aug, -4000, 00, 00, 00, 260313.5, wxDateTime::Sat, -1, -1 },
- { 24, wxDateTime::Nov, -4713, 00, 00, 00, -0.5, wxDateTime::Mon, -1, -1 },
-};
-
-// this test miscellaneous static wxDateTime functions
-static void TestTimeStatic()
-{
- puts("\n*** wxDateTime static methods test ***");
-
- // some info about the current date
- int year = wxDateTime::GetCurrentYear();
- printf("Current year %d is %sa leap one and has %d days.\n",
- year,
- wxDateTime::IsLeapYear(year) ? "" : "not ",
- wxDateTime::GetNumberOfDays(year));
-
- wxDateTime::Month month = wxDateTime::GetCurrentMonth();
- printf("Current month is '%s' ('%s') and it has %d days\n",
- wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(),
- wxDateTime::GetMonthName(month).c_str(),
- wxDateTime::GetNumberOfDays(month));
-
- // leap year logic
- static const size_t nYears = 5;
- static const size_t years[2][nYears] =
- {
- // first line: the years to test
- { 1990, 1976, 2000, 2030, 1984, },
-
- // second line: TRUE if leap, FALSE otherwise
- { FALSE, TRUE, TRUE, FALSE, TRUE }
- };
-
- for ( size_t n = 0; n < nYears; n++ )
- {
- int year = years[0][n];
- bool should = years[1][n] != 0,
- is = wxDateTime::IsLeapYear(year);
-
- printf("Year %d is %sa leap year (%s)\n",
- year,
- is ? "" : "not ",
- should == is ? "ok" : "ERROR");
-
- wxASSERT( should == wxDateTime::IsLeapYear(year) );
- }
-}
-
-// test constructing wxDateTime objects
-static void TestTimeSet()
-{
- puts("\n*** wxDateTime construction test ***");
-
- for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d1 = testDates[n];
- wxDateTime dt = d1.DT();
-
- Date d2;
- d2.Init(dt.GetTm());
-
- wxString s1 = d1.Format(),
- s2 = d2.Format();
-
- printf("Date: %s == %s (%s)\n",
- s1.c_str(), s2.c_str(),
- s1 == s2 ? "ok" : "ERROR");
- }
-}
-
-// test time zones stuff
-static void TestTimeZones()
-{
- puts("\n*** wxDateTime timezone test ***");
-
- wxDateTime now = wxDateTime::Now();
-
- printf("Current GMT time:\t%s\n", now.Format("%c", wxDateTime::GMT0).c_str());
- printf("Unix epoch (GMT):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::GMT0).c_str());
- printf("Unix epoch (EST):\t%s\n", wxDateTime((time_t)0).Format("%c", wxDateTime::EST).c_str());
- printf("Current time in Paris:\t%s\n", now.Format("%c", wxDateTime::CET).c_str());
- printf(" Moscow:\t%s\n", now.Format("%c", wxDateTime::MSK).c_str());
- printf(" New York:\t%s\n", now.Format("%c", wxDateTime::EST).c_str());
-
- wxDateTime::Tm tm = now.GetTm();
- if ( wxDateTime(tm) != now )
- {
- printf("ERROR: got %s instead of %s\n",
- wxDateTime(tm).Format().c_str(), now.Format().c_str());
- }
-}
-
-// test some minimal support for the dates outside the standard range
-static void TestTimeRange()
-{
- puts("\n*** wxDateTime out-of-standard-range dates test ***");
-
- static const char *fmt = "%d-%b-%Y %H:%M:%S";
-
- printf("Unix epoch:\t%s\n",
- wxDateTime(2440587.5).Format(fmt).c_str());
- printf("Feb 29, 0: \t%s\n",
- wxDateTime(29, wxDateTime::Feb, 0).Format(fmt).c_str());
- printf("JDN 0: \t%s\n",
- wxDateTime(0.0).Format(fmt).c_str());
- printf("Jan 1, 1AD:\t%s\n",
- wxDateTime(1, wxDateTime::Jan, 1).Format(fmt).c_str());
- printf("May 29, 2099:\t%s\n",
- wxDateTime(29, wxDateTime::May, 2099).Format(fmt).c_str());
-}
-
-static void TestTimeTicks()
-{
- puts("\n*** wxDateTime ticks test ***");
-
- for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d = testDates[n];
- if ( d.ticks == -1 )
- continue;
-
- wxDateTime dt = d.DT();
- long ticks = (dt.GetValue() / 1000).ToLong();
- printf("Ticks of %s:\t% 10ld", d.Format().c_str(), ticks);
- if ( ticks == d.ticks )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %ld, delta = %ld)\n",
- d.ticks, ticks - d.ticks);
- }
-
- dt = d.DT().ToTimezone(wxDateTime::GMT0);
- ticks = (dt.GetValue() / 1000).ToLong();
- printf("GMtks of %s:\t% 10ld", d.Format().c_str(), ticks);
- if ( ticks == d.gmticks )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %ld, delta = %ld)\n",
- d.gmticks, ticks - d.gmticks);
- }
- }
-
- puts("");
-}
-
-// test conversions to JDN &c
-static void TestTimeJDN()
-{
- puts("\n*** wxDateTime to JDN test ***");
-
- for ( size_t n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d = testDates[n];
- wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
- double jdn = dt.GetJulianDayNumber();
-
- printf("JDN of %s is:\t% 15.6f", d.Format().c_str(), jdn);
- if ( jdn == d.jdn )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %f, delta = %f)\n",
- d.jdn, jdn - d.jdn);
- }
- }
-}
-
-// test week days computation
-static void TestTimeWDays()
-{
- puts("\n*** wxDateTime weekday test ***");
-
- // test GetWeekDay()
- size_t n;
- for ( n = 0; n < WXSIZEOF(testDates); n++ )
- {
- const Date& d = testDates[n];
- wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
-
- wxDateTime::WeekDay wday = dt.GetWeekDay();
- printf("%s is: %s",
- d.Format().c_str(),
- wxDateTime::GetWeekDayName(wday).c_str());
- if ( wday == d.wday )
- {
- puts(" (ok)");
- }
- else
- {
- printf(" (ERROR: should be %s)\n",
- wxDateTime::GetWeekDayName(d.wday).c_str());
- }
- }
-
- puts("");
-
- // test SetToWeekDay()
- struct WeekDateTestData
- {
- Date date; // the real date (precomputed)
- int nWeek; // its week index in the month
- wxDateTime::WeekDay wday; // the weekday
- wxDateTime::Month month; // the month
- int year; // and the year
-
- wxString Format() const
- {
- wxString s, which;
- switch ( nWeek < -1 ? -nWeek : nWeek )
- {
- case 1: which = "first"; break;
- case 2: which = "second"; break;
- case 3: which = "third"; break;
- case 4: which = "fourth"; break;
- case 5: which = "fifth"; break;
-
- case -1: which = "last"; break;
- }
-
- if ( nWeek < -1 )
- {
- which += " from end";
- }
-
- s.Printf("The %s %s of %s in %d",
- which.c_str(),
- wxDateTime::GetWeekDayName(wday).c_str(),
- wxDateTime::GetMonthName(month).c_str(),
- year);
-
- return s;
- }
- };
-
- // the array data was generated by the following python program
- /*
-from DateTime import *
-from whrandom import *
-from string import *
-
-monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
-wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
-
-week = DateTimeDelta(7)
-
-for n in range(20):
- year = randint(1900, 2100)
- month = randint(1, 12)
- day = randint(1, 28)
- dt = DateTime(year, month, day)
- wday = dt.day_of_week
-
- countFromEnd = choice([-1, 1])
- weekNum = 0;
-
- while dt.month is month:
- dt = dt - countFromEnd * week
- weekNum = weekNum + countFromEnd
-
- data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'wday': wdayNames[wday] }
-
- print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)d, "\
- "wxDateTime::%(wday)s, wxDateTime::%(month)s, %(year)d }," % data
- */
-
- static const WeekDateTestData weekDatesTestData[] =
- {
- { { 20, wxDateTime::Mar, 2045 }, 3, wxDateTime::Mon, wxDateTime::Mar, 2045 },
- { { 5, wxDateTime::Jun, 1985 }, -4, wxDateTime::Wed, wxDateTime::Jun, 1985 },
- { { 12, wxDateTime::Nov, 1961 }, -3, wxDateTime::Sun, wxDateTime::Nov, 1961 },
- { { 27, wxDateTime::Feb, 2093 }, -1, wxDateTime::Fri, wxDateTime::Feb, 2093 },
- { { 4, wxDateTime::Jul, 2070 }, -4, wxDateTime::Fri, wxDateTime::Jul, 2070 },
- { { 2, wxDateTime::Apr, 1906 }, -5, wxDateTime::Mon, wxDateTime::Apr, 1906 },
- { { 19, wxDateTime::Jul, 2023 }, -2, wxDateTime::Wed, wxDateTime::Jul, 2023 },
- { { 5, wxDateTime::May, 1958 }, -4, wxDateTime::Mon, wxDateTime::May, 1958 },
- { { 11, wxDateTime::Aug, 1900 }, 2, wxDateTime::Sat, wxDateTime::Aug, 1900 },
- { { 14, wxDateTime::Feb, 1945 }, 2, wxDateTime::Wed, wxDateTime::Feb, 1945 },
- { { 25, wxDateTime::Jul, 1967 }, -1, wxDateTime::Tue, wxDateTime::Jul, 1967 },
- { { 9, wxDateTime::May, 1916 }, -4, wxDateTime::Tue, wxDateTime::May, 1916 },
- { { 20, wxDateTime::Jun, 1927 }, 3, wxDateTime::Mon, wxDateTime::Jun, 1927 },
- { { 2, wxDateTime::Aug, 2000 }, 1, wxDateTime::Wed, wxDateTime::Aug, 2000 },
- { { 20, wxDateTime::Apr, 2044 }, 3, wxDateTime::Wed, wxDateTime::Apr, 2044 },
- { { 20, wxDateTime::Feb, 1932 }, -2, wxDateTime::Sat, wxDateTime::Feb, 1932 },
- { { 25, wxDateTime::Jul, 2069 }, 4, wxDateTime::Thu, wxDateTime::Jul, 2069 },
- { { 3, wxDateTime::Apr, 1925 }, 1, wxDateTime::Fri, wxDateTime::Apr, 1925 },
- { { 21, wxDateTime::Mar, 2093 }, 3, wxDateTime::Sat, wxDateTime::Mar, 2093 },
- { { 3, wxDateTime::Dec, 2074 }, -5, wxDateTime::Mon, wxDateTime::Dec, 2074 },
- };