GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs2.cc Lines: 1136 1138 99.8 %
Date: 2017-11-29 Branches: 3857 9454 40.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "unittests/unittests.h"
22
23
#include "fs/files.h"
24
25
#include "fs/virtfs/fs.h"
26
#include "fs/virtfs/rwops.h"
27
28
#include "utils/checkutils.h"
29
#include "utils/foreach.h"
30
#include "utils/stringutils.h"
31
32
PRAGMA48(GCC diagnostic push)
33
PRAGMA48(GCC diagnostic ignored "-Wshadow")
34
#include <SDL_rwops.h>
35
PRAGMA48(GCC diagnostic pop)
36
37
#ifndef UNITTESTS_CATCH
38
#include <algorithm>
39
#endif  // UNITTESTS_CATCH
40
41
#include "debug.h"
42
43
42
static bool inList(StringVect list,
44
                   const std::string &name)
45
{
46
238
    FOR_EACH (StringVectCIter, it, list)
47
    {
48
70
        if (*it == name)
49
            return true;
50
    }
51
    return false;
52
}
53
54
34
static bool inList(StringVect list,
55
                   const std::string &dir,
56
                   const std::string &name)
57
{
58
102
    const std::string path = pathJoin(dir, name);
59
220
    FOR_EACH (StringVectCIter, it, list)
60
    {
61
84
        if (*it == path)
62
            return true;
63
    }
64
    return false;
65
}
66
67
6
static void removeTemp(StringVect &restrict list)
68
{
69
6
    int cnt = 0;
70
18
    std::sort(list.begin(), list.end());
71
72
174
    FOR_EACH (StringVectIter, it, list)
73
    {
74
312
        if (*it != "serverlistplus.xml.part")
75
        {
76
312
            logger->log("file: %d %s",
77
                cnt,
78
156
                (*it).c_str());
79
156
            cnt ++;
80
        }
81
    }
82
83
174
    FOR_EACH (StringVectIter, it, list)
84
    {
85
312
        if (*it == "serverlistplus.xml.part")
86
        {
87
            list.erase(it);
88
            return;
89
        }
90
    }
91
}
92
93
6
TEST_CASE("VirtFs2 isDirectory1", "")
94
{
95

8
    VirtFs::init(".");
96
8
    std::string name("data/test/test.zip");
97
4
    std::string prefix;
98

2
    if (Files::existsLocal(name) == false)
99
2
        prefix = "../" + prefix;
100
101

4
    VirtFs::mountDir(prefix + "data",
102
        Append_false);
103
104




14
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
105




14
    REQUIRE(VirtFs::isDirectory("test/units.xml/") == false);
106




14
    REQUIRE(VirtFs::isDirectory("test//units.xml") == false);
107




14
    REQUIRE(VirtFs::isDirectory("test/units123.xml") == false);
108




14
    REQUIRE(VirtFs::isDirectory("test//units123.xml") == false);
109




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
110




14
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
111




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
112




14
    REQUIRE(VirtFs::isDirectory("test") == true);
113




14
    REQUIRE(VirtFs::isDirectory("test/") == true);
114




14
    REQUIRE(VirtFs::isDirectory("test//") == true);
115




14
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
116




14
    REQUIRE(VirtFs::isDirectory("test//dir1") == true);
117




14
    REQUIRE(VirtFs::isDirectory("test//dir1/") == true);
118




14
    REQUIRE(VirtFs::isDirectory("test//dir1//") == true);
119




14
    REQUIRE(VirtFs::isDirectory("test\\dir1/") == true);
120




14
    REQUIRE(VirtFs::isDirectory("test/dir1//") == true);
121




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
122




14
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
123




14
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
124
125

4
    VirtFs::mountDir(prefix + "data/test",
126
        Append_false);
127
128




14
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
129




14
    REQUIRE(VirtFs::isDirectory("test/units.xml/") == false);
130




14
    REQUIRE(VirtFs::isDirectory("test//units.xml") == false);
131




12
    REQUIRE(VirtFs::isDirectory("test/units123.xml") == false);
132




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
133




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
134




14
    REQUIRE(VirtFs::isDirectory("test") == true);
135




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
136




14
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
137




14
    REQUIRE(VirtFs::isDirectory("test\\dir1") == true);
138
139

4
    VirtFs::unmountDir(prefix + "data/test");
140
141




14
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
142




14
    REQUIRE(VirtFs::isDirectory("test/units123.xml") == false);
143




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
144




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
145




14
    REQUIRE(VirtFs::isDirectory("units.xml/") == false);
146




14
    REQUIRE(VirtFs::isDirectory("test") == true);
147




14
    REQUIRE(VirtFs::isDirectory("test/") == true);
148




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
149




14
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
150
151

4
    VirtFs::unmountDirSilent(prefix + "data");
152
2
    VirtFs::deinit();
153
2
}
154
155
6
TEST_CASE("VirtFs2 isDirectory2", "")
156
{
157

8
    VirtFs::init(".");
158
8
    std::string name("data/test/test.zip");
159
4
    std::string prefix;
160

2
    if (Files::existsLocal(name) == false)
161
2
        prefix = "../" + prefix;
162
163

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
164
        Append_false);
165
166




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml") == false);
167




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml/") == false);
168




14
    REQUIRE(VirtFs::isDirectory("dir2//units.xml") == false);
169




14
    REQUIRE(VirtFs::isDirectory("dir2/units123.xml") == false);
170




14
    REQUIRE(VirtFs::isDirectory("dir2//units123.xml") == false);
171




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
172




14
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
173




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
174




14
    REQUIRE(VirtFs::isDirectory("dir") == true);
175




14
    REQUIRE(VirtFs::isDirectory("dir2/") == true);
176




14
    REQUIRE(VirtFs::isDirectory("dir2//") == true);
177




14
    REQUIRE(VirtFs::isDirectory("dir/1") == true);
178




14
    REQUIRE(VirtFs::isDirectory("dir//1") == true);
179




14
    REQUIRE(VirtFs::isDirectory("dir\\1/") == true);
180




14
    REQUIRE(VirtFs::isDirectory("dir/1") == true);
181




14
    REQUIRE(VirtFs::isDirectory("dir/1/zzz") == false);
182




14
    REQUIRE(VirtFs::isDirectory("test/dir1\\") == false);
183




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
184




14
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
185




14
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
186
187

4
    VirtFs::mountZip(prefix + "data/test/test.zip",
188
        Append_false);
189
190




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml") == false);
191




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml/") == false);
192




14
    REQUIRE(VirtFs::isDirectory("dir2\\units.xml") == false);
193




14
    REQUIRE(VirtFs::isDirectory("dir2/units123.xml") == false);
194




14
    REQUIRE(VirtFs::isDirectory("dir2//units123.xml") == false);
195




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
196




14
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
197




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
198




14
    REQUIRE(VirtFs::isDirectory("dir") == true);
199




14
    REQUIRE(VirtFs::isDirectory("dir2/") == true);
200




14
    REQUIRE(VirtFs::isDirectory("dir2\\") == true);
201




14
    REQUIRE(VirtFs::isDirectory("dir/1") == true);
202




14
    REQUIRE(VirtFs::isDirectory("dir//1") == true);
203




14
    REQUIRE(VirtFs::isDirectory("dir//1/") == true);
204




14
    REQUIRE(VirtFs::isDirectory("dir/1") == true);
205




14
    REQUIRE(VirtFs::isDirectory("dir/1/zzz") == false);
206




14
    REQUIRE(VirtFs::isDirectory("test/dir1//") == false);
207




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
208




14
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
209




14
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
210
211

4
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
212
213




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml") == false);
214




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml/") == false);
215




14
    REQUIRE(VirtFs::isDirectory("dir2//units.xml") == false);
216




14
    REQUIRE(VirtFs::isDirectory("dir2/units123.xml") == false);
217




14
    REQUIRE(VirtFs::isDirectory("dir2//units123.xml") == false);
218




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
219




14
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
220




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
221




14
    REQUIRE(VirtFs::isDirectory("dir") == true);
