|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out |
| 2 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 3 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out |
| 5 | + |
| 6 | +#include <sycl/sycl.hpp> |
| 7 | + |
| 8 | +using namespace sycl; |
| 9 | + |
| 10 | +int main() { |
| 11 | + // Test new constructors, initially each with empty string messages. |
| 12 | + std::string emptyStr; |
| 13 | + const char *emptyCharPtr = ""; |
| 14 | + |
| 15 | + // Going to set the error code values to each of the enum (0-12). |
| 16 | + exception ex1(make_error_code(errc::runtime), emptyStr); // errc::runtime == 1 |
| 17 | + exception ex2(make_error_code(errc::kernel), emptyCharPtr); |
| 18 | + exception ex3(make_error_code(errc::accessor)); |
| 19 | + exception ex4(static_cast<int>(errc::nd_range), sycl_category(), emptyStr); |
| 20 | + exception ex5(static_cast<int>(errc::event), sycl_category(), emptyCharPtr); |
| 21 | + exception ex6(static_cast<int>(errc::kernel_argument), sycl_category()); |
| 22 | + |
| 23 | + queue Q; |
| 24 | + context ctx = Q.get_context(); |
| 25 | + exception ex7(ctx, make_error_code(errc::build), emptyStr); |
| 26 | + exception ex8(ctx, make_error_code(errc::invalid), emptyCharPtr); |
| 27 | + exception ex9(ctx, make_error_code(errc::memory_allocation)); |
| 28 | + exception ex10(ctx, static_cast<int>(errc::platform), sycl_category(), |
| 29 | + emptyStr); |
| 30 | + exception ex11(ctx, static_cast<int>(errc::profiling), sycl_category(), |
| 31 | + emptyCharPtr); |
| 32 | + exception ex12(ctx, static_cast<int>(errc::feature_not_supported), |
| 33 | + sycl_category()); |
| 34 | + |
| 35 | + std::vector<exception> v{ex1, ex2, ex3, ex4, ex5, ex6, |
| 36 | + ex7, ex8, ex9, ex10, ex11, ex12}; |
| 37 | + for (int i = 0; i < 12; i++) { |
| 38 | + exception ex = v[i]; |
| 39 | + assert(ex.code().value() == i + 1 && |
| 40 | + "unexpected error_code.value() retrieved"); |
| 41 | + assert(ex.category() == sycl_category() && "expected SYCL error category"); |
| 42 | + if (i < 6) { |
| 43 | + assert(!ex.has_context() && |
| 44 | + "none of the first six exceptions should have a context"); |
| 45 | + } else { |
| 46 | + assert(ex.has_context() && ex.get_context() == ctx && |
| 47 | + "the second six exceptions should have a context equal to ctx"); |
| 48 | + } |
| 49 | + assert(strlen(ex.what()) == 0 && |
| 50 | + "all these exceptions were initialized with empty strings. We " |
| 51 | + "should not have anything in the 'what' message"); |
| 52 | + } |
| 53 | + |
| 54 | + // Now test constructor with a real string value, including one containing |
| 55 | + // null string terminator |
| 56 | + std::string testString("this is a test"); |
| 57 | + exception ex_string1(make_error_code(errc::kernel_not_supported), testString); |
| 58 | + assert(testString.compare(ex_string1.what()) == 0); |
| 59 | + testString[0] = '\0'; |
| 60 | + exception ex_early_terminated(make_error_code(errc::kernel_not_supported), |
| 61 | + testString); |
| 62 | + assert(ex_early_terminated.code().value() == |
| 63 | + static_cast<int>(errc::kernel_not_supported)); |
| 64 | + char testCharPtr[] = "this is also a test"; |
| 65 | + exception ex_string2(make_error_code(errc::backend_mismatch), testCharPtr); |
| 66 | + assert(strcmp(ex_string2.what(), testCharPtr) == 0); |
| 67 | + |
| 68 | + // Test sycl_category. |
| 69 | + assert(std::string("sycl").compare(sycl_category().name()) == 0 && |
| 70 | + "sycl_category name should be 'sycl'"); |
| 71 | + |
| 72 | + // Test make_error_code. |
| 73 | + std::error_code ec = make_error_code(errc::feature_not_supported); |
| 74 | + assert(ec.value() == static_cast<int>(errc::feature_not_supported)); |
| 75 | + assert(std::string("sycl").compare(ec.category().name()) == 0 && |
| 76 | + "error code category name should be 'sycl'"); |
| 77 | + |
| 78 | + // Test enum |
| 79 | + static_assert(std::is_error_code_enum<sycl::errc>::value, |
| 80 | + "errc enum should identify as error code"); |
| 81 | + static_assert(!std::is_error_condition_enum<sycl::errc>::value, |
| 82 | + "errc enum should not identify as error condition"); |
| 83 | + |
| 84 | + // Test errc_for and backends. Should compile without complaint. |
| 85 | + constexpr int EC = 1; |
| 86 | + sycl::backend_traits<sycl::backend::opencl>::errc someOpenCLErrCode{EC}; |
| 87 | + sycl::errc_for<sycl::backend::opencl> anotherOpenCLErrCode{EC}; |
| 88 | + assert(someOpenCLErrCode == anotherOpenCLErrCode); |
| 89 | + sycl::backend_traits<sycl::backend::ext_oneapi_level_zero>::errc |
| 90 | + someL0ErrCode{EC}; |
| 91 | + sycl::errc_for<sycl::backend::ext_oneapi_level_zero> anotherL0ErrCode{EC}; |
| 92 | + assert(someL0ErrCode == anotherL0ErrCode); |
| 93 | + sycl::backend_traits<sycl::backend::host>::errc someHOSTErrCode{EC}; |
| 94 | + sycl::errc_for<sycl::backend::host> anotherHOSTErrCode{EC}; |
| 95 | + assert(someHOSTErrCode == anotherHOSTErrCode); |
| 96 | + sycl::backend_traits<sycl::backend::ext_oneapi_cuda>::errc someCUDAErrCode{ |
| 97 | + EC}; |
| 98 | + sycl::errc_for<sycl::backend::ext_oneapi_cuda> anotherCUDAErrCode{EC}; |
| 99 | + assert(someCUDAErrCode == anotherCUDAErrCode); |
| 100 | + sycl::backend_traits<sycl::backend::ext_intel_esimd_emulator>::errc |
| 101 | + someESIMDErrCode{EC}; |
| 102 | + sycl::errc_for<sycl::backend::ext_intel_esimd_emulator> anotherESIMDErrCode{ |
| 103 | + EC}; |
| 104 | + assert(someESIMDErrCode == anotherESIMDErrCode); |
| 105 | + sycl::backend_traits<sycl::backend::ext_oneapi_hip>::errc someHIPErrCode{EC}; |
| 106 | + sycl::errc_for<sycl::backend::ext_oneapi_hip> anotherHIPErrCode{EC}; |
| 107 | + assert(someHIPErrCode == anotherHIPErrCode); |
| 108 | + |
| 109 | + std::cout << "OK" << std::endl; |
| 110 | + return 0; |
| 111 | +} |
0 commit comments