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