222




14
    REQUIRE(VirtFs::isDirectory("dir2/") == false);
223




14
    REQUIRE(VirtFs::isDirectory("dir2//") == false);
224




14
    REQUIRE(VirtFs::isDirectory("dir/1") == false);
225




14
    REQUIRE(VirtFs::isDirectory("dir\\1") == false);
226




14
    REQUIRE(VirtFs::isDirectory("dir//1/") == false);
227




14
    REQUIRE(VirtFs::isDirectory("dir/1") == false);
228




14
    REQUIRE(VirtFs::isDirectory("dir/1/zzz") == false);
229




14
    REQUIRE(VirtFs::isDirectory("test/dir1//") == false);
230




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
231




14
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
232




14
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
233
234

4
    VirtFs::unmountZip(prefix + "data/test/test.zip");
235
2
    VirtFs::deinit();
236
2
}
237
238
6
TEST_CASE("VirtFs2 isDirectory3", "")
239
{
240

8
    VirtFs::init(".");
241
8
    std::string name("data/test/test.zip");
242
4
    std::string prefix;
243

2
    if (Files::existsLocal(name) == false)
244
2
        prefix = "../" + prefix;
245
246

10
    VirtFs::mountDir2(prefix + "data",
247
        "test",
248
        Append_false);
249
250




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
251




14
    REQUIRE(VirtFs::isDirectory("units.xml/") == false);
252




14
    REQUIRE(VirtFs::isDirectory("units123.xml") == false);
253




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
254




14
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
255




14
    REQUIRE(VirtFs::isDirectory("test") == false);
256




14
    REQUIRE(VirtFs::isDirectory("dir1") == true);
257




14
    REQUIRE(VirtFs::isDirectory("dir2//") == true);
258




14
    REQUIRE(VirtFs::isDirectory("test/dir1") == false);
259




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
260




14
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
261




14
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
262
263

10
    VirtFs::unmountDirSilent2(prefix + "data",
264
        "test");
265
2
    VirtFs::deinit();
266
2
}
267
268
6
TEST_CASE("VirtFs2 isDirectory4", "")
269
{
270

8
    VirtFs::init(".");
271
8
    std::string name("data/test/test.zip");
272
4
    std::string prefix;
273

2
    if (Files::existsLocal(name) == false)
274
2
        prefix = "../" + prefix;
275
276

10
    VirtFs::mountZip2(prefix + "data/test/test2.zip",
277
        "dir",
278
        Append_false);
279
280




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml") == false);
281




14
    REQUIRE(VirtFs::isDirectory("dir2/units.xml/") == false);
282




14
    REQUIRE(VirtFs::isDirectory("dir2//units.xml") == false);
283




14
    REQUIRE(VirtFs::isDirectory("dir2/units123.xml") == false);
284




14
    REQUIRE(VirtFs::isDirectory("dir2//units123.xml") == false);
285




14
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
286




14
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
287




14
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
288




14
    REQUIRE(VirtFs::isDirectory("1") == true);
289




14
    REQUIRE(VirtFs::isDirectory("gpl") == true);
290




14
    REQUIRE(VirtFs::isDirectory("dir2/") == false);
291




14
    REQUIRE(VirtFs::isDirectory("dir/1") == false);
292




14
    REQUIRE(VirtFs::isDirectory("dir/1/zzz") == false);
293




14
    REQUIRE(VirtFs::isDirectory("test/dir1\\") == false);
294




14
    REQUIRE(VirtFs::isDirectory("testQ") == false);
295




14
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
296




14
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
297
298

10
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
299
        "dir");
300
2
    VirtFs::deinit();
