yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie HermaszewskaCorrect include dir for libslang (#5539)7b570feed

master
2.3 KiB80 linesraw
1// unit-test-string-escape.cpp
2
3#include "../../source/core/slang-string-escape-util.h"
4#include "unit-test/slang-unit-test.h"
5
6using namespace Slang;
7
8static bool _checkConversion(StringEscapeHandler* handler, const UnownedStringSlice& check)
9{
10    StringBuilder buf;
11    handler->appendEscaped(check, buf);
12
13    StringBuilder decode;
14    handler->appendUnescaped(buf.getUnownedSlice(), decode);
15
16    return decode == check;
17}
18
19static bool _checkDecode(const UnownedStringSlice& encoded, const UnownedStringSlice& decoded)
20{
21    auto handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::Cpp);
22
23    StringBuilder buf;
24    StringEscapeUtil::appendUnquoted(handler, encoded, buf);
25    return buf == decoded;
26}
27
28#define SLANG_ENCODED_DECODED(x)      \
29    const auto encoded = toSlice(#x); \
30    const auto decoded = toSlice(x);
31
32SLANG_UNIT_TEST(StringEscape)
33{
34    // Check greedy hex digits
35    {
36        // \x can have any number of hex digits
37        const char text[] = "\x000001";
38        SLANG_ASSERT(SLANG_COUNT_OF(text) == 2 && text[0] == 1);
39    }
40
41    // Check octal greedy
42    {
43        //\ + up to 3 octal digits
44        const char text[] = "\0011";
45        SLANG_ASSERT(SLANG_COUNT_OF(text) == 3 && text[0] == 1 && text[1] == '1');
46
47        const char text2[] = "\78";
48        SLANG_ASSERT(SLANG_COUNT_OF(text2) == 3 && text2[0] == 7 && text2[1] == '8');
49    }
50
51    {
52        auto handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::Cpp);
53
54        SLANG_CHECK(_checkConversion(
55            handler,
56            toSlice("\0\1\2"
57                    "2")));
58    }
59
60    {
61        auto handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::Cpp);
62
63        // We can't just use '\uxxxx', because it has to be translatable into an output character in
64        // MSVC (not into utf8) Can make work perhaps with something like #pragma
65        // execution_character_set("utf-8") But for now we don't worry
66        //
67        // Visual Studio does not appear to support '\U' by default, presumably because wchar_t is
68        // 16 bits
69
70        {
71            SLANG_ENCODED_DECODED("\a\b\0hey~\u0023\n\0");
72            SLANG_CHECK(_checkDecode(encoded, decoded));
73        }
74
75        {
76            SLANG_ENCODED_DECODED("\n\v\b\t\1\02\003\x5z\x00007f\0");
77            SLANG_CHECK(_checkDecode(encoded, decoded));
78        }
79    }
80}