aboutsummaryrefslogtreecommitdiff
path: root/tests/filesystem.cpp
blob: 18a94c8ff76f83af7f3f219cfc9e5ed669d97573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "testUtils.h"

using namespace irr;
using namespace core;
using namespace io;

static bool testgetAbsoluteFilename(io::IFileSystem* fs)
{
	bool result=true;
	io::path apath = fs->getAbsolutePath("media");
	io::path cwd = fs->getWorkingDirectory();
	if (apath!=(cwd+"/media"))
	{
		logTestString("getAbsolutePath failed on existing dir %s\n", apath.c_str());
		result = false;
	}

	apath = fs->getAbsolutePath("../media/");
	core::deletePathFromPath(cwd, 1);
	if (apath!=(cwd+"media/"))
	{
		logTestString("getAbsolutePath failed on dir with postfix / %s\n", apath.c_str());
		result = false;
	}

	apath = fs->getAbsolutePath ("../nothere.txt");   // file does not exist
	if (apath!=(cwd+"nothere.txt"))
	{
		logTestString("getAbsolutePath failed on non-existing file %s\n", apath.c_str());
		result = false;
	}

	return result;
}

static bool testFlattenFilename(io::IFileSystem* fs)
{
	bool result=true;
	io::path tmpString="../tmp";
	io::path refString="../tmp/";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	tmpString="tmp/tmp/../";
	refString="tmp/";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	tmpString="tmp/tmp/..";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	tmpString="tmp/next/../third";
	refString="tmp/third/";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	tmpString="this/tmp/next/../../my/fourth";
	refString="this/my/fourth/";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	tmpString="this/is/../../../a/fifth/test/";
	refString="../a/fifth/test/";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	tmpString="this/../is/../../a/sixth/test/";
	refString="../a/sixth/test/";
	fs->flattenFilename(tmpString);
	if (tmpString != refString)
	{
		logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
		result = false;
	}

	return result;
}

static bool testgetRelativeFilename(io::IFileSystem* fs)
{
	bool result=true;
	io::path apath = fs->getAbsolutePath("media");
	io::path cwd = fs->getWorkingDirectory();
	if (fs->getRelativeFilename(apath, cwd) != "media")
	{
		logTestString("getRelativePath failed on %s\n", apath.c_str());
		result = false;
	}

	apath = fs->getAbsolutePath("../media/");
	if (fs->getRelativeFilename(apath, cwd) != "../media/")
	{
		logTestString("getRelativePath failed on %s\n", apath.c_str());
		result = false;
	}

	return result;
}

bool filesystem(void)
{
	IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d<u32>(1, 1));
	assert(device);
	if(!device)
		return false;
	
	io::IFileSystem * fs = device->getFileSystem ();
	if ( !fs )
		return false;
	
	bool result = true;
	
	io::path workingDir = device->getFileSystem()->getWorkingDirectory();
	
	io::path empty;
	if ( fs->existFile(empty) )
	{
		logTestString("Empty filename should not exist.\n");
		result = false;
	}
	
	io::path newWd = workingDir + "/media";
	bool changed = device->getFileSystem()->changeWorkingDirectoryTo(newWd);
	assert(changed);
	
	if ( fs->existFile(empty) )
	{
		logTestString("Empty filename should not exist even in another workingdirectory.\n");
		result = false;
	}
	
	// The working directory must be restored for the other tests to work.
	changed = device->getFileSystem()->changeWorkingDirectoryTo(workingDir.c_str());
	assert(changed);
	
	// adding  a folder archive which just should not really change anything
	device->getFileSystem()->addFileArchive( "./" );
	
	if ( fs->existFile(empty) )
	{
		logTestString("Empty filename should not exist in folder file archive.\n");
		result = false;
	}
	
	// remove it again to not affect other tests
	device->getFileSystem()->removeFileArchive( device->getFileSystem()->getFileArchiveCount() );

	result &= testFlattenFilename(fs);
	result &= testgetAbsoluteFilename(fs);
	result &= testgetRelativeFilename(fs);

	device->closeDevice();
	device->run();
	device->drop();

	return result;
}