301
2
}
302
303
6
TEST_CASE("VirtFs2 openRead1", "")
304
{
305

8
    VirtFs::init(".");
306
8
    std::string name("data/test/test.zip");
307
4
    std::string prefix;
308

2
    if (Files::existsLocal(name) == false)
309
2
        prefix = "../" + prefix;
310
311

4
    VirtFs::mountDir(prefix + "data",
312
        Append_false);
313
314
2
    VirtFs::File *file = nullptr;
315
316

8
    file = VirtFs::openRead("test/units.xml");
317



8
    REQUIRE(file != nullptr);
318
2
    VirtFs::close(file);
319

8
    file = VirtFs::openRead("test\\units.xml");
320



8
    REQUIRE(file != nullptr);
321
2
    VirtFs::close(file);
322

8
    file = VirtFs::openRead("test/units123.xml");
323



8
    REQUIRE(file == nullptr);
324

8
    file = VirtFs::openRead("tesQ/units.xml");
325



8
    REQUIRE(file == nullptr);
326

8
    file = VirtFs::openRead("units.xml");
327



8
    REQUIRE(file == nullptr);
328

8
    file = VirtFs::openRead("testQ");
329



8
    REQUIRE(file == nullptr);
330
331

4
    VirtFs::mountDir(prefix + "data/test",
332
        Append_false);
333
334

8
    file = VirtFs::openRead("test/units.xml");
335



8
    REQUIRE(file != nullptr);
336
2
    VirtFs::close(file);
337

8
    file = VirtFs::openRead("test/units123.xml");
338



8
    REQUIRE(file == nullptr);
339

8
    file = VirtFs::openRead("tesQ/units.xml");
340



8
    REQUIRE(file == nullptr);
341

8
    file = VirtFs::openRead("units.xml");
342



8
    REQUIRE(file != nullptr);
343
2
    VirtFs::close(file);
344

8
    file = VirtFs::openRead("testQ");
345



8
    REQUIRE(file == nullptr);
346
347

4
    VirtFs::unmountDir(prefix + "data/test");
348
349

8
    file = VirtFs::openRead("test/units.xml");
350



8
    REQUIRE(file != nullptr);
351
2
    VirtFs::close(file);
352

8
    file = VirtFs::openRead("test/units123.xml");
353



8
    REQUIRE(file == nullptr);
354

8
    file = VirtFs::openRead("tesQ/units.xml");
355



8
    REQUIRE(file == nullptr);
356

8
    file = VirtFs::openRead("units.xml");
357



8
    REQUIRE(file == nullptr);
358

8
    file = VirtFs::openRead("testQ");
359



8
    REQUIRE(file == nullptr);
360
361

4
    VirtFs::unmountDir(prefix + "data");
362
2
    VirtFs::deinit();
363
2
}
364
365
6
TEST_CASE("VirtFs2 openRead2", "")
366
{
367

8
    VirtFs::init(".");
368
8
    std::string name("data/test/test.zip");
369
8
    std::string prefix("data/test/");
370

2
    if (Files::existsLocal(name) == false)
371
2
        prefix = "../" + prefix;
372
373

4
    VirtFs::mountZip(prefix + "test2.zip",
374
        Append_false);
375
376
2
    VirtFs::File *file = nullptr;
377
378

8
    file = VirtFs::openRead("dir2/units.xml");
379



8
    REQUIRE(file != nullptr);
380
2
    VirtFs::close(file);
381

8
    file = VirtFs::openRead("dir2\\units.xml");
382



8
    REQUIRE(file != nullptr);
383
2
    VirtFs::close(file);
384

8
    file = VirtFs::openRead("dir2/units123.xml");
385



8
    REQUIRE(file == nullptr);
386

8
    file = VirtFs::openRead("tesQ/units.xml");
387



8
    REQUIRE(file == nullptr);
388

8
    file = VirtFs::openRead("units.xml1");
389



8
    REQUIRE(file == nullptr);
390

8
    file = VirtFs::openRead("testQ");
391



8
    REQUIRE(file == nullptr);
392

8
    file = VirtFs::openRead("dir/brimmedhat.png");
393



8
    REQUIRE(file == nullptr);
394

8
    file = VirtFs::openRead("dir//brimmedhat.png");
395



8
    REQUIRE(file == nullptr);
396
397

4
    VirtFs::mountZip(prefix + "test.zip",
398
        Append_false);
399
400

8
    file = VirtFs::openRead("dir2/units.xml");
401



8
    REQUIRE(file != nullptr);
402
2
    VirtFs::close(file);
403

8
    file = VirtFs::openRead("dir2//units.xml");
404



8
    REQUIRE(file != nullptr);
405
2
    VirtFs::close(file);
406

8
    file = VirtFs::openRead("dir2/units123.xml");
407



8
    REQUIRE(file == nullptr);
408

8
    file = VirtFs::openRead("tesQ/units.xml");
409



8
    REQUIRE(file == nullptr);
410

8
    file = VirtFs::openRead("units.xml1");
411



8
    REQUIRE(file == nullptr);
412

8
    file = VirtFs::openRead("testQ");
413



8
    REQUIRE(file == nullptr);
414

8
    file = VirtFs::openRead("dir/brimmedhat.png");
415



8
    REQUIRE(file != nullptr);
416
2
    VirtFs::close(file);
417
418

4
    VirtFs::unmountZip(prefix + "test.zip");
419
420

8
    file = VirtFs::openRead("dir2/units.xml");
421



8
    REQUIRE(file != nullptr);
422
2
    VirtFs::close(file);
423

8
    file = VirtFs::openRead("dir2\\/\\units.xml");
424



8
    REQUIRE(file != nullptr);
425
2
    VirtFs::close(file);
426

8
    file = VirtFs::openRead("dir2/units123.xml");
427



8
    REQUIRE(file == nullptr);
428

8
    file = VirtFs::openRead("tesQ/units.xml");
429



8
    REQUIRE(file == nullptr);
430

8
    file = VirtFs::openRead("units.xml1");
431



8
    REQUIRE(file == nullptr);
432

8
    file = VirtFs::openRead("testQ");
433



8
    REQUIRE(file == nullptr);
434

8
    file = VirtFs::openRead("dir/brimmedhat.png");
435



8
    REQUIRE(file == nullptr);
436
437

4
    VirtFs::unmountZip(prefix + "test2.zip");
438
439
2
    VirtFs::deinit();
440
2
}
441
442
6
TEST_CASE("VirtFs2 openRead3", "")
443
{
444

8
    VirtFs::init(".");
445
8
    std::string name("data/test/test.zip");
446
4
    std::string prefix;
447

2
    if (Files::existsLocal(name) == false)
448
2
        prefix = "../" + prefix;
449
450

10
    VirtFs::mountDir2(prefix + "data",
451
        "test",
452
        Append_false);
453
454
2
    VirtFs::File *file = nullptr;
455
456

8
    file = VirtFs::openRead("units.xml");
457



8
    REQUIRE(file != nullptr);
458
2
    VirtFs::close(file);
459

8
    file = VirtFs::openRead("test/units.xml");
460



8
    REQUIRE(file == nullptr);
461

8
    file = VirtFs::openRead("units123.xml");
462



8
    REQUIRE(file == nullptr);
463

8
    file = VirtFs::openRead("tesQ/units.xml");
464



8
    REQUIRE(file == nullptr);
465

8
    file = VirtFs::openRead("testQ");
466



8
    REQUIRE(file == nullptr);
467

8
    file = VirtFs::openRead("file1.txt");
468



8
    REQUIRE(file == nullptr);
469

8
    file = VirtFs::openRead("file2.txt");
470



8
    REQUIRE(file == nullptr);
471
472

10
    VirtFs::mountDir2(prefix + "data/test",
473
        "dir2",
474
        Append_false);
475
476

8
    file = VirtFs::openRead("units.xml");
477



8
    REQUIRE(file != nullptr);
478
2
    VirtFs::close(file);
479

8
    file = VirtFs::openRead("test/units.xml");
480



8
    REQUIRE(file == nullptr);
481

8
    file = VirtFs::openRead("units123.xml");
482



8
    REQUIRE(file == nullptr);
483

8
    file = VirtFs::openRead("tesQ/units.xml");
484



8
    REQUIRE(file == nullptr);
485

8
    file = VirtFs::openRead("testQ");
486



8
    REQUIRE(file == nullptr);
487

8
    file = VirtFs::openRead("file1.txt");
488



8
    REQUIRE(file != nullptr);
489
2
    VirtFs::close(file);
490

8
    file = VirtFs::openRead("file2.txt");
491



8
    REQUIRE(file != nullptr);
492
2
    VirtFs::close(file);
493
494

10
    VirtFs::unmountDir2(prefix + "data/test",
495
        "dir2");
496
497

8
    file = VirtFs::openRead("units.xml");
498



8
    REQUIRE(file != nullptr);
499
2
    VirtFs::close(file);
500

8
    file = VirtFs::openRead("test/units.xml");
501



8
    REQUIRE(file == nullptr);
502

8
    file = VirtFs::openRead("units123.xml");
503



8
    REQUIRE(file == nullptr);
504

8
    file = VirtFs::openRead("tesQ/units.xml");
505



8
    REQUIRE(file == nullptr);
506

8
    file = VirtFs::openRead("testQ");
507



8
    REQUIRE(file == nullptr);
508

8
    file = VirtFs::openRead("file1.txt");
509



8
    REQUIRE(file == nullptr);
510

8
    file = VirtFs::openRead("file2.txt");
511



8
    REQUIRE(file == nullptr);
512
513

10
    VirtFs::unmountDir2(prefix + "data",
514
        "test");
515
2
    VirtFs::deinit();
516
2
}
517
518
6
TEST_CASE("VirtFs2 openRead4", "")
519
{
520

8
    VirtFs::init(".");
521
8
    std::string name("data/test/test.zip");
522
8
    std::string prefix("data/test/");
523

2
    if (Files::existsLocal(name) == false)
524
2
        prefix = "../" + prefix;
525
526

10
    VirtFs::mountZip2(prefix + "test2.zip",
527
        "dir",
528
        Append_false);
529
530
2
    VirtFs::File *file = nullptr;
531
532

8
    file = VirtFs::openRead("dye.png");
533



8
    REQUIRE(file != nullptr);
534
2
    VirtFs::close(file);
535

8
    file = VirtFs::openRead("1\\test.txt");
536



8
    REQUIRE(file != nullptr);
537
2
    VirtFs::close(file);
538

8
    file = VirtFs::openRead("dir/dye.png");
539



8
    REQUIRE(file == nullptr);
540

8
    file = VirtFs::openRead("tesQ/units.xml");
541



8
    REQUIRE(file == nullptr);
542

8
    file = VirtFs::openRead("dye.png1");
543



8
    REQUIRE(file == nullptr);
544

8
    file = VirtFs::openRead("testQ");
545



8
    REQUIRE(file == nullptr);
546

8
    file = VirtFs::openRead("brimmedhat.png");
547



8
    REQUIRE(file == nullptr);
548
549

10
    VirtFs::mountZip2(prefix + "test.zip",
550
        "dir",
551
        Append_false);
552
553

8
    file = VirtFs::openRead("dye.png");
554



8
    REQUIRE(file != nullptr);
555
2
    VirtFs::close(file);
556

8
    file = VirtFs::openRead("1\\test.txt");
557



8
    REQUIRE(file != nullptr);
558
2
    VirtFs::close(file);
559

8
    file = VirtFs::openRead("dir/dye.png");
560



8
    REQUIRE(file == nullptr);
561

8
    file = VirtFs::openRead("tesQ/units.xml");
562



8
    REQUIRE(file == nullptr);
563

8
    file = VirtFs::openRead("dye.png1");
564



8
    REQUIRE(file == nullptr);
565

8
    file = VirtFs::openRead("testQ");
566



8
    REQUIRE(file == nullptr);
567

8
    file = VirtFs::openRead("brimmedhat.png");
568



8
    REQUIRE(file != nullptr);
569
2
    VirtFs::close(file);
570
571

10
    VirtFs::unmountZip2(prefix + "test.zip",
572
        "dir");
573
574

8
    file = VirtFs::openRead("dye.png");
575



8
    REQUIRE(file != nullptr);
576
2
    VirtFs::close(file);
577

8
    file = VirtFs::openRead("1\\test.txt");
578



8
    REQUIRE(file != nullptr);
579
2
    VirtFs::close(file);
580

8
    file = VirtFs::openRead("dir/dye.png");
581



8
    REQUIRE(file == nullptr);
582

8
    file = VirtFs::openRead("tesQ/units.xml");
583



8
    REQUIRE(file == nullptr);
584

8
    file = VirtFs::openRead("dye.png1");
585



8
    REQUIRE(file == nullptr);
586

8
    file = VirtFs::openRead("testQ");
587



8
    REQUIRE(file == nullptr);
588

8
    file = VirtFs::openRead("brimmedhat.png");
589



8
    REQUIRE(file == nullptr);
590
591

10
    VirtFs::unmountZip2(prefix + "test2.zip",
592
        "dir");
593
594
2
    VirtFs::deinit();
595
2
}
596
597
6
TEST_CASE("VirtFs2 permitLinks", "")
598
{
599

8
    VirtFs::init(".");
600
8
    std::string name("data/test/test.zip");
601
4
    std::string prefix;
602

2
    if (Files::existsLocal(name) == false)
603
2
        prefix = "../" + prefix;
604
605

4
    VirtFs::mountDir(prefix + "data",
606
        Append_false);
607
608

8
    const int cnt1 = VirtFs::exists("test/test2.txt") ? 26 : 25;
609
2
    const int cnt2 = 26;
610
611
4
    StringVect list;
612
2
    VirtFs::permitLinks(false);
613

8
    VirtFs::getFiles("test", list);
614
2
    removeTemp(list);
615
4
    const size_t sz = list.size();
616



8
    REQUIRE(sz == cnt1);
617
618
2
    list.clear();
619
2
    VirtFs::permitLinks(true);
620

8
    VirtFs::getFiles("test", list);
621
2
    removeTemp(list);
622



10
    REQUIRE(list.size() == cnt2);
623
624
2
    list.clear();
625
2
    VirtFs::permitLinks(false);
626

8
    VirtFs::getFiles("test", list);
627
2
    removeTemp(list);
628



10
    REQUIRE(list.size() == cnt1);
629
630

4
    VirtFs::unmountDirSilent(prefix + "data");
631
2
    VirtFs::deinit();
632
2
}
633
634
6
TEST_CASE("VirtFs2 read1", "")
635
{
636

8
    VirtFs::init(".");
637
8
    std::string name("data/test/test.zip");
638
4
    std::string prefix;
639

2
    if (Files::existsLocal(name) == false)
640
2
        prefix = "../" + prefix;
641
642

4
    VirtFs::mountDir(prefix + "data",
643
        Append_false);
644
645

8
    VirtFs::File *file = VirtFs::openRead("test/test.txt");
646



8
    REQUIRE(file != nullptr);
647




8
    REQUIRE(VirtFs::fileLength(file) == 23);
648
2
    const int fileSize = VirtFs::fileLength(file);
649
650
2
    void *restrict buffer = calloc(fileSize + 1, 1);
651




8
    REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
652



8
    REQUIRE(strcmp(static_cast<char*>(buffer),
653
        "test line 1\ntest line 2") == 0);
654




8
    REQUIRE(VirtFs::tell(file) == fileSize);
655




8
    REQUIRE(VirtFs::eof(file) != 0);
656
657
2
    free(buffer);
658
2
    buffer = calloc(fileSize + 1, 1);
659




8
    REQUIRE(VirtFs::seek(file, 12) != 0);
660




8
    REQUIRE(VirtFs::eof(file) == 0);
661




8
    REQUIRE(VirtFs::tell(file) == 12);
662




8
    REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
663



8
    REQUIRE(strcmp(static_cast<char*>(buffer),
664
        "test line 2") == 0);
665




8
    REQUIRE(VirtFs::eof(file) != 0);
666
667
2
    VirtFs::close(file);
668
2
    free(buffer);
669
670

4
    VirtFs::unmountDir(prefix + "data");
671
2
    VirtFs::deinit();
672
2
}
673
674
16
TEST_CASE("VirtFs2 read2", "")
675
{
676

48
    VirtFs::init(".");
677
48
    std::string name("data/test/test.zip");
678
48
    std::string prefix("data/test/");
679

12
    if (Files::existsLocal(name) == false)
680
12
        prefix = "../" + prefix;
681
682

24
    VirtFs::mountZip(prefix + "test2.zip",
683
        Append_false);
684
12
    VirtFs::File *file = nullptr;
685
12
    void *restrict buffer = nullptr;
686
687



84
    SECTION("test 1")
688
    {
689

8
        file = VirtFs::openRead("dir2//test.txt");
690



8
        REQUIRE(file != nullptr);
691




8
        REQUIRE(VirtFs::fileLength(file) == 23);
692
2
        const int fileSize = VirtFs::fileLength(file);
693
694
2
        buffer = calloc(fileSize + 1, 1);
695




8
        REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
696



8
        REQUIRE(strcmp(static_cast<char*>(buffer),
697
            "test line 1\ntest line 2") == 0);
698




8
        REQUIRE(VirtFs::tell(file) == fileSize);
699




8
        REQUIRE(VirtFs::eof(file) != 0);
700
    }
701
702



84
    SECTION("test 2")
703
    {
704

8
        file = VirtFs::openRead("dir2\\/test.txt");
705



8
        REQUIRE(file != nullptr);
706




8
        REQUIRE(VirtFs::fileLength(file) == 23);
707
2
        const int fileSize = VirtFs::fileLength(file);
708
709
2
        buffer = calloc(fileSize + 1, 1);
710




8
        REQUIRE(VirtFs::seek(file, 12) != 0);
711




8
        REQUIRE(VirtFs::eof(file) == 0);
712




8
        REQUIRE(VirtFs::tell(file) == 12);
713




8
        REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
714



8
        REQUIRE(strcmp(static_cast<char*>(buffer),
715
            "test line 2") == 0);
716




8
        REQUIRE(VirtFs::eof(file) != 0);
717
    }
718
719



84
    SECTION("test 3")
720
    {
721

8
        file = VirtFs::openRead("dir2//test.txt");
722



8
        REQUIRE(file != nullptr);
723
2
        const int fileSize = VirtFs::fileLength(file);
724
725
2
        buffer = calloc(fileSize + 1, 1);
726
48
        for (int f = 0; f < fileSize; f ++)
727
        {
728




184
            REQUIRE(VirtFs::seek(file, f) != 0);
729




184
            REQUIRE(VirtFs::eof(file) == 0);
730




184
            REQUIRE(VirtFs::tell(file) == f);
731
        }
732
    }
733
734



84
    SECTION("test 4")
735
    {
736

8
        file = VirtFs::openRead("dir2/test.txt");
737



8
        REQUIRE(file != nullptr);
738
2
        const int fileSize = VirtFs::fileLength(file);
739
2
        const char *restrict const str = "test line 1\ntest line 2";
740
2
        buffer = calloc(fileSize + 1, 1);
741
46
        for (int f = 0; f < fileSize - 1; f ++)
742
        {
743




176
            REQUIRE(VirtFs::read(file, buffer, 1, 1) == 1);
744



176
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
745




176
            REQUIRE(VirtFs::eof(file) == 0);
746




176
            REQUIRE(VirtFs::tell(file) == f + 1);
747
        }
748




8
        REQUIRE(VirtFs::read(file, buffer, 1, 1) == 1);
749



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
750




8
        REQUIRE(VirtFs::eof(file) != 0);
751




8
        REQUIRE(VirtFs::tell(file) == fileSize);
752
    }
753
754



84
    SECTION("test 5")
755
    {
756

8
        file = VirtFs::openRead("dir2\\\\test.txt");
757



8
        REQUIRE(file != nullptr);
758
2
        const int fileSize = VirtFs::fileLength(file);
759
2
        const char *restrict const str = "test line 1\ntest line 2";
760
2
        buffer = calloc(fileSize + 1, 1);
761
24
        for (int f = 0; f < fileSize - 1; f += 2)
762
        {
763




88
            REQUIRE(VirtFs::read(file, buffer, 2, 1) == 1);
764



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
765



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
766




88
            REQUIRE(VirtFs::eof(file) == 0);
767




88
            REQUIRE(VirtFs::tell(file) == f + 2);
768
        }
769




8
        REQUIRE(VirtFs::eof(file) == 0);
770




8
        REQUIRE(VirtFs::tell(file) == 22);
771




8
        REQUIRE(VirtFs::read(file, buffer, 2, 1) == 0);
772




8
        REQUIRE(VirtFs::eof(file) == 0);
773
    }
774
775



84
    SECTION("test 6")
776
    {
777

8
        file = VirtFs::openRead("dir2//test.txt");
778



8
        REQUIRE(file != nullptr);
779
2
        const int fileSize = VirtFs::fileLength(file);
780
2
        const char *restrict const str = "test line 1\ntest line 2";
781
2
        buffer = calloc(fileSize + 1, 1);
782
24
        for (int f = 0; f < fileSize - 1; f += 2)
783
        {
784




88
            REQUIRE(VirtFs::read(file, buffer, 1, 2) == 2);
785



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
786



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
787




88
            REQUIRE(VirtFs::eof(file) == 0);
788




88
            REQUIRE(VirtFs::tell(file) == f + 2);
789
        }
790




8
        REQUIRE(VirtFs::eof(file) == 0);
791




8
        REQUIRE(VirtFs::tell(file) == 22);
792




8
        REQUIRE(VirtFs::read(file, buffer, 1, 2) == 1);
793



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
794




8
        REQUIRE(VirtFs::eof(file) != 0);
795
    }
796
797
12
    VirtFs::close(file);
798
12
    free(buffer);
799

24
    VirtFs::unmountZip(prefix + "test2.zip");
800
12
    VirtFs::deinit();
801
12
}
802
803
6
TEST_CASE("VirtFs2 loadFile1", "")
804
{
805

8
    VirtFs::init(".");
806
2
    int fileSize = 0;
807
8
    std::string name("data/test/test.zip");
808
4
    std::string prefix;
809

2
    if (Files::existsLocal(name) == false)
810
2
        prefix = "../" + prefix;
811
812

4
    VirtFs::mountDir(prefix + "data",
813
        Append_false);
814
815

8
    const char *const buffer = VirtFs::loadFile("test/test.txt", fileSize);
816



8
    REQUIRE(static_cast<const void*>(buffer) != nullptr);
817



8
    REQUIRE(fileSize == 23);
818



8
    REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0);
