@@ -25,27 +25,29 @@ and must not be misrepresented as being the original software.
2525source distribution.
2626*********************************************************************/
2727
28+ #include < tmxlite/FreeFuncs.hpp>
2829#include < tmxlite/Map.hpp>
2930#include < tmxlite/ObjectGroup.hpp>
3031#include < tmxlite/LayerGroup.hpp>
3132#include < tmxlite/TileLayer.hpp>
33+ #include < tmxlite/Tileset.hpp>
3234
3335#include < iostream>
3436#include < array>
3537#include < string>
3638
3739namespace
3840{
39- const std::array<std::string, 4u > LayerStrings =
40- {
41- std::string (" Tile" ),
42- std::string (" Object" ),
43- std::string (" Image" ),
44- std::string (" Group" ),
45- };
46- }
4741
48- int main ()
42+ const std::array<std::string, 4u > LayerStrings =
43+ {
44+ std::string (" Tile" ),
45+ std::string (" Object" ),
46+ std::string (" Image" ),
47+ std::string (" Group" ),
48+ };
49+
50+ void testLoadMap ()
4951{
5052 tmx::Map map;
5153
@@ -69,6 +71,7 @@ int main()
6971 {
7072 std::cout << " Tileset: " << tileset.getName () << std::endl;
7173 std::cout << " Tileset class: " << tileset.getClass () << std::endl;
74+ std::cout << " Tileset first GID: " << tileset.getFirstGID () << std::endl;
7275 }
7376
7477 std::cout << " Map has " << mapProperties.size () << " properties" << std::endl;
@@ -169,11 +172,121 @@ int main()
169172 {
170173 std::cout << " Failed loading map" << std::endl;
171174 }
175+ }
176+
177+ void testLoadTilesetWithoutMap ()
178+ {
179+ tmx::Tileset ts;
180+ if (!ts.loadWithoutMap (" images/tilemap/platform.tsx" ))
181+ {
182+ std::cout << " Failed to load tileset" << std::endl;
183+ return ;
184+ }
185+
186+ std::cout << " Loaded tileset without map: " << ts.getName () << std::endl;
187+ std::cout << " Tileset class: " << ts.getClass () << std::endl;
188+ std::cout << " Tileset image: " << ts.getImagePath () << std::endl;
189+ std::cout << " Tileset first GID: " << ts.getFirstGID () << std::endl;
190+ if (ts.getColumnCount () > 0 )
191+ {
192+ std::cout << " Tiles in tileset: " << ts.getTileCount () << std::endl;
193+ }
194+ }
195+
196+ class TestFailure {};
197+
198+ template <typename T1 , typename T2 >
199+ void checkEqImpl (const char * expr1, T1 && v1, const char * expr2, T2 && v2, const char * file, ssize_t line)
200+ {
201+ if (v1 != v2)
202+ {
203+ std::cout << " FAIL: " << expr1 << " = " << expr2
204+ << " \n Left: " << v1 << " \n Right: " << v2
205+ << " \n at " << file << " :" << line
206+ << std::endl;
207+ throw TestFailure ();
208+ }
209+ else
210+ {
211+ std::cout << " OK: " << expr1 << " = " << expr2 << " = " << v1 << std::endl;
212+ }
213+ }
214+
215+ #define CHECK_EQ (a, b ) checkEqImpl(#a, (a), #b, (b), __FILE__, __LINE__)
216+
217+ struct RevertWindowsPathHandling
218+ {
219+ bool oldValue = tmx::enableWindowsPathHandling;
220+ ~RevertWindowsPathHandling ()
221+ {
222+ tmx::enableWindowsPathHandling = oldValue;
223+ }
224+ };
225+
226+ void testResolvingPaths ()
227+ {
228+ RevertWindowsPathHandling revertWindowsPathHandling;
229+ tmx::enableWindowsPathHandling = true ;
230+
231+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " A/B/C" ), " A/B/C/a/b/c" );
232+ CHECK_EQ (tmx::resolveFilePath (" a/b/c/" , " A/B/C" ), " A/B/C/a/b/c" );
233+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " A/B/C/" ), " A/B/C/a/b/c" );
234+
235+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " " ), " a/b/c" );
236+ CHECK_EQ (tmx::resolveFilePath (" " , " A/B/C" ), " A/B/C" );
237+
238+ CHECK_EQ (tmx::resolveFilePath (" a///b//c" , " A//B///C" ), " A/B/C/a/b/c" );
239+
240+ CHECK_EQ (tmx::resolveFilePath (" /a/b/c" , " A/B/C" ), " /a/b/c" );
241+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " /A/B/C" ), " /A/B/C/a/b/c" );
242+
243+ CHECK_EQ (tmx::resolveFilePath (" ./a/b/c" , " A/B/C" ), " A/B/C/a/b/c" );
244+ CHECK_EQ (tmx::resolveFilePath (" ../a/b/c" , " A/B/C" ), " A/B/a/b/c" );
245+ CHECK_EQ (tmx::resolveFilePath (" ../../a/b/c" , " A/B/C" ), " A/a/b/c" );
246+ CHECK_EQ (tmx::resolveFilePath (" ../../../a/b/c" , " A/B/C" ), " a/b/c" );
247+ CHECK_EQ (tmx::resolveFilePath (" ../../../../a/b/c" , " A/B/C" ), " ../a/b/c" );
248+ CHECK_EQ (tmx::resolveFilePath (" ../../../../../a/b/c" , " A/B/C" ), " ../../a/b/c" );
249+
250+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " ./A/B/C" ), " A/B/C/a/b/c" );
251+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " ../A/B/C" ), " ../A/B/C/a/b/c" );
252+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " ../../A/B/C" ), " ../../A/B/C/a/b/c" );
253+
254+ CHECK_EQ (tmx::resolveFilePath (" a/../b/c" , " A/B/C" ), " A/B/C/b/c" );
255+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " A/../B/C" ), " B/C/a/b/c" );
256+ CHECK_EQ (tmx::resolveFilePath (" a/../b/../c" , " A/B/C" ), " A/B/C/c" );
257+ CHECK_EQ (tmx::resolveFilePath (" a/b/c" , " A/../B/../C" ), " C/a/b/c" );
258+ CHECK_EQ (tmx::resolveFilePath (" a/../b/../c" , " A/../B/../C" ), " C/c" );
259+
260+ CHECK_EQ (tmx::resolveFilePath (" a/b/c/../.." , " A/B/C" ), " A/B/C/a" );
261+ CHECK_EQ (tmx::resolveFilePath (" a/b/c/../../.." , " A/B/C" ), " A/B/C" );
262+ CHECK_EQ (tmx::resolveFilePath (" a/b/c/../../../.." , " A/B/C" ), " A/B" );
263+
264+ CHECK_EQ (tmx::resolveFilePath (" /a/../b/c" , " A/B/C" ), " /b/c" );
265+ CHECK_EQ (tmx::resolveFilePath (" /a/../../b/c" , " A/B/C" ), " /../b/c" ); // "/b/c" would also be valid but not worth handling this weird case IMO
266+
267+ CHECK_EQ (tmx::resolveFilePath (" C:/a/../b/c" , " A/B/C" ), " C:/b/c" );
268+ CHECK_EQ (tmx::resolveFilePath (" a/../b/c" , " C:/A/B/C" ), " C:/A/B/C/b/c" );
269+ CHECK_EQ (tmx::resolveFilePath (" C:/a/../b/c" , " C:/A/B/C" ), " C:/b/c" );
270+
271+ CHECK_EQ (tmx::resolveFilePath (" a\\ ..\\ b\\ c" , " C:\\ A\\ B\\ ..\\ C" ), " C:/A/C/b/c" );
272+ }
273+
274+ } // namespace
275+
276+ int main ()
277+ {
278+ testLoadMap ();
279+ std::cout << std::endl << " ------------------------------" << std::endl << std::endl;
280+ testLoadTilesetWithoutMap ();
281+ std::cout << std::endl << " ------------------------------" << std::endl << std::endl;
282+ testResolvingPaths ();
283+ std::cout << std::endl << " ------------------------------" << std::endl << std::endl;
172284
173285#if defined(PAUSE_AT_END)
174286 std::cout << std::endl << " Press return to quit..." <<std::endl;
175287 std::cin.get ();
176288#endif
177289
290+ std::cout << " Test complete" << std::endl;
178291 return 0 ;
179292}
0 commit comments