yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
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{ 10StringBuilder buf ; 11handler -> appendEscaped (check ,buf ); 12 13StringBuilder decode ; 14handler -> appendUnescaped (buf .getUnownedSlice (),decode ); 15 16return decode == check ; 17} 18 19static bool _checkDecode (const UnownedStringSlice & encoded ,const UnownedStringSlice & decoded ) 20{ 21auto handler = StringEscapeUtil ::getHandler (StringEscapeUtil ::Style ::Cpp ); 22 23StringBuilder buf ; 24StringEscapeUtil ::appendUnquoted (handler ,encoded ,buf ); 25return 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 37const char text []= "\x000001" ; 38SLANG_ASSERT (SLANG_COUNT_OF (text )== 2 && text [0 ]== 1 ); 39 } 40 41// Check octal greedy 42 { 43//\ + up to 3 octal digits 44const char text []= "\0011" ; 45SLANG_ASSERT (SLANG_COUNT_OF (text )== 3 && text [0 ]== 1 && text [1 ]== '1' ); 46 47const char text2 []= "\78" ; 48SLANG_ASSERT (SLANG_COUNT_OF (text2 )== 3 && text2 [0 ]== 7 && text2 [1 ]== '8' ); 49 } 50 51 { 52auto handler = StringEscapeUtil ::getHandler (StringEscapeUtil ::Style ::Cpp ); 53 54SLANG_CHECK (_checkConversion ( 55handler , 56toSlice ("\0\1\2" 57"2" ))); 58 } 59 60 { 61auto 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 { 71SLANG_ENCODED_DECODED ("\a\b\0hey~\u0023\n\0" ); 72SLANG_CHECK (_checkDecode (encoded ,decoded )); 73 } 74 75 { 76SLANG_ENCODED_DECODED ("\n\v\b\t\1\02\003\x5z\x00007f\0" ); 77SLANG_CHECK (_checkDecode (encoded ,decoded )); 78 } 79 } 80}