819
2
    delete [] buffer;
820
821

4
    VirtFs::unmountDir(prefix + "data");
822
2
    VirtFs::deinit();
823
2
}
824
825
8
TEST_CASE("VirtFs2 loadFile2", "")
826
{
827

16
    VirtFs::init(".");
828
4
    int fileSize = 0;
829
16
    std::string name("data/test/test.zip");
830
8
    std::string prefix;
831

4
    if (Files::existsLocal(name) == false)
832
4
        prefix = "../" + prefix;
833
834

8
    VirtFs::mountZip(prefix + "data/test/test2.zip",
835
        Append_false);
836
837



28
    SECTION("test 1")
838
    {
839
8
        const char *restrict buffer = VirtFs::loadFile("dir2//test.txt",
840
2
            fileSize);
841



8
        REQUIRE(static_cast<const void*>(buffer) != nullptr);
842



8
        REQUIRE(fileSize == 23);
843



8
        REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0);
844
2
        delete [] buffer;
845
    }
846
847



28
    SECTION("test 2")
848
    {
849
8
        const char *restrict buffer = VirtFs::loadFile("dir2\\/test.txt",
850
2
            fileSize);
851



8
        REQUIRE(static_cast<const void*>(buffer) != nullptr);
852



8
        REQUIRE(fileSize == 23);
853



8
        REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0);
