summaryrefslogtreecommitdiff
path: root/tools/slang-unit-test/unit-test-file-system.cpp
blob: e3e2bb3cf0b81fb3e4dd337158338e82c6d2ba3e (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// unit-test-file-system.cpp

#include "../../source/core/slang-file-system.h"

#include "../../source/core/slang-riff-file-system.h"
#include "../../source/core/slang-zip-file-system.h"

#include "../../source/core/slang-memory-file-system.h"

#include "../../source/core/slang-deflate-compression-system.h"
#include "../../source/core/slang-lz4-compression-system.h"

#include "tools/unit-test/slang-unit-test.h"

using namespace Slang;

namespace { 

enum class FileSystemType
{
	Zip,
	RiffUncompressed,
	RiffDeflate,
	RiffLZ4,
	Memory,
	Relative,
	CountOf,
};

struct Entry 
{
	typedef Entry ThisType;

	bool operator<(const ThisType& rhs) const { return name < rhs.name; }
	bool operator==(const ThisType& rhs) const { return name == rhs.name && type == rhs.type; }
	bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }

	SlangPathType type;
	String name;
};

} // 

static SlangResult _createAndCheckFile(ISlangMutableFileSystem* fileSystem, const char* path, const char* contents)
{
	UnownedStringSlice contentsSlice(contents);

	SLANG_RETURN_ON_FAIL(fileSystem->saveFile(path, contentsSlice.begin(), contentsSlice.getLength()));

	SlangPathType pathType;
	SLANG_RETURN_ON_FAIL(fileSystem->getPathType(path, &pathType));

	if (pathType != SLANG_PATH_TYPE_FILE)
	{
		return SLANG_FAIL;
	}

	ComPtr<ISlangBlob> blob;
	SLANG_RETURN_ON_FAIL(fileSystem->loadFile(path, blob.writeRef()));

	if (blob->getBufferSize() != contentsSlice.getLength())
	{
		return SLANG_FAIL;
	}
	if (contentsSlice != UnownedStringSlice((const char*)blob->getBufferPointer(), blob->getBufferSize()))
	{
		return SLANG_FAIL;
	}

	return SLANG_OK;
}

static SlangResult _createAndCheckDirectory(ISlangMutableFileSystem* fileSystem, const char* path)
{
	SLANG_RETURN_ON_FAIL(fileSystem->createDirectory(path));

	SlangPathType pathType;
	SLANG_RETURN_ON_FAIL(fileSystem->getPathType(path, &pathType));

	if (pathType != SLANG_PATH_TYPE_DIRECTORY)
	{
		return SLANG_FAIL;
	}

	return SLANG_OK;
}

static void _entryCallback(SlangPathType pathType, const char* name, void* userData)
{
	List<Entry>& out = *(List<Entry>*)userData;
	out.add(Entry{pathType, name});
}

static SlangResult _enumeratePath(ISlangMutableFileSystem* fileSystem, const char* path, const ConstArrayView<Entry>& entries)
{
	List<Entry> contents;
	
	SLANG_RETURN_ON_FAIL(fileSystem->enumeratePathContents(path, _entryCallback, (void*)&contents));
	
	contents.sort();

	if (contents.getArrayView() != entries)
	{
		return SLANG_FAIL;
	}

	return SLANG_OK;
}

static SlangResult _checkSimplifiedPath(ISlangMutableFileSystem* fileSystem, const char* path, const char* normalPath)
{
	ComPtr<ISlangBlob> simplifiedPathBlob;
	SLANG_RETURN_ON_FAIL(fileSystem->getSimplifiedPath(path, simplifiedPathBlob.writeRef()));

	auto simplifiedPath = StringUtil::getString(simplifiedPathBlob);

	if (simplifiedPath != normalPath)
	{
		return SLANG_FAIL;
	}

	return SLANG_OK;
}

