1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/interactive/input.cpp
3 // Purpose: Miscellaneous tests requiring user input
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
25 #include "wx/wxcrt.h" // for wxPuts
26 #include "wx/wxcrtvararg.h" // for wxPrintf
28 // ----------------------------------------------------------------------------
29 // conditional compilation
30 // ----------------------------------------------------------------------------
34 #define TEST_INFO_FUNCTIONS
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class InteractiveInputTestCase
: public CppUnit::TestCase
45 InteractiveInputTestCase() { }
48 CPPUNIT_TEST_SUITE( InteractiveInputTestCase
);
49 CPPUNIT_TEST( TestSingleIstance
);
50 CPPUNIT_TEST( TestFtpInteractive
);
51 CPPUNIT_TEST( TestDiskInfo
);
52 CPPUNIT_TEST( TestRegExInteractive
);
53 CPPUNIT_TEST( TestDateTimeInteractive
);
54 CPPUNIT_TEST_SUITE_END();
56 void TestSingleIstance();
57 void TestFtpInteractive();
59 void TestRegExInteractive();
60 void TestDateTimeInteractive();
62 wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase
);
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
70 // do not run this test by default!
72 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase
, "InteractiveInputTestCase" );
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // misc information functions
80 // ----------------------------------------------------------------------------
84 void InteractiveInputTestCase::TestDiskInfo()
86 #ifdef TEST_INFO_FUNCTIONS
87 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
92 wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
93 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
97 pathname
[wxStrlen(pathname
) - 1] = 0;
99 if (pathname
[0] == '\0' || wxStrcmp(pathname
, "quit") == 0)
102 wxLongLong total
, free
;
103 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
105 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
109 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
110 (total
/ 1024).ToString().c_str(),
111 (free
/ 1024).ToString().c_str(),
119 #endif // TEST_INFO_FUNCTIONS
123 // ----------------------------------------------------------------------------
124 // regular expressions
125 // ----------------------------------------------------------------------------
127 #include "wx/regex.h"
129 void InteractiveInputTestCase::TestRegExInteractive()
132 wxPuts(wxT("*** Testing RE interactively ***"));
137 wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
138 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
141 // kill the last '\n'
142 pattern
[wxStrlen(pattern
) - 1] = 0;
144 if (pattern
[0] == '\0' || wxStrcmp(pattern
, "quit") == 0)
148 if ( !re
.Compile(pattern
) )
156 wxPrintf(wxT("Enter text to match: "));
157 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
160 // kill the last '\n'
161 text
[wxStrlen(text
) - 1] = 0;
163 if ( !re
.Matches(text
) )
165 wxPrintf(wxT("No match.\n"));
169 wxPrintf(wxT("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
172 for ( size_t n
= 1; ; n
++ )
174 if ( !re
.GetMatch(&start
, &len
, n
) )
179 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
180 n
, wxString(text
+ start
, len
).c_str());
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 #include "wx/protocol/ftp.h"
196 #include "wx/protocol/log.h"
198 #define FTP_ANONYMOUS
200 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
202 static const wxChar
*hostname
= "localhost";
205 void InteractiveInputTestCase::TestFtpInteractive()
210 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
213 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
214 #else // !FTP_ANONYMOUS
216 wxFgets(user
, WXSIZEOF(user
), stdin
);
217 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
220 wxChar password
[256];
221 wxPrintf(wxT("Password for %s: "), password
);
222 wxFgets(password
, WXSIZEOF(password
), stdin
);
223 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
224 ftp
.SetPassword(password
);
226 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
227 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
229 if ( !ftp
.Connect(hostname
) )
231 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
237 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
238 hostname
, ftp
.Pwd().c_str());
244 wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
245 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
248 // kill the last '\n'
249 buf
[wxStrlen(buf
) - 1] = 0;
251 if (buf
[0] == '\0' || wxStrcmp(buf
, "quit") == 0)
254 // special handling of LIST and NLST as they require data connection
255 wxString
start(buf
, 4);
257 if ( start
== wxT("LIST") || start
== wxT("NLST") )
260 if ( wxStrlen(buf
) > 4 )
264 if ( !ftp
.GetList(files
, wildcard
, start
== wxT("LIST")) )
266 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
270 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
271 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
272 size_t count
= files
.GetCount();
273 for ( size_t n
= 0; n
< count
; n
++ )
275 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
277 wxPuts(wxT("--- End of the file list"));
282 wxChar ch
= ftp
.SendCommand(buf
);
283 wxPrintf(wxT("Command %s"), ch
? wxT("succeeded") : wxT("failed"));
286 wxPrintf(wxT(" (return code %c)"), ch
);
289 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
302 #include "wx/datetime.h"
304 void InteractiveInputTestCase::TestDateTimeInteractive()
307 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
313 wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
314 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
317 // kill the last '\n'
318 buf
[wxStrlen(buf
) - 1] = 0;
320 if ( buf
[0] == '\0' || wxStrcmp(buf
, "quit") == 0 )
324 const wxChar
*p
= dt
.ParseDate(buf
);
327 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
333 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
336 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
337 dt
.Format(wxT("%b %d, %Y")).c_str(),
339 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
340 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
341 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
345 #endif // TEST_DATETIME
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 #include "wx/snglinst.h"
355 void InteractiveInputTestCase::TestSingleIstance()
358 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
360 wxSingleInstanceChecker checker
;
361 if ( checker
.Create(wxT(".wxconsole.lock")) )
363 if ( checker
.IsAnotherRunning() )
365 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
369 // wait some time to give time to launch another instance
370 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
371 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
374 else // failed to create
376 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
380 #endif // defined(TEST_SNGLINST)