854
2
        delete [] buffer;
855
    }
856
857

8
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
858
4
    VirtFs::deinit();
859
4
}
860
861
6
TEST_CASE("VirtFs2 loadFile3", "")
862
{
863

8
    VirtFs::init(".");
864
2
    int fileSize = 0;
865
8
    std::string name("data/test/test.zip");
866
4
    std::string prefix;
867

2
    if (Files::existsLocal(name) == false)
868
2
        prefix = "../" + prefix;
869
870

10
    VirtFs::mountDir2(prefix + "data",
871
        "test",
872
        Append_false);
873
874

8
    const char *const buffer = VirtFs::loadFile("test.txt", fileSize);
875



8
    REQUIRE(static_cast<const void*>(buffer) != nullptr);
876



8
    REQUIRE(fileSize == 23);
877



8
    REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0);
878
2
    delete [] buffer;
879
880

10
    VirtFs::unmountDir2(prefix + "data",
881
        "test");
882
2
    VirtFs::deinit();
883
2
}
884
885
8
TEST_CASE("VirtFs2 loadFile4", "")
886
{
887

16
    VirtFs::init(".");
888
4
    int fileSize = 0;
889
16
    std::string name("data/test/test.zip");
890
8
    std::string prefix;
891

4
    if (Files::existsLocal(name) == false)
892
4
        prefix = "../" + prefix;
893
894

20
    VirtFs::mountZip2(prefix + "data/test/test2.zip",
895
        "dir2",
896
        Append_false);
897
898



28
    SECTION("test 1")
899
    {
900
8
        const char *restrict buffer = VirtFs::loadFile("test.txt",
901
2
            fileSize);
902



8
        REQUIRE(static_cast<const void*>(buffer) != nullptr);
903



8
        REQUIRE(fileSize == 23);
904



8
        REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0);
905
2
        delete [] buffer;
906
    }
907
908



28
    SECTION("test 2")
909
    {
910
8
        const char *restrict buffer = VirtFs::loadFile("test.txt",
911
2
            fileSize);
912



8
        REQUIRE(static_cast<const void*>(buffer) != nullptr);
913



8
        REQUIRE(fileSize == 23);
914



8
        REQUIRE(strncmp(buffer, "test line 1\ntest line 2", 23) == 0);
915
2
        delete [] buffer;
916
    }
917
918

20
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
919
        "dir2");
920
4
    VirtFs::deinit();
921
4
}
922
923
6
TEST_CASE("VirtFs2 rwops_read1", "")
924
{
925

8
    VirtFs::init(".");
926
8
    std::string name("data/test/test.zip");
927
4
    std::string prefix;
928

2
    if (Files::existsLocal(name) == false)
929
2
        prefix = "../" + prefix;
930
931

4
    VirtFs::mountDir(prefix + "data",
932
        Append_false);
933
934

8
    SDL_RWops *file = VirtFs::rwopsOpenRead("test/test.txt");
935



8
    REQUIRE(file != nullptr);
936
#ifdef USE_SDL2
937
    REQUIRE(file->size(file) == 23);
938
#endif  // USE_SDL2
939
940
2
    const int fileSize = 23;
941
942
2
    void *restrict buffer = calloc(fileSize + 1, 1);
943




8
    REQUIRE(file->read(file, buffer, 1, fileSize) == fileSize);
944



8
    REQUIRE(strcmp(static_cast<char*>(buffer),
945
        "test line 1\ntest line 2") == 0);
946




8
    REQUIRE(file->seek(file, 0, SEEK_CUR) == fileSize);
947
948
2
    free(buffer);
949
2
    buffer = calloc(fileSize + 1, 1);
950




8
    REQUIRE(file->seek(file, 12, SEEK_SET) != 0);
951




8
    REQUIRE(file->seek(file, 0, SEEK_CUR) == 12);
952




8
    REQUIRE(file->read(file, buffer, 1, 11) == 11);
953



8
    REQUIRE(strcmp(static_cast<char*>(buffer),
954
        "test line 2") == 0);
955
956
2
    file->close(file);
957
2
    free(buffer);
958
959

4
    VirtFs::unmountDir(prefix + "data");
960
2
    VirtFs::deinit();
961
2
}
962
963
20
TEST_CASE("VirtFs2 rwops_read2", "")
964
{
965

64
    VirtFs::init(".");
966
64
    std::string name("data/test/test.zip");
967
64
    std::string prefix("data/test/");
968

16
    if (Files::existsLocal(name) == false)
969
16
        prefix = "../" + prefix;
970
971

32
    VirtFs::mountZip(prefix + "test2.zip",
972
        Append_false);
973
16
    SDL_RWops *file = nullptr;
974
16
    void *restrict buffer = nullptr;
975
976



112
    SECTION("test 1")
977
    {
978

8
        file = VirtFs::rwopsOpenRead("dir2//test.txt");
979



8
        REQUIRE(file != nullptr);
980
#ifdef USE_SDL2
981
        REQUIRE(file->size(file) == 23);
982
#endif  // USE_SDL2
983
2
        const int fileSize = 23;
984
985
2
        buffer = calloc(fileSize + 1, 1);
986




8
        REQUIRE(file->read(file, buffer, 1, fileSize) == fileSize);
987



8
        REQUIRE(strcmp(static_cast<char*>(buffer),
988
            "test line 1\ntest line 2") == 0);
989




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == fileSize);
990
    }
