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