gtest-param-test.h.pump 18 KB

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