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

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

#include "test-context.h"

using namespace Slang;

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

    {
        typedef RiffContainer::ScopeChunk ScopeChunk;
        typedef RiffContainer::Chunk::Kind Kind;

        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);
            }
        }

    }

#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
}

SLANG_UNIT_TEST("Riff", riffUnitTest);