summaryrefslogtreecommitdiff
path: root/tools/slang-unit-test/unit-test-riff.cpp
blob: 2902a9af53e78dfae56031353e1bc56fab666c8c (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
// unit-test-riff.cpp

#include "../../source/core/slang-riff.h"

#include "../../source/core/slang-random-generator.h"

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

using namespace Slang;

static void _writeRandom(RandomGenerator* rand, size_t maxSize, RiffContainer& ioContainer, List<uint8_t>& ioData)
{
    while (true)
    {
        const Index oldCount = ioData.getCount();

        const size_t allocSize = size_t(rand->nextInt32InRange(1, 50));

        if (allocSize + oldCount > maxSize)
        {
            break;
        }

        ioData.setCount(oldCount + Index(allocSize));
        rand->nextData(ioData.getBuffer() + oldCount, allocSize);

        // Write
        ioContainer.write(ioData.getBuffer() + oldCount, allocSize);
    }

    // Should be a single block with same data as the List
    RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(ioContainer.getCurrentChunk());
    SLANG_ASSERT(dataChunk);
}

SLANG_UNIT_TEST(riff)
{
    typedef RiffContainer::ScopeChunk ScopeChunk;
    typedef RiffContainer::Chunk::Kind Kind;

    const FourCC markThings = SLANG_FOUR_CC('T', 'H', 'I', 'N');
    const FourCC markData = SLANG_FOUR_CC('D', 'A', 'T', 'A');

    {
        RiffContainer container;

        {
            ScopeChunk scopeContainer(&container, Kind::List, markThings);        
            {
                ScopeChunk scopeChunk(&container, Kind::Data, markData);

                const char hello[] = "Hello ";
                const char world[] = "World!";

                container.write(hello, sizeof(hello));
                container.write(world, sizeof(world));
            }

            {
                ScopeChunk scopeChunk(&container, Kind::Data, markData);

                const char test0[] = "Testing... ";
                const char test1[] = "Testing!";

                container.write(test0, sizeof(test0));
                container.write(test1, sizeof(test1));
            }

            {
                ScopeChunk innerScopeContainer(&container, Kind::List, markThings);

                {
                    ScopeChunk scopeChunk(&container, Kind::Data, markData);

                    const char another[] = "Another?";
                    container.write(another, sizeof(another));
                }
            }
        }

        SLANG_CHECK(container.isFullyConstructed());
        SLANG_CHECK(RiffContainer::isChunkOk(container.getRoot()));

        {
            StringBuilder builder;
            {
                StringWriter writer(&builder, 0);
                RiffUtil::dump(container.getRoot(), &writer);
            }

            {
                OwnedMemoryStream stream(FileAccess::ReadWrite); 
                SLANG_CHECK(SLANG_SUCCEEDED(RiffUtil::write(container.getRoot(), true, &stream)));

                stream.seek(SeekOrigin::Start, 0);

                RiffContainer readContainer;
                SLANG_CHECK(SLANG_SUCCEEDED(RiffUtil::read(&stream, readContainer)));

                // Dump the read contents
                StringBuilder readBuilder;
                {
                    StringWriter writer(&readBuilder, 0);
                    RiffUtil::dump(readContainer.getRoot(), &writer);
                }

                // They should be the same
                SLANG_CHECK(readBuilder == builder);
            }
        }

    }

    // Test writing as a stream only allocates a single data block (as long as there is enough space).
    {
        RiffContainer container;

        ScopeChunk scopeChunk(&container, Kind::List, markData);
        {
            ScopeChunk scopeChunk(&container, Kind::Data, markData);
            RefPtr<RandomGenerator> rand = RandomGenerator::create(0x345234);

            List<uint8_t> data;
            _writeRandom(rand, container.getMemoryArena().getBlockPayloadSize() / 2, container, data); 

            // Should be a single block with same data as the List
            RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(container.getCurrentChunk());
            SLANG_ASSERT(dataChunk);

            // It should be a single block
            SLANG_CHECK(dataChunk->getSingleData() != nullptr);

            SLANG_CHECK(dataChunk->isEqual(data.getBuffer(), data.getCount()));

        }
    } 

    // Test writing across multiple data blocks
    {
        RefPtr<RandomGenerator> rand = RandomGenerator::create(0x345234);

        for (Int i = 0 ; i < 100; ++i)
        {
            RiffContainer container;

            const size_t maxSize = rand->nextInt32InRange(1, int32_t(container.getMemoryArena().getBlockPayloadSize() * 3));
            
            ScopeChunk scopeChunk(&container, Kind::List, markData);
            {
                ScopeChunk scopeChunk(&container, Kind::Data, markData);
            
                List<uint8_t> data;
                _writeRandom(rand, maxSize, container, data);

                // Should be a single block with same data as the List
                RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(container.getCurrentChunk());
                SLANG_CHECK(dataChunk && dataChunk->isEqual(data.getBuffer(), data.getCount()));
            }
        }
    }

#if 0
    {
        RiffContainer container;
        {
            FileStream readStream("ambient-drop.wav", FileMode::Open, FileAccess::Read, FileShare::ReadWrite);
            SLANG_CHECK(SLANG_SUCCEEDED(RiffUtil::read(&readStream, container)));
            RiffUtil::dump(container.getRoot(), StdWriters::getOut());
        }
        // Write it
        {

            FileStream writeStream("check.wav", FileMode::Create, FileAccess::Write, FileShare::ReadWrite);
            SLANG_CHECK(SLANG_SUCCEEDED(RiffUtil::write(container.getRoot(), true, &writeStream)));
        }
    }
#endif
}