gtest-param-test.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // Copyright 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Macros and functions for implementing parameterized tests
  31. // in Google C++ Testing and Mocking Framework (Google Test)
  32. //
  33. // This file is generated by a SCRIPT. DO NOT EDIT BY HAND!
  34. //
  35. // GOOGLETEST_CM0001 DO NOT DELETE
  36. #ifndef GTEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
  37. #define GTEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
  38. // Value-parameterized tests allow you to test your code with different
  39. // parameters without writing multiple copies of the same test.
  40. //
  41. // Here is how you use value-parameterized tests:
  42. #if 0
  43. // To write value-parameterized tests, first you should define a fixture
  44. // class. It is usually derived from testing::TestWithParam<T> (see below for
  45. // another inheritance scheme that's sometimes useful in more complicated
  46. // class hierarchies), where the type of your parameter values.
  47. // TestWithParam<T> is itself derived from testing::Test. T can be any
  48. // copyable type. If it's a raw pointer, you are responsible for managing the
  49. // lifespan of the pointed values.
  50. class FooTest : public ::testing::TestWithParam<const char*> {
  51. // You can implement all the usual class fixture members here.
  52. };
  53. // Then, use the TEST_P macro to define as many parameterized tests
  54. // for this fixture as you want. The _P suffix is for "parameterized"
  55. // or "pattern", whichever you prefer to think.
  56. TEST_P(FooTest, DoesBlah) {
  57. // Inside a test, access the test parameter with the GetParam() method
  58. // of the TestWithParam<T> class:
  59. EXPECT_TRUE(foo.Blah(GetParam()));
  60. ...
  61. }
  62. TEST_P(FooTest, HasBlahBlah) {
  63. ...
  64. }
  65. // Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test
  66. // case with any set of parameters you want. Google Test defines a number
  67. // of functions for generating test parameters. They return what we call
  68. // (surprise!) parameter generators. Here is a summary of them, which
  69. // are all in the testing namespace:
  70. //
  71. //
  72. // Range(begin, end [, step]) - Yields values {begin, begin+step,
  73. // begin+step+step, ...}. The values do not
  74. // include end. step defaults to 1.
  75. // Values(v1, v2, ..., vN) - Yields values {v1, v2, ..., vN}.
  76. // ValuesIn(container) - Yields values from a C-style array, an STL
  77. // ValuesIn(begin,end) container, or an iterator range [begin, end).
  78. // Bool() - Yields sequence {false, true}.
  79. // Combine(g1, g2, ..., gN) - Yields all combinations (the Cartesian product
  80. // for the math savvy) of the values generated
  81. // by the N generators.
  82. //
  83. // For more details, see comments at the definitions of these functions below
  84. // in this file.
  85. //
  86. // The following statement will instantiate tests from the FooTest test suite
  87. // each with parameter values "meeny", "miny", and "moe".
  88. INSTANTIATE_TEST_SUITE_P(InstantiationName,
  89. FooTest,
  90. Values("meeny", "miny", "moe"));
  91. // To distinguish different instances of the pattern, (yes, you
  92. // can instantiate it more than once) the first argument to the
  93. // INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the
  94. // actual test suite name. Remember to pick unique prefixes for different
  95. // instantiations. The tests from the instantiation above will have
  96. // these names:
  97. //
  98. // * InstantiationName/FooTest.DoesBlah/0 for "meeny"
  99. // * InstantiationName/FooTest.DoesBlah/1 for "miny"
  100. // * InstantiationName/FooTest.DoesBlah/2 for "moe"
  101. // * InstantiationName/FooTest.HasBlahBlah/0 for "meeny"
  102. // * InstantiationName/FooTest.HasBlahBlah/1 for "miny"
  103. // * InstantiationName/FooTest.HasBlahBlah/2 for "moe"
  104. //
  105. // You can use these names in --gtest_filter.
  106. //
  107. // This statement will instantiate all tests from FooTest again, each
  108. // with parameter values "cat" and "dog":
  109. const char* pets[] = {"cat", "dog"};
  110. INSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets));
  111. // The tests from the instantiation above will have these names:
  112. //
  113. // * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat"
  114. // * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog"
  115. // * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat"
  116. // * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog"
  117. //
  118. // Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests
  119. // in the given test suite, whether their definitions come before or
  120. // AFTER the INSTANTIATE_TEST_SUITE_P statement.
  121. //
  122. // Please also note that generator expressions (including parameters to the
  123. // generators) are evaluated in InitGoogleTest(), after main() has started.
  124. // This allows the user on one hand, to adjust generator parameters in order
  125. // to dynamically determine a set of tests to run and on the other hand,
  126. // give the user a chance to inspect the generated tests with Google Test
  127. // reflection API before RUN_ALL_TESTS() is executed.
  128. //
  129. // You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc
  130. // for more examples.
  131. //
  132. // In the future, we plan to publish the API for defining new parameter
  133. // generators. But for now this interface remains part of the internal
  134. // implementation and is subject to change.
  135. //
  136. //
  137. // A parameterized test fixture must be derived from testing::Test and from
  138. // testing::WithParamInterface<T>, where T is the type of the parameter
  139. // values. Inheriting from TestWithParam<T> satisfies that requirement because
  140. // TestWithParam<T> inherits from both Test and WithParamInterface. In more
  141. // complicated hierarchies, however, it is occasionally useful to inherit
  142. // separately from Test and WithParamInterface. For example:
  143. class BaseTest : public ::testing::Test {
  144. // You can inherit all the usual members for a non-parameterized test
  145. // fixture here.
  146. };
  147. class DerivedTest : public BaseTest, public ::testing::WithParamInterface<int> {
  148. // The usual test fixture members go here too.
  149. };
  150. TEST_F(BaseTest, HasFoo) {
  151. // This is an ordinary non-parameterized test.
  152. }
  153. TEST_P(DerivedTest, DoesBlah) {
  154. // GetParam works just the same here as if you inherit from TestWithParam.
  155. EXPECT_TRUE(foo.Blah(GetParam()));
  156. }
  157. #endif // 0
  158. #include <iterator>
  159. #include <utility>
  160. #include "gtest/internal/gtest-internal.h"
  161. #include "gtest/internal/gtest-param-util.h"
  162. #include "gtest/internal/gtest-port.h"
  163. namespace testing {
  164. // Functions producing parameter generators.
  165. //
  166. // Google Test uses these generators to produce parameters for value-
  167. // parameterized tests. When a parameterized test suite is instantiated
  168. // with a particular generator, Google Test creates and runs tests
  169. // for each element in the sequence produced by the generator.
  170. //
  171. // In the following sample, tests from test suite FooTest are instantiated
  172. // each three times with parameter values 3, 5, and 8:
  173. //
  174. // class FooTest : public TestWithParam<int> { ... };
  175. //
  176. // TEST_P(FooTest, TestThis) {
  177. // }
  178. // TEST_P(FooTest, TestThat) {
  179. // }
  180. // INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8));
  181. //
  182. // Range() returns generators providing sequences of values in a range.
  183. //
  184. // Synopsis:
  185. // Range(start, end)
  186. // - returns a generator producing a sequence of values {start, start+1,
  187. // start+2, ..., }.
  188. // Range(start, end, step)
  189. // - returns a generator producing a sequence of values {start, start+step,
  190. // start+step+step, ..., }.
  191. // Notes:
  192. // * The generated sequences never include end. For example, Range(1, 5)
  193. // returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2)
  194. // returns a generator producing {1, 3, 5, 7}.
  195. // * start and end must have the same type. That type may be any integral or
  196. // floating-point type or a user defined type satisfying these conditions:
  197. // * It must be assignable (have operator=() defined).
  198. // * It must have operator+() (operator+(int-compatible type) for
  199. // two-operand version).
  200. // * It must have operator<() defined.
  201. // Elements in the resulting sequences will also have that type.
  202. // * Condition start < end must be satisfied in order for resulting sequences
  203. // to contain any elements.
  204. //
  205. template <typename T, typename IncrementT>
  206. internal::ParamGenerator<T> Range(T start, T end, IncrementT step) {
  207. return internal::ParamGenerator<T>(
  208. new internal::RangeGenerator<T, IncrementT>(start, end, step));
  209. }
  210. template <typename T>
  211. internal::ParamGenerator<T> Range(T start, T end) {
  212. return Range(start, end, 1);
  213. }
  214. // ValuesIn() function allows generation of tests with parameters coming from
  215. // a container.
  216. //
  217. // Synopsis:
  218. // ValuesIn(const T (&array)[N])
  219. // - returns a generator producing sequences with elements from
  220. // a C-style array.
  221. // ValuesIn(const Container& container)
  222. // - returns a generator producing sequences with elements from
  223. // an STL-style container.
  224. // ValuesIn(Iterator begin, Iterator end)
  225. // - returns a generator producing sequences with elements from
  226. // a range [begin, end) defined by a pair of STL-style iterators. These
  227. // iterators can also be plain C pointers.
  228. //
  229. // Please note that ValuesIn copies the values from the containers
  230. // passed in and keeps them to generate tests in RUN_ALL_TESTS().
  231. //
  232. // Examples:
  233. //
  234. // This instantiates tests from test suite StringTest
  235. // each with C-string values of "foo", "bar", and "baz":
  236. //
  237. // const char* strings[] = {"foo", "bar", "baz"};
  238. // INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings));
  239. //
  240. // This instantiates tests from test suite StlStringTest
  241. // each with STL strings with values "a" and "b":
  242. //
  243. // ::std::vector< ::std::string> GetParameterStrings() {
  244. // ::std::vector< ::std::string> v;
  245. // v.push_back("a");
  246. // v.push_back("b");
  247. // return v;
  248. // }
  249. //
  250. // INSTANTIATE_TEST_SUITE_P(CharSequence,
  251. // StlStringTest,
  252. // ValuesIn(GetParameterStrings()));
  253. //
  254. //
  255. // This will also instantiate tests from CharTest
  256. // each with parameter values 'a' and 'b':
  257. //
  258. // ::std::list<char> GetParameterChars() {
  259. // ::std::list<char> list;
  260. // list.push_back('a');
  261. // list.push_back('b');
  262. // return list;
  263. // }
  264. // ::std::list<char> l = GetParameterChars();
  265. // INSTANTIATE_TEST_SUITE_P(CharSequence2,
  266. // CharTest,
  267. // ValuesIn(l.begin(), l.end()));
  268. //
  269. template <typename ForwardIterator>
  270. internal::ParamGenerator<
  271. typename std::iterator_traits<ForwardIterator>::value_type>
  272. ValuesIn(ForwardIterator begin, ForwardIterator end) {
  273. typedef typename std::iterator_traits<ForwardIterator>::value_type ParamType;
  274. return internal::ParamGenerator<ParamType>(
  275. new internal::ValuesInIteratorRangeGenerator<ParamType>(begin, end));
  276. }
  277. template <typename T, size_t N>
  278. internal::ParamGenerator<T> ValuesIn(const T (&array)[N]) {
  279. return ValuesIn(array, array + N);
  280. }
  281. template <class Container>
  282. internal::ParamGenerator<typename Container::value_type> ValuesIn(
  283. const Container& container) {
  284. return ValuesIn(container.begin(), container.end());
  285. }
  286. // Values() allows generating tests from explicitly specified list of
  287. // parameters.
  288. //
  289. // Synopsis:
  290. // Values(T v1, T v2, ..., T vN)
  291. // - returns a generator producing sequences with elements v1, v2, ..., vN.
  292. //
  293. // For example, this instantiates tests from test suite BarTest each
  294. // with values "one", "two", and "three":
  295. //
  296. // INSTANTIATE_TEST_SUITE_P(NumSequence,
  297. // BarTest,
  298. // Values("one", "two", "three"));
  299. //
  300. // This instantiates tests from test suite BazTest each with values 1, 2, 3.5.
  301. // The exact type of values will depend on the type of parameter in BazTest.
  302. //
  303. // INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));
  304. //
  305. //
  306. template <typename... T>
  307. internal::ValueArray<T...> Values(T... v) {
  308. return internal::ValueArray<T...>(std::move(v)...);
  309. }
  310. // Bool() allows generating tests with parameters in a set of (false, true).
  311. //
  312. // Synopsis:
  313. // Bool()
  314. // - returns a generator producing sequences with elements {false, true}.
  315. //
  316. // It is useful when testing code that depends on Boolean flags. Combinations
  317. // of multiple flags can be tested when several Bool()'s are combined using
  318. // Combine() function.
  319. //
  320. // In the following example all tests in the test suite FlagDependentTest
  321. // will be instantiated twice with parameters false and true.
  322. //
  323. // class FlagDependentTest : public testing::TestWithParam<bool> {
  324. // virtual void SetUp() {
  325. // external_flag = GetParam();
  326. // }
  327. // }
  328. // INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool());
  329. //
  330. inline internal::ParamGenerator<bool> Bool() {
  331. return Values(false, true);
  332. }
  333. // Combine() allows the user to combine two or more sequences to produce
  334. // values of a Cartesian product of those sequences' elements.
  335. //
  336. // Synopsis:
  337. // Combine(gen1, gen2, ..., genN)
  338. // - returns a generator producing sequences with elements coming from
  339. // the Cartesian product of elements from the sequences generated by
  340. // gen1, gen2, ..., genN. The sequence elements will have a type of
  341. // std::tuple<T1, T2, ..., TN> where T1, T2, ..., TN are the types
  342. // of elements from sequences produces by gen1, gen2, ..., genN.
  343. //
  344. // Combine can have up to 10 arguments.
  345. //
  346. // Example:
  347. //
  348. // This will instantiate tests in test suite AnimalTest each one with
  349. // the parameter values tuple("cat", BLACK), tuple("cat", WHITE),
  350. // tuple("dog", BLACK), and tuple("dog", WHITE):
  351. //
  352. // enum Color { BLACK, GRAY, WHITE };
  353. // class AnimalTest
  354. // : public testing::TestWithParam<std::tuple<const char*, Color> > {...};
  355. //
  356. // TEST_P(AnimalTest, AnimalLooksNice) {...}
  357. //
  358. // INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest,
  359. // Combine(Values("cat", "dog"),
  360. // Values(BLACK, WHITE)));
  361. //
  362. // This will instantiate tests in FlagDependentTest with all variations of two
  363. // Boolean flags:
  364. //
  365. // class FlagDependentTest
  366. // : public testing::TestWithParam<std::tuple<bool, bool> > {
  367. // virtual void SetUp() {
  368. // // Assigns external_flag_1 and external_flag_2 values from the tuple.
  369. // std::tie(external_flag_1, external_flag_2) = GetParam();
  370. // }
  371. // };
  372. //
  373. // TEST_P(FlagDependentTest, TestFeature1) {
  374. // // Test your code using external_flag_1 and external_flag_2 here.
  375. // }
  376. // INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest,
  377. // Combine(Bool(), Bool()));
  378. //
  379. template <typename... Generator>
  380. internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
  381. return internal::CartesianProductHolder<Generator...>(g...);
  382. }
  383. #define TEST_P(test_suite_name, test_name) \
  384. class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
  385. : public test_suite_name { \
  386. public: \
  387. GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \
  388. virtual void TestBody(); \
  389. \
  390. private: \
  391. static int AddToRegistry() { \
  392. ::testing::UnitTest::GetInstance() \
  393. ->parameterized_test_registry() \
  394. .GetTestSuitePatternHolder<test_suite_name>( \
  395. #test_suite_name, \
  396. ::testing::internal::CodeLocation(__FILE__, __LINE__)) \
  397. ->AddTestPattern( \
  398. GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name), \
  399. new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \
  400. test_suite_name, test_name)>()); \
  401. return 0; \
  402. } \
  403. static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \
  404. GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_suite_name, \
  405. test_name)); \
  406. }; \
  407. int GTEST_TEST_CLASS_NAME_(test_suite_name, \
  408. test_name)::gtest_registering_dummy_ = \
  409. GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry(); \
  410. void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody()
  411. // The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify
  412. // generator and an optional function or functor that generates custom test name
  413. // suffixes based on the test parameters. Such a function or functor should
  414. // accept one argument of type testing::TestParamInfo<class ParamType>, and
  415. // return std::string.
  416. //
  417. // testing::PrintToStringParamName is a builtin test suffix generator that
  418. // returns the value of testing::PrintToString(GetParam()).
  419. //
  420. // Note: test names must be non-empty, unique, and may only contain ASCII
  421. // alphanumeric characters or underscore. Because PrintToString adds quotes
  422. // to std::string and C strings, it won't work for these types.
  423. #define GTEST_EXPAND_(arg) arg
  424. #define GTEST_GET_FIRST_(first, ...) first
  425. #define GTEST_GET_SECOND_(first, second, ...) second
  426. #define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \
  427. static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \
  428. gtest_##prefix##test_suite_name##_EvalGenerator_() { \
  429. return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \
  430. } \
  431. static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \
  432. const ::testing::TestParamInfo<test_suite_name::ParamType>& info) { \
  433. if (::testing::internal::AlwaysFalse()) { \
  434. ::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \
  435. __VA_ARGS__, \
  436. ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
  437. DUMMY_PARAM_))); \
  438. auto t = std::make_tuple(__VA_ARGS__); \
  439. static_assert(std::tuple_size<decltype(t)>::value <= 2, \
  440. "Too Many Args!"); \
  441. } \
  442. return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \
  443. __VA_ARGS__, \
  444. ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
  445. DUMMY_PARAM_))))(info); \
  446. } \
  447. static int gtest_##prefix##test_suite_name##_dummy_ \
  448. GTEST_ATTRIBUTE_UNUSED_ = \
  449. ::testing::UnitTest::GetInstance() \
  450. ->parameterized_test_registry() \
  451. .GetTestSuitePatternHolder<test_suite_name>( \
  452. #test_suite_name, \
  453. ::testing::internal::CodeLocation(__FILE__, __LINE__)) \
  454. ->AddTestSuiteInstantiation( \
  455. #prefix, &gtest_##prefix##test_suite_name##_EvalGenerator_, \
  456. &gtest_##prefix##test_suite_name##_EvalGenerateName_, \
  457. __FILE__, __LINE__)
  458. // Legacy API is deprecated but still available
  459. #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  460. #define INSTANTIATE_TEST_CASE_P \
  461. static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \
  462. ""); \
  463. INSTANTIATE_TEST_SUITE_P
  464. #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  465. } // namespace testing
  466. #endif // GTEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_