github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/test/APITest.h (about)

     1  //===- Test.h - Simple macros for API unit tests ----------------*- C++ -*-===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  //
    18  // This file define simple macros for declaring test functions and running them.
    19  // The actual checking must be performed on the outputs with FileCheck.
    20  //
    21  //===----------------------------------------------------------------------===//
    22  
    23  #ifndef MLIR_TEST_TEST_H_
    24  #define MLIR_TEST_TEST_H_
    25  
    26  #include <functional>
    27  #include <vector>
    28  
    29  namespace test_detail {
    30  // Returns a mutable list of known test functions.  Used internally by test
    31  // macros to add and run tests.  This function is static to ensure it creates a
    32  // new list in each test file.
    33  static std::vector<std::function<void()>> &tests() {
    34    static std::vector<std::function<void()>> list;
    35    return list;
    36  }
    37  
    38  // Test registration class.  Used internally by test macros to register tests
    39  // during static allocation.
    40  struct TestRegistration {
    41    explicit TestRegistration(std::function<void()> func) {
    42      test_detail::tests().push_back(func);
    43    }
    44  };
    45  } // end namespace test_detail
    46  
    47  /// Declares a test function with the given name and adds it to the list of
    48  /// known tets.  The body of the function must follow immediately.  Example:
    49  ///
    50  /// TEST_FUNC(mytest) {
    51  ///   // CHECK: expected-output-here
    52  ///   emitSomethingToStdOut();
    53  /// }
    54  ///
    55  #define TEST_FUNC(name)                                                        \
    56    void name();                                                                 \
    57    static test_detail::TestRegistration name##Registration(name);               \
    58    void name()
    59  
    60  /// Runs all registered tests.  Example:
    61  ///
    62  /// int main() {
    63  ///   RUN_TESTS();
    64  ///   return 0;
    65  /// }
    66  #define RUN_TESTS                                                              \
    67    []() {                                                                       \
    68      for (auto f : test_detail::tests())                                        \
    69        f();                                                                     \
    70    }
    71  
    72  #endif // MLIR_TEST_TEST_H_