991
992



112
    SECTION("test 2")
993
    {
994

8
        file = VirtFs::rwopsOpenRead("dir2\\/test.txt");
995



8
        REQUIRE(file != nullptr);
996
#ifdef USE_SDL2
997
        REQUIRE(file->size(file) == 23);
998
#endif  // USE_SDL2
999
2
        const int fileSize = 23;
1000
1001
2
        buffer = calloc(fileSize + 1, 1);
1002




8
        REQUIRE(file->seek(file, 12, SEEK_SET) != 0);
1003




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 12);
1004




8
        REQUIRE(file->read(file, buffer, 1, 11) == 11);
1005



8
        REQUIRE(strcmp(static_cast<char*>(buffer),
1006
            "test line 2") == 0);
1007
    }
1008
1009



112
    SECTION("test 3")
1010
    {
1011

8
        file = VirtFs::rwopsOpenRead("dir2//test.txt");
1012



8
        REQUIRE(file != nullptr);
1013
2
        const int fileSize = 23;
1014
1015
2
        buffer = calloc(fileSize + 1, 1);
1016
48
        for (int f = 0; f < fileSize; f ++)
1017
        {
1018




184
            REQUIRE(file->seek(file, f, SEEK_SET) == f);
1019




184
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f);
1020
        }
1021
    }
1022
1023



112
    SECTION("test 4")
1024
    {
1025

8
        file = VirtFs::rwopsOpenRead("dir2/test.txt");
1026



8
        REQUIRE(file != nullptr);
1027
2
        const int fileSize = 23;
1028
2
        const char *restrict const str = "test line 1\ntest line 2";
1029
2
        buffer = calloc(fileSize + 1, 1);
1030
46
        for (int f = 0; f < fileSize - 1; f ++)
1031
        {
1032




176
            REQUIRE(file->read(file, buffer, 1, 1) == 1);
1033



176
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1034




176
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 1);
1035
        }
1036




8
        REQUIRE(file->read(file, buffer, 1, 1) == 1);
1037



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
1038




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == fileSize);
1039
    }
1040
1041



112
    SECTION("test 5")
1042
    {
1043

8
        file = VirtFs::rwopsOpenRead("dir2\\\\test.txt");
1044



8
        REQUIRE(file != nullptr);
1045
2
        const int fileSize = 23;
1046
2
        const char *restrict const str = "test line 1\ntest line 2";
1047
2
        buffer = calloc(fileSize + 1, 1);
1048
24
        for (int f = 0; f < fileSize - 1; f += 2)
1049
        {
1050




88
            REQUIRE(file->read(file, buffer, 2, 1) == 1);
1051



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1052



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
1053




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 2);
1054
        }
1055




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 22);
1056




8
        REQUIRE(file->read(file, buffer, 2, 1) == 0);
1057
    }
1058
1059



112
    SECTION("test 6")
1060
    {
1061

8
        file = VirtFs::rwopsOpenRead("dir2//test.txt");
1062



8
        REQUIRE(file != nullptr);
1063
2
        const int fileSize = 23;
1064
2
        const char *restrict const str = "test line 1\ntest line 2";
1065
2
        buffer = calloc(fileSize + 1, 1);
1066
24
        for (int f = 0; f < fileSize - 1; f += 2)
1067
        {
1068




88
            REQUIRE(file->read(file, buffer, 1, 2) == 2);
1069



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1070



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
1071




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 2);
1072
        }
1073




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 22);
1074




8
        REQUIRE(file->read(file, buffer, 1, 2) == 1);
1075



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
1076
    }
1077
1078



112
    SECTION("test 7")
1079
    {
1080

8
        file = VirtFs::rwopsOpenRead("dir2//test.txt");
1081



8
        REQUIRE(file != nullptr);
1082
2
        const int fileSize = 23;
1083
2
        const char *restrict const str = "test line 1\ntest line 2";
1084
2
        buffer = calloc(fileSize + 1, 1);
1085
24
        for (int f = 0; f < fileSize - 1; f += 2)
1086
        {
1087




88
            REQUIRE(file->read(file, buffer, 1, 2) == 2);
1088




88
            REQUIRE(file->seek(file, -2, SEEK_CUR) == f);
1089




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f);
1090




88
            REQUIRE(file->seek(file, 2, SEEK_CUR) == f + 2);
1091



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1092



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
1093




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 2);
1094
        }
1095




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 22);
1096




8
        REQUIRE(file->read(file, buffer, 1, 2) == 1);
1097



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
1098
    }
1099
1100



112
    SECTION("test 8")
1101
    {
1102

8
        file = VirtFs::rwopsOpenRead("dir2//test.txt");
1103



8
        REQUIRE(file != nullptr);
1104
2
        const int fileSize = 23;
1105
2
        const char *restrict const str = "test line 1\ntest line 2";
1106
2
        buffer = calloc(fileSize + 1, 1);
1107
24
        for (int f = 0; f < fileSize - 1; f += 2)
1108
        {
1109




88
            REQUIRE(file->seek(file, -f - 2, SEEK_END) == 23 - f - 2);
1110




88
            REQUIRE(file->read(file, buffer, 1, 2) == 2);
1111



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[23 - f - 2]);
1112



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[23 - f - 2 + 1]);
1113




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == 23 - f);
1114
        }
1115
        // 3
1116




8
        REQUIRE(file->seek(file, 1, SEEK_CUR) == 4);
1117
        // 4
1118




8
        REQUIRE(file->read(file, buffer, 1, 2) == 2);
1119
        // 6
1120



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[4]);
1121



8
        REQUIRE(static_cast<char*>(buffer)[1] == str[5]);
1122




8
        REQUIRE(file->seek(file, -7, SEEK_CUR) == -1);
1123




8
        REQUIRE(file->seek(file, 6, SEEK_SET) == 6);
1124




8
        REQUIRE(file->seek(file, -6, SEEK_CUR) == 0);
1125
    }
1126
1127
16
    if (file != nullptr)
1128
16
        file->close(file);
1129
16
    free(buffer);
1130

32
    VirtFs::unmountZip(prefix + "test2.zip");
1131
16
    VirtFs::deinit();
1132
16
}
1133
1134
20
TEST_CASE("VirtFs2 rwops_read3", "")
1135
{
1136

64
    VirtFs::init(".");
1137
64
    std::string name("data/test/test.zip");
1138
32
    std::string prefix;
1139

16
    if (Files::existsLocal(name) == false)
1140
16
        prefix = "../" + prefix;
1141
1142

32
    VirtFs::mountDir(prefix + "data",
1143
        Append_false);
1144
16
    SDL_RWops *file = nullptr;
1145
16
    void *restrict buffer = nullptr;
1146
1147



112
    SECTION("test 1")
1148
    {
1149

8
        file = VirtFs::rwopsOpenRead("test/test.txt");
1150



8
        REQUIRE(file != nullptr);
1151
#ifdef USE_SDL2
1152
        REQUIRE(file->size(file) == 23);
1153
#endif  // USE_SDL2
1154
2
        const int fileSize = 23;
1155
1156
2
        buffer = calloc(fileSize + 1, 1);
1157




8
        REQUIRE(file->read(file, buffer, 1, fileSize) == fileSize);
1158



8
        REQUIRE(strcmp(static_cast<char*>(buffer),
1159
            "test line 1\ntest line 2") == 0);
1160




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == fileSize);
1161
    }
