Skip to content

Commit 1f28e09

Browse files
authored
[UR] Fix Windows build of urDeviceGetSelected tests (#18116)
The `setenv` and `unsetenv` functions are not available on Windows, instead use `_putenv_s` wrapped in our own interfaces to fix the UR CTS build on Windows.
1 parent eb19e81 commit 1f28e09

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

unified-runtime/test/conformance/device/urDeviceGetSelected.cpp

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,37 @@
66

77
#include <uur/fixtures.h>
88

9+
#ifdef _MSC_VER
10+
#include <Windows.h>
11+
#endif
12+
13+
namespace uur {
14+
static int set_env(const char *name, const char *value) {
15+
#ifdef _MSC_VER
16+
return _putenv_s(name, value);
17+
#else
18+
return setenv(name, value, 1);
19+
#endif
20+
}
21+
22+
static int unset_env(const char *name) {
23+
#ifdef _MSC_VER
24+
return _putenv_s(name, "");
25+
#else
26+
return unsetenv(name);
27+
#endif
28+
}
29+
30+
} // namespace uur
31+
932
using urDeviceGetSelectedTest = uur::urPlatformTest;
1033
UUR_INSTANTIATE_PLATFORM_TEST_SUITE(urDeviceGetSelectedTest);
1134

1235
/* adpater agnostic tests -- none assume the existence or support of any
1336
* specific adapter */
1437

1538
TEST_P(urDeviceGetSelectedTest, Success) {
16-
unsetenv("ONEAPI_DEVICE_SELECTOR");
39+
uur::unset_env("ONEAPI_DEVICE_SELECTOR");
1740
uint32_t count = 0;
1841
ASSERT_SUCCESS(
1942
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -27,7 +50,7 @@ TEST_P(urDeviceGetSelectedTest, Success) {
2750
}
2851

2952
TEST_P(urDeviceGetSelectedTest, SuccessSubsetOfDevices) {
30-
unsetenv("ONEAPI_DEVICE_SELECTOR");
53+
uur::unset_env("ONEAPI_DEVICE_SELECTOR");
3154
uint32_t count = 0;
3255
ASSERT_SUCCESS(
3356
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -38,13 +61,13 @@ TEST_P(urDeviceGetSelectedTest, SuccessSubsetOfDevices) {
3861
std::vector<ur_device_handle_t> devices(count - 1);
3962
ASSERT_SUCCESS(urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, count - 1,
4063
devices.data(), nullptr));
41-
for (auto device : devices) {
64+
for (auto *device : devices) {
4265
ASSERT_NE(nullptr, device);
4366
}
4467
}
4568

4669
TEST_P(urDeviceGetSelectedTest, SuccessSelected_StarColonStar) {
47-
setenv("ONEAPI_DEVICE_SELECTOR", "*:*", 1);
70+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:*");
4871
uint32_t count = 0;
4972
ASSERT_SUCCESS(
5073
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -74,7 +97,7 @@ TEST_P(urDeviceGetSelectedTest, SuccessSelected_StarColonStar) {
7497
}
7598

7699
TEST_P(urDeviceGetSelectedTest, SuccessSelected_StarColonZero) {
77-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0", 1);
100+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0");
78101
uint32_t count = 0;
79102
ASSERT_SUCCESS(
80103
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -88,7 +111,7 @@ TEST_P(urDeviceGetSelectedTest, SuccessSelected_StarColonZero) {
88111
}
89112

90113
TEST_P(urDeviceGetSelectedTest, SuccessSelected_StarColonZeroCommaStar) {
91-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0,*", 1);
114+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0,*");
92115
uint32_t count = 0;
93116
ASSERT_SUCCESS(
94117
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -102,15 +125,15 @@ TEST_P(urDeviceGetSelectedTest, SuccessSelected_StarColonZeroCommaStar) {
102125
}
103126

104127
TEST_P(urDeviceGetSelectedTest, SuccessSelected_DiscardStarColonStar) {
105-
setenv("ONEAPI_DEVICE_SELECTOR", "!*:*", 1);
128+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "!*:*");
106129
uint32_t count = 0;
107130
ASSERT_SUCCESS(
108131
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
109132
ASSERT_EQ(count, 0);
110133
}
111134

112135
TEST_P(urDeviceGetSelectedTest, SuccessSelected_SelectAndDiscard) {
113-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0;!*:*", 1);
136+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0;!*:*");
114137
uint32_t count = 0;
115138
ASSERT_SUCCESS(
116139
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -119,7 +142,7 @@ TEST_P(urDeviceGetSelectedTest, SuccessSelected_SelectAndDiscard) {
119142

120143
TEST_P(urDeviceGetSelectedTest,
121144
SuccessSelected_SelectSomethingAndDiscardSomethingElse) {
122-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0;!*:1", 1);
145+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0;!*:1");
123146
uint32_t count = 0;
124147
ASSERT_SUCCESS(
125148
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -133,23 +156,23 @@ TEST_P(urDeviceGetSelectedTest,
133156
}
134157

135158
TEST_P(urDeviceGetSelectedTest, InvalidNullHandlePlatform) {
136-
unsetenv("ONEAPI_DEVICE_SELECTOR");
159+
uur::unset_env("ONEAPI_DEVICE_SELECTOR");
137160
uint32_t count = 0;
138161
ASSERT_EQ_RESULT(
139162
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
140163
urDeviceGetSelected(nullptr, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
141164
}
142165

143166
TEST_P(urDeviceGetSelectedTest, InvalidEnumerationDevicesType) {
144-
unsetenv("ONEAPI_DEVICE_SELECTOR");
167+
uur::unset_env("ONEAPI_DEVICE_SELECTOR");
145168
uint32_t count = 0;
146169
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_ENUMERATION,
147170
urDeviceGetSelected(platform, UR_DEVICE_TYPE_FORCE_UINT32, 0,
148171
nullptr, &count));
149172
}
150173

151174
TEST_P(urDeviceGetSelectedTest, InvalidValueNumEntries) {
152-
unsetenv("ONEAPI_DEVICE_SELECTOR");
175+
uur::unset_env("ONEAPI_DEVICE_SELECTOR");
153176
uint32_t count = 0;
154177
ASSERT_SUCCESS(
155178
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
@@ -161,7 +184,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidValueNumEntries) {
161184
}
162185

163186
TEST_P(urDeviceGetSelectedTest, InvalidMissingBackend) {
164-
setenv("ONEAPI_DEVICE_SELECTOR", ":garbage", 1);
187+
uur::set_env("ONEAPI_DEVICE_SELECTOR", ":garbage");
165188
uint32_t count = 0;
166189
ASSERT_EQ_RESULT(
167190
UR_RESULT_ERROR_UNKNOWN,
@@ -170,7 +193,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingBackend) {
170193
}
171194

172195
TEST_P(urDeviceGetSelectedTest, InvalidGarbageBackendString) {
173-
setenv("ONEAPI_DEVICE_SELECTOR", "garbage:0", 1);
196+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "garbage:0");
174197
uint32_t count = 0;
175198
ASSERT_EQ_RESULT(
176199
UR_RESULT_ERROR_INVALID_VALUE,
@@ -179,20 +202,20 @@ TEST_P(urDeviceGetSelectedTest, InvalidGarbageBackendString) {
179202
}
180203

181204
TEST_P(urDeviceGetSelectedTest, SuccessCaseSensitive) {
182-
setenv("ONEAPI_DEVICE_SELECTOR", "OpEnCl:0", 1);
205+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "OpEnCl:0");
183206
uint32_t count = 0;
184207
ASSERT_SUCCESS(
185208
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
186209
}
187210

188211
TEST_P(urDeviceGetSelectedTest, InvalidMissingFilterStrings) {
189-
setenv("ONEAPI_DEVICE_SELECTOR", "*", 1);
212+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*");
190213
uint32_t count = 0;
191214
ASSERT_EQ_RESULT(
192215
UR_RESULT_ERROR_INVALID_VALUE,
193216
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
194217
ASSERT_EQ(count, 0);
195-
setenv("ONEAPI_DEVICE_SELECTOR", "*:", 1);
218+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:");
196219
uint32_t count2 = 0;
197220
ASSERT_EQ_RESULT(
198221
UR_RESULT_ERROR_INVALID_VALUE,
@@ -201,7 +224,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingFilterStrings) {
201224
}
202225

203226
TEST_P(urDeviceGetSelectedTest, InvalidMissingFilterString) {
204-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0,,2", 1);
227+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0,,2");
205228
uint32_t count = 0;
206229
ASSERT_EQ_RESULT(
207230
UR_RESULT_ERROR_UNKNOWN,
@@ -210,7 +233,7 @@ TEST_P(urDeviceGetSelectedTest, InvalidMissingFilterString) {
210233
}
211234

212235
TEST_P(urDeviceGetSelectedTest, InvalidTooManyDotsInFilterString) {
213-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0.1.2.3", 1);
236+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0.1.2.3");
214237
uint32_t count = 0;
215238
ASSERT_EQ_RESULT(
216239
UR_RESULT_ERROR_INVALID_VALUE,
@@ -219,13 +242,13 @@ TEST_P(urDeviceGetSelectedTest, InvalidTooManyDotsInFilterString) {
219242
}
220243

221244
TEST_P(urDeviceGetSelectedTest, InvalidBadWildardInFilterString) {
222-
setenv("ONEAPI_DEVICE_SELECTOR", "*:*.", 1);
245+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:*.");
223246
uint32_t count = 0;
224247
ASSERT_EQ_RESULT(
225248
UR_RESULT_ERROR_INVALID_VALUE,
226249
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
227250
ASSERT_EQ(count, 0);
228-
setenv("ONEAPI_DEVICE_SELECTOR", "*:*.0", 1);
251+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:*.0");
229252
uint32_t count2 = 0;
230253
ASSERT_EQ_RESULT(
231254
UR_RESULT_ERROR_INVALID_VALUE,
@@ -234,23 +257,23 @@ TEST_P(urDeviceGetSelectedTest, InvalidBadWildardInFilterString) {
234257
}
235258

236259
TEST_P(urDeviceGetSelectedTest, InvalidSelectingNonexistentDevice) {
237-
setenv("ONEAPI_DEVICE_SELECTOR", "*:4321", 1);
260+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:4321");
238261
uint32_t count = 0;
239262
ASSERT_SUCCESS(
240263
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
241264
ASSERT_EQ(count, 0);
242265
}
243266

244267
TEST_P(urDeviceGetSelectedTest, InvalidSelectingNonexistentSubDevice) {
245-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0.4321", 1);
268+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0.4321");
246269
uint32_t count = 0;
247270
ASSERT_SUCCESS(
248271
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));
249272
ASSERT_EQ(count, 0);
250273
}
251274

252275
TEST_P(urDeviceGetSelectedTest, InvalidSelectingNonexistentSubSubDevice) {
253-
setenv("ONEAPI_DEVICE_SELECTOR", "*:0.0.4321", 1);
276+
uur::set_env("ONEAPI_DEVICE_SELECTOR", "*:0.0.4321");
254277
uint32_t count = 0;
255278
ASSERT_SUCCESS(
256279
urDeviceGetSelected(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count));

0 commit comments

Comments
 (0)