yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
af27de015
master
1// unit-test-create-blob.cpp 2 3#include "slang-com-ptr.h" 4#include "slang.h" 5#include "unit-test/slang-unit-test.h" 6 7#include <stdio.h> 8#include <stdlib.h> 9#include <string.h> 10 11using namespace Slang ; 12 13// Test the slang_createBlob function 14SLANG_UNIT_TEST (createBlob ) 15{ 16// Test 1: Basic functionality with valid string data 17 { 18const char * testData = "Hello, World!" ; 19size_t dataSize = strlen (testData ); 20 21ComPtr < ISlangBlob > blob ; 22blob = slang_createBlob ((const void * )testData ,dataSize ); 23 24SLANG_CHECK (blob != nullptr ); 25 26if (blob ) 27 { 28SLANG_CHECK (blob -> getBufferSize ()== dataSize ); 29SLANG_CHECK (memcmp (blob -> getBufferPointer (),testData ,dataSize )== 0 ); 30 } 31 } 32 33// Test 2: Test with binary data (non-string) 34 { 35const uint8_t binaryData []= {0x00 ,0x01 ,0x02 ,0x03 ,0xFF ,0xFE ,0xFD ,0xFC }; 36size_t dataSize = sizeof (binaryData ); 37 38ComPtr < ISlangBlob > blob ; 39blob = slang_createBlob ((const void * )binaryData ,dataSize ); 40 41SLANG_CHECK (blob != nullptr ); 42 43if (blob ) 44 { 45SLANG_CHECK (blob -> getBufferSize ()== dataSize ); 46SLANG_CHECK (memcmp (blob -> getBufferPointer (),binaryData ,dataSize )== 0 ); 47 } 48 } 49 50// Test 3: Test with large data 51 { 52const size_t largeSize = 1024 * 1024 ;// 1MB 53char * largeData = new char [largeSize ]; 54 55// Fill with pattern 56for (size_t i = 0 ;i < largeSize ;i ++ ) 57 { 58largeData [i ]= (char )(i %256 ); 59 } 60 61ComPtr < ISlangBlob > blob ; 62blob = slang_createBlob ((const void * )largeData ,largeSize ); 63 64SLANG_CHECK (blob != nullptr ); 65 66if (blob ) 67 { 68SLANG_CHECK (blob -> getBufferSize ()== largeSize ); 69SLANG_CHECK (memcmp (blob -> getBufferPointer (),largeData ,largeSize )== 0 ); 70 } 71 72delete []largeData ; 73 } 74 75// Test 4: Test with null pointer and non-zero size (should fail) 76 { 77char * testData = nullptr ; 78ComPtr < ISlangBlob > blob ; 79blob = slang_createBlob ((const void * )testData ,10 ); 80 81SLANG_CHECK (blob == nullptr ); 82 } 83 84// Test 5: Test with null pointer and zero size (should fail) 85 { 86char * testData = nullptr ; 87ComPtr < ISlangBlob > blob ; 88blob = slang_createBlob ((const void * )testData ,0 ); 89 90SLANG_CHECK (blob == nullptr ); 91 } 92 93// Test 6: Test with valid pointer and zero size (should fail) 94 { 95const char * testData = "test" ; 96 97ComPtr < ISlangBlob > blob ; 98blob = slang_createBlob ((const void * )testData ,0 ); 99 100SLANG_CHECK (blob == nullptr ); 101 } 102 103// Test 7: Test with void* version of the function 104 { 105const char * testData = "Test void* version" ; 106size_t dataSize = strlen (testData ); 107 108ComPtr < ISlangBlob > blob ; 109blob = slang_createBlob ((const void * )testData ,dataSize ); 110 111SLANG_CHECK (blob != nullptr ); 112 113if (blob ) 114 { 115SLANG_CHECK (blob -> getBufferSize ()== dataSize ); 116SLANG_CHECK (memcmp (blob -> getBufferPointer (),testData ,dataSize )== 0 ); 117 } 118 } 119 120// Test 8: Test multiple blobs with same data 121 { 122const char * testData = "Shared data" ; 123size_t dataSize = strlen (testData ); 124 125ComPtr < ISlangBlob > blob1 ; 126ComPtr < ISlangBlob > blob2 ; 127ComPtr < ISlangBlob > blob3 ; 128blob1 = slang_createBlob ((const void * )testData ,dataSize ); 129blob2 = slang_createBlob ((const void * )testData ,dataSize ); 130blob3 = slang_createBlob ((const void * )testData ,dataSize ); 131 132SLANG_CHECK (blob1 != nullptr ); 133SLANG_CHECK (blob2 != nullptr ); 134SLANG_CHECK (blob3 != nullptr ); 135 136// All should have same content 137SLANG_CHECK (blob1 -> getBufferSize ()== dataSize ); 138SLANG_CHECK (blob2 -> getBufferSize ()== dataSize ); 139SLANG_CHECK (blob3 -> getBufferSize ()== dataSize ); 140 141SLANG_CHECK (memcmp (blob1 -> getBufferPointer (),testData ,dataSize )== 0 ); 142SLANG_CHECK (memcmp (blob2 -> getBufferPointer (),testData ,dataSize )== 0 ); 143SLANG_CHECK (memcmp (blob3 -> getBufferPointer (),testData ,dataSize )== 0 ); 144 } 145 146// Test 9: Test memory management (blob should be independent) 147 { 148const char * testData = "Memory test" ; 149size_t dataSize = strlen (testData ); 150 151ComPtr < ISlangBlob > blob ; 152blob = slang_createBlob ((const void * )testData ,dataSize ); 153 154// Modify original data - blob should remain unchanged 155char * mutableData = new char [dataSize + 1 ]; 156memcpy (mutableData ,testData ,dataSize + 1 ); 157mutableData [0 ]= 'X' ;// Change first character 158 159SLANG_CHECK (blob -> getBufferSize ()== dataSize ); 160SLANG_CHECK ( 161memcmp (blob -> getBufferPointer (),testData ,dataSize )== 1620 );// Should still match original 163SLANG_CHECK ( 164memcmp (blob -> getBufferPointer (),mutableData ,dataSize )!= 1650 );// Should not match modified 166 167delete []mutableData ; 168 } 169}