Skip to content

Commit 6638a8c

Browse files
steffenlarsenbb-sycl
authored andcommitted
[SYCL] Add test for exception when selected device not in context (intel#1527)
This commit adds a test for checking that the queue constructor throws an exception if the selected device is not in the supplied context. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 154af66 commit 6638a8c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %BE_RUN_PLACEHOLDER %t.out
3+
4+
// This test checks that the queue constructor throws a sycl::exception if the
5+
// device selected by the provided selector is not in the specified context.
6+
7+
#include <sycl/sycl.hpp>
8+
9+
#include <iostream>
10+
#include <optional>
11+
12+
std::optional<sycl::device>
13+
FindOtherDevice(const sycl::device &ExcludedDevice) {
14+
for (sycl::device Device : sycl::device::get_devices())
15+
if (Device != ExcludedDevice)
16+
return Device;
17+
return std::nullopt;
18+
}
19+
20+
int main() {
21+
const sycl::device SelectedDevice(sycl::default_selector_v);
22+
std::optional<sycl::device> OtherDevice = FindOtherDevice(SelectedDevice);
23+
24+
if (!OtherDevice.has_value()) {
25+
std::cout << "No other device found. Skipping test." << std::endl;
26+
return 0;
27+
}
28+
29+
sycl::context Ctx(*OtherDevice);
30+
try {
31+
sycl::queue q(Ctx, sycl::default_selector_v);
32+
std::cout << "Queue constructor did not throw." << std::endl;
33+
return 1;
34+
} catch (const sycl::exception &e) {
35+
assert(e.code() == sycl::errc::invalid &&
36+
"Error code should be sycl::errc::invalid.");
37+
}
38+
return 0;
39+
}

0 commit comments

Comments
 (0)