Home | History | Annotate | Download | only in tests
      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 "tensorflow/compiler/xla/types.h"
     34 #include "tensorflow/core/platform/test.h"
     35 
     36 #define DISABLED_ON_CPU(X) X
     37 #define DISABLED_ON_CPU_PARALLEL(X) X
     38 #define DISABLED_ON_GPU(X) X
     39 #define DISABLED_ON_INTERPRETER(X) X
     40 
     41 // We need this macro instead of pasting directly to support nesting
     42 // the DISABLED_ON_FOO macros, as in the definition of DISABLED_ON_CPU.
     43 // Otherwise the pasting is applied before macro expansion completes.
     44 #define XLA_TEST_PASTE(A, B) A##B
     45 
     46 // We turn off clang-format so we can indent the macros for readability.
     47 // clang-format off
     48 
     49 #ifdef XLA_TEST_BACKEND_CPU
     50 # undef DISABLED_ON_CPU
     51 # define DISABLED_ON_CPU(X) XLA_TEST_PASTE(DISABLED_, X)
     52 #endif  // XLA_TEST_BACKEND_CPU
     53 
     54 #ifdef XLA_TEST_BACKEND_CPU_PARALLEL
     55 # undef DISABLED_ON_CPU
     56 # define DISABLED_ON_CPU(X) XLA_TEST_PASTE(DISABLED_, X)
     57 # undef DISABLED_ON_CPU_PARALLEL
     58 # define DISABLED_ON_CPU_PARALLEL(X) XLA_TEST_PASTE(DISABLED_, X)
     59 #endif  // XLA_TEST_BACKEND_CPU_PARALLEL
     60 
     61 #ifdef XLA_TEST_BACKEND_GPU
     62 # undef DISABLED_ON_GPU
     63 # define DISABLED_ON_GPU(X) XLA_TEST_PASTE(DISABLED_, X)
     64 #endif  // XLA_TEST_BACKEND_GPU
     65 
     66 #ifdef XLA_TEST_BACKEND_INTERPRETER
     67 # undef DISABLED_ON_INTERPRETER
     68 # define DISABLED_ON_INTERPRETER(X) XLA_TEST_PASTE(DISABLED_, X)
     69 #endif  // XLA_TEST_BACKEND_INTERPRETER
     70 
     71 // clang-format on
     72 
     73 namespace xla {
     74 
     75 // Reads a disabled manifest file to resolve whether test cases should be
     76 // disabled on a particular platform. For a test that should be disabled,
     77 // returns DISABLED_ prepended to its name; otherwise returns the test name
     78 // unmodified.
     79 string PrependDisabledIfIndicated(const string& test_case_name,
     80                                   const string& test_name);
     81 
     82 }  // namespace xla
     83 
     84 // This is the internal "gtest" class instantiation -- it is identical to the
     85 // GTEST_TEST_ macro, except that we intercept the test name for potential
     86 // modification by PrependDisabledIfIndicated. That file can use an arbitrary
     87 // heuristic to decide whether the test case should be disabled, and we
     88 // determine whether the test case should be disabled by resolving the (test
     89 // case name, test name) in a manifest file.
     90 #define XLA_GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)   \
     91   class GTEST_TEST_CLASS_NAME_(test_case_name, test_name)                     \
     92       : public parent_class {                                                 \
     93    public:                                                                    \
     94     GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}                    \
     95                                                                               \
     96    private:                                                                   \
     97     virtual void TestBody();                                                  \
     98     static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;     \
     99     GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_case_name,    \
    100                                                            test_name));       \
    101   };                                                                          \
    102                                                                               \
    103   ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name,           \
    104                                                     test_name)::test_info_ =  \
    105       ::testing::internal::MakeAndRegisterTestInfo(                           \
    106           #test_case_name,                                                    \
    107           ::xla::PrependDisabledIfIndicated(#test_case_name, #test_name)      \
    108               .c_str(),                                                       \
    109           nullptr, nullptr,                                                   \
    110           ::testing::internal::CodeLocation(__FILE__, __LINE__), (parent_id), \
    111           parent_class::SetUpTestCase, parent_class::TearDownTestCase,        \
    112           new ::testing::internal::TestFactoryImpl<GTEST_TEST_CLASS_NAME_(    \
    113               test_case_name, test_name)>);                                   \
    114   void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()
    115 
    116 // This is identical to the TEST_F macro from "gtest", but it potentially
    117 // disables the test based on an external manifest file, DISABLED_MANIFEST.
    118 //
    119 // Per usual, you can see what tests are available via --gunit_list_tests and
    120 // choose to run tests that have been disabled via the manifest via
    121 // --gunit_also_run_disabled_tests.
    122 #define XLA_TEST_F(test_fixture, test_name)              \
    123   XLA_GTEST_TEST_(test_fixture, test_name, test_fixture, \
    124                   ::testing::internal::GetTypeId<test_fixture>())
    125 
    126 // Likewise, this is identical to the TEST_P macro from "gtest", but
    127 // potentially disables the test based on the DISABLED_MANIFEST file.
    128 //
    129 // We have to wrap this in an outer layer so that any DISABLED_ON_* macros will
    130 // be properly expanded before the stringification occurs.
    131 #define XLA_TEST_P_IMPL_(test_case_name, test_name)                            \
    132   class GTEST_TEST_CLASS_NAME_(test_case_name, test_name)                      \
    133       : public test_case_name {                                                \
    134    public:                                                                     \
    135     GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}                     \
    136     virtual void TestBody();                                                   \
    137                                                                                \
    138    private:                                                                    \
    139     static int AddToRegistry() {                                               \
    140       ::testing::UnitTest::GetInstance()                                       \
    141           ->parameterized_test_registry()                                      \
    142           .GetTestCasePatternHolder<test_case_name>(                           \
    143               #test_case_name,                                                 \
    144               ::testing::internal::CodeLocation(__FILE__, __LINE__))           \
    145           ->AddTestPattern(                                                    \
    146               #test_case_name,                                                 \
    147               ::xla::PrependDisabledIfIndicated(#test_case_name, #test_name)   \
    148                   .c_str(),                                                    \
    149               new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \
    150                   test_case_name, test_name)>());                              \
    151       return 0;                                                                \
    152     }                                                                          \
    153     static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_;               \
    154     GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_case_name,     \
    155                                                            test_name));        \
    156   };                                                                           \
    157   int GTEST_TEST_CLASS_NAME_(test_case_name,                                   \
    158                              test_name)::gtest_registering_dummy_ =            \
    159       GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry();      \
    160   void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()
    161 
    162 #define XLA_TEST_P(test_case_name, test_name) \
    163   XLA_TEST_P_IMPL_(test_case_name, test_name)
    164 
    165 // This is identical to the TEST_F macro from "gtest", but it potentially
    166 // disables the test based on an external manifest file, DISABLED_MANIFEST.
    167 #define XLA_TYPED_TEST(CaseName, TestName)                                     \
    168   template <typename gtest_TypeParam_>                                         \
    169   class GTEST_TEST_CLASS_NAME_(CaseName, TestName)                             \
    170       : public CaseName<gtest_TypeParam_> {                                    \
    171    private:                                                                    \
    172     typedef CaseName<gtest_TypeParam_> TestFixture;                            \
    173     typedef gtest_TypeParam_ TypeParam;                                        \
    174     virtual void TestBody();                                                   \
    175   };                                                                           \
    176   bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ =   \
    177       ::testing::internal::TypeParameterizedTest<                              \
    178           CaseName,                                                            \
    179           ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName,    \
    180                                                                   TestName)>,  \
    181           GTEST_TYPE_PARAMS_(CaseName)>::                                      \
    182           Register(                                                            \
    183               "", ::testing::internal::CodeLocation(__FILE__, __LINE__),       \
    184               #CaseName,                                                       \
    185               ::xla::PrependDisabledIfIndicated(#CaseName, #TestName).c_str(), \
    186               0);                                                              \
    187   template <typename gtest_TypeParam_>                                         \
    188   void GTEST_TEST_CLASS_NAME_(CaseName,                                        \
    189                               TestName)<gtest_TypeParam_>::TestBody()
    190 
    191 #endif  // TENSORFLOW_COMPILER_XLA_TESTS_TEST_MACROS_H_
    192