Skip to content

Commit a9282fd

Browse files
committed
add wstring conversion stuff and env var resets
1 parent a4c9629 commit a9282fd

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

tools/clang/unittests/HLSL/ValidationTest.cpp

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,6 +4210,59 @@ TEST_F(ValidationTest, ValidateWithHash) {
42104210
VERIFY_ARE_EQUAL(memcmp(Result, pHeader->Hash.Digest, sizeof(Result)), 0);
42114211
}
42124212

4213+
std::wstring string_to_wstring(const std::string &str) {
4214+
if (str.empty())
4215+
return L"";
4216+
4217+
int size_needed =
4218+
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0);
4219+
std::wstring wstrTo(size_needed, 0);
4220+
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstrTo[0],
4221+
size_needed);
4222+
return wstrTo;
4223+
}
4224+
4225+
std::string wstring_to_utf8(const std::wstring &wstr) {
4226+
if (wstr.empty())
4227+
return "";
4228+
4229+
int size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, nullptr, 0,
4230+
nullptr, nullptr);
4231+
std::string result(size - 1, 0); // -1 to exclude null terminator
4232+
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, &result[0], size, nullptr,
4233+
nullptr);
4234+
return result;
4235+
}
4236+
4237+
std::string GetEnvVarA(const std::string &varName) {
4238+
#ifdef _WIN32
4239+
DWORD size = GetEnvironmentVariableA(varName.c_str(), nullptr, 0);
4240+
if (size == 0) {
4241+
return ""; // Not found or empty
4242+
}
4243+
4244+
std::string buffer(size - 1, '\0'); // size includes null terminator
4245+
GetEnvironmentVariableA(varName.c_str(), &buffer[0], size);
4246+
return buffer;
4247+
#else
4248+
const char *result = std::getenv(varName.c_str());
4249+
return result ? std::string(result) : std::string();
4250+
#endif
4251+
}
4252+
4253+
void SetEnvVarW(const std::wstring &varName, const std::wstring &varValue) {
4254+
#ifdef _WIN32
4255+
VERIFY_IS_TRUE(SetEnvironmentVariableW(varName.c_str(), varValue.c_str()));
4256+
// also update the CRT environment
4257+
_putenv_s(wstring_to_utf8(varName).c_str(),
4258+
wstring_to_utf8(varValue).c_str());
4259+
#else
4260+
std::string name_utf8 = wstring_to_utf8(varName);
4261+
std::string value_utf8 = wstring_to_utf8(varValue);
4262+
setenv(name_utf8.c_str(), value_utf8.c_str(), 1);
4263+
#endif
4264+
}
4265+
42134266
// For now, 3 things are tested:
42144267
// 1. The environment variable is not set. GetDxilDllPath() is empty and
42154268
// DxilDllFailedToLoad() returns false
@@ -4225,20 +4278,23 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
42254278
DxcDllExtValidationSupport m_dllExtSupport1;
42264279
DxcDllExtValidationSupport m_dllExtSupport2;
42274280

4281+
// capture any existing value in the environment variable,
4282+
// so that it can be restored after the test
4283+
std::string oldEnvVal = GetEnvVarA("DXC_DXIL_DLL_PATH");
4284+
42284285
// 1. with no env var set, test GetDxilDllPath() and DxilDllFailedToLoad()
4229-
m_dllExtSupport1.Initialize();
4286+
4287+
// make sure the variable is cleared, in case other tests may have set it
4288+
SetEnvVarW(L"DXC_DXIL_DLL_PATH", L"");
4289+
4290+
// empty initialization should succeed
4291+
VERIFY_SUCCEEDED(m_dllExtSupport1.Initialize());
4292+
42304293
VERIFY_IS_FALSE(m_dllExtSupport1.DxilDllFailedToLoad());
42314294
VERIFY_ARE_EQUAL(m_dllExtSupport1.GetDxilDllPath(), "");
42324295

42334296
// 2. Test with a bogus path in the environment variable
4234-
#ifdef _WIN32
4235-
SetEnvironmentVariableW(L"DXC_DXIL_DLL_PATH", L"bogus");
4236-
// also update the CRT environment
4237-
_putenv_s("DXC_DXIL_DLL_PATH", "bogus");
4238-
4239-
#else
4240-
setenv("DXC_DXIL_DLL_PATH", "bogus", 1);
4241-
#endif
4297+
SetEnvVarW(L"DXC_DXIL_DLL_PATH", L"bogus");
42424298

42434299
if (!m_dllExtSupport2.IsEnabled()) {
42444300
VERIFY_SUCCEEDED(m_dllExtSupport2.Initialize());
@@ -4276,6 +4332,11 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
42764332
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance2(pMalloc, CLSID_DxcValidator,
42774333
__uuidof(IDxcValidator),
42784334
(IUnknown **)&pValidator));
4335+
4336+
// reset the environment variable to its previous value, if it had one.
4337+
if (!oldEnvVal.empty()) {
4338+
SetEnvVarW(L"DXC_DXIL_DLL_PATH", string_to_wstring(oldEnvVal));
4339+
}
42794340
}
42804341

42814342
TEST_F(ValidationTest, ValidatePreviewBypassHash) {

0 commit comments

Comments
 (0)