summaryrefslogtreecommitdiff
path: root/tools/slang-unit-test/unit-test-compression.cpp
blob: 486a252b236633012adf343ad0fc3827ee7408c4 (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
// unit-compression.cpp
#include "tools/unit-test/slang-unit-test.h"

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

using namespace Slang;

static ICompressionSystem* _getCompressionSystem(CompressionSystemType type)
{
    switch (type)
    {
        case CompressionSystemType::Deflate: return DeflateCompressionSystem::getSingleton(); break;
        case CompressionSystemType::LZ4:     return LZ4CompressionSystem::getSingleton(); break;
        default: break;
    }
    return nullptr;
}

SLANG_UNIT_TEST(compression)
{
    // Test out compression systems
    for (Index i = 0; i < Count(CompressionSystemType::CountOf); ++i)
    {
        ICompressionSystem* system = _getCompressionSystem(CompressionSystemType(i));
     
        if (!system)
        {
            continue;
        }

        const char src[] = "Some text to compress";
        size_t srcSize = sizeof(src);

        ComPtr<ISlangBlob> compressedBlob;

        // Use the default style
        CompressionStyle style;

        SLANG_CHECK(SLANG_SUCCEEDED(system->compress(&style, src, srcSize, compressedBlob.writeRef()))); 
        
        // Now lets decompress
        List<char> decompressedData;
        decompressedData.setCount(srcSize);

        SLANG_CHECK(SLANG_SUCCEEDED(system->decompress(compressedBlob->getBufferPointer(), compressedBlob->getBufferSize(), srcSize, decompressedData.getBuffer())));
        SLANG_CHECK(::memcmp(src, decompressedData.getBuffer(), srcSize) == 0);
    }
}