1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/interactive/input.cpp
3 // Purpose: Miscellaneous tests requiring user input
4 // Author: Francesco Montorsi (extracted from console sample)
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
24 #include "wx/wxcrt.h" // for wxPuts
25 #include "wx/wxcrtvararg.h" // for wxPrintf
27 // ----------------------------------------------------------------------------
28 // conditional compilation
29 // ----------------------------------------------------------------------------
33 #define TEST_INFO_FUNCTIONS
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class InteractiveInputTestCase
: public CppUnit::TestCase
46 InteractiveInputTestCase() { }
49 CPPUNIT_TEST_SUITE( InteractiveInputTestCase
);
50 CPPUNIT_TEST( TestSingleIstance
);
51 CPPUNIT_TEST( TestFtpInteractive
);
52 CPPUNIT_TEST( TestDiskInfo
);
53 CPPUNIT_TEST( TestRegExInteractive
);
54 CPPUNIT_TEST( TestDateTimeInteractive
);
55 CPPUNIT_TEST_SUITE_END();
57 void TestSingleIstance();
58 void TestFtpInteractive();
60 void TestRegExInteractive();
61 void TestDateTimeInteractive();
63 wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase
);
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
71 // do not run this test by default!
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase
, "InteractiveInputTestCase" );
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // misc information functions
81 // ----------------------------------------------------------------------------
85 void InteractiveInputTestCase::TestDiskInfo()
87 #ifdef TEST_INFO_FUNCTIONS
88 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
93 wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
94 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
98 pathname
[wxStrlen(pathname
) - 1] = 0;
100 if (pathname
[0] == '\0' || wxStrcmp(pathname
, "quit") == 0)
103 wxLongLong total
, free
;
104 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
106 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
110 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
111 (total
/ 1024).ToString().c_str(),
112 (free
/ 1024).ToString().c_str(),
120 #endif // TEST_INFO_FUNCTIONS
124 // ----------------------------------------------------------------------------
125 // regular expressions
126 // ----------------------------------------------------------------------------
128 #include "wx/regex.h"
130 void InteractiveInputTestCase::TestRegExInteractive()
133 wxPuts(wxT("*** Testing RE interactively ***"));
138 wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
139 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
142 // kill the last '\n'
143 pattern
[wxStrlen(pattern
) - 1] = 0;
145 if (pattern
[0] == '\0' || wxStrcmp(pattern
, "quit") == 0)
149 if ( !re
.Compile(pattern
) )
157 wxPrintf(wxT("Enter text to match: "));
158 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
161 // kill the last '\n'
162 text
[wxStrlen(text
) - 1] = 0;
164 if ( !re
.Matches(text
) )
166 wxPrintf(wxT("No match.\n"));
170 wxPrintf(wxT("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
173 for ( size_t n
= 1; ; n
++ )
175 if ( !re
.GetMatch(&start
, &len
, n
) )
180 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
181 n
, wxString(text
+ start
, len
).c_str());
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 #include "wx/protocol/ftp.h"
197 #include "wx/protocol/log.h"
199 #define FTP_ANONYMOUS
201 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
203 static const wxChar
*hostname
= "localhost";
206 void InteractiveInputTestCase::TestFtpInteractive()
211 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
214 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
215 #else // !FTP_ANONYMOUS
217 wxFgets(user
, WXSIZEOF(user
), stdin
);
218 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
221 wxChar password
[256];
222 wxPrintf(wxT("Password for %s: "), password
);
223 wxFgets(password
, WXSIZEOF(password
), stdin
);
224 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
225 ftp
.SetPassword(password
);
227 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
228 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
230 if ( !ftp
.Connect(hostname
) )
232 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
238 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
239 hostname
, ftp
.Pwd().c_str());
245 wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
246 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
249 // kill the last '\n'
250 buf
[wxStrlen(buf
) - 1] = 0;
252 if (buf
[0] == '\0' || wxStrcmp(buf
, "quit") == 0)
255 // special handling of LIST and NLST as they require data connection
256 wxString
start(buf
, 4);
258 if ( start
== wxT("LIST") || start
== wxT("NLST") )
261 if ( wxStrlen(buf
) > 4 )
265 if ( !ftp
.GetList(files
, wildcard
, start
== wxT("LIST")) )
267 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
271 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
272 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
273 size_t count
= files
.GetCount();
274 for ( size_t n
= 0; n
< count
; n
++ )
276 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
278 wxPuts(wxT("--- End of the file list"));
283 wxChar ch
= ftp
.SendCommand(buf
);
284 wxPrintf(wxT("Command %s"), ch
? wxT("succeeded") : wxT("failed"));
287 wxPrintf(wxT(" (return code %c)"), ch
);
290 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
303 #include "wx/datetime.h"
305 void InteractiveInputTestCase::TestDateTimeInteractive()
308 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
314 wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
315 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
318 // kill the last '\n'
319 buf
[wxStrlen(buf
) - 1] = 0;
321 if ( buf
[0] == '\0' || wxStrcmp(buf
, "quit") == 0 )
325 const wxChar
*p
= dt
.ParseDate(buf
);
328 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
334 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
337 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
338 dt
.Format(wxT("%b %d, %Y")).c_str(),
340 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
341 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
342 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
346 #endif // TEST_DATETIME
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 #include "wx/snglinst.h"
356 void InteractiveInputTestCase::TestSingleIstance()
359 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
361 wxSingleInstanceChecker checker
;
362 if ( checker
.Create(wxT(".wxconsole.lock")) )
364 if ( checker
.IsAnotherRunning() )
366 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
370 // wait some time to give time to launch another instance
371 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
372 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
375 else // failed to create
377 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
381 #endif // defined(TEST_SNGLINST)