diff options
Diffstat (limited to 'tools/slang-test/unit-test-compression.cpp')
| -rw-r--r-- | tools/slang-test/unit-test-compression.cpp | 185 |
1 files changed, 121 insertions, 64 deletions
diff --git a/tools/slang-test/unit-test-compression.cpp b/tools/slang-test/unit-test-compression.cpp index d9900bbfa..b70bd8545 100644 --- a/tools/slang-test/unit-test-compression.cpp +++ b/tools/slang-test/unit-test-compression.cpp @@ -4,6 +4,9 @@ #include "../../source/core/slang-zip-file-system.h" +#include "../../source/core/slang-lz4-compression-system.h" +#include "../../source/core/slang-deflate-compression-system.h" + using namespace Slang; static bool _equals(const void* data, size_t size, ISlangBlob* blob) @@ -31,102 +34,156 @@ static List<String> _getContents(ISlangFileSystemExt* fileSystem, const char* pa static void compressionUnitTest() { - // Create a zip to add stuff to - RefPtr<CompressedFileSystem> buildFileSystem; - CompressedFileSystem::createZip(buildFileSystem); - - const char contents[] = "I'm compressed"; - const char contents2[] = "Some more stuff"; - const char contents3[] = "Replace it"; + const SlangArchiveType archiveTypes[] = + { + SLANG_ARCHIVE_TYPE_RIFF, + SLANG_ARCHIVE_TYPE_RIFF_DEFLATE, + SLANG_ARCHIVE_TYPE_RIFF_LZ4, + SLANG_ARCHIVE_TYPE_ZIP + }; + for (auto archiveType : archiveTypes) { - ISlangMutableFileSystem* fileSystem = buildFileSystem; + // Test out archive file systems + RefPtr<ArchiveFileSystem> archiveFileSystem; + SLANG_CHECK(SLANG_SUCCEEDED(createArchiveFileSystem(archiveType, archiveFileSystem))); + + const char contents[] = "I'm compressed"; + const char contents2[] = "Some more stuff"; + const char contents3[] = "Replace it"; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->createDirectory("hello"))); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->createDirectory("hello2"))); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->remove("hello"))); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->createDirectory("hello"))); + { + ISlangMutableFileSystem* fileSystem = archiveFileSystem; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("file.txt", contents, SLANG_COUNT_OF(contents)))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->createDirectory("hello"))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->createDirectory("hello2"))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->remove("hello"))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->createDirectory("hello"))); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("file2.txt", contents2, SLANG_COUNT_OF(contents2)))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("file.txt", contents, SLANG_COUNT_OF(contents)))); - ComPtr<ISlangBlob> blob; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file.txt", blob.writeRef()))); - SLANG_CHECK(_equals(contents, blob)); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("file2.txt", contents2, SLANG_COUNT_OF(contents2)))); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file2.txt", blob.writeRef()))); - SLANG_CHECK(_equals(contents2, blob)); + ComPtr<ISlangBlob> blob; + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file.txt", blob.writeRef()))); + SLANG_CHECK(_equals(contents, blob)); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("file2.txt", contents3, SLANG_COUNT_OF(contents3)))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file2.txt", blob.writeRef()))); + SLANG_CHECK(_equals(contents2, blob)); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file2.txt", blob.writeRef()))); - SLANG_CHECK(_equals(contents3, blob)); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("file2.txt", contents3, SLANG_COUNT_OF(contents3)))); - // Check the path type - { - SlangPathType pathType; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType("file2.txt", &pathType))); - SLANG_CHECK(pathType == SLANG_PATH_TYPE_FILE); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file2.txt", blob.writeRef()))); + SLANG_CHECK(_equals(contents3, blob)); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType("hello", &pathType))); - SLANG_CHECK(pathType == SLANG_PATH_TYPE_DIRECTORY); - } - - // Enumerate - { - for (const auto& obj : _getContents(fileSystem, "")) + // Check the path type { - // All of these should exist SlangPathType pathType; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType(obj.getBuffer(), &pathType))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType("file2.txt", &pathType))); + SLANG_CHECK(pathType == SLANG_PATH_TYPE_FILE); + + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType("hello", &pathType))); + SLANG_CHECK(pathType == SLANG_PATH_TYPE_DIRECTORY); } - } - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("implicit-path/file2.txt", contents3, SLANG_COUNT_OF(contents3)))); + // Enumerate + { + for (const auto& obj : _getContents(fileSystem, "")) + { + // All of these should exist + SlangPathType pathType; + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType(obj.getBuffer(), &pathType))); + } + } - { - SlangPathType pathType; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType("implicit-path", &pathType))); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->saveFile("implicit-path/file2.txt", contents3, SLANG_COUNT_OF(contents3)))); - SLANG_CHECK(pathType == SLANG_PATH_TYPE_DIRECTORY); + { + SlangPathType pathType; + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType("implicit-path", &pathType))); - List<String> objs = _getContents(fileSystem, "implicit-path"); + SLANG_CHECK(pathType == SLANG_PATH_TYPE_DIRECTORY); - // It contains a file - SLANG_CHECK(objs.getCount() == 1); + List<String> objs = _getContents(fileSystem, "implicit-path"); - for (const auto& obj : objs) - { - String path = Path::combine("implicit-path", obj); + // It contains a file + SLANG_CHECK(objs.getCount() == 1); - // All of these should exist - SlangPathType pathType; - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType(path.getBuffer(), &pathType))); + for (const auto& obj : objs) + { + String path = Path::combine("implicit-path", obj); + + // All of these should exist + SlangPathType pathType; + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->getPathType(path.getBuffer(), &pathType))); + } + + // Make an explicit path, and see whe have the same results + fileSystem->createDirectory("implicit-path"); + + objs = _getContents(fileSystem, "implicit-path"); + SLANG_CHECK(objs.getCount() == 1); } + } + + + // Load and check its okay + + { + ComPtr<ISlangBlob> archiveBlob; + SLANG_CHECK(SLANG_SUCCEEDED(archiveFileSystem->storeArchive(false, archiveBlob.writeRef()))); + + + RefPtr<ArchiveFileSystem> fileSystem; +#if 0 + SLANG_CHECK(SLANG_SUCCEEDED(createArchiveFileSystem(archiveType, fileSystem))); + + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadArchive(archiveBlob->getBufferPointer(), archiveBlob->getBufferSize()))); +#else + SLANG_CHECK(SLANG_SUCCEEDED(loadArchiveFileSystem(archiveBlob->getBufferPointer(), archiveBlob->getBufferSize(), fileSystem))); +#endif - // Make an explicit path, and see whe have the same results - fileSystem->createDirectory("implicit-path"); + ComPtr<ISlangBlob> blob; - objs = _getContents(fileSystem, "implicit-path"); - SLANG_CHECK(objs.getCount() == 1); + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file.txt", blob.writeRef()))); + SLANG_CHECK(_equals(contents, blob)); + + SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file2.txt", blob.writeRef()))); + SLANG_CHECK(_equals(contents3, blob)); } } - // Load and check its okay + // Test out compression systems + for (Index i = 0; i < 2; ++i) { - const auto archive = buildFileSystem->getArchive(); + // Lets try lz4 + + ICompressionSystem* system = nullptr; + if (i == 0) + { + system = LZ4CompressionSystem::getSingleton(); + } + else + { + system = DeflateCompressionSystem::getSingleton(); + } + + const char src[] = "Some text to compress"; + size_t srcSize = sizeof(src); + + ComPtr<ISlangBlob> compressedBlob; - RefPtr<CompressedFileSystem> fileSystem; - CompressedFileSystem::createZip(archive.getBuffer(), archive.getCount(), fileSystem); + CompressionStyle style; - ComPtr<ISlangBlob> blob; + SLANG_CHECK(SLANG_SUCCEEDED(system->compress(&style, src, srcSize, compressedBlob.writeRef()))); + + // Now lets decompress - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file.txt", blob.writeRef()))); - SLANG_CHECK(_equals(contents, blob)); + List<char> decompressedData; + decompressedData.setCount(srcSize); - SLANG_CHECK(SLANG_SUCCEEDED(fileSystem->loadFile("file2.txt", blob.writeRef()))); - SLANG_CHECK(_equals(contents3, blob)); + SLANG_CHECK(SLANG_SUCCEEDED(system->decompress(compressedBlob->getBufferPointer(), compressedBlob->getBufferSize(), srcSize, decompressedData.getBuffer()))); + SLANG_CHECK(memcmp(src, decompressedData.getBuffer(), srcSize) == 0); } } |
