]> git.lizzy.rs Git - irrlicht.git/blob - tests/filesystem.cpp
Fix bug introduced in last merge from svn trunk
[irrlicht.git] / tests / filesystem.cpp
1 #include "testUtils.h"\r
2 \r
3 using namespace irr;\r
4 using namespace core;\r
5 using namespace io;\r
6 \r
7 static bool testgetAbsoluteFilename(io::IFileSystem* fs)\r
8 {\r
9         bool result=true;\r
10         io::path apath = fs->getAbsolutePath("media");\r
11         io::path cwd = fs->getWorkingDirectory();\r
12         if (apath!=(cwd+"/media"))\r
13         {\r
14                 logTestString("getAbsolutePath failed on existing dir %s\n", apath.c_str());\r
15                 result = false;\r
16         }\r
17 \r
18         apath = fs->getAbsolutePath("../media/");\r
19         core::deletePathFromPath(cwd, 1);\r
20         if (apath!=(cwd+"media/"))\r
21         {\r
22                 logTestString("getAbsolutePath failed on dir with postfix / %s\n", apath.c_str());\r
23                 result = false;\r
24         }\r
25 \r
26         apath = fs->getAbsolutePath ("../nothere.txt");   // file does not exist\r
27         if (apath!=(cwd+"nothere.txt"))\r
28         {\r
29                 logTestString("getAbsolutePath failed on non-existing file %s\n", apath.c_str());\r
30                 result = false;\r
31         }\r
32 \r
33         return result;\r
34 }\r
35 \r
36 static bool testFlattenFilename(io::IFileSystem* fs)\r
37 {\r
38         bool result=true;\r
39         io::path tmpString="../tmp";\r
40         io::path refString="../tmp/";\r
41         fs->flattenFilename(tmpString);\r
42         if (tmpString != refString)\r
43         {\r
44                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
45                 result = false;\r
46         }\r
47 \r
48         tmpString="tmp/tmp/../";\r
49         refString="tmp/";\r
50         fs->flattenFilename(tmpString);\r
51         if (tmpString != refString)\r
52         {\r
53                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
54                 result = false;\r
55         }\r
56 \r
57         tmpString="tmp/tmp/..";\r
58         fs->flattenFilename(tmpString);\r
59         if (tmpString != refString)\r
60         {\r
61                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
62                 result = false;\r
63         }\r
64 \r
65         tmpString="tmp/next/../third";\r
66         refString="tmp/third/";\r
67         fs->flattenFilename(tmpString);\r
68         if (tmpString != refString)\r
69         {\r
70                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
71                 result = false;\r
72         }\r
73 \r
74         tmpString="this/tmp/next/../../my/fourth";\r
75         refString="this/my/fourth/";\r
76         fs->flattenFilename(tmpString);\r
77         if (tmpString != refString)\r
78         {\r
79                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
80                 result = false;\r
81         }\r
82 \r
83         tmpString="this/is/../../../a/fifth/test/";\r
84         refString="../a/fifth/test/";\r
85         fs->flattenFilename(tmpString);\r
86         if (tmpString != refString)\r
87         {\r
88                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
89                 result = false;\r
90         }\r
91 \r
92         tmpString="this/../is/../../a/sixth/test/";\r
93         refString="../a/sixth/test/";\r
94         fs->flattenFilename(tmpString);\r
95         if (tmpString != refString)\r
96         {\r
97                 logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());\r
98                 result = false;\r
99         }\r
100 \r
101         return result;\r
102 }\r
103 \r
104 static bool testgetRelativeFilename(io::IFileSystem* fs)\r
105 {\r
106         bool result=true;\r
107         io::path apath = fs->getAbsolutePath("media");\r
108         io::path cwd = fs->getWorkingDirectory();\r
109         if (fs->getRelativeFilename(apath, cwd) != "media")\r
110         {\r
111                 logTestString("getRelativePath failed on %s\n", apath.c_str());\r
112                 result = false;\r
113         }\r
114 \r
115         apath = fs->getAbsolutePath("../media/");\r
116         if (fs->getRelativeFilename(apath, cwd) != "../media/")\r
117         {\r
118                 logTestString("getRelativePath failed on %s\n", apath.c_str());\r
119                 result = false;\r
120         }\r
121 \r
122         return result;\r
123 }\r
124 \r
125 bool filesystem(void)\r
126 {\r
127         IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d<u32>(1, 1));\r
128         assert_log(device);\r
129         if(!device)\r
130                 return false;\r
131 \r
132         io::IFileSystem * fs = device->getFileSystem ();\r
133         if ( !fs )\r
134                 return false;\r
135 \r
136         bool result = true;\r
137 \r
138         io::path workingDir = device->getFileSystem()->getWorkingDirectory();\r
139 \r
140         io::path empty;\r
141         if ( fs->existFile(empty) )\r
142         {\r
143                 logTestString("Empty filename should not exist.\n");\r
144                 result = false;\r
145         }\r
146 \r
147         io::path newWd = workingDir + "/media";\r
148         bool changed = device->getFileSystem()->changeWorkingDirectoryTo(newWd);\r
149         assert_log(changed);\r
150 \r
151         if ( fs->existFile(empty) )\r
152         {\r
153                 logTestString("Empty filename should not exist even in another workingdirectory.\n");\r
154                 result = false;\r
155         }\r
156 \r
157         // The working directory must be restored for the other tests to work.\r
158         changed = device->getFileSystem()->changeWorkingDirectoryTo(workingDir.c_str());\r
159         assert_log(changed);\r
160 \r
161         // adding  a folder archive which just should not really change anything\r
162         device->getFileSystem()->addFileArchive( "./" );\r
163 \r
164         if ( fs->existFile(empty) )\r
165         {\r
166                 logTestString("Empty filename should not exist in folder file archive.\n");\r
167                 result = false;\r
168         }\r
169 \r
170         // remove it again to not affect other tests\r
171         device->getFileSystem()->removeFileArchive( device->getFileSystem()->getFileArchiveCount() );\r
172 \r
173         result &= testFlattenFilename(fs);\r
174         result &= testgetAbsoluteFilename(fs);\r
175         result &= testgetRelativeFilename(fs);\r
176 \r
177         device->closeDevice();\r
178         device->run();\r
179         device->drop();\r
180 \r
181         return result;\r
182 }\r
183 \r