yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
1// unit-test-byte-encode.cpp 2 3#include "../../source/core/slang-byte-encode-util.h" 4#include "../../source/core/slang-list.h" 5#include "../../source/core/slang-random-generator.h" 6#include "unit-test/slang-unit-test.h" 7 8#include <stdio.h> 9#include <stdlib.h> 10 11using namespace Slang ; 12 13static void checkUInt32 (uint32_t value ) 14{ 15uint8_t buffer [ByteEncodeUtil ::kMaxLiteEncodeUInt32 + 1 ]; 16 17int writeLen = ByteEncodeUtil ::encodeLiteUInt32 (value ,buffer ); 18buffer [writeLen ]= 0xcd ; 19 20uint32_t decode ; 21int readLen = ByteEncodeUtil ::decodeLiteUInt32 (buffer ,& decode ); 22 23SLANG_CHECK (readLen == writeLen && decode == value ); 24} 25 26SLANG_UNIT_TEST (byteEncode ) 27{ 28DefaultRandomGenerator randGen (0x5346536a ); 29 30 { 31SLANG_CHECK (ByteEncodeUtil ::calcMsb8 (0 )== -1 ); 32SLANG_CHECK (ByteEncodeUtil ::calcMsb8 (1 )== 0 ); 33SLANG_CHECK (ByteEncodeUtil ::calcMsb8 (0x81 )== 7 ); 34 } 35 36 { 37SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0 )== -1 ); 38SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x81 )== 7 ); 39SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x00000001 )== 0 ); 40SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x00000081 )== 7 ); 41SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x00000181 )== 8 ); 42SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x00008181 )== 15 ); 43SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x00018181 )== 16 ); 44SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x00818181 )== 23 ); 45SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x01818181 )== 24 ); 46SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0x81818181 )== 31 ); 47SLANG_CHECK (ByteEncodeUtil ::calcMsb32 (0xffffffff )== 31 ); 48 } 49 50 { 51SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00000000 )== -1 ); 52SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00000001 )== 0 ); 53SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00000081 )== 0 ); 54SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00000181 )== 1 ); 55SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00008181 )== 1 ); 56SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00018181 )== 2 ); 57SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x00818181 )== 2 ); 58SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x01818181 )== 3 ); 59SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0x81818181 )== 3 ); 60SLANG_CHECK (ByteEncodeUtil ::calcMsByte32 (0xffffffff )== 3 ); 61 } 62 63 { 64SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x00000001 )== 0 ); 65SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x00000081 )== 0 ); 66SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x00000181 )== 1 ); 67SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x00008181 )== 1 ); 68SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x00018181 )== 2 ); 69SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x00818181 )== 2 ); 70SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x01818181 )== 3 ); 71SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0x81818181 )== 3 ); 72SLANG_CHECK (ByteEncodeUtil ::calcNonZeroMsByte32 (0xffffffff )== 3 ); 73 } 74 75 { 76const int blockSize = 1024 ; 77 78List < uint8_t > encodedBuffer ; 79encodedBuffer .setCount (ByteEncodeUtil ::kMaxLiteEncodeUInt32 * blockSize ); 80 81List < uint32_t > initialBuffer ; 82initialBuffer .setCount (blockSize ); 83List < uint32_t > decodeBuffer ; 84decodeBuffer .setCount (blockSize ); 85// Put in cache? 86memset (decodeBuffer .begin (),0 ,blockSize * sizeof (uint32_t )); 87 88for (int i = 0 ;i < blockSize ;i ++ ) 89 { 90const int v = ByteEncodeUtil ::calcMsb8 (uint32_t ((randGen .nextInt32 ()& 0xf ) |1 )); 91 92// Make the commonality of different numbers that bytes are most common, then shorts 93// etc.. 94uint32_t mask ; 95switch (v ) 96 { 97case 0 : 98mask = 0xffffffff ; 99break ; 100case 1 : 101mask = 0x00ffffff ; 102break ; 103case 2 : 104mask = 0x0000ffff ; 105break ; 106case 3 : 107mask = 0x000000ff ; 108break ; 109 } 110 111initialBuffer [i ]= randGen .nextInt32 ()& mask ; 112 } 113 114size_t numEncodeBytes = ByteEncodeUtil ::encodeLiteUInt32 ( 115initialBuffer .begin (), 116blockSize , 117encodedBuffer .begin ()); 118 119SLANG_CHECK ( 120ByteEncodeUtil ::calcEncodeLiteSizeUInt32 (initialBuffer .begin (),blockSize )== 121numEncodeBytes ); 122 123size_t numEncodeBytes2 = ByteEncodeUtil ::decodeLiteUInt32 ( 124encodedBuffer .begin (), 125blockSize , 126decodeBuffer .begin ()); 127 128SLANG_CHECK (numEncodeBytes2 == numEncodeBytes ); 129 130SLANG_CHECK ( 131memcmp (decodeBuffer .begin (),initialBuffer .begin (),sizeof (uint32_t )* blockSize )== 0 ); 132 } 133 134 { 135checkUInt32 (uint32_t (0 )); 136checkUInt32 (uint32_t (0x7fffff )); 137checkUInt32 (uint32_t (0x7fff )); 138checkUInt32 (uint32_t (0x7f )); 139checkUInt32 (uint32_t (0x7fffffff )); 140checkUInt32 (uint32_t (0xffffffff )); 141 142#if 1 143for (int64_t i = 0 ;i < SLANG_INT64 (0x100000000 );i += 371 ) 144 { 145checkUInt32 (uint32_t (i )); 146 } 147#else 148for (int64_t i = 0 ;i < SLANG_INT64 (0x100000000 );i += 1 ) 149 { 150checkUInt32 (uint32_t (i )); 151 } 152#endif 153 } 154}