]> git.saurik.com Git - wxWidgets.git/blame - tests/filename/filenametest.cpp
revert memory leak fix, it causes crash
[wxWidgets.git] / tests / filename / filenametest.cpp
CommitLineData
f095b1fc
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/filename/filename.cpp
3// Purpose: wxFileName unit test
4// Author: Vadim Zeitlin
5// Created: 2004-07-25
6// RCS-ID: $Id$
7// Copyright: (c) 2004 Vadim Zeitlin
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
8899b155 14#include "testprec.h"
f095b1fc
VZ
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
f7c69b90 21 #include "wx/utils.h"
f095b1fc
VZ
22#endif // WX_PRECOMP
23
24#include "wx/filename.h"
c08dd08b 25#include "wx/filefn.h"
f095b1fc 26
f095b1fc
VZ
27// ----------------------------------------------------------------------------
28// test data
29// ----------------------------------------------------------------------------
30
31static struct FileNameInfo
32{
33 const wxChar *fullname;
34 const wxChar *volume;
35 const wxChar *path;
36 const wxChar *name;
37 const wxChar *ext;
38 bool isAbsolute;
39 wxPathFormat format;
40} filenames[] =
41{
42 // Unix file names
43 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX },
44 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX },
45 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX },
46 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX },
47 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX },
48 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX },
49 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX },
50 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX },
51 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX },
52
53 // Windows file names
54 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
55 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS },
56 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
57 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
58 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS },
bf7f7793
RR
59
60 // NB: when using the wxFileName::GetLongPath() function on these two strings,
61 // the program will hang various seconds. All those time is taken by the
62 // call to the win32 API GetLongPathName()...
f095b1fc
VZ
63 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
64 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS },
65
bf7f7793 66
f095b1fc
VZ
67 // wxFileName support for Mac file names is broken currently
68#if 0
69 // Mac file names
70 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC },
71 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC },
72 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC },
73 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC },
74 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
75 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
76#endif // 0
77
78 // VMS file names
bf7f7793
RR
79 // NB: on Windows they have the same effect of the \\server\\ strings
80 // (see the note above)
f095b1fc
VZ
81 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS },
82 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS },
83};
84
85// ----------------------------------------------------------------------------
86// test class
87// ----------------------------------------------------------------------------
88
89class FileNameTestCase : public CppUnit::TestCase
90{
91public:
92 FileNameTestCase() { }
93
94private:
95 CPPUNIT_TEST_SUITE( FileNameTestCase );
96 CPPUNIT_TEST( TestConstruction );
33366127 97 CPPUNIT_TEST( TestComparison );
f095b1fc 98 CPPUNIT_TEST( TestSplit );
4524a24b 99 CPPUNIT_TEST( TestSetPath );
c08dd08b 100 CPPUNIT_TEST( TestStrip );
bf7f7793 101 CPPUNIT_TEST( TestNormalize );
60c0dfe5
VZ
102#ifdef __WINDOWS__
103 CPPUNIT_TEST( TestShortLongPath );
104#endif // __WINDOWS__
f095b1fc
VZ
105 CPPUNIT_TEST_SUITE_END();
106
107 void TestConstruction();
33366127 108 void TestComparison();
f095b1fc
VZ
109 void TestSplit();
110 void TestSetPath();
c08dd08b 111 void TestStrip();
bf7f7793 112 void TestNormalize();
60c0dfe5
VZ
113#ifdef __WINDOWS__
114 void TestShortLongPath();
115#endif // __WINDOWS__
f095b1fc
VZ
116
117 DECLARE_NO_COPY_CLASS(FileNameTestCase)
118};
119
120// register in the unnamed registry so that these tests are run by default
121CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
122
123// also include in it's own registry so that these tests can be run alone
124CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
125
126void FileNameTestCase::TestConstruction()
127{
128 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
129 {
130 const FileNameInfo& fni = filenames[n];
131
132 wxFileName fn(fni.fullname, fni.format);
133
134 wxString fullname = fn.GetFullPath(fni.format);
135 CPPUNIT_ASSERT( fullname == fni.fullname );
136
137 CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) );
4c2deb19
VZ
138
139 if ( *fni.volume && *fni.path )
140 {
141 // check that specifying the volume separately or as part of the
142 // path doesn't make any difference
143 wxString pathWithVolume = fni.volume;
144 pathWithVolume += wxFileName::GetVolumeSeparator(fni.format);
145 pathWithVolume += fni.path;
146
147 CPPUNIT_ASSERT( fn == wxFileName(pathWithVolume,
148 fni.name,
149 fni.ext,
150 fni.format) );
151 }
f095b1fc
VZ
152 }
153}
154
33366127
VZ
155void FileNameTestCase::TestComparison()
156{
523cd68a
VZ
157 wxFileName fn1(wxT("/tmp/file1"));
158 wxFileName fn2(wxT("/tmp/dir2/../file2"));
159 fn1.Normalize();
160 fn2.Normalize();
161 CPPUNIT_ASSERT(fn1.GetPath() == fn2.GetPath());
33366127
VZ
162}
163
f095b1fc
VZ
164void FileNameTestCase::TestSplit()
165{
166 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
167 {
168 const FileNameInfo& fni = filenames[n];
169 wxString volume, path, name, ext;
170 wxFileName::SplitPath(fni.fullname,
171 &volume, &path, &name, &ext, fni.format);
172
173 CPPUNIT_ASSERT( volume == fni.volume );
174 CPPUNIT_ASSERT( path == fni.path );
175 CPPUNIT_ASSERT( name == fni.name );
176 CPPUNIT_ASSERT( ext == fni.ext );
177 }
dfecbee5
VZ
178
179 // special case of empty extension
180 wxFileName fn(_T("foo."));
181 CPPUNIT_ASSERT( fn.GetFullPath() == _T("foo.") );
f095b1fc
VZ
182}
183
4524a24b
VZ
184void FileNameTestCase::TestSetPath()
185{
186 wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS);
67050b6d
VS
187 fn.SetPath(_T("c:\\temp"), wxPATH_DOS);
188 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) );
4524a24b 189
67050b6d
VS
190 fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX);
191 fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX);
192 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) );
4524a24b
VZ
193}
194
bf7f7793
RR
195void FileNameTestCase::TestNormalize()
196{
197 // prepare some data to be used later
198 wxString sep = wxFileName::GetPathSeparator();
199 wxString cwd = wxGetCwd();
200 wxString home = wxGetUserHome();
201
202 cwd.Replace(sep, wxT("/"));
203 if (cwd.Last() != wxT('/'))
204 cwd += wxT('/');
205 home.Replace(sep, wxT("/"));
206 if (home.Last() != wxT('/'))
207 home += wxT('/');
208
209 // since we will always be testing paths using the wxPATH_UNIX
210 // format, we need to remove the volume, if present
211 if (home.Contains(wxT(':')))
212 home = home.AfterFirst(wxT(':'));
213 if (cwd.Contains(wxT(':')))
214 cwd = cwd.AfterFirst(wxT(':'));
215
216 static struct FileNameTest
217 {
218 wxString original;
219 int flags;
220 wxString expected;
221 } tests[] =
222 {
223 // test wxPATH_NORM_ENV_VARS
224#ifdef __WXMSW__
225 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") },
226#else
227 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") },
228#endif
229
230 // test wxPATH_NORM_DOTS
231 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS, wxT("") },
232
233 // test wxPATH_NORM_TILDE
234 // NB: do the tilde expansion also under Windows to test if it works there too
235 { wxT("/a/b/~"), wxPATH_NORM_TILDE, wxT("/a/b/~") },
236 { wxT("/~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") },
237 { wxT("~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") },
238
239 // test wxPATH_NORM_ABSOLUTE
240 { wxT("a/b/"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/") },
241 { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/c.ext") },
242 { wxT("/a"), wxPATH_NORM_ABSOLUTE, wxT("/a") },
243
244 // test giving no flags at all to Normalize()
245 { wxT("a/b/"), 0, wxT("a/b/") },
246 { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") },
247 { wxT("/a"), 0, wxT("/a") }
248 };
249
250 // set the env var ABCDEF
251 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
252
253 for (size_t i=0; i < WXSIZEOF(tests); i++)
254 {
255 wxFileName fn(tests[i].original, wxPATH_UNIX);
256
257 // be sure this normalization does not fail
258 CPPUNIT_ASSERT( fn.Normalize(tests[i].flags, cwd, wxPATH_UNIX) );
259
260 // compare result with expected string
261 CPPUNIT_ASSERT_EQUAL( tests[i].expected, fn.GetFullPath(wxPATH_UNIX) );
262 }
263}
264
ff3d9a35 265wxString wxTestStripExtension(wxString szFile)
c08dd08b
RN
266{
267 wxStripExtension(szFile);
268 return szFile;
269}
270
271void FileNameTestCase::TestStrip()
272{
273 //test a crash
ff3d9a35 274 CPPUNIT_ASSERT( wxTestStripExtension( _T("") ) == _T("") );
c08dd08b
RN
275
276 //others
ff3d9a35
RN
277 CPPUNIT_ASSERT( wxTestStripExtension( _T(".") ) == _T("") );
278 CPPUNIT_ASSERT( wxTestStripExtension( _T(".wav") ) == _T("") );
279 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav") ) == _T("good") );
280 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav.wav") ) == _T("good.wav") );
281}
60c0dfe5
VZ
282
283#ifdef __WINDOWS__
284
285void FileNameTestCase::TestShortLongPath()
286{
3f302e75 287 wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
60c0dfe5
VZ
288
289 // incredibly enough, GetLongPath() used to return different results during
290 // the first and subsequent runs, test for this
291 CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() );
292 CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() );
293}
294
295#endif // __WINDOWS__