summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-compression.cpp
blob: 8274d34108734816fbc274abb79c21ff450c2a4c (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
// unit-compression.cpp
#include "../../source/core/slang-deflate-compression-system.h"
#include "../../source/core/slang-lz4-compression-system.h"
#include "unit-test/slang-unit-test.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);
    }
}