X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/239446b4150e55c1dfed186340f144d78c2f1cc9..ff83a7701caa5615f8a2cee0ce2cc216a22af19a:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index ab1b4c9ffb..cd100faafb 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -32,11 +32,11 @@ //#define TEST_ARRAYS //#define TEST_DIR //#define TEST_LOG +//#define TEST_LONGLONG //#define TEST_MIME //#define TEST_STRINGS //#define TEST_THREADS #define TEST_TIME -//#define TEST_LONGLONG // ============================================================================ // implementation @@ -144,8 +144,8 @@ static void TestMimeEnum() wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]); if ( !filetype ) { - printf("nothing known about the filetype '%s'!\n", - mimetypes[n].c_str()); + printf("nothing known about the filetype '%s'!\n", + mimetypes[n].c_str()); continue; } @@ -195,17 +195,19 @@ static void TestSpeed() printf("Summing longs took %ld milliseconds.\n", sw.Time()); } +#if wxUSE_LONGLONG_NATIVE { wxStopWatch sw; - __int64 l = 0; + wxLongLong_t l = 0; for ( n = 0; n < max; n++ ) { l += n; } - printf("Summing __int64s took %ld milliseconds.\n", sw.Time()); + printf("Summing wxLongLong_t took %ld milliseconds.\n", sw.Time()); } +#endif // wxUSE_LONGLONG_NATIVE { wxStopWatch sw; @@ -222,24 +224,39 @@ static void TestSpeed() static void TestDivision() { + puts("*** Testing wxLongLong division ***\n"); + #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3) // seed pseudo random generator - //srand((unsigned)time(NULL)); + srand((unsigned)time(NULL)); + wxLongLong q, r; size_t nTested = 0; - for ( size_t n = 0; n < 10000; n++ ) + 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()); - wxASSERT( (ll * 1000l)/1000l == ll ); + // get a random long (not wxLongLong for now) to divide it with + long l = rand(); + q = ll / l; + r = ll % l; + + // verify the result + wxASSERT_MSG( ll == q*l + r, "division failure" ); + + if ( !(nTested % 1000) ) + { + putchar('.'); + fflush(stdout); + } nTested++; } - printf("\n*** Tested %u divisions/multiplications: ok\n", nTested); + puts(" done!"); #undef MAKE_LL } @@ -252,6 +269,8 @@ static void TestDivision() #ifdef TEST_TIME +#include + #include // the test data @@ -302,7 +321,7 @@ struct Date wxString s; s.Printf("%02d-%s-%4d%s", day, - wxDateTime::GetMonthName(month, TRUE).c_str(), + wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(), abs(wxDateTime::ConvertYearToBC(year)), year > 0 ? "AD" : "BC"); return s; @@ -342,7 +361,7 @@ static void TestTimeStatic() wxDateTime::Month month = wxDateTime::GetCurrentMonth(); printf("Current month is '%s' ('%s') and it has %d days\n", - wxDateTime::GetMonthName(month, TRUE).c_str(), + wxDateTime::GetMonthName(month, wxDateTime::Name_Abbr).c_str(), wxDateTime::GetMonthName(month).c_str(), wxDateTime::GetNumberOfDays(month)); @@ -407,6 +426,13 @@ static void TestTimeZones() 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 @@ -585,10 +611,10 @@ for n in range(20): 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[] = { @@ -647,7 +673,9 @@ static void TestTimeWNumber() struct WeekNumberTestData { Date date; // the date - wxDateTime::wxDateTime_t week; // the week number + wxDateTime::wxDateTime_t week; // the week number in the year + wxDateTime::wxDateTime_t wmon; // the week number in the month + wxDateTime::wxDateTime_t wmon2; // same but week starts with Sun wxDateTime::wxDateTime_t dnum; // day number in the year }; @@ -660,6 +688,18 @@ from string import * monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] wdayNames = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ] +def GetMonthWeek(dt): + weekNumMonth = dt.iso_week[1] - DateTime(dt.year, dt.month, 1).iso_week[1] + 1 + if weekNumMonth < 0: + weekNumMonth = weekNumMonth + 53 + return weekNumMonth + +def GetLastSundayBefore(dt): + if dt.iso_week[2] == 7: + return dt + else: + return dt - DateTimeDelta(dt.iso_week[2]) + for n in range(20): year = randint(1900, 2100) month = randint(1, 12) @@ -667,34 +707,54 @@ for n in range(20): dt = DateTime(year, month, day) dayNum = dt.day_of_year weekNum = dt.iso_week[1] - - data = { 'day': rjust(`day`, 2), 'month': monthNames[month - 1], 'year': year, 'weekNum': rjust(`weekNum`, 2), 'dayNum': rjust(`dayNum`, 3) } - - print "{ { %(day)s, wxDateTime::%(month)s, %(year)d }, %(weekNum)s, "\ + weekNumMonth = GetMonthWeek(dt) + + weekNumMonth2 = 0 + dtSunday = GetLastSundayBefore(dt) + + while dtSunday >= GetLastSundayBefore(DateTime(dt.year, dt.month, 1)): + weekNumMonth2 = weekNumMonth2 + 1 + dtSunday = dtSunday - DateTimeDelta(7) + + data = { 'day': rjust(`day`, 2), \ + 'month': monthNames[month - 1], \ + 'year': year, \ + 'weekNum': rjust(`weekNum`, 2), \ + 'weekNumMonth': weekNumMonth, \ + 'weekNumMonth2': weekNumMonth2, \ + 'dayNum': rjust(`dayNum`, 3) } + + print " { { %(day)s, "\ + "wxDateTime::%(month)s, "\ + "%(year)d }, "\ + "%(weekNum)s, "\ + "%(weekNumMonth)s, "\ + "%(weekNumMonth2)s, "\ "%(dayNum)s }," % data + */ static const WeekNumberTestData weekNumberTestDates[] = { - { { 2, wxDateTime::Jul, 2093 }, 27, 183 }, - { { 25, wxDateTime::Jun, 1986 }, 26, 176 }, - { { 15, wxDateTime::Jun, 2014 }, 24, 166 }, - { { 20, wxDateTime::Jul, 2018 }, 29, 201 }, - { { 3, wxDateTime::Aug, 2074 }, 31, 215 }, - { { 26, wxDateTime::Jul, 2012 }, 30, 208 }, - { { 4, wxDateTime::Nov, 1915 }, 44, 308 }, - { { 11, wxDateTime::Feb, 2035 }, 6, 42 }, - { { 15, wxDateTime::Feb, 1942 }, 7, 46 }, - { { 5, wxDateTime::Jan, 2087 }, 1, 5 }, - { { 6, wxDateTime::Nov, 2016 }, 44, 311 }, - { { 6, wxDateTime::Jun, 2057 }, 23, 157 }, - { { 25, wxDateTime::Feb, 1976 }, 9, 56 }, - { { 12, wxDateTime::Jan, 2073 }, 2, 12 }, - { { 12, wxDateTime::Sep, 2040 }, 37, 256 }, - { { 15, wxDateTime::Jul, 1931 }, 29, 196 }, - { { 23, wxDateTime::Mar, 2084 }, 12, 83 }, - { { 12, wxDateTime::Dec, 1970 }, 50, 346 }, - { { 6, wxDateTime::Sep, 1996 }, 36, 250 }, - { { 7, wxDateTime::Jan, 2076 }, 2, 7 }, + { { 27, wxDateTime::Dec, 1966 }, 52, 5, 5, 361 }, + { { 22, wxDateTime::Jul, 1926 }, 29, 4, 4, 203 }, + { { 22, wxDateTime::Oct, 2076 }, 43, 4, 4, 296 }, + { { 1, wxDateTime::Jul, 1967 }, 26, 1, 1, 182 }, + { { 8, wxDateTime::Nov, 2004 }, 46, 2, 2, 313 }, + { { 21, wxDateTime::Mar, 1920 }, 12, 3, 4, 81 }, + { { 7, wxDateTime::Jan, 1965 }, 1, 2, 2, 7 }, + { { 19, wxDateTime::Oct, 1999 }, 42, 4, 4, 292 }, + { { 13, wxDateTime::Aug, 1955 }, 32, 2, 2, 225 }, + { { 18, wxDateTime::Jul, 2087 }, 29, 3, 3, 199 }, + { { 2, wxDateTime::Sep, 2028 }, 35, 1, 1, 246 }, + { { 28, wxDateTime::Jul, 1945 }, 30, 5, 4, 209 }, + { { 15, wxDateTime::Jun, 1901 }, 24, 3, 3, 166 }, + { { 10, wxDateTime::Oct, 1939 }, 41, 3, 2, 283 }, + { { 3, wxDateTime::Dec, 1965 }, 48, 1, 1, 337 }, + { { 23, wxDateTime::Feb, 1940 }, 8, 4, 4, 54 }, + { { 2, wxDateTime::Jan, 1987 }, 1, 1, 1, 2 }, + { { 11, wxDateTime::Aug, 2079 }, 32, 2, 2, 223 }, + { { 2, wxDateTime::Feb, 2063 }, 5, 1, 1, 33 }, + { { 16, wxDateTime::Oct, 1942 }, 42, 3, 3, 289 }, }; for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ ) @@ -704,8 +764,11 @@ for n in range(20): wxDateTime dt = d.DT(); - wxDateTime::wxDateTime_t week = dt.GetWeekOfYear(), - dnum = dt.GetDayOfYear(); + wxDateTime::wxDateTime_t + week = dt.GetWeekOfYear(wxDateTime::Monday_First), + wmon = dt.GetWeekOfMonth(wxDateTime::Monday_First), + wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First), + dnum = dt.GetDayOfYear(); printf("%s: the day number is %d", d.FormatDate().c_str(), dnum); @@ -718,7 +781,27 @@ for n in range(20): printf(" (ERROR: should be %d)", wn.dnum); } - printf(", week number is %d", week); + printf(", week in month is %d", wmon); + if ( wmon == wn.wmon ) + { + printf(" (ok)"); + } + else + { + printf(" (ERROR: should be %d)", wn.wmon); + } + + printf(" or %d", wmon2); + if ( wmon2 == wn.wmon2 ) + { + printf(" (ok)"); + } + else + { + printf(" (ERROR: should be %d)", wn.wmon2); + } + + printf(", week in year is %d", week); if ( week == wn.week ) { puts(" (ok)"); @@ -789,7 +872,7 @@ static void TestTimeDST() size_t n = year - 1990; const Date& dBegin = datesDST[0][n]; const Date& dEnd = datesDST[1][n]; - + if ( dBegin.SameDay(dtBegin.GetTm()) && dEnd.SameDay(dtEnd.GetTm()) ) { puts(" (ok)"); @@ -813,6 +896,292 @@ static void TestTimeDST() } } +// test wxDateTime -> text conversion +static void TestTimeFormat() +{ + puts("\n*** wxDateTime formatting test ***"); + + // some information may be lost during conversion, so store what kind + // of info should we recover after a round trip + enum CompareKind + { + CompareNone, // don't try comparing + CompareBoth, // dates and times should be identical + CompareDate, // dates only + CompareTime // time only + }; + + static const struct + { + CompareKind compareKind; + const char *format; + } formatTestFormats[] = + { + { CompareBoth, "---> %c" }, + { CompareDate, "Date is %A, %d of %B, in year %Y" }, + { CompareBoth, "Date is %x, time is %X" }, + { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" }, + { CompareNone, "The day of year: %j, the week of year: %W" }, + }; + + static const Date formatTestDates[] = + { + { 29, wxDateTime::May, 1976, 18, 30, 00 }, + { 31, wxDateTime::Dec, 1999, 23, 30, 00 }, +#if 0 + // this test can't work for other centuries because it uses two digit + // years in formats, so don't even try it + { 29, wxDateTime::May, 2076, 18, 30, 00 }, + { 29, wxDateTime::Feb, 2400, 02, 15, 25 }, + { 01, wxDateTime::Jan, -52, 03, 16, 47 }, +#endif + }; + + // an extra test (as it doesn't depend on date, don't do it in the loop) + printf("%s\n", wxDateTime::Now().Format("Our timezone is %Z").c_str()); + + for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ ) + { + puts(""); + + wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT(); + for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ ) + { + wxString s = dt.Format(formatTestFormats[n].format); + printf("%s", s.c_str()); + + // what can we recover? + int kind = formatTestFormats[n].compareKind; + + // convert back + wxDateTime dt2; + const wxChar *result = dt2.ParseFormat(s, formatTestFormats[n].format); + if ( !result ) + { + // converion failed - should it have? + if ( kind == CompareNone ) + puts(" (ok)"); + else + puts(" (ERROR: conversion back failed)"); + } + else if ( *result ) + { + // should have parsed the entire string + puts(" (ERROR: conversion back stopped too soon)"); + } + else + { + bool equal = FALSE; // suppress compilaer warning + switch ( kind ) + { + case CompareBoth: + equal = dt2 == dt; + break; + + case CompareDate: + equal = dt.IsSameDate(dt2); + break; + + case CompareTime: + equal = dt.IsSameTime(dt2); + break; + } + + if ( !equal ) + { + printf(" (ERROR: got back '%s' instead of '%s')\n", + dt2.Format().c_str(), dt.Format().c_str()); + } + else + { + puts(" (ok)"); + } + } + } + } +} + +// test text -> wxDateTime conversion +static void TestTimeParse() +{ + puts("\n*** wxDateTime parse test ***"); + + struct ParseTestData + { + const char *format; + Date date; + bool good; + }; + + static const ParseTestData parseTestDates[] = + { + { "Sat, 18 Dec 1999 00:46:40 +0100", { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE }, + { "Wed, 1 Dec 1999 05:17:20 +0300", { 1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE }, + }; + + for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ ) + { + const char *format = parseTestDates[n].format; + + printf("%s => ", format); + + wxDateTime dt; + if ( dt.ParseRfc822Date(format) ) + { + printf("%s ", dt.Format().c_str()); + + if ( parseTestDates[n].good ) + { + wxDateTime dtReal = parseTestDates[n].date.DT(); + if ( dt == dtReal ) + { + puts("(ok)"); + } + else + { + printf("(ERROR: should be %s)\n", dtReal.Format().c_str()); + } + } + else + { + puts("(ERROR: bad format)"); + } + } + else + { + printf("bad format (%s)\n", + parseTestDates[n].good ? "ERROR" : "ok"); + } + } +} + +static void TestInteractive() +{ + puts("\n*** interactive wxDateTime tests ***"); + + char buf[128]; + + for ( ;; ) + { + printf("Enter a date: "); + if ( !fgets(buf, WXSIZEOF(buf), stdin) ) + break; + + wxDateTime dt; + if ( !dt.ParseDate(buf) ) + { + puts("failed to parse the date"); + + continue; + } + + printf("%s: day %u, week of month %u/%u, week of year %u\n", + dt.FormatISODate().c_str(), + dt.GetDayOfYear(), + dt.GetWeekOfMonth(wxDateTime::Monday_First), + dt.GetWeekOfMonth(wxDateTime::Sunday_First), + dt.GetWeekOfYear(wxDateTime::Monday_First)); + } + + puts("\n*** done ***"); +} + +static void TestTimeArithmetics() +{ + puts("\n*** testing arithmetic operations on wxDateTime ***"); + + static const struct + { + wxDateSpan span; + const char *name; + } testArithmData[] = + { + { wxDateSpan::Day(), "day" }, + { wxDateSpan::Week(), "week" }, + { wxDateSpan::Month(), "month" }, + { wxDateSpan::Year(), "year" }, + { wxDateSpan(1, 2, 3, 4), "year, 2 months, 3 weeks, 4 days" }, + }; + + wxDateTime dt(29, wxDateTime::Dec, 1999), dt1, dt2; + + for ( size_t n = 0; n < WXSIZEOF(testArithmData); n++ ) + { + wxDateSpan span = testArithmData[n].span; + dt1 = dt + span; + dt2 = dt - span; + + const char *name = testArithmData[n].name; + printf("%s + %s = %s, %s - %s = %s\n", + dt.FormatISODate().c_str(), name, dt1.FormatISODate().c_str(), + dt.FormatISODate().c_str(), name, dt2.FormatISODate().c_str()); + + printf("Going back: %s", (dt1 - span).FormatISODate().c_str()); + if ( dt1 - span == dt ) + { + puts(" (ok)"); + } + else + { + printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str()); + } + + printf("Going forward: %s", (dt2 + span).FormatISODate().c_str()); + if ( dt2 + span == dt ) + { + puts(" (ok)"); + } + else + { + printf(" (ERROR: should be %s)\n", dt.FormatISODate().c_str()); + } + + printf("Double increment: %s", (dt2 + 2*span).FormatISODate().c_str()); + if ( dt2 + 2*span == dt1 ) + { + puts(" (ok)"); + } + else + { + printf(" (ERROR: should be %s)\n", dt2.FormatISODate().c_str()); + } + + puts(""); + } +} + +#if 0 + +// test compatibility with the old wxDate/wxTime classes +static void TestTimeCompatibility() +{ + puts("\n*** wxDateTime compatibility test ***"); + + printf("wxDate for JDN 0: %s\n", wxDate(0l).FormatDate().c_str()); + printf("wxDate for MJD 0: %s\n", wxDate(2400000).FormatDate().c_str()); + + double jdnNow = wxDateTime::Now().GetJDN(); + long jdnMidnight = (long)(jdnNow - 0.5); + printf("wxDate for today: %s\n", wxDate(jdnMidnight).FormatDate().c_str()); + + jdnMidnight = wxDate().Set().GetJulianDate(); + printf("wxDateTime for today: %s\n", + wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str()); + + int flags = wxEUROPEAN;//wxFULL; + wxDate date; + date.Set(); + printf("Today is %s\n", date.FormatDate(flags).c_str()); + for ( int n = 0; n < 7; n++ ) + { + printf("Previous %s is %s\n", + wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n), + date.Previous(n + 1).FormatDate(flags).c_str()); + } +} + +#endif // 0 + #endif // TEST_TIME // ---------------------------------------------------------------------------- @@ -1143,6 +1512,19 @@ static void TestStringSub() puts(""); } +static void TestStringFormat() +{ + puts("*** Testing wxString formatting ***"); + + wxString s; + s.Printf("%03d", 18); + + printf("Number 18: %s\n", wxString::Format("%03d", 18).c_str()); + printf("Number 18: %s\n", s.c_str()); + + puts(""); +} + #endif // TEST_STRINGS // ---------------------------------------------------------------------------- @@ -1162,7 +1544,11 @@ int main(int argc, char **argv) TestPChar(); TestString(); } - TestStringSub(); + if ( 0 ) + { + TestStringSub(); + } + TestStringFormat(); #endif // TEST_STRINGS #ifdef TEST_ARRAYS @@ -1254,16 +1640,21 @@ int main(int argc, char **argv) #ifdef TEST_TIME if ( 0 ) { - TestTimeSet(); - TestTimeStatic(); - TestTimeZones(); - TestTimeRange(); - TestTimeTicks(); - TestTimeJDN(); - TestTimeDST(); - TestTimeWDays(); + TestTimeSet(); + TestTimeStatic(); + TestTimeRange(); + TestTimeZones(); + TestTimeTicks(); + TestTimeJDN(); + TestTimeDST(); + TestTimeWDays(); + TestTimeWNumber(); + TestTimeParse(); + TestTimeFormat(); + TestTimeArithmetics(); } - TestTimeWNumber(); + if ( 0 ) + TestInteractive(); #endif // TEST_TIME wxUninitialize();