Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-19 10:45:45

0001 // Copyright 2008 Google Inc.
0002 // All Rights Reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // IWYU pragma: private, include "gtest/gtest.h"
0031 // IWYU pragma: friend gtest/.*
0032 // IWYU pragma: friend gmock/.*
0033 
0034 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
0035 #define GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
0036 
0037 // This header implements typed tests and type-parameterized tests.
0038 
0039 // Typed (aka type-driven) tests repeat the same test for types in a
0040 // list.  You must know which types you want to test with when writing
0041 // typed tests. Here's how you do it:
0042 
0043 #if 0
0044 
0045 // First, define a fixture class template.  It should be parameterized
0046 // by a type.  Remember to derive it from testing::Test.
0047 template <typename T>
0048 class [[nodiscard]] FooTest : public testing::Test {
0049  public:
0050   ...
0051   using List = ::std::list<T>;
0052   static T shared_;
0053   T value_;
0054 };
0055 
0056 // Next, associate a list of types with the test suite, which will be
0057 // repeated for each type in the list.  The using-declaration is necessary for
0058 // the macro to parse correctly.
0059 using MyTypes = ::testing::Types<char, int, unsigned int>;
0060 TYPED_TEST_SUITE(FooTest, MyTypes);
0061 
0062 // If the type list contains only one type, you can write that type
0063 // directly without Types<...>:
0064 //   TYPED_TEST_SUITE(FooTest, int);
0065 
0066 // Then, use TYPED_TEST() instead of TEST_F() to define as many typed
0067 // tests for this test suite as you want.
0068 TYPED_TEST(FooTest, DoesBlah) {
0069   // Inside a test, refer to the special name TypeParam to get the type
0070   // parameter.  Since we are inside a derived class template, C++ requires
0071   // us to visit the members of FooTest via 'this'.
0072   TypeParam n = this->value_;
0073 
0074   // To visit static members of the fixture, add the TestFixture::
0075   // prefix.
0076   n += TestFixture::shared_;
0077 
0078   // To refer to typedefs in the fixture, add the "typename
0079   // TestFixture::" prefix.
0080   typename TestFixture::List values;
0081   values.push_back(n);
0082   ...
0083 }
0084 
0085 TYPED_TEST(FooTest, HasPropertyA) { ... }
0086 
0087 // TYPED_TEST_SUITE takes an optional third argument which allows to specify a
0088 // class that generates custom test name suffixes based on the type. This should
0089 // be a class which has a static template function GetName(int index) returning
0090 // a string for each type. The provided integer index equals the index of the
0091 // type in the provided type list. In many cases the index can be ignored.
0092 //
0093 // For example:
0094 //   class MyTypeNames {
0095 //    public:
0096 //     template <typename T>
0097 //     static std::string GetName(int) {
0098 //       if (std::is_same<T, char>()) return "char";
0099 //       if (std::is_same<T, int>()) return "int";
0100 //       if (std::is_same<T, unsigned int>()) return "unsignedInt";
0101 //     }
0102 //   };
0103 //   TYPED_TEST_SUITE(FooTest, MyTypes, MyTypeNames);
0104 
0105 #endif  // 0
0106 
0107 // Type-parameterized tests are abstract test patterns parameterized
0108 // by a type.  Compared with typed tests, type-parameterized tests
0109 // allow you to define the test pattern without knowing what the type
0110 // parameters are.  The defined pattern can be instantiated with
0111 // different types any number of times, in any number of translation
0112 // units.
0113 //
0114 // If you are designing an interface or concept, you can define a
0115 // suite of type-parameterized tests to verify properties that any
0116 // valid implementation of the interface/concept should have.  Then,
0117 // each implementation can easily instantiate the test suite to verify
0118 // that it conforms to the requirements, without having to write
0119 // similar tests repeatedly.  Here's an example:
0120 
0121 #if 0
0122 
0123 // First, define a fixture class template.  It should be parameterized
0124 // by a type.  Remember to derive it from testing::Test.
0125 template <typename T>
0126 class [[nodiscard]] FooTest : public testing::Test {
0127   ...
0128 };
0129 
0130 // Next, declare that you will define a type-parameterized test suite
0131 // (the _P suffix is for "parameterized" or "pattern", whichever you
0132 // prefer):
0133 TYPED_TEST_SUITE_P(FooTest);
0134 
0135 // Then, use TYPED_TEST_P() to define as many type-parameterized tests
0136 // for this type-parameterized test suite as you want.
0137 TYPED_TEST_P(FooTest, DoesBlah) {
0138   // Inside a test, refer to TypeParam to get the type parameter.
0139   TypeParam n = 0;
0140   ...
0141 }
0142 
0143 TYPED_TEST_P(FooTest, HasPropertyA) { ... }
0144 
0145 // Now the tricky part: you need to register all test patterns before
0146 // you can instantiate them.  The first argument of the macro is the
0147 // test suite name; the rest are the names of the tests in this test
0148 // case.
0149 REGISTER_TYPED_TEST_SUITE_P(FooTest,
0150                             DoesBlah, HasPropertyA);
0151 
0152 // Finally, you are free to instantiate the pattern with the types you
0153 // want.  If you put the above code in a header file, you can #include
0154 // it in multiple C++ source files and instantiate it multiple times.
0155 //
0156 // To distinguish different instances of the pattern, the first
0157 // argument to the INSTANTIATE_* macro is a prefix that will be added
0158 // to the actual test suite name.  Remember to pick unique prefixes for
0159 // different instances.
0160 using MyTypes = ::testing::Types<char, int, unsigned int>;
0161 INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
0162 
0163 // If the type list contains only one type, you can write that type
0164 // directly without Types<...>:
0165 //   INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, int);
0166 //
0167 // Similar to the optional argument of TYPED_TEST_SUITE above,
0168 // INSTANTIATE_TEST_SUITE_P takes an optional fourth argument which allows to
0169 // generate custom names.
0170 //   INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes, MyTypeNames);
0171 
0172 #endif  // 0
0173 
0174 #include "gtest/internal/gtest-internal.h"
0175 #include "gtest/internal/gtest-port.h"
0176 #include "gtest/internal/gtest-type-util.h"
0177 
0178 // Implements typed tests.
0179 
0180 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
0181 //
0182 // Expands to the name of the typedef for the type parameters of the
0183 // given test suite.
0184 #define GTEST_TYPE_PARAMS_(TestSuiteName) gtest_type_params_##TestSuiteName##_
0185 
0186 // Expands to the name of the typedef for the NameGenerator, responsible for
0187 // creating the suffixes of the name.
0188 #define GTEST_NAME_GENERATOR_(TestSuiteName) \
0189   gtest_type_params_##TestSuiteName##_NameGenerator
0190 
0191 #define TYPED_TEST_SUITE(CaseName, Types, ...)                          \
0192   typedef ::testing::internal::GenerateTypeList<Types>::type            \
0193       GTEST_TYPE_PARAMS_(CaseName);                                     \
0194   typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \
0195   GTEST_NAME_GENERATOR_(CaseName)
0196 
0197 #define TYPED_TEST(CaseName, TestName)                                        \
0198   static_assert(sizeof(GTEST_STRINGIFY_(TestName)) > 1,                       \
0199                 "test-name must not be empty");                               \
0200   template <typename gtest_TypeParam_>                                        \
0201   class GTEST_TEST_CLASS_NAME_(CaseName, TestName)                            \
0202       : public CaseName<gtest_TypeParam_> {                                   \
0203    private:                                                                   \
0204     typedef CaseName<gtest_TypeParam_> TestFixture;                           \
0205     typedef gtest_TypeParam_ TypeParam;                                       \
0206     void TestBody() override;                                                 \
0207   };                                                                          \
0208   [[maybe_unused]] static bool gtest_##CaseName##_##TestName##_registered_ =  \
0209       ::testing::internal::TypeParameterizedTest<                             \
0210           CaseName,                                                           \
0211           ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName,   \
0212                                                                   TestName)>, \
0213           GTEST_TYPE_PARAMS_(                                                 \
0214               CaseName)>::Register("",                                        \
0215                                    ::testing::internal::CodeLocation(         \
0216                                        __FILE__, __LINE__),                   \
0217                                    GTEST_STRINGIFY_(CaseName),                \
0218                                    GTEST_STRINGIFY_(TestName), 0,             \
0219                                    ::testing::internal::GenerateNames<        \
0220                                        GTEST_NAME_GENERATOR_(CaseName),       \
0221                                        GTEST_TYPE_PARAMS_(CaseName)>());      \
0222   template <typename gtest_TypeParam_>                                        \
0223   void GTEST_TEST_CLASS_NAME_(CaseName,                                       \
0224                               TestName)<gtest_TypeParam_>::TestBody()
0225 
0226 // Legacy API is deprecated but still available
0227 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0228 #define TYPED_TEST_CASE                                                \
0229   static_assert(::testing::internal::TypedTestCaseIsDeprecated(), ""); \
0230   TYPED_TEST_SUITE
0231 #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0232 
0233 // Implements type-parameterized tests.
0234 
0235 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
0236 //
0237 // Expands to the namespace name that the type-parameterized tests for
0238 // the given type-parameterized test suite are defined in.  The exact
0239 // name of the namespace is subject to change without notice.
0240 #define GTEST_SUITE_NAMESPACE_(TestSuiteName) gtest_suite_##TestSuiteName##_
0241 
0242 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
0243 //
0244 // Expands to the name of the variable used to remember the names of
0245 // the defined tests in the given test suite.
0246 #define GTEST_TYPED_TEST_SUITE_P_STATE_(TestSuiteName) \
0247   gtest_typed_test_suite_p_state_##TestSuiteName##_
0248 
0249 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.
0250 //
0251 // Expands to the name of the variable used to remember the names of
0252 // the registered tests in the given test suite.
0253 #define GTEST_REGISTERED_TEST_NAMES_(TestSuiteName) \
0254   gtest_registered_test_names_##TestSuiteName##_
0255 
0256 // The variables defined in the type-parameterized test macros are
0257 // static as typically these macros are used in a .h file that can be
0258 // #included in multiple translation units linked together.
0259 #define TYPED_TEST_SUITE_P(SuiteName)              \
0260   static ::testing::internal::TypedTestSuitePState \
0261   GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName)
0262 
0263 // Legacy API is deprecated but still available
0264 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0265 #define TYPED_TEST_CASE_P                                                 \
0266   static_assert(::testing::internal::TypedTestCase_P_IsDeprecated(), ""); \
0267   TYPED_TEST_SUITE_P
0268 #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0269 
0270 #define TYPED_TEST_P(SuiteName, TestName)                     \
0271   namespace GTEST_SUITE_NAMESPACE_(SuiteName) {               \
0272   template <typename gtest_TypeParam_>                        \
0273   class TestName : public SuiteName<gtest_TypeParam_> {       \
0274    private:                                                   \
0275     typedef SuiteName<gtest_TypeParam_> TestFixture;          \
0276     typedef gtest_TypeParam_ TypeParam;                       \
0277     void TestBody() override;                                 \
0278   };                                                          \
0279   [[maybe_unused]] static bool gtest_##TestName##_defined_ =  \
0280       GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \
0281           __FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName),    \
0282           GTEST_STRINGIFY_(TestName));                        \
0283   }                                                           \
0284   template <typename gtest_TypeParam_>                        \
0285   void GTEST_SUITE_NAMESPACE_(                                \
0286       SuiteName)::TestName<gtest_TypeParam_>::TestBody()
0287 
0288 // Note: this won't work correctly if the trailing arguments are macros.
0289 #define REGISTER_TYPED_TEST_SUITE_P(SuiteName, ...)                         \
0290   namespace GTEST_SUITE_NAMESPACE_(SuiteName) {                             \
0291   typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_;      \
0292   }                                                                         \
0293   [[maybe_unused]] static const char* const GTEST_REGISTERED_TEST_NAMES_(   \
0294       SuiteName) =                                                          \
0295       GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \
0296           GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__)
0297 
0298 // Legacy API is deprecated but still available
0299 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0300 #define REGISTER_TYPED_TEST_CASE_P                                           \
0301   static_assert(::testing::internal::RegisterTypedTestCase_P_IsDeprecated(), \
0302                 "");                                                         \
0303   REGISTER_TYPED_TEST_SUITE_P
0304 #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0305 
0306 #define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...)     \
0307   static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1,                     \
0308                 "test-suit-prefix must not be empty");                    \
0309   [[maybe_unused]] static bool gtest_##Prefix##_##SuiteName =             \
0310       ::testing::internal::TypeParameterizedTestSuite<                    \
0311           SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_,  \
0312           ::testing::internal::GenerateTypeList<Types>::type>::           \
0313           Register(GTEST_STRINGIFY_(Prefix),                              \
0314                    ::testing::internal::CodeLocation(__FILE__, __LINE__), \
0315                    &GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName),           \
0316                    GTEST_STRINGIFY_(SuiteName),                           \
0317                    GTEST_REGISTERED_TEST_NAMES_(SuiteName),               \
0318                    ::testing::internal::GenerateNames<                    \
0319                        ::testing::internal::NameGeneratorSelector<        \
0320                            __VA_ARGS__>::type,                            \
0321                        ::testing::internal::GenerateTypeList<Types>::type>())
0322 
0323 // Legacy API is deprecated but still available
0324 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0325 #define INSTANTIATE_TYPED_TEST_CASE_P                                      \
0326   static_assert(                                                           \
0327       ::testing::internal::InstantiateTypedTestCase_P_IsDeprecated(), ""); \
0328   INSTANTIATE_TYPED_TEST_SUITE_P
0329 #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
0330 
0331 #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_