1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // Macros for use in enabling/disabling tests on particular 17 // platforms. Marking a gunit test as disabled still ensures that it 18 // compiles. 19 // 20 // Implementation note: the macros are structured as follows: 21 // * Define the disabled macro to just pass the test name through (which, in 22 // effect, does not disable it at all) 23 // * If a XLA_TEST_BACKEND_$TARGET macro indicates we're compiling for 24 // $TARGET platform, make the disabled macro truly disable the test; i.e. by 25 // redefining the DISABLED_ON_$TARGET macro to prepend "DISABLED_" to the test 26 // name. 27 28 #ifndef TENSORFLOW_COMPILER_XLA_TESTS_TEST_MACROS_H_ 29 #define TENSORFLOW_COMPILER_XLA_TESTS_TEST_MACROS_H_ 30 31 #include <string> 32 33 #include "absl/strings/string_view.h" 34 #include "tensorflow/compiler/xla/types.h" 35 #include "tensorflow/core/platform/test.h" 36 37 #define DISABLED_ON_CPU(X) X 38 #define DISABLED_ON_GPU(X) X 39 #define DISABLED_ON_GPU_ROCM(X) X 40 #define DISABLED_ON_INTERPRETER(X) X 41 #define DISABLED_ON_INTERPRETER_TSAN(X) X 42 43 // We need this macro instead of pasting directly to support nesting 44 // the DISABLED_ON_FOO macros, as in the definition of DISABLED_ON_CPU. 45 // Otherwise the pasting is applied before macro expansion completes. 46 #define XLA_TEST_PASTE(A, B) A##B 47 48 // We turn off clang-format so we can indent the macros for readability. 49 // clang-format off 50 51 #ifdef XLA_TEST_BACKEND_CPU 52 # undef DISABLED_ON_CPU 53 # define DISABLED_ON_CPU(X) XLA_TEST_PASTE(DISABLED_, X) 54 #endif // XLA_TEST_BACKEND_CPU 55 56 #ifdef XLA_TEST_BACKEND_GPU 57 # undef DISABLED_ON_GPU 58 # define DISABLED_ON_GPU(X) XLA_TEST_PASTE(DISABLED_, X) 59 60 #if TENSORFLOW_USE_ROCM 61 # undef DISABLED_ON_GPU_ROCM 62 # define DISABLED_ON_GPU_ROCM(X) XLA_TEST_PASTE(DISABLED_, X) 63 #endif // TENSORFLOW_USE_ROCM 64 65 #endif // XLA_TEST_BACKEND_GPU 66 67 #ifdef XLA_TEST_BACKEND_INTERPRETER 68 # undef DISABLED_ON_INTERPRETER 69 # define DISABLED_ON_INTERPRETER(X) XLA_TEST_PASTE(DISABLED_, X) 70 71 #ifdef THREAD_SANITIZER 72 # undef DISABLED_ON_INTERPRETER_TSAN 73 # define DISABLED_ON_INTERPRETER_TSAN(X) XLA_TEST_PASTE(DISABLED_, X) 74 #endif // THREAD_SANITIZER 75 76 #endif // XLA_TEST_BACKEND_INTERPRETER 77 78 // clang-format on 79 80 namespace xla { 81 82 // Reads a disabled manifest file to resolve whether test cases should be 83 // disabled on a particular platform. For a test that should be disabled, 84 // returns DISABLED_ prepended to its name; otherwise returns the test name 85 // unmodified. 86 std::string PrependDisabledIfIndicated(absl::string_view test_case_name, 87 absl::string_view test_name); 88 89 } // namespace xla 90 91 // This is the internal "gtest" class instantiation -- it is identical to the 92 // GTEST_TEST_ macro, except that we intercept the test name for potential 93 // modification by PrependDisabledIfIndicated. That file can use an arbitrary 94 // heuristic to decide whether the test case should be disabled, and we 95 // determine whether the test case should be disabled by resolving the (test 96 // case name, test name) in a manifest file. 97 #define XLA_GTEST_TEST_(test_case_name, test_name, parent_class) \ 98 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 99 : public parent_class { \ 100 public: \ 101 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {} \ 102 \ 103 private: \ 104 virtual void TestBody(); \ 105 static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ 106 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_case_name, \ 107 test_name)); \ 108 }; \ 109 \ 110 ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, \ 111 test_name)::test_info_ = \ 112 ::testing::RegisterTest( \ 113 #test_case_name, \ 114 ::xla::PrependDisabledIfIndicated(#test_case_name, #test_name) \ 115 .c_str(), \ 116 nullptr, nullptr, __FILE__, __LINE__, []() -> parent_class* { \ 117 return new GTEST_TEST_CLASS_NAME_(test_case_name, test_name)(); \ 118 }); \ 119 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 120 121 // This is identical to the TEST_F macro from "gtest", but it potentially 122 // disables the test based on an external manifest file, DISABLED_MANIFEST. 123 // 124 // Per usual, you can see what tests are available via --gunit_list_tests and 125 // choose to run tests that have been disabled via the manifest via 126 // --gunit_also_run_disabled_tests. 127 #define XLA_TEST_F(test_fixture, test_name) \ 128 XLA_GTEST_TEST_(test_fixture, test_name, test_fixture) 129 130 // Likewise, this is identical to the TEST_P macro from "gtest", but 131 // potentially disables the test based on the DISABLED_MANIFEST file. 132 // 133 // We have to wrap this in an outer layer so that any DISABLED_ON_* macros will 134 // be properly expanded before the stringification occurs. 135 #define XLA_TEST_P_IMPL_(test_case_name, test_name) \ 136 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 137 : public test_case_name { \ 138 public: \ 139 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {} \ 140 virtual void TestBody(); \ 141 \ 142 private: \ 143 static int AddToRegistry() { \ 144 ::testing::UnitTest::GetInstance() \ 145 ->parameterized_test_registry() \ 146 .GetTestCasePatternHolder<test_case_name>( \ 147 #test_case_name, \ 148 ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ 149 ->AddTestPattern( \ 150 #test_case_name, \ 151 ::xla::PrependDisabledIfIndicated(#test_case_name, #test_name) \ 152 .c_str(), \ 153 new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \ 154 test_case_name, test_name)>()); \ 155 return 0; \ 156 } \ 157 static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \ 158 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_case_name, \ 159 test_name)); \ 160 }; \ 161 int GTEST_TEST_CLASS_NAME_(test_case_name, \ 162 test_name)::gtest_registering_dummy_ = \ 163 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \ 164 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 165 166 #define XLA_TEST_P(test_case_name, test_name) \ 167 XLA_TEST_P_IMPL_(test_case_name, test_name) 168 169 // This is identical to the TEST_F macro from "gtest", but it potentially 170 // disables the test based on an external manifest file, DISABLED_MANIFEST. 171 #define XLA_TYPED_TEST(CaseName, TestName) \ 172 template <typename gtest_TypeParam_> \ 173 class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ 174 : public CaseName<gtest_TypeParam_> { \ 175 private: \ 176 typedef CaseName<gtest_TypeParam_> TestFixture; \ 177 typedef gtest_TypeParam_ TypeParam; \ 178 virtual void TestBody(); \ 179 }; \ 180 bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \ 181 ::testing::internal::TypeParameterizedTest< \ 182 CaseName, \ 183 ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName, \ 184 TestName)>, \ 185 GTEST_TYPE_PARAMS_(CaseName)>:: \ 186 Register( \ 187 "", ::testing::internal::CodeLocation(__FILE__, __LINE__), \ 188 #CaseName, \ 189 ::xla::PrependDisabledIfIndicated(#CaseName, #TestName).c_str(), \ 190 0); \ 191 template <typename gtest_TypeParam_> \ 192 void GTEST_TEST_CLASS_NAME_(CaseName, \ 193 TestName)<gtest_TypeParam_>::TestBody() 194 195 #endif // TENSORFLOW_COMPILER_XLA_TESTS_TEST_MACROS_H_ 196