gtest-param-test.h.pump 19 KB

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