Mercurial > fife-parpg
comparison tests/core_tests/test_zip.cpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2005-2008 by the FIFE team * | |
3 * http://www.fifengine.de * | |
4 * This file is part of FIFE. * | |
5 * * | |
6 * FIFE is free software; you can redistribute it and/or modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program is distributed in the hope that it will be useful, * | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * | |
20 ***************************************************************************/ | |
21 | |
22 // Standard C++ library includes | |
23 #include <iostream> | |
24 #include <iomanip> | |
25 | |
26 // Platform specific includes | |
27 | |
28 // 3rd party library includes | |
29 | |
30 // FIFE includes | |
31 // These includes are split up in two parts, separated by one empty line | |
32 // First block: files included from the FIFE root src directory | |
33 // Second block: files included from the same folder | |
34 #include "vfs/vfs.h" | |
35 #include "util/time/timemanager.h" | |
36 #include "vfs/vfs.h" | |
37 #include "vfs/vfsdirectory.h" | |
38 #include "vfs/zip/zipsource.h" | |
39 #include "vfs/raw/rawdata.h" | |
40 #include "util/base/exception.h" | |
41 | |
42 #include "fife_unit_test.h" | |
43 | |
44 using boost::unit_test::test_suite; | |
45 using namespace FIFE; | |
46 | |
47 // Environment | |
48 struct environment { | |
49 boost::shared_ptr<TimeManager> timemanager; | |
50 | |
51 environment() | |
52 : timemanager(new TimeManager()) {} | |
53 }; | |
54 | |
55 using boost::unit_test::test_suite; | |
56 using namespace FIFE; | |
57 | |
58 static const std::string COMPRESSED_FILE = "../data/testmap.zip"; | |
59 static const std::string RAW_FILE = "../data/test.map"; | |
60 | |
61 #ifdef FIFE_BOOST_VERSION_103300 | |
62 void test_decoder() { | |
63 #else | |
64 BOOST_AUTO_TEST_CASE( ZipSource_test ) { | |
65 #endif | |
66 environment env; | |
67 | |
68 boost::shared_ptr<VFS> vfs(new VFS()); | |
69 vfs->addSource(new VFSDirectory(vfs.get())); | |
70 | |
71 if ((!vfs->exists(COMPRESSED_FILE))) { | |
72 BOOST_ERROR("Test source " << COMPRESSED_FILE << " not found"); | |
73 return; | |
74 } | |
75 | |
76 vfs->addSource(new ZipSource(vfs.get(), COMPRESSED_FILE)); | |
77 | |
78 BOOST_CHECK_THROW(vfs->open("does-not-exist"), NotFound); | |
79 std::set<std::string> dirlist = vfs->listDirectories("ziptest_content"); | |
80 BOOST_CHECK(dirlist.size() == 4); | |
81 BOOST_CHECK(std::find(dirlist.begin(), dirlist.end(), "maps") != dirlist.end()); | |
82 BOOST_CHECK(std::find(dirlist.begin(), dirlist.end(), "testdir1") != dirlist.end()); | |
83 BOOST_CHECK(std::find(dirlist.begin(), dirlist.end(), "testdir2") != dirlist.end()); | |
84 BOOST_CHECK(std::find(dirlist.begin(), dirlist.end(), "testdir3") != dirlist.end()); | |
85 | |
86 std::set<std::string> filelist = vfs->listFiles("ziptest_content"); | |
87 BOOST_CHECK(filelist.size() == 0); | |
88 filelist = vfs->listFiles("ziptest_content/testdir1"); | |
89 BOOST_CHECK(filelist.size() == 4); | |
90 BOOST_CHECK(std::find(filelist.begin(), filelist.end(), "file") != filelist.end()); | |
91 BOOST_CHECK(std::find(filelist.begin(), filelist.end(), "file-a") != filelist.end()); | |
92 BOOST_CHECK(std::find(filelist.begin(), filelist.end(), "file-b") != filelist.end()); | |
93 BOOST_CHECK(std::find(filelist.begin(), filelist.end(), "file-c") != filelist.end()); | |
94 | |
95 BOOST_CHECK(vfs->listFiles("ziptest_content/testdir3").size() == 0); | |
96 BOOST_CHECK(vfs->listDirectories("ziptest_content/testdir1").size() == 0); | |
97 | |
98 | |
99 if ((!vfs->exists(RAW_FILE)) || (!vfs->exists("ziptest_content/maps/test.map"))) { | |
100 BOOST_ERROR("Test files not found"); | |
101 } | |
102 | |
103 RawData* fraw = vfs->open(RAW_FILE); | |
104 RawData* fcomp = vfs->open("ziptest_content/maps/test.map"); | |
105 | |
106 if (fraw->getDataLength() != fcomp->getDataLength()) { | |
107 std::cout << "raw length = " << fraw->getDataLength() \ | |
108 << ", compressed length = " << fcomp->getDataLength() << std::endl; | |
109 BOOST_ERROR("Data length mismatch"); | |
110 } else { | |
111 std::cout << "data length match, length = " << fcomp->getDataLength() << std::endl; | |
112 } | |
113 | |
114 unsigned int smaller_len = fraw->getDataLength(); | |
115 if (fcomp->getDataLength() < smaller_len) { | |
116 smaller_len = fcomp->getDataLength(); | |
117 } | |
118 | |
119 uint8_t* d_raw = new uint8_t[fraw->getDataLength()]; | |
120 uint8_t* d_comp = new uint8_t[fcomp->getDataLength()]; | |
121 fraw->readInto(d_raw,fraw->getDataLength()); | |
122 fcomp->readInto(d_comp,fcomp->getDataLength()); | |
123 | |
124 std::cout << "scanning data..." << std::endl; | |
125 for (unsigned int i = 0; i < smaller_len; i++) { | |
126 uint8_t rawc = d_raw[i]; | |
127 uint8_t compc = d_comp[i]; | |
128 if (rawc != compc) { | |
129 BOOST_ERROR("Data mismatch"); | |
130 std::cout | |
131 << "raw: " << std::setbase(16) << rawc | |
132 << " comp: " << std::setbase(16) << compc << std::endl; | |
133 break; | |
134 } | |
135 } | |
136 std::cout << "scanning finished" << std::endl; | |
137 delete[] d_raw; | |
138 delete[] d_comp; | |
139 delete fraw; | |
140 delete fcomp; | |
141 } | |
142 | |
143 #ifdef FIFE_BOOST_VERSION_103300 | |
144 test_suite* init_unit_test_suite(int argc, char** const argv) { | |
145 test_suite* test = BOOST_TEST_SUITE("DAT2 tests"); | |
146 test->add(BOOST_TEST_CASE(&test_decoder), 0); | |
147 return test; | |
148 } | |
149 #endif |