1162
1163



112
    SECTION("test 2")
1164
    {
1165

8
        file = VirtFs::rwopsOpenRead("test\\test.txt");
1166



8
        REQUIRE(file != nullptr);
1167
#ifdef USE_SDL2
1168
        REQUIRE(file->size(file) == 23);
1169
#endif  // USE_SDL2
1170
2
        const int fileSize = 23;
1171
1172
2
        buffer = calloc(fileSize + 1, 1);
1173




8
        REQUIRE(file->seek(file, 12, SEEK_SET) != 0);
1174




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 12);
1175




8
        REQUIRE(file->read(file, buffer, 1, 11) == 11);
1176



8
        REQUIRE(strcmp(static_cast<char*>(buffer),
1177
            "test line 2") == 0);
1178
    }
1179
1180



112
    SECTION("test 3")
1181
    {
1182

8
        file = VirtFs::rwopsOpenRead("test\\/test.txt");
1183



8
        REQUIRE(file != nullptr);
1184
2
        const int fileSize = 23;
1185
1186
2
        buffer = calloc(fileSize + 1, 1);
1187
48
        for (int f = 0; f < fileSize; f ++)
1188
        {
1189




184
            REQUIRE(file->seek(file, f, SEEK_SET) == f);
1190




184
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f);
1191
        }
1192
    }
1193
1194



112
    SECTION("test 4")
1195
    {
1196

8
        file = VirtFs::rwopsOpenRead("test/test.txt");
1197



8
        REQUIRE(file != nullptr);
1198
2
        const int fileSize = 23;
1199
2
        const char *restrict const str = "test line 1\ntest line 2";
1200
2
        buffer = calloc(fileSize + 1, 1);
1201
46
        for (int f = 0; f < fileSize - 1; f ++)
1202
        {
1203




176
            REQUIRE(file->read(file, buffer, 1, 1) == 1);
1204



176
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1205




176
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 1);
1206
        }
1207




8
        REQUIRE(file->read(file, buffer, 1, 1) == 1);
1208



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
1209




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == fileSize);
1210
    }
1211
1212



112
    SECTION("test 5")
1213
    {
1214

8
        file = VirtFs::rwopsOpenRead("test///test.txt");
1215



8
        REQUIRE(file != nullptr);
1216
2
        const int fileSize = 23;
1217
2
        const char *restrict const str = "test line 1\ntest line 2";
1218
2
        buffer = calloc(fileSize + 1, 1);
1219
24
        for (int f = 0; f < fileSize - 1; f += 2)
1220
        {
1221




88
            REQUIRE(file->read(file, buffer, 2, 1) == 1);
1222



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1223



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
1224




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 2);
1225
        }
1226




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 22);
1227




8
        REQUIRE(file->read(file, buffer, 2, 1) == 0);
1228
    }
1229
1230



112
    SECTION("test 6")
1231
    {
1232

8
        file = VirtFs::rwopsOpenRead("test\\\\test.txt");
1233



8
        REQUIRE(file != nullptr);
1234
2
        const int fileSize = 23;
1235
2
        const char *restrict const str = "test line 1\ntest line 2";
1236
2
        buffer = calloc(fileSize + 1, 1);
1237
24
        for (int f = 0; f < fileSize - 1; f += 2)
1238
        {
1239




88
            REQUIRE(file->read(file, buffer, 1, 2) == 2);
1240



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1241



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
1242




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 2);
1243
        }
1244




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 22);
1245




8
        REQUIRE(file->read(file, buffer, 1, 2) == 1);
1246



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
1247
    }
1248
1249



112
    SECTION("test 7")
1250
    {
1251

8
        file = VirtFs::rwopsOpenRead("test//test.txt");
1252



8
        REQUIRE(file != nullptr);
1253
2
        const int fileSize = 23;
1254
2
        const char *restrict const str = "test line 1\ntest line 2";
1255
2
        buffer = calloc(fileSize + 1, 1);
1256
24
        for (int f = 0; f < fileSize - 1; f += 2)
1257
        {
1258




88
            REQUIRE(file->read(file, buffer, 1, 2) == 2);
1259




88
            REQUIRE(file->seek(file, -2, SEEK_CUR) == f);
1260




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f);
1261




88
            REQUIRE(file->seek(file, 2, SEEK_CUR) == f + 2);
1262



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[f]);
1263



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[f + 1]);
1264




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == f + 2);
1265
        }
1266




8
        REQUIRE(file->seek(file, 0, SEEK_CUR) == 22);
1267




8
        REQUIRE(file->read(file, buffer, 1, 2) == 1);
1268



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[22]);
1269
    }
1270
1271



112
    SECTION("test 8")
1272
    {
1273

8
        file = VirtFs::rwopsOpenRead("test/test.txt");
1274



8
        REQUIRE(file != nullptr);
1275
2
        const int fileSize = 23;
1276
2
        const char *restrict const str = "test line 1\ntest line 2";
1277
2
        buffer = calloc(fileSize + 1, 1);
1278
24
        for (int f = 0; f < fileSize - 1; f += 2)
1279
        {
1280




88
            REQUIRE(file->seek(file, -f - 2, SEEK_END) == 23 - f - 2);
1281




88
            REQUIRE(file->read(file, buffer, 1, 2) == 2);
1282



88
            REQUIRE(static_cast<char*>(buffer)[0] == str[23 - f - 2]);
1283



88
            REQUIRE(static_cast<char*>(buffer)[1] == str[23 - f - 2 + 1]);
1284




88
            REQUIRE(file->seek(file, 0, SEEK_CUR) == 23 - f);
1285
        }
1286
        // 3
1287




8
        REQUIRE(file->seek(file, 1, SEEK_CUR) == 4);
1288
        // 4
1289




8
        REQUIRE(file->read(file, buffer, 1, 2) == 2);
1290
        // 6
1291



8
        REQUIRE(static_cast<char*>(buffer)[0] == str[4]);
1292



8
        REQUIRE(static_cast<char*>(buffer)[1] == str[5]);
1293




8
        REQUIRE(file->seek(file, -7, SEEK_CUR) == -1);
1294




8
        REQUIRE(file->seek(file, 6, SEEK_SET) == 6);
1295




8
        REQUIRE(file->seek(file, -6, SEEK_CUR) == 0);
1296
    }
1297
1298
16
    if (file != nullptr)
1299
16
        file->close(file);
1300
16
    free(buffer);
1301

32
    VirtFs::unmountDir(prefix + "data");
1302
16
    VirtFs::deinit();
1303
16
}
1304
1305
6
TEST_CASE("VirtFs2 getFiles zip1", "")
1306
{
1307

8
    VirtFs::init(".");
1308
8
    std::string name("data/test/test.zip");
1309
4
    std::string prefix;
1310

2
    if (Files::existsLocal(name) == false)
1311
2
        prefix = "../" + prefix;
1312
1313

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
1314
        Append_false);
1315
1316
4
    StringVect list;
1317

8
    VirtFs::getFiles("dir", list);
1318



10
    REQUIRE(list.size() == 2);
1319




12
    REQUIRE(inList(list, "dye.png"));
1320




12
    REQUIRE(inList(list, "hide.png"));
1321
1322
2
    list.clear();
1323

8
    VirtFs::getFiles("dir2", list);
1324



10
    REQUIRE(list.size() == 4);
1325




12
    REQUIRE(inList(list, "hide.png"));
1326




12
    REQUIRE(inList(list, "paths.xml"));
1327




12
    REQUIRE(inList(list, "test.txt"));
1328




