GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs2.cc Lines: 1186 1188 99.8 %
Date: 2018-07-14 Branches: 3861 9494 40.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2018  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
static bool inList(StringVect list,
44
                   const std::string &name)
45
{
46










121
    FOR_EACH (StringVectCIter, it, list)
47
    {
48










37
        if (*it == name)
49
            return true;
50
    }
51
    return false;
52
}
53
54
17
static bool inList(StringVect list,
55
                   const std::string &dir,
56
                   const std::string &name)
57
{
58
51
    const std::string path = pathJoin(dir, name);
59
111
    FOR_EACH (StringVectCIter, it, list)
60
    {
61
43
        if (*it == path)
62
            return true;
63
    }
64
    return false;
65
}
66
67
3
static void removeTemp(StringVect &restrict list)
68
{
69
3
    int cnt = 0;
70
9
    std::sort(list.begin(), list.end());
71
72
90
    FOR_EACH (StringVectIter, it, list)
73
    {
74
162
        if (*it != "serverlistplus.xml.part")
75
        {
76
81
            logger->log("file: %d %s",
77
                cnt,
78
162
                (*it).c_str());
79
81
            cnt ++;
80
        }
81
    }
82
83
90
    FOR_EACH (StringVectIter, it, list)
84
    {
85
162
        if (*it == "serverlistplus.xml.part")
86
        {
87
            list.erase(it);
88
            return;
89
        }
90
    }
91
}
92
93
3
TEST_CASE("VirtFs2 isDirectory1", "")
94
{
95
4
    VirtFs::init(".");
96
4
    std::string name("data/test/test.zip");
97
2
    std::string prefix;
98

1
    if (Files::existsLocal(name) == false)
99
2
        prefix = "../" + prefix;
100
101
2
    VirtFs::mountDir(prefix + "data",
102
1
        Append_false);
103
104




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




7
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
124
125
2
    VirtFs::mountDir(prefix + "data/test",
126
1
        Append_false);
127
128




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




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




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




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




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




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




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




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




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




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

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




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




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




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




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




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




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




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




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




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

2
    VirtFs::unmountDirSilent(prefix + "data");
152
1
    VirtFs::deinit();
153
1
}
154
155
3
TEST_CASE("VirtFs2 isDirectory2", "")
156
{
157
4
    VirtFs::init(".");
158
4
    std::string name("data/test/test.zip");
159
2
    std::string prefix;
160

1
    if (Files::existsLocal(name) == false)
161
2
        prefix = "../" + prefix;
162
163
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
164
1
        Append_false);
165
166




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




7
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
186
187
2
    VirtFs::mountZip(prefix + "data/test/test.zip",
188
1
        Append_false);
189
190




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

2
    VirtFs::unmountZip(prefix + "data/test/test.zip");
235
1
    VirtFs::deinit();
236
1
}
237
238
3
TEST_CASE("VirtFs2 isDirectory3", "")
239
{
240
4
    VirtFs::init(".");
241
4
    std::string name("data/test/test.zip");
242
2
    std::string prefix;
243

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

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




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




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




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




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




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




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




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




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




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




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




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




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

5
    VirtFs::unmountDirSilent2(prefix + "data",
264
1
        "test");
265
1
    VirtFs::deinit();
266
1
}
267
268
3
TEST_CASE("VirtFs2 isDirectory4", "")
269
{
270
4
    VirtFs::init(".");
271
4
    std::string name("data/test/test.zip");
272
2
    std::string prefix;
273

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

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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

5
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
299
1
        "dir");
300
1
    VirtFs::deinit();
