yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
1// unit-test-offset-container.cpp 2 3#include "../../source/core/slang-offset-container.h" 4#include "unit-test/slang-unit-test.h" 5 6using namespace Slang ; 7 8static void _checkEncodeDecode (uint32_t size ) 9{ 10uint8_t encode [OffsetString ::kMaxSizeEncodeSize ]; 11 12size_t encodeSize = OffsetString ::calcEncodedSize (size ,encode ); 13 14size_t decodedSize ; 15const char * chars = OffsetString ::decodeSize ((const char * )encode ,decodedSize ); 16 17SLANG_CHECK (decodedSize == size ); 18SLANG_CHECK (chars - (const char * )encode == encodeSize ); 19} 20 21namespace 22{// anonymous 23 24struct Root 25{ 26Offset32Array < Offset32Ptr < OffsetString >> dirs ; 27Offset32Ptr < OffsetString > name ; 28float value ; 29}; 30 31}// namespace 32 33SLANG_UNIT_TEST (offsetContainer ) 34{ 35_checkEncodeDecode (253 ); 36 37for (int64_t i = 0 ;i < 0x100000000 ;i += (i /2 )+ 1 ) 38 { 39_checkEncodeDecode (uint32_t (i )); 40 } 41 42 { 43OffsetContainer container ; 44 45const char * strings []= { 46"Hello" , 47"World" , 48nullptr , 49 }; 50 51 { 52auto & base = container .asBase (); 53 54Offset32Ptr < Root > root = container .newObject < Root > (); 55 56auto array = container .newArray < Offset32Ptr < OffsetString >> (SLANG_COUNT_OF (strings )); 57for (Int i = 0 ;i < SLANG_COUNT_OF (strings );++ i ) 58 { 59base [array [i ]]= container .newString (strings [i ]); 60 } 61base [root ]-> dirs = array ; 62 } 63 64 { 65List < uint8_t > copy ; 66copy .addRange (container .getData (),container .getDataCount ()); 67 68MemoryOffsetBase base ; 69base .set (copy .getBuffer (),copy .getCount ()); 70 71Root * root = (Root * )(copy .getBuffer ()+ kStartOffset ); 72 73SLANG_CHECK (root -> dirs .getCount ()== SLANG_COUNT_OF (strings )); 74 75Int count = root -> dirs .getCount (); 76for (Int i = 0 ;i < count ;++ i ) 77 { 78OffsetString * str = base .asRaw (base .asRaw (root -> dirs [i ])); 79 80const char * check = strings [i ]; 81 82if (check ) 83 { 84SLANG_CHECK (str != nullptr ); 85const char * strCstr = str -> getCstr (); 86SLANG_CHECK (strcmp (strCstr ,check )== 0 ); 87 } 88else 89 { 90SLANG_CHECK (str == nullptr ); 91 } 92 } 93 94 { 95Index index = 0 ; 96for (const auto v :root -> dirs ) 97 { 98OffsetString * str = base .asRaw (base .asRaw (v )); 99const char * check = strings [index ]; 100if (check ) 101 { 102SLANG_CHECK (str != nullptr ); 103const char * strCstr = str -> getCstr (); 104SLANG_CHECK (strcmp (strCstr ,check )== 0 ); 105 } 106else 107 { 108SLANG_CHECK (str == nullptr ); 109 } 110 111index ++ ; 112 } 113 } 114 } 115 } 116}