12
    REQUIRE(inList(list, "units.xml"));
1329
1330

4
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
1331
2
    VirtFs::deinit();
1332
2
}
1333
1334
6
TEST_CASE("VirtFs2 getFiles zip2", "")
1335
{
1336

8
    VirtFs::init(".");
1337
8
    std::string name("data/test/test.zip");
1338
4
    std::string prefix;
1339

2
    if (Files::existsLocal(name) == false)
1340
2
        prefix = "../" + prefix;
1341
1342

10
    VirtFs::mountZip2(prefix + "data/test/test2.zip",
1343
        "dir",
1344
        Append_false);
1345
1346
4
    StringVect list;
1347

8
    VirtFs::getFiles(dirSeparator, list);
1348



10
    REQUIRE(list.size() == 2);
1349




12
    REQUIRE(inList(list, "dye.png"));
1350




12
    REQUIRE(inList(list, "hide.png"));
1351
1352
2
    list.clear();
1353

8
    VirtFs::getFiles("1", list);
1354



10
    REQUIRE(list.size() == 2);
1355




12
    REQUIRE(inList(list, "file1.txt"));
1356




12
    REQUIRE(inList(list, "test.txt"));
1357
1358

10
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
1359
        "dir");
1360
2
    VirtFs::deinit();
1361
2
}
1362
1363
6
TEST_CASE("VirtFs2 getDirs1", "")
1364
{
1365

8
    VirtFs::init(".");
1366
8
    std::string name("data/test/test.zip");
1367
4
    std::string prefix;
1368

2
    if (Files::existsLocal(name) == false)
1369
2
        prefix = "../" + prefix;
1370
1371

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
1372
        Append_false);
1373
1374
4
    StringVect list;
1375

8
    VirtFs::getDirs("dir", list);
1376



10
    REQUIRE(list.size() == 2);
1377




12
    REQUIRE(inList(list, "1"));
1378




12
    REQUIRE(inList(list, "gpl"));
1379
2
    list.clear();
1380
1381

8
    VirtFs::getDirs("dir2", list);
1382



8
    REQUIRE(list.empty());
1383
1384

4
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
1385
2
    VirtFs::deinit();
1386
2
}
1387
1388
8
TEST_CASE("VirtFs2 getDirs2", "")
1389
{
1390

16
    VirtFs::init(".");
1391
16
    std::string name("data/test/test.zip");
1392
8
    std::string prefix;
1393

4
    if (Files::existsLocal(name) == false)
1394
4
        prefix = "../" + prefix;
1395
8
    StringVect list;
1396
1397



28
    SECTION("dir1")
1398
    {
1399

4
        VirtFs::mountDir(prefix + "data/test",
1400
            Append_false);
1401
1402

8
        VirtFs::getDirs("/", list);
1403
//        REQUIRE(list.size() == 2);
1404




12
        REQUIRE(inList(list, "dir1"));
1405




12
        REQUIRE(inList(list, "dir2"));
1406
2
        list.clear();
1407
1408

8
        VirtFs::getDirs("dir1", list);
1409



8
        REQUIRE(list.empty());
1410
1411

4
        VirtFs::unmountDir(prefix + "data/test");
1412
    }
1413
1414



28
    SECTION("dir2")
1415
    {
1416

4
        VirtFs::mountDir(prefix + "data",
1417
            Append_false);
1418
1419

8
        VirtFs::getDirs("sfx", list);
1420




12
        REQUIRE(inList(list, "system"));
1421
2
        list.clear();
1422
1423

8
        VirtFs::getDirs("evol", list);
1424



10
        REQUIRE(list.size() == 2);
1425




12
        REQUIRE(inList(list, "icons"));
1426




12
        REQUIRE(inList(list, "images"));
1427
1428

4
        VirtFs::unmountDir(prefix + "data");
1429
    }
1430
1431
4
    VirtFs::deinit();
1432
4
}
1433
1434
6
TEST_CASE("VirtFs2 getDirs3", "")
1435
{
1436

8
    VirtFs::init(".");
1437
8
    std::string name("data/test/test.zip");
1438
4
    std::string prefix;
1439

2
    if (Files::existsLocal(name) == false)
1440
2
        prefix = "../" + prefix;
1441
1442

10
    VirtFs::mountZip2(prefix + "data/test/test2.zip",
1443
        "dir",
1444
        Append_false);
1445
1446
4
    StringVect list;
1447

8
    VirtFs::getDirs(dirSeparator, list);
1448



10
    REQUIRE(list.size() == 2);
1449




12
    REQUIRE(inList(list, "1"));
1450




12
    REQUIRE(inList(list, "gpl"));
1451
2
    list.clear();
1452
1453

8
    VirtFs::getDirs("1", list);
1454



8
    REQUIRE(list.empty());
1455
1456

10
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
1457
        "dir");
1458
2
    VirtFs::deinit();
1459
2
}
1460
1461
6
TEST_CASE("VirtFs2 getDirs4", "")
1462
{
1463

8
    VirtFs::init(".");
1464
8
    std::string name("data/test/test.zip");
1465
4
    std::string prefix;
1466

2
    if (Files::existsLocal(name) == false)
1467
2
        prefix = "../" + prefix;
1468
4
    StringVect list;
1469
1470



14
    SECTION("dir1")
1471
    {
1472

10
        VirtFs::mountDir2(prefix + "data",
1473
            "test",
1474
            Append_false);
1475
1476

8
        VirtFs::getDirs("/", list);
1477




12
        REQUIRE(inList(list, "dir1"));
1478




12
        REQUIRE(inList(list, "dir2"));
1479
2
        list.clear();
1480
1481

8
        VirtFs::getDirs("dir1", list);
1482



8
        REQUIRE(list.empty());
1483
1484

10
        VirtFs::unmountDir2(prefix + "data",
1485
            "test");
1486
    }
1487
1488
2
    VirtFs::deinit();
1489
2
}
1490
1491
6
TEST_CASE("VirtFs2 getFilesWithDir1", "")
1492
{
1493

8
    VirtFs::init(".");
1494
8
    std::string name("data/test/test.zip");
1495
4
    std::string prefix;
1496

2
    if (Files::existsLocal(name) == false)
1497
2
        prefix = "../" + prefix;
1498
1499

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
1500
        Append_false);
1501
1502
4
    StringVect list;
1503

8
    VirtFs::getFilesWithDir("dir", list);
1504



10
    REQUIRE(list.size() == 2);
1505





18
    REQUIRE(inList(list, "dir", "dye.png"));
1506





18
    REQUIRE(inList(list, "dir", "hide.png"));
1507
2
    list.clear();
1508
1509

8
    VirtFs::getFilesWithDir("dir2", list);
1510



10
    REQUIRE(list.size() == 4);
1511





18
    REQUIRE(inList(list, "dir2", "hide.png"));
1512





18
    REQUIRE(inList(list, "dir2", "paths.xml"));
1513





18
    REQUIRE(inList(list, "dir2", "test.txt"));
1514





18
    REQUIRE(inList(list, "dir2", "units.xml"));
1515
2
    list.clear();
1516
1517

8
    VirtFs::getFilesWithDir(dirSeparator, list);
1518



10
    REQUIRE(list.size() > 2);
1519





18
    REQUIRE(inList(list, dirSeparator, "test.txt"));
1520





18
    REQUIRE(inList(list, dirSeparator, "units.xml"));
1521
2
    list.clear();
1522
1523

4
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
1524
2
    VirtFs::deinit();
1525
2
}
1526
1527
8
TEST_CASE("VirtFs2 getFilesWithDir2", "")
1528
{
1529

16
    VirtFs::init(".");
1530
16
    std::string name("data/test/test.zip");
1531
8
    std::string prefix;
1532

4
    if (Files::existsLocal(name) == false)
1533
4
        prefix = "../" + prefix;
1534
8
    StringVect list;
1535
1536



28
    SECTION("dir1")
1537
    {
1538

4
        VirtFs::mountDir(prefix + "data/graphics",
1539
            Append_false);
1540
1541