1 /*******************************************************************************
2 * Copyright 2019-2020 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 #define TEST_F_(test_fixture, test_name) TEST_F(test_fixture, test_name)
58 
59 #define CPU_TEST_F(test_fixture, test_name) \
60     TEST_F_(test_fixture, TEST_CONCAT(test_name, _CPU))
61 
62 #define GPU_TEST_F(test_fixture, test_name) \
63     TEST_F_(test_fixture, TEST_CONCAT(test_name, _GPU))
64 
65 #define TEST_P_(test_fixture, test_name) TEST_P(test_fixture, test_name)
66 
67 #define CPU_TEST_P(test_fixture, test_name) \
68     TEST_P_(test_fixture, TEST_CONCAT(test_name, _CPU))
69 
70 #define GPU_TEST_P(test_fixture, test_name) \
71     TEST_P_(test_fixture, TEST_CONCAT(test_name, _GPU))
72 
73 #define INSTANTIATE_TEST_SUITE_P_(prefix, test_case_name, generator) \
74     INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator)
75 
76 #define CPU_INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator) \
77     INSTANTIATE_TEST_SUITE_P_( \
78             TEST_CONCAT(prefix, _CPU), test_case_name, generator)
79 
80 #define GPU_INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator) \
81     INSTANTIATE_TEST_SUITE_P_( \
82             TEST_CONCAT(prefix, _GPU), test_case_name, generator)
83 
84 #define GPU_INSTANTIATE_TEST_SUITE_P_(prefix, test_case_name, generator) \
85     GPU_INSTANTIATE_TEST_SUITE_P(prefix, test_case_name, generator)
86 
87 #ifdef DNNL_ENABLE_MEM_DEBUG
88 #define DERIVED_TEST_CLASS(test_fixture, test_name) \
89     test_fixture##_##test_name##_Derived_Test
90 
91 #define HANDLE_EXCEPTIONS_FOR_TEST_SETUP(...) \
92     void SetUp() override { \
93         catch_expected_failures([=]() { Testing(); }, false, dnnl_success); \
94     } \
95     void Testing()
96 
97 // Wrapper around TEST from gtest, intended to catch exceptions thrown by a unit
98 // test.
99 #define HANDLE_EXCEPTIONS_FOR_TEST(test_fixture, test_name) \
100     class DERIVED_TEST_CLASS(test_fixture, test_name) : public test_fixture { \
101         void TestBody() override {} \
102 \
103     public: \
104         void Test_failures(); \
105     }; \
106     TEST(test_fixture, test_name) { \
107         catch_expected_failures( \
108                 [=]() { \
109                     DERIVED_TEST_CLASS(test_fixture, test_name) \
110                     ().Test_failures(); \
111                 }, \
112                 false, dnnl_success, false); \
113     } \
114     void DERIVED_TEST_CLASS(test_fixture, test_name)::Test_failures()
115 
116 // Wrapper around TEST_F from gtest, intended to catch exceptions thrown by a
117 // test fixture.
118 #define HANDLE_EXCEPTIONS_FOR_TEST_F(test_fixture, test_name) \
119     class DERIVED_TEST_CLASS(test_fixture, test_name) : public test_fixture { \
120         void TestBody() override {} \
121 \
122     public: \
123         DERIVED_TEST_CLASS(test_fixture, test_name)() { SetUp(); } \
124         void Test_failures(); \
125     }; \
126     TEST_F(test_fixture, test_name) { \
127         catch_expected_failures( \
128                 [=]() { \
129                     DERIVED_TEST_CLASS(test_fixture, test_name) \
130                     ().Test_failures(); \
131                 }, \
132                 false, dnnl_success, false); \
133     } \
134     void DERIVED_TEST_CLASS(test_fixture, test_name)::Test_failures()
135 
136 // Wrapper around TEST_P from gtest, intended to catch exceptions thrown by
137 // a parametrized test.
138 #define HANDLE_EXCEPTIONS_FOR_TEST_P(test_fixture, test_name) \
139     class DERIVED_TEST_CLASS(test_fixture, test_name) : public test_fixture { \
140         void TestBody() override {} \
141 \
142     public: \
143         DERIVED_TEST_CLASS(test_fixture, test_name)() { SetUp(); } \
144         void Test_failures(); \
145     }; \
146     TEST_P(test_fixture, test_name) { \
147         catch_expected_failures( \
148                 [=]() { \
149                     DERIVED_TEST_CLASS(test_fixture, test_name) \
150                     ().Test_failures(); \
151                 }, \
152                 false, dnnl_success); \
153     } \
154     void DERIVED_TEST_CLASS(test_fixture, test_name)::Test_failures()
155 
156 #else
157 #define HANDLE_EXCEPTIONS_FOR_TEST_SETUP(...) void SetUp() override
158 #define HANDLE_EXCEPTIONS_FOR_TEST(test_fixture, test_name) \
159     TEST(test_fixture, test_name)
160 #define HANDLE_EXCEPTIONS_FOR_TEST_F(test_fixture, test_name) \
161     TEST_F(test_fixture, test_name)
162 #define HANDLE_EXCEPTIONS_FOR_TEST_P(test_fixture, test_name) \
163     TEST_P(test_fixture, test_name)
164 #endif
165 
166 #endif
167