static SlangResult _test(FileSystemType type)
{
	ComPtr<ISlangMutableFileSystem> fileSystem;

	switch (type)
	{
		case FileSystemType::Zip:
		{
			SLANG_RETURN_ON_FAIL(ZipFileSystem::create(fileSystem));
			break;
		}
		case FileSystemType::RiffUncompressed:
		{
			fileSystem = new RiffFileSystem(nullptr);
			break;
		}
		case FileSystemType::RiffDeflate:
		{
			fileSystem = new RiffFileSystem(DeflateCompressionSystem::getSingleton());
			break;
		}
		case FileSystemType::RiffLZ4:
		{
			fileSystem = new RiffFileSystem(LZ4CompressionSystem::getSingleton());
			break;
		}
		case FileSystemType::Memory:
		{
			fileSystem = new MemoryFileSystem;
			break;
		}
		case FileSystemType::Relative:
		{
			ComPtr<ISlangMutableFileSystem> memoryFileSystem(new MemoryFileSystem);
			memoryFileSystem->createDirectory("base");

			fileSystem = new RelativeFileSystem(memoryFileSystem, "base");
			break;
		}
	}

	SLANG_RETURN_ON_FAIL(_createAndCheckFile(fileSystem, "a", "someText"));
	SLANG_RETURN_ON_FAIL(_createAndCheckFile(fileSystem, "b", "A longer bit of text...."));

	SLANG_RETURN_ON_FAIL(_createAndCheckDirectory(fileSystem, "d"));
	SLANG_RETURN_ON_FAIL(_createAndCheckFile(fileSystem, "d/a", "Some more silly stuff"));
	SLANG_RETURN_ON_FAIL(_createAndCheckFile(fileSystem, "d\\b", "Lets go empty"));

	// Lets find all the files in the directory

	{
		const Entry entries[] =  { {SLANG_PATH_TYPE_FILE, "a" }, {SLANG_PATH_TYPE_FILE, "b" } };
		SLANG_RETURN_ON_FAIL(_enumeratePath(fileSystem, "d", makeConstArrayView(entries)));
	}

	{
		const Entry entries[] = { {SLANG_PATH_TYPE_FILE, "a" }, {SLANG_PATH_TYPE_FILE, "b" }, {SLANG_PATH_TYPE_DIRECTORY, "d" } };
		SLANG_RETURN_ON_FAIL(_enumeratePath(fileSystem, ".", makeConstArrayView(entries)));
	}

	{
		SLANG_RETURN_ON_FAIL(_checkSimplifiedPath(fileSystem, "d/../a", "a"));
	}
	
	SLANG_RETURN_ON_FAIL(fileSystem->remove("d/a"));
	{
		const Entry entries[] = { {SLANG_PATH_TYPE_FILE, "b" } };
		SLANG_RETURN_ON_FAIL(_enumeratePath(fileSystem, "d", makeConstArrayView(entries)));
	}
	SLANG_RETURN_ON_FAIL(fileSystem->remove("d\\b"));
	{
		SLANG_RETURN_ON_FAIL(_enumeratePath(fileSystem, "d", makeConstArrayView((const Entry*)nullptr, 0)));
	}

	// If it's removed it can't be removed again
	SLANG_CHECK(SLANG_FAILED(fileSystem->remove("d\\b")));

	// Remove the directory
	SLANG_RETURN_ON_FAIL(fileSystem->remove("d"));

	{
		const Entry entries[] = { {SLANG_PATH_TYPE_FILE, "a" }, {SLANG_PATH_TYPE_FILE, "b" } };
		SLANG_RETURN_ON_FAIL(_enumeratePath(fileSystem, ".", makeConstArrayView(entries)));
	}

	return SLANG_OK;
}

SLANG_UNIT_TEST(fileSystem)
{
	for (Index i = 0; i < Count(FileSystemType::CountOf); ++i)
	{
		const auto type = FileSystemType(i);

		auto const res = _test(type);

		SLANG_CHECK(SLANG_SUCCEEDED(res));
	}
}