Skip to content

Commit cae57ff

Browse files
committed
Refactor the test so it is more verbose on fails
1 parent 2ba98e5 commit cae57ff

File tree

1 file changed

+68
-63
lines changed

1 file changed

+68
-63
lines changed

sycl/test-e2e/Basic/vector/int-convert.cpp

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
#include <string>
1515
#include <type_traits>
1616

17-
// Debug prints are hidden under macro to reduce amount of output in CI runs
18-
// and thus speed up tests. However, they are useful when debugging the test
19-
// locally and can be quickly turned on in there.
20-
#ifdef ENABLE_DEBUG_OUTPUT
21-
2217
template <typename T> std::string to_string() { return "unknown type"; }
2318
template <> std::string to_string<std::byte>() { return "std::byte"; }
2419
template <> std::string to_string<char>() { return "char"; }
@@ -36,33 +31,25 @@ template <> std::string to_string<unsigned long long>() {
3631
}
3732
template <> std::string to_string<bool>() { return "bool"; }
3833

39-
#define DEBUG_PRINT(x) std::cout << x << std::endl;
40-
41-
#else
42-
#define DEBUG_PRINT(x)
43-
#endif
44-
4534
template <typename T>
46-
void check_vectors_equal(sycl::vec<T, 4> a, sycl::vec<T, 4> b) {
47-
bool all_good =
35+
bool check_vectors_equal(sycl::vec<T, 4> a, sycl::vec<T, 4> b,
36+
const std::string &fail_message) {
37+
bool result =
4838
a.x() == b.x() && a.y() == b.y() && a.z() == b.z() && a.w() == b.w();
49-
if (!all_good) {
50-
DEBUG_PRINT("host and device results mismatch:");
51-
DEBUG_PRINT(
52-
"\t{" << static_cast<int>(a.x()) << ", " << static_cast<int>(a.y())
53-
<< ", " << static_cast<int>(a.z()) << ", "
54-
<< static_cast<int>(a.w()) << "} vs {" << static_cast<int>(b.x())
55-
<< ", " << static_cast<int>(b.y()) << ", "
56-
<< static_cast<int>(b.z()) << ", " << static_cast<int>(b.w())
57-
<< "}");
39+
if (!result) {
40+
std::cout << fail_message << std::endl;
41+
std::cout << "\t{" << static_cast<int>(a.x()) << ", "
42+
<< static_cast<int>(a.y()) << ", " << static_cast<int>(a.z())
43+
<< ", " << static_cast<int>(a.w()) << "} vs {"
44+
<< static_cast<int>(b.x()) << ", " << static_cast<int>(b.y())
45+
<< ", " << static_cast<int>(b.z()) << ", "
46+
<< static_cast<int>(b.w()) << "}" << std::endl;
5847
}
59-
assert(all_good);
60-
}
6148

62-
template <typename From, typename To> void check_convert() {
63-
DEBUG_PRINT("checking vec<" << to_string<From>() << ", 4>::convert<"
64-
<< to_string<To>() << ">()");
49+
return result;
50+
}
6551

52+
template <typename From, typename To> bool check_convert() {
6653
sycl::vec<From, 4> input;
6754
if constexpr (std::is_signed_v<From>) {
6855
input = sycl::vec<From, 4>{static_cast<From>(37), static_cast<From>(0),
@@ -84,66 +71,84 @@ template <typename From, typename To> void check_convert() {
8471
auto acc = buf.get_host_access();
8572
auto deviceResult = acc[0];
8673

87-
// Host and device results must match.
88-
check_vectors_equal(hostResult, deviceResult);
74+
std::string test =
75+
"(vec<" + to_string<From>() + ", 4>::convert<" + to_string<To>() + ">)";
8976

77+
// Host and device results must match.
78+
bool host_and_device_match = check_vectors_equal(
79+
hostResult, deviceResult, "host and device results do not match " + test);
9080
// And they should match with a reference, which is for integer conversions
9181
// can be computed with a simple static_cast.
9282
// Strictly speaking, integer conversions are underspecified in the SYCL 2020
9383
// spec, but `static_cast` implementation matches SYCL-CTS, so we will leave
9484
// it here for now as well.
9585
// See https://github.com/KhronosGroup/SYCL-Docs/issues/492
96-
assert(deviceResult.x() == static_cast<To>(input.x()));
97-
assert(deviceResult.y() == static_cast<To>(input.y()));
98-
assert(deviceResult.z() == static_cast<To>(input.z()));
99-
assert(deviceResult.w() == static_cast<To>(input.w()));
86+
sycl::vec<To, 4> reference{
87+
static_cast<To>(input.x()), static_cast<To>(input.y()),
88+
static_cast<To>(input.z()), static_cast<To>(input.w())};
89+
bool device_matches_reference = check_vectors_equal(
90+
deviceResult, reference, "device results don't match reference " + test);
91+
bool host_matches_reference = check_vectors_equal(
92+
hostResult, reference, "host resutls don't match reference " + test);
93+
94+
return host_and_device_match && device_matches_reference &&
95+
host_matches_reference;
10096
}
10197

10298
template <class T>
10399
constexpr auto has_unsigned_v =
104100
std::is_integral_v<T> && !std::is_same_v<T, bool> &&
105101
!std::is_same_v<T, sycl::byte> && !std::is_same_v<T, std::byte>;
106102

107-
template <typename From, typename To> void check_signed_unsigned_convert_to() {
108-
check_convert<From, To>();
103+
template <typename From, typename To> bool check_signed_unsigned_convert_to() {
104+
bool pass = true;
105+
pass &= check_convert<From, To>();
109106
if constexpr (has_unsigned_v<To>)
110-
check_convert<From, std::make_unsigned_t<To>>();
107+
pass &= check_convert<From, std::make_unsigned_t<To>>();
111108
if constexpr (has_unsigned_v<From>)
112-
check_convert<std::make_unsigned_t<From>, To>();
109+
pass &= check_convert<std::make_unsigned_t<From>, To>();
113110
if constexpr (has_unsigned_v<To> && has_unsigned_v<From>)
114-
check_convert<std::make_unsigned_t<From>, std::make_unsigned_t<To>>();
111+
pass &=
112+
check_convert<std::make_unsigned_t<From>, std::make_unsigned_t<To>>();
113+
return pass;
115114
}
116115

117-
template <typename From> void check_convert_from() {
118-
check_signed_unsigned_convert_to<From, sycl::byte>();
116+
template <typename From> bool check_convert_from() {
117+
bool pass = true;
118+
pass &= check_signed_unsigned_convert_to<From, sycl::byte>();
119119
// FIXME: enable test cases below once compilation issues for them are fixed
120120
// check_signed_unsigned_convert_to<From, std::byte>();
121-
check_signed_unsigned_convert_to<From, std::int8_t>();
122-
check_signed_unsigned_convert_to<From, std::int16_t>();
123-
check_signed_unsigned_convert_to<From, std::int32_t>();
124-
check_signed_unsigned_convert_to<From, std::int64_t>();
125-
check_signed_unsigned_convert_to<From, bool>();
126-
check_signed_unsigned_convert_to<From, char>();
127-
check_signed_unsigned_convert_to<From, signed char>();
128-
check_signed_unsigned_convert_to<From, short>();
129-
check_signed_unsigned_convert_to<From, int>();
130-
check_signed_unsigned_convert_to<From, long>();
131-
check_signed_unsigned_convert_to<From, long long>();
121+
pass &= check_signed_unsigned_convert_to<From, std::int8_t>();
122+
pass &= check_signed_unsigned_convert_to<From, std::int16_t>();
123+
pass &= check_signed_unsigned_convert_to<From, std::int32_t>();
124+
pass &= check_signed_unsigned_convert_to<From, std::int64_t>();
125+
pass &= check_signed_unsigned_convert_to<From, bool>();
126+
pass &= check_signed_unsigned_convert_to<From, char>();
127+
pass &= check_signed_unsigned_convert_to<From, signed char>();
128+
pass &= check_signed_unsigned_convert_to<From, short>();
129+
pass &= check_signed_unsigned_convert_to<From, int>();
130+
pass &= check_signed_unsigned_convert_to<From, long>();
131+
pass &= check_signed_unsigned_convert_to<From, long long>();
132+
133+
return pass;
132134
}
133135

134136
int main() {
135-
check_convert_from<sycl::byte>();
137+
bool pass = true;
138+
pass &= check_convert_from<sycl::byte>();
136139
// FIXME: enable test cases below once compilation issues for them are fixed
137140
// check_convert_from<std::byte>();
138-
check_convert_from<std::int8_t>();
139-
check_convert_from<std::int16_t>();
140-
check_convert_from<std::int32_t>();
141-
check_convert_from<std::int64_t>();
142-
check_convert_from<char>();
143-
check_convert_from<signed char>();
144-
check_convert_from<short>();
145-
check_convert_from<int>();
146-
check_convert_from<long>();
147-
check_convert_from<long long>();
148-
check_convert_from<bool>();
141+
pass &= check_convert_from<std::int8_t>();
142+
pass &= check_convert_from<std::int16_t>();
143+
pass &= check_convert_from<std::int32_t>();
144+
pass &= check_convert_from<std::int64_t>();
145+
pass &= check_convert_from<char>();
146+
pass &= check_convert_from<signed char>();
147+
pass &= check_convert_from<short>();
148+
pass &= check_convert_from<int>();
149+
pass &= check_convert_from<long>();
150+
pass &= check_convert_from<long long>();
151+
pass &= check_convert_from<bool>();
152+
153+
return static_cast<int>(!pass);
149154
}

0 commit comments

Comments
 (0)