1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/interactive/input.cpp
// Purpose: Miscellaneous tests requiring user input
// Author: Francesco Montorsi (extracted from console sample)
// Created: 2010-06-21
// Copyright: (c) 2010 wxWidgets team
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "wx/app.h"
#include "wx/wxcrt.h" // for wxPuts
#include "wx/wxcrtvararg.h" // for wxPrintf
// ----------------------------------------------------------------------------
// conditional compilation
// ----------------------------------------------------------------------------
#define TEST_SNGLINST
#define TEST_FTP
#define TEST_INFO_FUNCTIONS
#if wxUSE_REGEX
#define TEST_REGEX
#endif
#define TEST_DATETIME
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class InteractiveInputTestCase : public CppUnit::TestCase
{
public:
InteractiveInputTestCase() { }
private:
CPPUNIT_TEST_SUITE( InteractiveInputTestCase );
CPPUNIT_TEST( TestSingleIstance );
CPPUNIT_TEST( TestFtpInteractive );
CPPUNIT_TEST( TestDiskInfo );
CPPUNIT_TEST( TestRegExInteractive );
CPPUNIT_TEST( TestDateTimeInteractive );
CPPUNIT_TEST_SUITE_END();
void TestSingleIstance();
void TestFtpInteractive();
void TestDiskInfo();
void TestRegExInteractive();
void TestDateTimeInteractive();
wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase);
};
// ----------------------------------------------------------------------------
// CppUnit macros
// ----------------------------------------------------------------------------
//CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
// do not run this test by default!
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase, "InteractiveInputTestCase" );
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// misc information functions
// ----------------------------------------------------------------------------
#include "wx/utils.h"
void InteractiveInputTestCase::TestDiskInfo()
{
#ifdef TEST_INFO_FUNCTIONS
wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
for ( ;; )
{
wxChar pathname[128];
wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
break;
// kill the last '\n'
pathname[wxStrlen(pathname) - 1] = 0;
if (pathname[0] == '\0' || wxStrcmp(pathname, "quit") == 0)
break;
wxLongLong total, free;
if ( !wxGetDiskSpace(pathname, &total, &free) )
{
wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
}
else
{
wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
(total / 1024).ToString().c_str(),
(free / 1024).ToString().c_str(),
pathname);
}
wxPuts("\n");
}
wxPuts("\n");
#endif // TEST_INFO_FUNCTIONS
}
// ----------------------------------------------------------------------------
// regular expressions
// ----------------------------------------------------------------------------
#include "wx/regex.h"
void InteractiveInputTestCase::TestRegExInteractive()
{
#ifdef TEST_REGEX
wxPuts(wxT("*** Testing RE interactively ***"));
for ( ;; )
{
wxChar pattern[128];
wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
break;
// kill the last '\n'
pattern[wxStrlen(pattern) - 1] = 0;
if (pattern[0] == '\0' || wxStrcmp(pattern, "quit") == 0)
break;
wxRegEx re;
if ( !re.Compile(pattern) )
{
continue;
}
wxChar text[128];
for ( ;; )
{
wxPrintf(wxT("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(wxT("No match.\n"));
}
else
{
wxPrintf(wxT("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(wxT("Subexpr %u matched '%s'\n"),
n, wxString(text + start, len).c_str());
}
}
}
wxPuts("\n");
}
#endif // TEST_REGEX
}
// ----------------------------------------------------------------------------
// FTP
// ----------------------------------------------------------------------------
#include "wx/protocol/ftp.h"
#include "wx/protocol/log.h"
#define FTP_ANONYMOUS
#ifdef FTP_ANONYMOUS
static const wxChar *hostname = wxT("ftp.wxwidgets.org");
#else
static const wxChar *hostname = "localhost";
#endif
void InteractiveInputTestCase::TestFtpInteractive()
{
#ifdef TEST_FTP
wxFTP ftp;
wxPuts(wxT("\n*** Interactive wxFTP test ***"));
#ifdef FTP_ANONYMOUS
wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
#else // !FTP_ANONYMOUS
wxChar user[256];
wxFgets(user, WXSIZEOF(user), stdin);
user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
ftp.SetUser(user);
wxChar password[256];
wxPrintf(wxT("Password for %s: "), password);
wxFgets(password, WXSIZEOF(password), stdin);
password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
ftp.SetPassword(password);
wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
if ( !ftp.Connect(hostname) )
{
wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
return;
}
else
{
wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
hostname, ftp.Pwd().c_str());
}
wxChar buf[128];
for ( ;; )
{
wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
break;
// kill the last '\n'
buf[wxStrlen(buf) - 1] = 0;
if (buf[0] == '\0' || wxStrcmp(buf, "quit") == 0)
break;
// special handling of LIST and NLST as they require data connection
wxString start(buf, 4);
start.MakeUpper();
if ( start == wxT("LIST") || start == wxT("NLST") )
{
wxString wildcard;
if ( wxStrlen(buf) > 4 )
wildcard = buf + 5;
wxArrayString files;
if ( !ftp.GetList(files, wildcard, start == wxT("LIST")) )
{
wxPrintf(wxT("ERROR: failed to get %s of files\n"), start.c_str());
}
else
{
wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
size_t count = files.GetCount();
for ( size_t n = 0; n < count; n++ )
{
wxPrintf(wxT("\t%s\n"), files[n].c_str());
}
wxPuts(wxT("--- End of the file list"));
}
}
else // !list
{
wxChar ch = ftp.SendCommand(buf);
wxPrintf(wxT("Command %s"), ch ? wxT("succeeded") : wxT("failed"));
if ( ch )
{
wxPrintf(wxT(" (return code %c)"), ch);
}
wxPrintf(wxT(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
}
}
wxPuts(wxT("\n"));
#endif // TEST_FTP
}
// ----------------------------------------------------------------------------
// date time
// ----------------------------------------------------------------------------
#include "wx/math.h"
#include "wx/datetime.h"
void InteractiveInputTestCase::TestDateTimeInteractive()
{
#ifdef TEST_DATETIME
wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
wxChar buf[128];
for ( ;; )
{
wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
break;
// kill the last '\n'
buf[wxStrlen(buf) - 1] = 0;
if ( buf[0] == '\0' || wxStrcmp(buf, "quit") == 0 )
break;
wxDateTime dt;
const wxChar *p = dt.ParseDate(buf);
if ( !p )
{
wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf);
continue;
}
else if ( *p )
{
wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p - buf);
}
wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
dt.Format(wxT("%b %d, %Y")).c_str(),
dt.GetDayOfYear(),
dt.GetWeekOfMonth(wxDateTime::Monday_First),
dt.GetWeekOfMonth(wxDateTime::Sunday_First),
dt.GetWeekOfYear(wxDateTime::Monday_First));
}
wxPuts("\n");
#endif // TEST_DATETIME
}
// ----------------------------------------------------------------------------
// single instance
// ----------------------------------------------------------------------------
#include "wx/snglinst.h"
void InteractiveInputTestCase::TestSingleIstance()
{
#ifdef TEST_SNGLINST
wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
wxSingleInstanceChecker checker;
if ( checker.Create(wxT(".wxconsole.lock")) )
{
if ( checker.IsAnotherRunning() )
{
wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
return;
}
// wait some time to give time to launch another instance
wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
wxFgetc(stdin);
}
else // failed to create
{
wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
}
wxPuts("\n");
#endif // defined(TEST_SNGLINST)
}
|