summaryrefslogtreecommitdiff
path: root/tools/slang-test/unit-relative-container.cpp
blob: a9ff7c0fe46e31bc7f4744222b0e989771148d80 (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
// unit-test-path.cpp

#include "../../source/core/slang-relative-container.h"

#include "test-context.h"

using namespace Slang;

static void _checkEncodeDecode(uint32_t size)
{
    uint8_t encode[RelativeString::kMaxSizeEncodeSize];

    size_t encodeSize = RelativeString::calcEncodedSize(size, encode);

    size_t decodedSize;
    const char* chars = RelativeString::decodeSize((const char*)encode, decodedSize);

    SLANG_CHECK(decodedSize == size);
    SLANG_CHECK(chars - (const char*)encode == encodeSize);
}

namespace { // anonymous

struct Root
{
    Relative32Array<Relative32Ptr<RelativeString> > dirs;
    Relative32Ptr<RelativeString> name;
    float value;
};

} // anonymous

static void relativeContainerUnitTest()
{
    _checkEncodeDecode(253);

    for (int64_t i = 0; i < 0x100000000; i += (i / 2) + 1)
    {
        _checkEncodeDecode(uint32_t(i));
    }

    {
        RelativeContainer container;

        const char* strings[] =
        {
            "Hello",
            "World",
            nullptr,
        };

        {
            Safe32Ptr<Root> root = container.newObject<Root>();

            auto array = container.newArray<Relative32Ptr<RelativeString>>(SLANG_COUNT_OF(strings));
            for (Int i = 0; i < SLANG_COUNT_OF(strings); ++i)
            {
                array[i] = container.newString(strings[i]);
            }

            root->dirs = array;
        }

        {
            RelativeContainer copy;
            copy.set(container.getData(), container.getDataCount()); 

            Root* root = (Root*)copy.getData();

            SLANG_CHECK(root->dirs.getCount() == SLANG_COUNT_OF(strings));

            Int count = root->dirs.getCount();
            for (Int i = 0; i < count; ++i)
            {
                RelativeString* str = root->dirs[i];

                const char* check = strings[i];

                if (check)
                {
                    SLANG_CHECK(str != nullptr);
                    const char* strCstr = str->getCstr();
                    SLANG_CHECK(strcmp(strCstr, check) == 0);
                }
                else
                {
                    SLANG_CHECK(str == nullptr);
                }
            }
        }
    }
}

SLANG_UNIT_TEST("RelativeContainer", relativeContainerUnitTest);