From af27de01532904508e6a630c213249e93fdd1c66 Mon Sep 17 00:00:00 2001 From: jarcherNV Date: Fri, 15 Aug 2025 13:16:09 -0700 Subject: Add static functions to create blobs from data (#8179) Add helper functions to create ISlangBlob and load module data from source. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- tools/slang-unit-test/unit-test-create-blob.cpp | 169 ++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 tools/slang-unit-test/unit-test-create-blob.cpp (limited to 'tools/slang-unit-test/unit-test-create-blob.cpp') diff --git a/tools/slang-unit-test/unit-test-create-blob.cpp b/tools/slang-unit-test/unit-test-create-blob.cpp new file mode 100644 index 000000000..edb136415 --- /dev/null +++ b/tools/slang-unit-test/unit-test-create-blob.cpp @@ -0,0 +1,169 @@ +// unit-test-create-blob.cpp + +#include "slang-com-ptr.h" +#include "slang.h" +#include "unit-test/slang-unit-test.h" + +#include +#include +#include + +using namespace Slang; + +// Test the slang_createBlob function +SLANG_UNIT_TEST(createBlob) +{ + // Test 1: Basic functionality with valid string data + { + const char* testData = "Hello, World!"; + size_t dataSize = strlen(testData); + + ComPtr blob; + blob = slang_createBlob((const void*)testData, dataSize); + + SLANG_CHECK(blob != nullptr); + + if (blob) + { + SLANG_CHECK(blob->getBufferSize() == dataSize); + SLANG_CHECK(memcmp(blob->getBufferPointer(), testData, dataSize) == 0); + } + } + + // Test 2: Test with binary data (non-string) + { + const uint8_t binaryData[] = {0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0xFC}; + size_t dataSize = sizeof(binaryData); + + ComPtr blob; + blob = slang_createBlob((const void*)binaryData, dataSize); + + SLANG_CHECK(blob != nullptr); + + if (blob) + { + SLANG_CHECK(blob->getBufferSize() == dataSize); + SLANG_CHECK(memcmp(blob->getBufferPointer(), binaryData, dataSize) == 0); + } + } + + // Test 3: Test with large data + { + const size_t largeSize = 1024 * 1024; // 1MB + char* largeData = new char[largeSize]; + + // Fill with pattern + for (size_t i = 0; i < largeSize; i++) + { + largeData[i] = (char)(i % 256); + } + + ComPtr blob; + blob = slang_createBlob((const void*)largeData, largeSize); + + SLANG_CHECK(blob != nullptr); + + if (blob) + { + SLANG_CHECK(blob->getBufferSize() == largeSize); + SLANG_CHECK(memcmp(blob->getBufferPointer(), largeData, largeSize) == 0); + } + + delete[] largeData; + } + + // Test 4: Test with null pointer and non-zero size (should fail) + { + char* testData = nullptr; + ComPtr blob; + blob = slang_createBlob((const void*)testData, 10); + + SLANG_CHECK(blob == nullptr); + } + + // Test 5: Test with null pointer and zero size (should fail) + { + char* testData = nullptr; + ComPtr blob; + blob = slang_createBlob((const void*)testData, 0); + + SLANG_CHECK(blob == nullptr); + } + + // Test 6: Test with valid pointer and zero size (should fail) + { + const char* testData = "test"; + + ComPtr blob; + blob = slang_createBlob((const void*)testData, 0); + + SLANG_CHECK(blob == nullptr); + } + + // Test 7: Test with void* version of the function + { + const char* testData = "Test void* version"; + size_t dataSize = strlen(testData); + + ComPtr blob; + blob = slang_createBlob((const void*)testData, dataSize); + + SLANG_CHECK(blob != nullptr); + + if (blob) + { + SLANG_CHECK(blob->getBufferSize() == dataSize); + SLANG_CHECK(memcmp(blob->getBufferPointer(), testData, dataSize) == 0); + } + } + + // Test 8: Test multiple blobs with same data + { + const char* testData = "Shared data"; + size_t dataSize = strlen(testData); + + ComPtr blob1; + ComPtr blob2; + ComPtr blob3; + blob1 = slang_createBlob((const void*)testData, dataSize); + blob2 = slang_createBlob((const void*)testData, dataSize); + blob3 = slang_createBlob((const void*)testData, dataSize); + + SLANG_CHECK(blob1 != nullptr); + SLANG_CHECK(blob2 != nullptr); + SLANG_CHECK(blob3 != nullptr); + + // All should have same content + SLANG_CHECK(blob1->getBufferSize() == dataSize); + SLANG_CHECK(blob2->getBufferSize() == dataSize); + SLANG_CHECK(blob3->getBufferSize() == dataSize); + + SLANG_CHECK(memcmp(blob1->getBufferPointer(), testData, dataSize) == 0); + SLANG_CHECK(memcmp(blob2->getBufferPointer(), testData, dataSize) == 0); + SLANG_CHECK(memcmp(blob3->getBufferPointer(), testData, dataSize) == 0); + } + + // Test 9: Test memory management (blob should be independent) + { + const char* testData = "Memory test"; + size_t dataSize = strlen(testData); + + ComPtr blob; + blob = slang_createBlob((const void*)testData, dataSize); + + // Modify original data - blob should remain unchanged + char* mutableData = new char[dataSize + 1]; + memcpy(mutableData, testData, dataSize + 1); + mutableData[0] = 'X'; // Change first character + + SLANG_CHECK(blob->getBufferSize() == dataSize); + SLANG_CHECK( + memcmp(blob->getBufferPointer(), testData, dataSize) == + 0); // Should still match original + SLANG_CHECK( + memcmp(blob->getBufferPointer(), mutableData, dataSize) != + 0); // Should not match modified + + delete[] mutableData; + } +} -- cgit v1.2.3