X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/68ee7c47300b37e408ef9a04ec9f83af78e90817..f3314fbcb44e8840cd3353a7f8e22207bf26c03d:/samples/console/console.cpp?ds=sidebyside diff --git a/samples/console/console.cpp b/samples/console/console.cpp index af0988d9a0..771db68871 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 +//#define TEST_TIME // ============================================================================ // implementation @@ -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; @@ -227,6 +229,7 @@ static void TestDivision() // seed pseudo random generator //srand((unsigned)time(NULL)); + wxLongLong q, r; size_t nTested = 0; for ( size_t n = 0; n < 10000; n++ ) { @@ -234,7 +237,12 @@ static void TestDivision() // 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; + + wxASSERT_MSG( ll == q*l + r, "division failure" ); nTested++; } @@ -304,7 +312,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; @@ -344,7 +352,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)); @@ -820,36 +828,102 @@ static void TestTimeFormat() { puts("\n*** wxDateTime formatting test ***"); - static const char *formatTestFormats[] = + // 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[] = { - "%c", - "Date is %A, %d of %B, in year %Y", - "Date is %x, time is %X", - "Time is %H:%M:%S or %I:%M:%S %p", - "The day of year: %j, the week of year: %W", + { 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[] = { - { }, // unused { 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); d++ ) + for ( size_t d = 0; d < WXSIZEOF(formatTestDates) + 1; d++ ) { puts(""); - wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d].DT(); + wxDateTime dt = d == 0 ? wxDateTime::Now() : formatTestDates[d - 1].DT(); for ( size_t n = 0; n < WXSIZEOF(formatTestFormats); n++ ) { - printf("%s\n", dt.Format(formatTestFormats[n]).c_str()); + 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)"); + } + } } } } @@ -1270,6 +1344,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 // ---------------------------------------------------------------------------- @@ -1289,7 +1376,11 @@ int main(int argc, char **argv) TestPChar(); TestString(); } - TestStringSub(); + if ( 0 ) + { + TestStringSub(); + } + TestStringFormat(); #endif // TEST_STRINGS #ifdef TEST_ARRAYS