301
1
}
302
303
3
TEST_CASE("VirtFs2 openRead1", "")
304
{
305
4
    VirtFs::init(".");
306
4
    std::string name("data/test/test.zip");
307
2
    std::string prefix;
308

1
    if (Files::existsLocal(name) == false)
309
2
        prefix = "../" + prefix;
310
311
2
    VirtFs::mountDir(prefix + "data",
312
1
        Append_false);
313
314
1
    VirtFs::File *file = nullptr;
315
316

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



4
    REQUIRE(file != nullptr);
318
1
    VirtFs::close(file);
319

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



4
    REQUIRE(file != nullptr);
321
1
    VirtFs::close(file);
322

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



4
    REQUIRE(file == nullptr);
324

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



4
    REQUIRE(file == nullptr);
326

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



4
    REQUIRE(file == nullptr);
328

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



4
    REQUIRE(file == nullptr);
330
331
2
    VirtFs::mountDir(prefix + "data/test",
332
1
        Append_false);
333
334

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



4
    REQUIRE(file != nullptr);
336
1
    VirtFs::close(file);
337

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



4
    REQUIRE(file == nullptr);
339

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



4
    REQUIRE(file == nullptr);
341

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



4
    REQUIRE(file != nullptr);
343
1
    VirtFs::close(file);
344

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



4
    REQUIRE(file == nullptr);
346
347

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

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



4
    REQUIRE(file != nullptr);
351
1
    VirtFs::close(file);
352

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



4
    REQUIRE(file == nullptr);
354

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



4
    REQUIRE(file == nullptr);
356

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



4
    REQUIRE(file == nullptr);
358

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



4
    REQUIRE(file == nullptr);
360
361

2
    VirtFs::unmountDir(prefix + "data");
362
1
    VirtFs::deinit();
363
1
}
364
365
3
TEST_CASE("VirtFs2 openRead2", "")
366
{
367
4
    VirtFs::init(".");
368
4
    std::string name("data/test/test.zip");
369
4
    std::string prefix("data/test/");
370

1
    if (Files::existsLocal(name) == false)
371
2
        prefix = "../" + prefix;
372
373
2
    VirtFs::mountZip(prefix + "test2.zip",
374
1
        Append_false);
375
376
1
    VirtFs::File *file = nullptr;
377
378

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



4
    REQUIRE(file != nullptr);
380
1
    VirtFs::close(file);
381

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



4
    REQUIRE(file != nullptr);
383
1
    VirtFs::close(file);
384

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



4
    REQUIRE(file == nullptr);
386

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



4
    REQUIRE(file == nullptr);
388

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



4
    REQUIRE(file == nullptr);
390

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



4
    REQUIRE(file == nullptr);
392

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



4
    REQUIRE(file == nullptr);
394

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



4
    REQUIRE(file == nullptr);
396
397
2
    VirtFs::mountZip(prefix + "test.zip",
398
1
        Append_false);
399
400

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



4
    REQUIRE(file != nullptr);
402
1
    VirtFs::close(file);
403

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



4
    REQUIRE(file != nullptr);
405
1
    VirtFs::close(file);
406

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



4
    REQUIRE(file == nullptr);
408

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



4
    REQUIRE(file == nullptr);
410

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



4
    REQUIRE(file == nullptr);
412

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



4
    REQUIRE(file == nullptr);
414

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



4
    REQUIRE(file != nullptr);
416
1
    VirtFs::close(file);
417
418

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

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



4
    REQUIRE(file != nullptr);
422
1
    VirtFs::close(file);
423

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



4
    REQUIRE(file != nullptr);
425
1
    VirtFs::close(file);
426

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



4
    REQUIRE(file == nullptr);
428

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



4
    REQUIRE(file == nullptr);
430

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



4
    REQUIRE(file == nullptr);
432

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



4
    REQUIRE(file == nullptr);
434

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



4
    REQUIRE(file == nullptr);
436
437

2
    VirtFs::unmountZip(prefix + "test2.zip");
438
439
1
    VirtFs::deinit();
440
1
}
441
442
3
TEST_CASE("VirtFs2 openRead3", "")
443
{
444
4
    VirtFs::init(".");
445
4
    std::string name("data/test/test.zip");
446
2
    std::string prefix;
447

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

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

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



4
    REQUIRE(file != nullptr);
458
1
    VirtFs::close(file);
459

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



4
    REQUIRE(file == nullptr);
461

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



4
    REQUIRE(file == nullptr);
463

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



4
    REQUIRE(file == nullptr);
465

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



4
    REQUIRE(file == nullptr);
467

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



4
    REQUIRE(file == nullptr);
469

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



4
    REQUIRE(file == nullptr);
471
472

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

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



4
    REQUIRE(file != nullptr);
478
1
    VirtFs::close(file);
479

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



4
    REQUIRE(file == nullptr);
481

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



4
    REQUIRE(file == nullptr);
483

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



4
    REQUIRE(file == nullptr);
485

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



4
    REQUIRE(file == nullptr);
487

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



4
    REQUIRE(file != nullptr);
489
1
    VirtFs::close(file);
490

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



4
    REQUIRE(file != nullptr);
492
1
    VirtFs::close(file);
493
494

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

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



4
    REQUIRE(file != nullptr);
499
1
    VirtFs::close(file);
500

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



4
    REQUIRE(file == nullptr);
502

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



4
    REQUIRE(file == nullptr);
504

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



4
    REQUIRE(file == nullptr);
506

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



4
    REQUIRE(file == nullptr);
508

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



4
    REQUIRE(file == nullptr);
510

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



4
    REQUIRE(file == nullptr);
512
513

5
    VirtFs::unmountDir2(prefix + "data",
514
1
        "test");
515
1
    VirtFs::deinit();
516
1
}
517
518
3
TEST_CASE("VirtFs2 openRead4", "")
519
{
520
4
    VirtFs::init(".");
521
4
    std::string name("data/test/test.zip");
522
4
    std::string prefix("data/test/");
523

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

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

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



4
    REQUIRE(file != nullptr);
534
1
    VirtFs::close(file);
535

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



4
    REQUIRE(file != nullptr);
537
1
    VirtFs::close(file);
538

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



4
    REQUIRE(file == nullptr);
540

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



4
    REQUIRE(file == nullptr);
542

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



4
    REQUIRE(file == nullptr);
544

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



4
    REQUIRE(file == nullptr);
546

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



4
    REQUIRE(file == nullptr);
548
549

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

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



4
    REQUIRE(file != nullptr);
555
1
    VirtFs::close(file);
556

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



4
    REQUIRE(file != nullptr);
558
1
    VirtFs::close(file);
559

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



4
    REQUIRE(file == nullptr);
561

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



4
    REQUIRE(file == nullptr);
563

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



4
    REQUIRE(file == nullptr);
565

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



4
    REQUIRE(file == nullptr);
567

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



4
    REQUIRE(file != nullptr);
569
1
    VirtFs::close(file);
570
571

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

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



4
    REQUIRE(file != nullptr);
576
1
    VirtFs::close(file);
577

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



4
    REQUIRE(file != nullptr);
579
1
    VirtFs::close(file);
580

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



4
    REQUIRE(file == nullptr);
582

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



4
    REQUIRE(file == nullptr);
584

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



4
    REQUIRE(file == nullptr);
586

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



4
    REQUIRE(file == nullptr);
588

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



4
    REQUIRE(file == nullptr);
590
591

5
    VirtFs::unmountZip2(prefix + "test2.zip",
592
1
        "dir");
593
594
1
    VirtFs::deinit();
595
1
}
596
597
3
TEST_CASE("VirtFs2 permitLinks", "")
598
{
599
4
    VirtFs::init(".");
600
4
    std::string name("data/test/test.zip");
601
2
    std::string prefix;
602

1
    if (Files::existsLocal(name) == false)
603
2
        prefix = "../" + prefix;
604
605
2
    VirtFs::mountDir(prefix + "data",
606
1
        Append_false);
607
608

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

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



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

4
    VirtFs::getFiles("test", list);
621
1
    removeTemp(list);
622



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

4
    VirtFs::getFiles("test", list);
627
1
    removeTemp(list);
628



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

2
    VirtFs::unmountDirSilent(prefix + "data");
631
1
    VirtFs::deinit();
632
1
}
633
634
3
TEST_CASE("VirtFs2 read1", "")
635
{
636
4
    VirtFs::init(".");
637
4
    std::string name("data/test/test.zip");
638
2
    std::string prefix;
639

1
    if (Files::existsLocal(name) == false)
640
2
        prefix = "../" + prefix;
641
642
2
    VirtFs::mountDir(prefix + "data",
643
1
        Append_false);
644
645

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



4
    REQUIRE(file != nullptr);
647




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




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



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




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




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




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




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




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




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



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




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

2
    VirtFs::unmountDir(prefix + "data");
671
1
    VirtFs::deinit();
672
1
}
673
674
8
TEST_CASE("VirtFs2 read2", "")
675
{
676
24
    VirtFs::init(".");
677
24
    std::string name("data/test/test.zip");
678
24
    std::string prefix("data/test/");
679

6
    if (Files::existsLocal(name) == false)
680
12
        prefix = "../" + prefix;
681
682
12
    VirtFs::mountZip(prefix + "test2.zip",
683
6
        Append_false);
684
6
    VirtFs::File *file = nullptr;
685
6
    void *restrict buffer = nullptr;
686
687



42
    SECTION("test 1")
688
    {
689

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



4
        REQUIRE(file != nullptr);
691




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




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



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




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




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



42
    SECTION("test 2")
703
    {
704

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



4
        REQUIRE(file != nullptr);
706




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




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




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




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




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



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




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



42
    SECTION("test 3")
720
    {
721

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



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




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




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




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



42
    SECTION("test 4")
735
    {
736

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



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




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



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




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




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




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



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




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




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



42
    SECTION("test 5")
755
    {
756

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



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




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



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



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




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




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




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




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




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




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



42
    SECTION("test 6")
776
    {
777

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



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




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



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



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




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




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




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




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




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



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




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

12
    VirtFs::unmountZip(prefix + "test2.zip");
800
6
    VirtFs::deinit();
801
6
}
802
803
3
TEST_CASE("VirtFs2 loadFile1", "")
804
{
805
4
    VirtFs::init(".");
806
1
    int fileSize = 0;
807
4
    std::string name("data/test/test.zip");
808
2
    std::string prefix;
809

1
    if (Files::existsLocal(name) == false)
810
2
        prefix = "../" + prefix;
811
812
2
    VirtFs::mountDir(prefix + "data",
813
1
        Append_false);
814
815

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



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



4
    REQUIRE(fileSize == 23);
818



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

2
    VirtFs::unmountDir(prefix + "data");
822
1
    VirtFs::deinit();
823
1
}
824
825
4
TEST_CASE("VirtFs2 loadFile2", "")
826
{
827
8
    VirtFs::init(".");
828
2
    int fileSize = 0;
829
8
    std::string name("data/test/test.zip");
830
4
    std::string prefix;
831

2
    if (Files::existsLocal(name) == false)
832
4
        prefix = "../" + prefix;
833
834
4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
835
2
        Append_false);
836
837



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



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



4
        REQUIRE(fileSize == 23);
843



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



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



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



4
        REQUIRE(fileSize == 23);
853



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

4
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
858
2
    VirtFs::deinit();
859
2
}
860
861
3
TEST_CASE("VirtFs2 loadFile3", "")
862
{
863
4
    VirtFs::init(".");
864
1
    int fileSize = 0;
865
4
    std::string name("data/test/test.zip");
866
2
    std::string prefix;
867

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

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

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



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



4
    REQUIRE(fileSize == 23);
877



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

5
    VirtFs::unmountDir2(prefix + "data",
881
1
        "test");
882
1
    VirtFs::deinit();
883
1
}
884
885
4
TEST_CASE("VirtFs2 loadFile4", "")
886
{
887
8
    VirtFs::init(".");
888
2
    int fileSize = 0;
889
8
    std::string name("data/test/test.zip");
890
4
    std::string prefix;
891

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

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



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



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



4
        REQUIRE(fileSize == 23);
904



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



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



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



4
        REQUIRE(fileSize == 23);
914



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

10
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
919
2
        "dir2");
920
2
    VirtFs::deinit();
921
2
}
922
923
3
TEST_CASE("VirtFs2 rwops_read1", "")
924
{
925
4
    VirtFs::init(".");
926
4
    std::string name("data/test/test.zip");
927
2
    std::string prefix;
928

1
    if (Files::existsLocal(name) == false)
929
2
        prefix = "../" + prefix;
930
931
2
    VirtFs::mountDir(prefix + "data",
932
1
        Append_false);
933
934

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



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




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



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




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




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




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




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



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

2
    VirtFs::unmountDir(prefix + "data");
960
1
    VirtFs::deinit();
961
1
}
962
963
10
TEST_CASE("VirtFs2 rwops_read2", "")
964
{
965
32
    VirtFs::init(".");
966
32
    std::string name("data/test/test.zip");
967
32
    std::string prefix("data/test/");
968

8
    if (Files::existsLocal(name) == false)
969
16
        prefix = "../" + prefix;
970
971
16
    VirtFs::mountZip(prefix + "test2.zip",
972
8
        Append_false);
973
8
    SDL_RWops *file = nullptr;
974
8
    void *restrict buffer = nullptr;
975
976



56
    SECTION("test 1")
977
    {
978

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



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




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



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




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



56
    SECTION("test 2")
993
    {
994

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



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




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




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




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



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



56
    SECTION("test 3")
1010
    {
1011

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



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




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




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



56
    SECTION("test 4")
1024
    {
1025

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



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




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



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




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




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



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




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



56
    SECTION("test 5")
1042
    {
1043

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



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




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



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



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




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




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




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



56
    SECTION("test 6")
1060
    {
1061

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



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




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



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



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




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




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




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



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



56
    SECTION("test 7")
1079
    {
1080

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



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




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




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




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




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



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



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




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




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




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



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



56
    SECTION("test 8")
1101
    {
1102

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



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




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




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



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



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




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




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




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



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



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




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




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




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

16
    VirtFs::unmountZip(prefix + "test2.zip");
1131
8
    VirtFs::deinit();
1132
8
}
1133
1134
10
TEST_CASE("VirtFs2 rwops_read3", "")
1135
{
1136
32
    VirtFs::init(".");
1137
32
    std::string name("data/test/test.zip");
1138
16
    std::string prefix;
1139

8
    if (Files::existsLocal(name) == false)
1140
16
        prefix = "../" + prefix;
1141
1142
16
    VirtFs::mountDir(prefix + "data",
1143
8
        Append_false);
1144
8
    SDL_RWops *file = nullptr;
1145
8
    void *restrict buffer = nullptr;
1146
1147



56
    SECTION("test 1")
1148
    {
1149

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



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




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



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




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



56
    SECTION("test 2")
1164
    {
1165

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



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




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




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




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



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



56
    SECTION("test 3")
1181
    {
1182

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



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




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




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



56
    SECTION("test 4")
1195
    {
1196

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



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




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



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




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




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



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




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



56
    SECTION("test 5")
1213
    {
1214

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



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




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



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



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




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




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




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



56
    SECTION("test 6")
1231
    {
1232

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



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




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



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



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




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




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




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



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



56
    SECTION("test 7")
1250
    {
1251

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



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




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




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




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




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



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



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




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




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




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



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



56
    SECTION("test 8")
1272
    {
1273

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



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




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




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



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



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




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




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




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



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



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




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




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




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

16
    VirtFs::unmountDir(prefix + "data");
1302
8
    VirtFs::deinit();
1303
8
}
1304
1305
3
TEST_CASE("VirtFs2 getFiles zip1", "")
1306
{
1307
4
    VirtFs::init(".");
1308
4
    std::string name("data/test/test.zip");
1309
2
    std::string prefix;
1310

1
    if (Files::existsLocal(name) == false)
1311
2
        prefix = "../" + prefix;
1312
1313
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
1314
1
        Append_false);
1315
1316
2
    StringVect list;
1317

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



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




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




7
    REQUIRE(inList(list, "hide.png"));
1321
1322
1
    list.clear();
1323

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



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




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




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




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




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

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
1331
1
    VirtFs::deinit();
1332
1
}
1333
1334
3
TEST_CASE("VirtFs2 getFiles zip2", "")
1335
{
1336
4
    VirtFs::init(".");
1337
4
    std::string name("data/test/test.zip");
1338
2
    std::string prefix;
1339

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

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

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



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




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




7
    REQUIRE(inList(list, "hide.png"));
1351
1352
1
    list.clear();
1353

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



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




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




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

5
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
1359
1
        "dir");
1360
1
    VirtFs::deinit();
1361
1
}
1362
1363
3
TEST_CASE("VirtFs2 getDirs1", "")
1364
{
1365
4
    VirtFs::init(".");
1366
4
    std::string name("data/test/test.zip");
1367
2
    std::string prefix;
1368

1
    if (Files::existsLocal(name) == false)
1369
2
        prefix = "../" + prefix;
1370
1371
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
1372
1
        Append_false);
1373
1374
2
    StringVect list;
1375

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



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




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




7
    REQUIRE(inList(list, "gpl"));
1379
1
    list.clear();
1380
1381

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



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

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
1385
1
    VirtFs::deinit();
1386
1
}
1387
1388
4
TEST_CASE("VirtFs2 getDirs2", "")
1389
{
1390
8
    VirtFs::init(".");
1391
8
    std::string name("data/test/test.zip");
1392
4
    std::string prefix;
1393

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



14
    SECTION("dir1")
1398
    {
1399
2
        VirtFs::mountDir(prefix + "data/test",
1400
1
            Append_false);
1401
1402

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




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




7
        REQUIRE(inList(list, "dir2"));
1406
1
        list.clear();
1407
1408

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



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

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



14
    SECTION("dir2")
1415
    {
1416
2
        VirtFs::mountDir(prefix + "data",
1417
1
            Append_false);
1418
1419

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




7
        REQUIRE(inList(list, "system"));
1421
1
        list.clear();
1422
1423

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



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




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




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

2
        VirtFs::unmountDir(prefix + "data");
1429
    }
1430
1431
2
    VirtFs::deinit();
1432
2
}
1433
1434
3
TEST_CASE("VirtFs2 getDirs3", "")
1435
{
1436
4
    VirtFs::init(".");
1437
4
    std::string name("data/test/test.zip");
1438
2
    std::string prefix;
1439

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

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

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



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




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




7
    REQUIRE(inList(list, "gpl"));
1451
1
    list.clear();
1452
1453

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



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

5
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
1457
1
        "dir");
1458
1
    VirtFs::deinit();
1459
1
}
1460
1461
3
TEST_CASE("VirtFs2 getDirs4", "")
1462
{
1463
4
    VirtFs::init(".");
1464
4
    std::string name("data/test/test.zip");
1465
2
    std::string prefix;
1466

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



7
    SECTION("dir1")
1471
    {
1472

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

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




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




7
        REQUIRE(inList(list, "dir2"));
1479
1
        list.clear();
1480
1481

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



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

5
        VirtFs::unmountDir2(prefix + "data",
1485
1
            "test");
1486
    }
1487
1488
1
    VirtFs::deinit();
1489
1
}
1490
1491
3
TEST_CASE("VirtFs2 getFilesWithDir1", "")
1492
{
1493
4
    VirtFs::init(".");
1494
4
    std::string name("data/test/test.zip");
1495
2
    std::string prefix;
1496

1
    if (Files::existsLocal(name) == false)
1497
2
        prefix = "../" + prefix;
1498
1499
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
1500
1
        Append_false);
1501
1502
2
    StringVect list;
1503

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



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





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





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

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



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





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





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





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





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

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



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





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





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

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
1524
1
    VirtFs::deinit();
1525
1
}
1526
1527
4
TEST_CASE("VirtFs2 getFilesWithDir2", "")
1528
{
1529
8
    VirtFs::init(".");
1530
8
    std::string name("data/test/test.zip");
1531
4
    std::string prefix;
1532

2
</