X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bc385ba9ebd0185d5bc3dcbf9fb3f35e3eac4d7a..0b190b0f6ae1de468733c1c8f8aad877a7d44792:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 66584979ea..b589b8fd9e 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -30,18 +30,61 @@ // what to test? //#define TEST_ARRAYS +//#define TEST_CMDLINE //#define TEST_DIR +//#define TEST_EXECUTE +#define TEST_FILECONF //#define TEST_LOG //#define TEST_LONGLONG //#define TEST_MIME //#define TEST_STRINGS -#define TEST_THREADS +//#define TEST_THREADS //#define TEST_TIME // ============================================================================ // implementation // ============================================================================ +#ifdef TEST_CMDLINE + +// ---------------------------------------------------------------------------- +// wxCmdLineParser +// ---------------------------------------------------------------------------- + +#include +#include + +static void ShowCmdLine(const wxCmdLineParser& parser) +{ + wxString s = "Input files: "; + + size_t count = parser.GetParamCount(); + for ( size_t param = 0; param < count; param++ ) + { + s << parser.GetParam(param) << ' '; + } + + s << '\n' + << "Verbose:\t" << (parser.Found("v") ? "yes" : "no") << '\n' + << "Quiet:\t" << (parser.Found("q") ? "yes" : "no") << '\n'; + + wxString strVal; + long lVal; + wxDateTime dt; + if ( parser.Found("o", &strVal) ) + s << "Output file:\t" << strVal << '\n'; + if ( parser.Found("i", &strVal) ) + s << "Input dir:\t" << strVal << '\n'; + if ( parser.Found("s", &lVal) ) + s << "Size:\t" << lVal << '\n'; + if ( parser.Found("d", &dt) ) + s << "Date:\t" << dt.FormatISODate() << '\n'; + + wxLogMessage(s); +} + +#endif // TEST_CMDLINE + // ---------------------------------------------------------------------------- // wxDir // ---------------------------------------------------------------------------- @@ -119,6 +162,97 @@ static void TestDirEnum() #endif // TEST_DIR +// ---------------------------------------------------------------------------- +// wxExecute +// ---------------------------------------------------------------------------- + +#ifdef TEST_EXECUTE + +#include + +static void TestExecute() +{ + puts("*** testing wxExecute ***"); + +#ifdef __UNIX__ + #define COMMAND "echo hi" +#elif defined(__WXMSW__) + #define COMMAND "command.com -c 'echo hi'" +#else + #error "no command to exec" +#endif // OS + + if ( wxExecute(COMMAND) == 0 ) + puts("\nOk."); + else + puts("\nError."); +} + +#endif // TEST_EXECUTE + +// ---------------------------------------------------------------------------- +// wxFileConfig +// ---------------------------------------------------------------------------- + +#ifdef TEST_FILECONF + +#include +#include + +static const struct FileConfTestData +{ + const wxChar *name; // value name + const wxChar *value; // the value from the file +} fcTestData[] = +{ + { _T("value1"), _T("one") }, + { _T("value2"), _T("two") }, + { _T("novalue"), _T("default") }, +}; + +static void TestFileConfRead() +{ + puts("*** testing wxFileConfig loading/reading ***"); + + wxFileConfig fileconf(_T("test"), wxEmptyString, + _T("testdata.fc"), wxEmptyString, + wxCONFIG_USE_RELATIVE_PATH); + + // test simple reading + puts("\nReading config file:"); + wxString defValue(_T("default")), value; + for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ ) + { + const FileConfTestData& data = fcTestData[n]; + value = fileconf.Read(data.name, defValue); + printf("\t%s = %s ", data.name, value.c_str()); + if ( value == data.value ) + { + puts("(ok)"); + } + else + { + printf("(ERROR: should be %s)\n", data.value); + } + } + + // test enumerating the entries + puts("\nEnumerating all root entries:"); + long dummy; + wxString name; + bool cont = fileconf.GetFirstEntry(name, dummy); + while ( cont ) + { + printf("\t%s = %s\n", + name.c_str(), + fileconf.Read(name.c_str(), _T("ERROR")).c_str()); + + cont = fileconf.GetNextEntry(name, dummy); + } +} + +#endif // TEST_FILECONF + // ---------------------------------------------------------------------------- // MIME types // ---------------------------------------------------------------------------- @@ -178,6 +312,19 @@ static void TestMimeEnum() #include #include +// 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()) + +#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; @@ -222,14 +369,75 @@ static void TestSpeed() } } -static void TestDivision() +static void TestLongLongConversion() { - puts("*** Testing wxLongLong division ***\n"); + puts("*** Testing wxLongLong conversions ***\n"); + + wxLongLong a; + size_t nTested = 0; + for ( size_t n = 0; n < 100000; n++ ) + { + a = RAND_LL(); - #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3) +#if wxUSE_LONGLONG_NATIVE + wxLongLongNative b(a.GetHi(), a.GetLo()); - // seed pseudo random generator - srand((unsigned)time(NULL)); + wxASSERT_MSG( a == b, "conversions failure" ); +#else + puts("Can't do it without native long long type, test skipped."); + + return; +#endif // wxUSE_LONGLONG_NATIVE + + if ( !(nTested % 1000) ) + { + putchar('.'); + fflush(stdout); + } + + nTested++; + } + + puts(" done!"); +} + +static void TestMultiplication() +{ + puts("*** 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, "multiplication failure" ); +#else // !wxUSE_LONGLONG_NATIVE + puts("Can't do it without native long long type, test skipped."); + + return; +#endif // wxUSE_LONGLONG_NATIVE + + if ( !(nTested % 1000) ) + { + putchar('.'); + fflush(stdout); + } + + nTested++; + } + + puts(" done!"); +} + +static void TestDivision() +{ + puts("*** Testing wxLongLong division ***\n"); wxLongLong q, r; size_t nTested = 0; @@ -244,8 +452,47 @@ static void TestDivision() 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, "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++; + } + + puts(" done!"); +} + +static void TestAddition() +{ + puts("*** 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()), + "addition failure" ); +#else // !wxUSE_LONGLONG_NATIVE + wxASSERT_MSG( c - b == a, "addition failure" ); +#endif // wxUSE_LONGLONG_NATIVE if ( !(nTested % 1000) ) { @@ -257,10 +504,55 @@ static void TestDivision() } puts(" done!"); +} + +static void TestBitOperations() +{ + puts("*** Testing wxLongLong bit operation ***\n"); + + wxLongLong a, c; + size_t nTested = 0; + for ( size_t n = 0; n < 100000; n++ ) + { + a = RAND_LL(); + +#if wxUSE_LONGLONG_NATIVE + for ( size_t n = 0; n < 33; n++ ) + { + wxLongLongNative b(a.GetHi(), a.GetLo()); - #undef MAKE_LL + b >>= n; + c = a >> n; + + wxASSERT_MSG( b == c, "bit shift failure" ); + + b = wxLongLongNative(a.GetHi(), a.GetLo()) << n; + c = a << n; + + wxASSERT_MSG( b == c, "bit shift failure" ); + } + +#else // !wxUSE_LONGLONG_NATIVE + puts("Can't do it without native long long type, test skipped."); + + return; +#endif // wxUSE_LONGLONG_NATIVE + + if ( !(nTested % 1000) ) + { + putchar('.'); + fflush(stdout); + } + + nTested++; + } + + puts(" done!"); } +#undef MAKE_LL +#undef RAND_LL + #endif // TEST_LONGLONG // ---------------------------------------------------------------------------- @@ -693,7 +985,7 @@ def GetMonthWeek(dt): if weekNumMonth < 0: weekNumMonth = weekNumMonth + 53 return weekNumMonth - + def GetLastSundayBefore(dt): if dt.iso_week[2] == 7: return dt @@ -1094,7 +1386,7 @@ static void TestTimeArithmetics() { wxDateSpan span; const char *name; - } testArithmData[] = + } testArithmData[] = { { wxDateSpan::Day(), "day" }, { wxDateSpan::Week(), "week" }, @@ -1102,7 +1394,7 @@ static void TestTimeArithmetics() { 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++ ) @@ -1150,6 +1442,31 @@ static void TestTimeArithmetics() } } +static void TestTimeHolidays() +{ + puts("\n*** testing wxDateTimeHolidayAuthority ***\n"); + + wxDateTime::Tm tm = wxDateTime(29, wxDateTime::May, 2000).GetTm(); + wxDateTime dtStart(1, tm.mon, tm.year), + dtEnd = dtStart.GetLastMonthDay(); + + wxDateTimeArray hol; + wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol); + + const wxChar *format = "%d-%b-%Y (%a)"; + + printf("All holidays between %s and %s:\n", + dtStart.Format(format).c_str(), dtEnd.Format(format).c_str()); + + size_t count = hol.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + printf("\t%s\n", hol[n].Format(format).c_str()); + } + + puts(""); +} + #if 0 // test compatibility with the old wxDate/wxTime classes @@ -1446,6 +1763,38 @@ void PrintArray(const char* name, const wxArrayString& array) #ifdef TEST_STRINGS #include "wx/timer.h" +#include "wx/tokenzr.h" + +static void TestStringConstruction() +{ + puts("*** Testing wxString constructores ***"); + + #define TEST_CTOR(args, res) \ + { \ + wxString s args ; \ + printf("wxString%s = %s ", #args, s.c_str()); \ + if ( s == res ) \ + { \ + puts("(ok)"); \ + } \ + else \ + { \ + printf("(ERROR: should be %s)\n", res); \ + } \ + } + + TEST_CTOR((_T('Z'), 4), _T("ZZZZ")); + TEST_CTOR((_T("Hello"), 4), _T("Hell")); + TEST_CTOR((_T("Hello"), 5), _T("Hello")); + // TEST_CTOR((_T("Hello"), 6), _T("Hello")); -- should give assert failure + + static const wxChar *s = _T("?really!"); + const wxChar *start = wxStrchr(s, _T('r')); + const wxChar *end = wxStrchr(s, _T('!')); + TEST_CTOR((start, end), _T("really")); + + puts(""); +} static void TestString() { @@ -1525,6 +1874,179 @@ static void TestStringFormat() puts(""); } +// returns "not found" for npos, value for all others +static wxString PosToString(size_t res) +{ + wxString s = res == wxString::npos ? wxString(_T("not found")) + : wxString::Format(_T("%u"), res); + return s; +} + +static void TestStringFind() +{ + puts("*** Testing wxString find() functions ***"); + + static const wxChar *strToFind = _T("ell"); + static const struct StringFindTest + { + const wxChar *str; + size_t start, + result; // of searching "ell" in str + } findTestData[] = + { + { _T("Well, hello world"), 0, 1 }, + { _T("Well, hello world"), 6, 7 }, + { _T("Well, hello world"), 9, wxString::npos }, + }; + + for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ ) + { + const StringFindTest& ft = findTestData[n]; + size_t res = wxString(ft.str).find(strToFind, ft.start); + + printf(_T("Index of '%s' in '%s' starting from %u is %s "), + strToFind, ft.str, ft.start, PosToString(res).c_str()); + + size_t resTrue = ft.result; + if ( res == resTrue ) + { + puts(_T("(ok)")); + } + else + { + printf(_T("(ERROR: should be %s)\n"), + PosToString(resTrue).c_str()); + } + } + + puts(""); +} + +// replace TABs with \t and CRs with \n +static wxString MakePrintable(const wxChar *s) +{ + wxString str(s); + (void)str.Replace(_T("\t"), _T("\\t")); + (void)str.Replace(_T("\n"), _T("\\n")); + (void)str.Replace(_T("\r"), _T("\\r")); + + return str; +} + +static void TestStringTokenizer() +{ + puts("*** Testing wxStringTokenizer ***"); + + static const wxChar *modeNames[] = + { + _T("default"), + _T("return empty"), + _T("return all empty"), + _T("with delims"), + _T("like strtok"), + }; + + static const struct StringTokenizerTest + { + const wxChar *str; // string to tokenize + const wxChar *delims; // delimiters to use + size_t count; // count of token + wxStringTokenizerMode mode; // how should we tokenize it + } tokenizerTestData[] = + { + { _T(""), _T(" "), 0 }, + { _T("Hello, world"), _T(" "), 2 }, + { _T("Hello, world "), _T(" "), 2 }, + { _T("Hello, world"), _T(","), 2 }, + { _T("Hello, world!"), _T(",!"), 2 }, + { _T("Hello,, world!"), _T(",!"), 3 }, + { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL }, + { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 }, + { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4 }, + { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY }, + { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL }, + { _T("01/02/99"), _T("/-"), 3 }, + { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS }, + }; + + for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ ) + { + const StringTokenizerTest& tt = tokenizerTestData[n]; + wxStringTokenizer tkz(tt.str, tt.delims, tt.mode); + + size_t count = tkz.CountTokens(); + printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "), + MakePrintable(tt.str).c_str(), + count, + MakePrintable(tt.delims).c_str(), + modeNames[tkz.GetMode()]); + if ( count == tt.count ) + { + puts(_T("(ok)")); + } + else + { + printf(_T("(ERROR: should be %u)\n"), tt.count); + + continue; + } + + // if we emulate strtok(), check that we do it correctly + wxChar *buf, *s, *last; + + if ( tkz.GetMode() == wxTOKEN_STRTOK ) + { + buf = new wxChar[wxStrlen(tt.str) + 1]; + wxStrcpy(buf, tt.str); + + s = wxStrtok(buf, tt.delims, &last); + } + else + { + buf = NULL; + } + + // now show the tokens themselves + size_t count2 = 0; + while ( tkz.HasMoreTokens() ) + { + wxString token = tkz.GetNextToken(); + + printf(_T("\ttoken %u: '%s'"), + ++count2, + MakePrintable(token).c_str()); + + if ( buf ) + { + if ( token == s ) + { + puts(" (ok)"); + } + else + { + printf(" (ERROR: should be %s)\n", s); + } + + s = wxStrtok(NULL, tt.delims, &last); + } + else + { + // nothing to compare with + puts(""); + } + } + + if ( count2 != count ) + { + puts(_T("\tERROR: token count mismatch")); + } + + delete [] buf; + } + + puts(""); +} + #endif // TEST_STRINGS // ---------------------------------------------------------------------------- @@ -1538,6 +2060,46 @@ int main(int argc, char **argv) fprintf(stderr, "Failed to initialize the wxWindows library, aborting."); } +#ifdef TEST_USLEEP + puts("Sleeping for 3 seconds... z-z-z-z-z..."); + wxUsleep(3000); +#endif // TEST_USLEEP + +#ifdef TEST_CMDLINE + static const wxCmdLineEntryDesc cmdLineDesc[] = + { + { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, + { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" }, + + { wxCMD_LINE_OPTION, "o", "output", "output file" }, + { wxCMD_LINE_OPTION, "i", "input", "input dir" }, + { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER }, + { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE }, + + { wxCMD_LINE_PARAM, NULL, NULL, "input file", + wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, + + { wxCMD_LINE_NONE } + }; + + wxCmdLineParser parser(cmdLineDesc, argc, argv); + + switch ( parser.Parse() ) + { + case -1: + wxLogMessage("Help was given, terminating."); + break; + + case 0: + ShowCmdLine(parser); + break; + + default: + wxLogMessage("Syntax error detected, aborting."); + break; + } +#endif // TEST_CMDLINE + #ifdef TEST_STRINGS if ( 0 ) { @@ -1546,9 +2108,12 @@ int main(int argc, char **argv) } if ( 0 ) { + TestStringConstruction(); TestStringSub(); + TestStringFormat(); + TestStringFind(); + TestStringTokenizer(); } - TestStringFormat(); #endif // TEST_STRINGS #ifdef TEST_ARRAYS @@ -1587,6 +2152,14 @@ int main(int argc, char **argv) TestDirEnum(); #endif // TEST_DIR +#ifdef TEST_EXECUTE + TestExecute(); +#endif // TEST_EXECUTE + +#ifdef TEST_FILECONF + TestFileConfRead(); +#endif // TEST_FILECONF + #ifdef TEST_LOG wxString s; for ( size_t n = 0; n < 8000; n++ ) @@ -1627,10 +2200,21 @@ int main(int argc, char **argv) #endif // TEST_THREADS #ifdef TEST_LONGLONG + // seed pseudo random generator + srand((unsigned)time(NULL)); + if ( 0 ) + { TestSpeed(); - if ( 1 ) + } + TestMultiplication(); + if ( 0 ) + { TestDivision(); + TestAddition(); + TestLongLongConversion(); + TestBitOperations(); + } #endif // TEST_LONGLONG #ifdef TEST_MIME @@ -1653,6 +2237,7 @@ int main(int argc, char **argv) TestTimeFormat(); TestTimeArithmetics(); } + TestTimeHolidays(); if ( 0 ) TestInteractive(); #endif // TEST_TIME