1/*******************************************************************************
2* Copyright 2019-2022 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#ifndef DNNL_TEST_MACROS_HPP
18#define DNNL_TEST_MACROS_HPP
19
20#include <iostream>
21
22#include "dnnl_test_common.hpp"
23#include "gtest/gtest.h"
24
25#define TEST_CONCAT_(a, b) a##b
26#define TEST_CONCAT(a, b) TEST_CONCAT_(a, b)
27
28#define SKIP_IF(cond, msg) \
29 do { \
30 if (cond) { \
31 std::cout << "[ SKIPPED ] " << (msg) << std::endl; \
32 return; \
33 } \
34 } while (0)
35
36#define SKIP_FOR_LOOP(cond, msg) \
37 if (cond) { \
38 std::cout << "[ SKIPPED ] " << (msg) << std::endl; \
39 continue; \
40 }
41
42#ifdef DNNL_SYCL_CUDA
43#define SKIP_IF_CUDA(cond, message) \
44 do { \
45 SKIP_IF(get_test_engine_kind() == engine::kind::gpu && (cond), \
46 (message)); \
47 } while (0)
48
49#define SKIP_FOR_LOOP_CUDA(cond, message) \
50 SKIP_FOR_LOOP( \
51 get_test_engine_kind() == engine::kind::gpu && (cond), (message));
52#else
53#define SKIP_IF_CUDA(cond, message)
54#define SKIP_FOR_LOOP_CUDA(cond, message)
55#endif
56
57#ifdef DNNL_SYCL_HIP
58#define SKIP_IF_HIP(cond, message) \
59 do { \
60 SKIP_IF(get_test_engine_kind() == engine::kind::gpu && (cond), \
61 (message)); \
62 } while (0)
63
64#define SKIP_FOR_LOOP_HIP(cond, message) \
65 SKIP_FOR_LOOP( \
66 get_test_engine_kind() == engine::kind::gpu && (cond), (message));
67#else
68#define SKIP_IF_HIP(cond, message)
69#define SKIP_FOR_LOOP_HIP(cond, message)
70#endif
71
72#define TEST_F_(test_fixture, test_name) TEST_F(test_fixture, test_name)
73
74#define CPU_TEST_F(test_fixture, test_name) \
75 TEST_F_(test_fixture, TEST_CONCAT(test_name, _CPU))
76
77#define GPU_TEST_F(test_fixture, test_name) \
78 TEST_F_(test_fixture, TEST_CONCAT(test_name, _GPU))
79
80#define TEST_P_(test_fixture, test_name) TEST_P(test_fixture, test_name)
81
82#define CPU_TEST_P(test_fixture, test_name) \
83 TEST_P_(test_fixture, TEST_CONCAT(test_name, _CPU))
84
85#define GPU_TEST_P(test_fixture, test_name) \
86 TEST_P_(test_fixture, TEST_CONCAT(test_name, _GPU))
87
88#define INSTANTIATE_TEST_SUITE_P_(prefix, test_case_name, generator) \
89 INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator)
90
91#define CPU_INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator) \
92 INSTANTIATE_TEST_SUITE_P_( \
93 TEST_CONCAT(prefix, _CPU), test_case_name, generator)
94
95#define GPU_INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator) \
96 INSTANTIATE_TEST_SUITE_P_( \
97 TEST_CONCAT(prefix, _GPU), test_case_name, generator)
98
99#define GPU_INSTANTIATE_TEST_SUITE_P_(prefix, test_case_name, generator) \
100 GPU_INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator)
101
102#ifdef DNNL_ENABLE_MEM_DEBUG
103#define DERIVED_TEST_CLASS(test_fixture, test_name) \
104 test_fixture##_##test_name##_Derived_Test
105
106#define HANDLE_EXCEPTIONS_FOR_TEST_SETUP(...) \
107 void SetUp() override { \
108 catch_expected_failures([=]() { Testing(); }, false, dnnl_success); \
109 } \
110 void Testing()
111
112// Wrapper around TEST from gtest, intended to catch exceptions thrown by a unit
113// test.
114#define HANDLE_EXCEPTIONS_FOR_TEST(test_fixture, test_name) \
115 class DERIVED_TEST_CLASS(test_fixture, test_name) : public test_fixture { \
116 void TestBody() override {} \
117\
118 public: \
119 void Test_failures(); \
120 }; \
121 TEST(test_fixture, test_name) { \
122 catch_expected_failures( \
123 [=]() { \
124 DERIVED_TEST_CLASS(test_fixture, test_name) \
125 ().Test_failures(); \
126 }, \
127 false, dnnl_success, false); \
128 } \
129 void DERIVED_TEST_CLASS(test_fixture, test_name)::Test_failures()
130
131// Wrapper around TEST_F from gtest, intended to catch exceptions thrown by a
132// test fixture.
133#define HANDLE_EXCEPTIONS_FOR_TEST_F(test_fixture, test_name) \
134 class DERIVED_TEST_CLASS(test_fixture, test_name) : public test_fixture { \
135 void TestBody() override {} \
136\
137 public: \
138 DERIVED_TEST_CLASS(test_fixture, test_name)() { SetUp(); } \
139 void Test_failures(); \
140 }; \
141 TEST_F(test_fixture, test_name) { \
142 catch_expected_failures( \
143 [=]() { \
144 DERIVED_TEST_CLASS(test_fixture, test_name) \
145 ().Test_failures(); \
146 }, \
147 false, dnnl_success, false); \
148 } \
149 void DERIVED_TEST_CLASS(test_fixture, test_name)::Test_failures()
150
151// Wrapper around TEST_P from gtest, intended to catch exceptions thrown by
152// a parametrized test.
153#define HANDLE_EXCEPTIONS_FOR_TEST_P(test_fixture, test_name) \
154 class DERIVED_TEST_CLASS(test_fixture, test_name) : public test_fixture { \
155 void TestBody() override {} \
156\
157 public: \
158 DERIVED_TEST_CLASS(test_fixture, test_name)() { SetUp(); } \
159 void Test_failures(); \
160 }; \
161 TEST_P(test_fixture, test_name) { \
162 catch_expected_failures( \
163 [=]() { \
164 DERIVED_TEST_CLASS(test_fixture, test_name) \
165 ().Test_failures(); \
166 }, \
167 false, dnnl_success); \
168 } \
169 void DERIVED_TEST_CLASS(test_fixture, test_name)::Test_failures()
170
171#else
172#define HANDLE_EXCEPTIONS_FOR_TEST_SETUP(...) void SetUp() override
173#define HANDLE_EXCEPTIONS_FOR_TEST(test_fixture, test_name) \
174 TEST(test_fixture, test_name)
175#define HANDLE_EXCEPTIONS_FOR_TEST_F(test_fixture, test_name) \
176 TEST_F(test_fixture, test_name)
177#define HANDLE_EXCEPTIONS_FOR_TEST_P(test_fixture, test_name) \
178 TEST_P(test_fixture, test_name)
179#endif
180
181#endif
182