You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1055 lines
38 KiB

  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. // Tests for Google Test itself. This file verifies that the parameter
  31. // generators objects produce correct parameter sequences and that
  32. // Google Test runtime instantiates correct tests from those sequences.
  33. #include "gtest/gtest.h"
  34. # include <algorithm>
  35. # include <iostream>
  36. # include <list>
  37. # include <sstream>
  38. # include <string>
  39. # include <vector>
  40. # include "src/gtest-internal-inl.h" // for UnitTestOptions
  41. # include "test/googletest-param-test-test.h"
  42. using ::std::vector;
  43. using ::std::sort;
  44. using ::testing::AddGlobalTestEnvironment;
  45. using ::testing::Bool;
  46. using ::testing::Combine;
  47. using ::testing::Message;
  48. using ::testing::Range;
  49. using ::testing::TestWithParam;
  50. using ::testing::Values;
  51. using ::testing::ValuesIn;
  52. using ::testing::internal::ParamGenerator;
  53. using ::testing::internal::UnitTestOptions;
  54. // Prints a value to a string.
  55. //
  56. // FIXME: remove PrintValue() when we move matchers and
  57. // EXPECT_THAT() from Google Mock to Google Test. At that time, we
  58. // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
  59. // EXPECT_THAT() and the matchers know how to print tuples.
  60. template <typename T>
  61. ::std::string PrintValue(const T& value) {
  62. return testing::PrintToString(value);
  63. }
  64. // Verifies that a sequence generated by the generator and accessed
  65. // via the iterator object matches the expected one using Google Test
  66. // assertions.
  67. template <typename T, size_t N>
  68. void VerifyGenerator(const ParamGenerator<T>& generator,
  69. const T (&expected_values)[N]) {
  70. typename ParamGenerator<T>::iterator it = generator.begin();
  71. for (size_t i = 0; i < N; ++i) {
  72. ASSERT_FALSE(it == generator.end())
  73. << "At element " << i << " when accessing via an iterator "
  74. << "created with the copy constructor.\n";
  75. // We cannot use EXPECT_EQ() here as the values may be tuples,
  76. // which don't support <<.
  77. EXPECT_TRUE(expected_values[i] == *it)
  78. << "where i is " << i
  79. << ", expected_values[i] is " << PrintValue(expected_values[i])
  80. << ", *it is " << PrintValue(*it)
  81. << ", and 'it' is an iterator created with the copy constructor.\n";
  82. ++it;
  83. }
  84. EXPECT_TRUE(it == generator.end())
  85. << "At the presumed end of sequence when accessing via an iterator "
  86. << "created with the copy constructor.\n";
  87. // Test the iterator assignment. The following lines verify that
  88. // the sequence accessed via an iterator initialized via the
  89. // assignment operator (as opposed to a copy constructor) matches
  90. // just the same.
  91. it = generator.begin();
  92. for (size_t i = 0; i < N; ++i) {
  93. ASSERT_FALSE(it == generator.end())
  94. << "At element " << i << " when accessing via an iterator "
  95. << "created with the assignment operator.\n";
  96. EXPECT_TRUE(expected_values[i] == *it)
  97. << "where i is " << i
  98. << ", expected_values[i] is " << PrintValue(expected_values[i])
  99. << ", *it is " << PrintValue(*it)
  100. << ", and 'it' is an iterator created with the copy constructor.\n";
  101. ++it;
  102. }
  103. EXPECT_TRUE(it == generator.end())
  104. << "At the presumed end of sequence when accessing via an iterator "
  105. << "created with the assignment operator.\n";
  106. }
  107. template <typename T>
  108. void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
  109. typename ParamGenerator<T>::iterator it = generator.begin();
  110. EXPECT_TRUE(it == generator.end());
  111. it = generator.begin();
  112. EXPECT_TRUE(it == generator.end());
  113. }
  114. // Generator tests. They test that each of the provided generator functions
  115. // generates an expected sequence of values. The general test pattern
  116. // instantiates a generator using one of the generator functions,
  117. // checks the sequence produced by the generator using its iterator API,
  118. // and then resets the iterator back to the beginning of the sequence
  119. // and checks the sequence again.
  120. // Tests that iterators produced by generator functions conform to the
  121. // ForwardIterator concept.
  122. TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
  123. const ParamGenerator<int> gen = Range(0, 10);
  124. ParamGenerator<int>::iterator it = gen.begin();
  125. // Verifies that iterator initialization works as expected.
  126. ParamGenerator<int>::iterator it2 = it;
  127. EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
  128. << "element same as its source points to";
  129. // Verifies that iterator assignment works as expected.
  130. ++it;
  131. EXPECT_FALSE(*it == *it2);
  132. it2 = it;
  133. EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
  134. << "element same as its source points to";
  135. // Verifies that prefix operator++() returns *this.
  136. EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
  137. << "refer to the original object";
  138. // Verifies that the result of the postfix operator++ points to the value
  139. // pointed to by the original iterator.
  140. int original_value = *it; // Have to compute it outside of macro call to be
  141. // unaffected by the parameter evaluation order.
  142. EXPECT_EQ(original_value, *(it++));
  143. // Verifies that prefix and postfix operator++() advance an iterator
  144. // all the same.
  145. it2 = it;
  146. ++it;
  147. ++it2;
  148. EXPECT_TRUE(*it == *it2);
  149. }
  150. // Tests that Range() generates the expected sequence.
  151. TEST(RangeTest, IntRangeWithDefaultStep) {
  152. const ParamGenerator<int> gen = Range(0, 3);
  153. const int expected_values[] = {0, 1, 2};
  154. VerifyGenerator(gen, expected_values);
  155. }
  156. // Edge case. Tests that Range() generates the single element sequence
  157. // as expected when provided with range limits that are equal.
  158. TEST(RangeTest, IntRangeSingleValue) {
  159. const ParamGenerator<int> gen = Range(0, 1);
  160. const int expected_values[] = {0};
  161. VerifyGenerator(gen, expected_values);
  162. }
  163. // Edge case. Tests that Range() with generates empty sequence when
  164. // supplied with an empty range.
  165. TEST(RangeTest, IntRangeEmpty) {
  166. const ParamGenerator<int> gen = Range(0, 0);
  167. VerifyGeneratorIsEmpty(gen);
  168. }
  169. // Tests that Range() with custom step (greater then one) generates
  170. // the expected sequence.
  171. TEST(RangeTest, IntRangeWithCustomStep) {
  172. const ParamGenerator<int> gen = Range(0, 9, 3);
  173. const int expected_values[] = {0, 3, 6};
  174. VerifyGenerator(gen, expected_values);
  175. }
  176. // Tests that Range() with custom step (greater then one) generates
  177. // the expected sequence when the last element does not fall on the
  178. // upper range limit. Sequences generated by Range() must not have
  179. // elements beyond the range limits.
  180. TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
  181. const ParamGenerator<int> gen = Range(0, 4, 3);
  182. const int expected_values[] = {0, 3};
  183. VerifyGenerator(gen, expected_values);
  184. }
  185. // Verifies that Range works with user-defined types that define
  186. // copy constructor, operator=(), operator+(), and operator<().
  187. class DogAdder {
  188. public:
  189. explicit DogAdder(const char* a_value) : value_(a_value) {}
  190. DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
  191. DogAdder operator=(const DogAdder& other) {
  192. if (this != &other)
  193. value_ = other.value_;
  194. return *this;
  195. }
  196. DogAdder operator+(const DogAdder& other) const {
  197. Message msg;
  198. msg << value_.c_str() << other.value_.c_str();
  199. return DogAdder(msg.GetString().c_str());
  200. }
  201. bool operator<(const DogAdder& other) const {
  202. return value_ < other.value_;
  203. }
  204. const std::string& value() const { return value_; }
  205. private:
  206. std::string value_;
  207. };
  208. TEST(RangeTest, WorksWithACustomType) {
  209. const ParamGenerator<DogAdder> gen =
  210. Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
  211. ParamGenerator<DogAdder>::iterator it = gen.begin();
  212. ASSERT_FALSE(it == gen.end());
  213. EXPECT_STREQ("cat", it->value().c_str());
  214. ASSERT_FALSE(++it == gen.end());
  215. EXPECT_STREQ("catdog", it->value().c_str());
  216. EXPECT_TRUE(++it == gen.end());
  217. }
  218. class IntWrapper {
  219. public:
  220. explicit IntWrapper(int a_value) : value_(a_value) {}
  221. IntWrapper(const IntWrapper& other) : value_(other.value_) {}
  222. IntWrapper operator=(const IntWrapper& other) {
  223. value_ = other.value_;
  224. return *this;
  225. }
  226. // operator+() adds a different type.
  227. IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
  228. bool operator<(const IntWrapper& other) const {
  229. return value_ < other.value_;
  230. }
  231. int value() const { return value_; }
  232. private:
  233. int value_;
  234. };
  235. TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
  236. const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
  237. ParamGenerator<IntWrapper>::iterator it = gen.begin();
  238. ASSERT_FALSE(it == gen.end());
  239. EXPECT_EQ(0, it->value());
  240. ASSERT_FALSE(++it == gen.end());
  241. EXPECT_EQ(1, it->value());
  242. EXPECT_TRUE(++it == gen.end());
  243. }
  244. // Tests that ValuesIn() with an array parameter generates
  245. // the expected sequence.
  246. TEST(ValuesInTest, ValuesInArray) {
  247. int array[] = {3, 5, 8};
  248. const ParamGenerator<int> gen = ValuesIn(array);
  249. VerifyGenerator(gen, array);
  250. }
  251. // Tests that ValuesIn() with a const array parameter generates
  252. // the expected sequence.
  253. TEST(ValuesInTest, ValuesInConstArray) {
  254. const int array[] = {3, 5, 8};
  255. const ParamGenerator<int> gen = ValuesIn(array);
  256. VerifyGenerator(gen, array);
  257. }
  258. // Edge case. Tests that ValuesIn() with an array parameter containing a
  259. // single element generates the single element sequence.
  260. TEST(ValuesInTest, ValuesInSingleElementArray) {
  261. int array[] = {42};
  262. const ParamGenerator<int> gen = ValuesIn(array);
  263. VerifyGenerator(gen, array);
  264. }
  265. // Tests that ValuesIn() generates the expected sequence for an STL
  266. // container (vector).
  267. TEST(ValuesInTest, ValuesInVector) {
  268. typedef ::std::vector<int> ContainerType;
  269. ContainerType values;
  270. values.push_back(3);
  271. values.push_back(5);
  272. values.push_back(8);
  273. const ParamGenerator<int> gen = ValuesIn(values);
  274. const int expected_values[] = {3, 5, 8};
  275. VerifyGenerator(gen, expected_values);
  276. }
  277. // Tests that ValuesIn() generates the expected sequence.
  278. TEST(ValuesInTest, ValuesInIteratorRange) {
  279. typedef ::std::vector<int> ContainerType;
  280. ContainerType values;
  281. values.push_back(3);
  282. values.push_back(5);
  283. values.push_back(8);
  284. const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
  285. const int expected_values[] = {3, 5, 8};
  286. VerifyGenerator(gen, expected_values);
  287. }
  288. // Edge case. Tests that ValuesIn() provided with an iterator range specifying a
  289. // single value generates a single-element sequence.
  290. TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
  291. typedef ::std::vector<int> ContainerType;
  292. ContainerType values;
  293. values.push_back(42);
  294. const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
  295. const int expected_values[] = {42};
  296. VerifyGenerator(gen, expected_values);
  297. }
  298. // Edge case. Tests that ValuesIn() provided with an empty iterator range
  299. // generates an empty sequence.
  300. TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
  301. typedef ::std::vector<int> ContainerType;
  302. ContainerType values;
  303. const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
  304. VerifyGeneratorIsEmpty(gen);
  305. }
  306. // Tests that the Values() generates the expected sequence.
  307. TEST(ValuesTest, ValuesWorks) {
  308. const ParamGenerator<int> gen = Values(3, 5, 8);
  309. const int expected_values[] = {3, 5, 8};
  310. VerifyGenerator(gen, expected_values);
  311. }
  312. // Tests that Values() generates the expected sequences from elements of
  313. // different types convertible to ParamGenerator's parameter type.
  314. TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
  315. const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
  316. const double expected_values[] = {3.0, 5.0, 8.0};
  317. VerifyGenerator(gen, expected_values);
  318. }
  319. TEST(ValuesTest, ValuesWorksForMaxLengthList) {
  320. const ParamGenerator<int> gen = Values(
  321. 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
  322. 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
  323. 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
  324. 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
  325. 410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
  326. const int expected_values[] = {
  327. 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
  328. 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
  329. 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
  330. 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
  331. 410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
  332. VerifyGenerator(gen, expected_values);
  333. }
  334. // Edge case test. Tests that single-parameter Values() generates the sequence
  335. // with the single value.
  336. TEST(ValuesTest, ValuesWithSingleParameter) {
  337. const ParamGenerator<int> gen = Values(42);
  338. const int expected_values[] = {42};
  339. VerifyGenerator(gen, expected_values);
  340. }
  341. // Tests that Bool() generates sequence (false, true).
  342. TEST(BoolTest, BoolWorks) {
  343. const ParamGenerator<bool> gen = Bool();
  344. const bool expected_values[] = {false, true};
  345. VerifyGenerator(gen, expected_values);
  346. }
  347. // Tests that Combine() with two parameters generates the expected sequence.
  348. TEST(CombineTest, CombineWithTwoParameters) {
  349. const char* foo = "foo";
  350. const char* bar = "bar";
  351. const ParamGenerator<std::tuple<const char*, int> > gen =
  352. Combine(Values(foo, bar), Values(3, 4));
  353. std::tuple<const char*, int> expected_values[] = {
  354. std::make_tuple(foo, 3), std::make_tuple(foo, 4), std::make_tuple(bar, 3),
  355. std::make_tuple(bar, 4)};
  356. VerifyGenerator(gen, expected_values);
  357. }
  358. // Tests that Combine() with three parameters generates the expected sequence.
  359. TEST(CombineTest, CombineWithThreeParameters) {
  360. const ParamGenerator<std::tuple<int, int, int> > gen =
  361. Combine(Values(0, 1), Values(3, 4), Values(5, 6));
  362. std::tuple<int, int, int> expected_values[] = {
  363. std::make_tuple(0, 3, 5), std::make_tuple(0, 3, 6),
  364. std::make_tuple(0, 4, 5), std::make_tuple(0, 4, 6),
  365. std::make_tuple(1, 3, 5), std::make_tuple(1, 3, 6),
  366. std::make_tuple(1, 4, 5), std::make_tuple(1, 4, 6)};
  367. VerifyGenerator(gen, expected_values);
  368. }
  369. // Tests that the Combine() with the first parameter generating a single value
  370. // sequence generates a sequence with the number of elements equal to the
  371. // number of elements in the sequence generated by the second parameter.
  372. TEST(CombineTest, CombineWithFirstParameterSingleValue) {
  373. const ParamGenerator<std::tuple<int, int> > gen =
  374. Combine(Values(42), Values(0, 1));
  375. std::tuple<int, int> expected_values[] = {std::make_tuple(42, 0),
  376. std::make_tuple(42, 1)};
  377. VerifyGenerator(gen, expected_values);
  378. }
  379. // Tests that the Combine() with the second parameter generating a single value
  380. // sequence generates a sequence with the number of elements equal to the
  381. // number of elements in the sequence generated by the first parameter.
  382. TEST(CombineTest, CombineWithSecondParameterSingleValue) {
  383. const ParamGenerator<std::tuple<int, int> > gen =
  384. Combine(Values(0, 1), Values(42));
  385. std::tuple<int, int> expected_values[] = {std::make_tuple(0, 42),
  386. std::make_tuple(1, 42)};
  387. VerifyGenerator(gen, expected_values);
  388. }
  389. // Tests that when the first parameter produces an empty sequence,
  390. // Combine() produces an empty sequence, too.
  391. TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
  392. const ParamGenerator<std::tuple<int, int> > gen =
  393. Combine(Range(0, 0), Values(0, 1));
  394. VerifyGeneratorIsEmpty(gen);
  395. }
  396. // Tests that when the second parameter produces an empty sequence,
  397. // Combine() produces an empty sequence, too.
  398. TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
  399. const ParamGenerator<std::tuple<int, int> > gen =
  400. Combine(Values(0, 1), Range(1, 1));
  401. VerifyGeneratorIsEmpty(gen);
  402. }
  403. // Edge case. Tests that combine works with the maximum number
  404. // of parameters supported by Google Test (currently 10).
  405. TEST(CombineTest, CombineWithMaxNumberOfParameters) {
  406. const char* foo = "foo";
  407. const char* bar = "bar";
  408. const ParamGenerator<
  409. std::tuple<const char*, int, int, int, int, int, int, int, int, int> >
  410. gen =
  411. Combine(Values(foo, bar), Values(1), Values(2), Values(3), Values(4),
  412. Values(5), Values(6), Values(7), Values(8), Values(9));
  413. std::tuple<const char*, int, int, int, int, int, int, int, int, int>
  414. expected_values[] = {std::make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  415. std::make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
  416. VerifyGenerator(gen, expected_values);
  417. }
  418. class NonDefaultConstructAssignString {
  419. public:
  420. NonDefaultConstructAssignString(const std::string& s) : str_(s) {}
  421. const std::string& str() const { return str_; }
  422. private:
  423. std::string str_;
  424. // Not default constructible
  425. NonDefaultConstructAssignString();
  426. // Not assignable
  427. void operator=(const NonDefaultConstructAssignString&);
  428. };
  429. TEST(CombineTest, NonDefaultConstructAssign) {
  430. const ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> > gen =
  431. Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"),
  432. NonDefaultConstructAssignString("B")));
  433. ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> >::iterator
  434. it = gen.begin();
  435. EXPECT_EQ(0, std::get<0>(*it));
  436. EXPECT_EQ("A", std::get<1>(*it).str());
  437. ++it;
  438. EXPECT_EQ(0, std::get<0>(*it));
  439. EXPECT_EQ("B", std::get<1>(*it).str());
  440. ++it;
  441. EXPECT_EQ(1, std::get<0>(*it));
  442. EXPECT_EQ("A", std::get<1>(*it).str());
  443. ++it;
  444. EXPECT_EQ(1, std::get<0>(*it));
  445. EXPECT_EQ("B", std::get<1>(*it).str());
  446. ++it;
  447. EXPECT_TRUE(it == gen.end());
  448. }
  449. // Tests that an generator produces correct sequence after being
  450. // assigned from another generator.
  451. TEST(ParamGeneratorTest, AssignmentWorks) {
  452. ParamGenerator<int> gen = Values(1, 2);
  453. const ParamGenerator<int> gen2 = Values(3, 4);
  454. gen = gen2;
  455. const int expected_values[] = {3, 4};
  456. VerifyGenerator(gen, expected_values);
  457. }
  458. // This test verifies that the tests are expanded and run as specified:
  459. // one test per element from the sequence produced by the generator
  460. // specified in INSTANTIATE_TEST_SUITE_P. It also verifies that the test's
  461. // fixture constructor, SetUp(), and TearDown() have run and have been
  462. // supplied with the correct parameters.
  463. // The use of environment object allows detection of the case where no test
  464. // case functionality is run at all. In this case TearDownTestSuite will not
  465. // be able to detect missing tests, naturally.
  466. template <int kExpectedCalls>
  467. class TestGenerationEnvironment : public ::testing::Environment {
  468. public:
  469. static TestGenerationEnvironment* Instance() {
  470. static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
  471. return instance;
  472. }
  473. void FixtureConstructorExecuted() { fixture_constructor_count_++; }
  474. void SetUpExecuted() { set_up_count_++; }
  475. void TearDownExecuted() { tear_down_count_++; }
  476. void TestBodyExecuted() { test_body_count_++; }
  477. void TearDown() override {
  478. // If all MultipleTestGenerationTest tests have been de-selected
  479. // by the filter flag, the following checks make no sense.
  480. bool perform_check = false;
  481. for (int i = 0; i < kExpectedCalls; ++i) {
  482. Message msg;
  483. msg << "TestsExpandedAndRun/" << i;
  484. if (UnitTestOptions::FilterMatchesTest(
  485. "TestExpansionModule/MultipleTestGenerationTest",
  486. msg.GetString().c_str())) {
  487. perform_check = true;
  488. }
  489. }
  490. if (perform_check) {
  491. EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
  492. << "Fixture constructor of ParamTestGenerationTest test case "
  493. << "has not been run as expected.";
  494. EXPECT_EQ(kExpectedCalls, set_up_count_)
  495. << "Fixture SetUp method of ParamTestGenerationTest test case "
  496. << "has not been run as expected.";
  497. EXPECT_EQ(kExpectedCalls, tear_down_count_)
  498. << "Fixture TearDown method of ParamTestGenerationTest test case "
  499. << "has not been run as expected.";
  500. EXPECT_EQ(kExpectedCalls, test_body_count_)
  501. << "Test in ParamTestGenerationTest test case "
  502. << "has not been run as expected.";
  503. }
  504. }
  505. private:
  506. TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
  507. tear_down_count_(0), test_body_count_(0) {}
  508. int fixture_constructor_count_;
  509. int set_up_count_;
  510. int tear_down_count_;
  511. int test_body_count_;
  512. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
  513. };
  514. const int test_generation_params[] = {36, 42, 72};
  515. class TestGenerationTest : public TestWithParam<int> {
  516. public:
  517. enum {
  518. PARAMETER_COUNT =
  519. sizeof(test_generation_params)/sizeof(test_generation_params[0])
  520. };
  521. typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
  522. TestGenerationTest() {
  523. Environment::Instance()->FixtureConstructorExecuted();
  524. current_parameter_ = GetParam();
  525. }
  526. void SetUp() override {
  527. Environment::Instance()->SetUpExecuted();
  528. EXPECT_EQ(current_parameter_, GetParam());
  529. }
  530. void TearDown() override {
  531. Environment::Instance()->TearDownExecuted();
  532. EXPECT_EQ(current_parameter_, GetParam());
  533. }
  534. static void SetUpTestSuite() {
  535. bool all_tests_in_test_case_selected = true;
  536. for (int i = 0; i < PARAMETER_COUNT; ++i) {
  537. Message test_name;
  538. test_name << "TestsExpandedAndRun/" << i;
  539. if ( !UnitTestOptions::FilterMatchesTest(
  540. "TestExpansionModule/MultipleTestGenerationTest",
  541. test_name.GetString())) {
  542. all_tests_in_test_case_selected = false;
  543. }
  544. }
  545. EXPECT_TRUE(all_tests_in_test_case_selected)
  546. << "When running the TestGenerationTest test case all of its tests\n"
  547. << "must be selected by the filter flag for the test case to pass.\n"
  548. << "If not all of them are enabled, we can't reliably conclude\n"
  549. << "that the correct number of tests have been generated.";
  550. collected_parameters_.clear();
  551. }
  552. static void TearDownTestSuite() {
  553. vector<int> expected_values(test_generation_params,
  554. test_generation_params + PARAMETER_COUNT);
  555. // Test execution order is not guaranteed by Google Test,
  556. // so the order of values in collected_parameters_ can be
  557. // different and we have to sort to compare.
  558. sort(expected_values.begin(), expected_values.end());
  559. sort(collected_parameters_.begin(), collected_parameters_.end());
  560. EXPECT_TRUE(collected_parameters_ == expected_values);
  561. }
  562. protected:
  563. int current_parameter_;
  564. static vector<int> collected_parameters_;
  565. private:
  566. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
  567. };
  568. vector<int> TestGenerationTest::collected_parameters_;
  569. TEST_P(TestGenerationTest, TestsExpandedAndRun) {
  570. Environment::Instance()->TestBodyExecuted();
  571. EXPECT_EQ(current_parameter_, GetParam());
  572. collected_parameters_.push_back(GetParam());
  573. }
  574. INSTANTIATE_TEST_SUITE_P(TestExpansionModule, TestGenerationTest,
  575. ValuesIn(test_generation_params));
  576. // This test verifies that the element sequence (third parameter of
  577. // INSTANTIATE_TEST_SUITE_P) is evaluated in InitGoogleTest() and neither at
  578. // the call site of INSTANTIATE_TEST_SUITE_P nor in RUN_ALL_TESTS(). For
  579. // that, we declare param_value_ to be a static member of
  580. // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in
  581. // main(), just before invocation of InitGoogleTest(). After calling
  582. // InitGoogleTest(), we set the value to 2. If the sequence is evaluated
  583. // before or after InitGoogleTest, INSTANTIATE_TEST_SUITE_P will create a
  584. // test with parameter other than 1, and the test body will fail the
  585. // assertion.
  586. class GeneratorEvaluationTest : public TestWithParam<int> {
  587. public:
  588. static int param_value() { return param_value_; }
  589. static void set_param_value(int param_value) { param_value_ = param_value; }
  590. private:
  591. static int param_value_;
  592. };
  593. int GeneratorEvaluationTest::param_value_ = 0;
  594. TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
  595. EXPECT_EQ(1, GetParam());
  596. }
  597. INSTANTIATE_TEST_SUITE_P(GenEvalModule, GeneratorEvaluationTest,
  598. Values(GeneratorEvaluationTest::param_value()));
  599. // Tests that generators defined in a different translation unit are
  600. // functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
  601. extern ParamGenerator<int> extern_gen;
  602. class ExternalGeneratorTest : public TestWithParam<int> {};
  603. TEST_P(ExternalGeneratorTest, ExternalGenerator) {
  604. // Sequence produced by extern_gen contains only a single value
  605. // which we verify here.
  606. EXPECT_EQ(GetParam(), 33);
  607. }
  608. INSTANTIATE_TEST_SUITE_P(ExternalGeneratorModule, ExternalGeneratorTest,
  609. extern_gen);
  610. // Tests that a parameterized test case can be defined in one translation
  611. // unit and instantiated in another. This test will be instantiated in
  612. // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
  613. // defined in gtest-param-test_test.h.
  614. TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
  615. EXPECT_EQ(0, GetParam() % 33);
  616. }
  617. // Tests that a parameterized test case can be instantiated with multiple
  618. // generators.
  619. class MultipleInstantiationTest : public TestWithParam<int> {};
  620. TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
  621. }
  622. INSTANTIATE_TEST_SUITE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
  623. INSTANTIATE_TEST_SUITE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
  624. // Tests that a parameterized test case can be instantiated
  625. // in multiple translation units. This test will be instantiated
  626. // here and in gtest-param-test_test2.cc.
  627. // InstantiationInMultipleTranslationUnitsTest fixture class
  628. // is defined in gtest-param-test_test.h.
  629. TEST_P(InstantiationInMultipleTranslationUnitsTest, IsMultipleOf42) {
  630. EXPECT_EQ(0, GetParam() % 42);
  631. }
  632. INSTANTIATE_TEST_SUITE_P(Sequence1, InstantiationInMultipleTranslationUnitsTest,
  633. Values(42, 42 * 2));
  634. // Tests that each iteration of parameterized test runs in a separate test
  635. // object.
  636. class SeparateInstanceTest : public TestWithParam<int> {
  637. public:
  638. SeparateInstanceTest() : count_(0) {}
  639. static void TearDownTestSuite() {
  640. EXPECT_GE(global_count_, 2)
  641. << "If some (but not all) SeparateInstanceTest tests have been "
  642. << "filtered out this test will fail. Make sure that all "
  643. << "GeneratorEvaluationTest are selected or de-selected together "
  644. << "by the test filter.";
  645. }
  646. protected:
  647. int count_;
  648. static int global_count_;
  649. };
  650. int SeparateInstanceTest::global_count_ = 0;
  651. TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
  652. EXPECT_EQ(0, count_++);
  653. global_count_++;
  654. }
  655. INSTANTIATE_TEST_SUITE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
  656. // Tests that all instantiations of a test have named appropriately. Test
  657. // defined with TEST_P(TestSuiteName, TestName) and instantiated with
  658. // INSTANTIATE_TEST_SUITE_P(SequenceName, TestSuiteName, generator) must be
  659. // named SequenceName/TestSuiteName.TestName/i, where i is the 0-based index of
  660. // the sequence element used to instantiate the test.
  661. class NamingTest : public TestWithParam<int> {};
  662. TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
  663. const ::testing::TestInfo* const test_info =
  664. ::testing::UnitTest::GetInstance()->current_test_info();
  665. EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_suite_name());
  666. Message index_stream;
  667. index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
  668. EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
  669. EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
  670. }
  671. INSTANTIATE_TEST_SUITE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
  672. // Tests that macros in test names are expanded correctly.
  673. class MacroNamingTest : public TestWithParam<int> {};
  674. #define PREFIX_WITH_FOO(test_name) Foo##test_name
  675. #define PREFIX_WITH_MACRO(test_name) Macro##test_name
  676. TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) {
  677. const ::testing::TestInfo* const test_info =
  678. ::testing::UnitTest::GetInstance()->current_test_info();
  679. EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_suite_name());
  680. EXPECT_STREQ("FooSomeTestName", test_info->name());
  681. }
  682. INSTANTIATE_TEST_SUITE_P(FortyTwo, MacroNamingTest, Values(42));
  683. // Tests the same thing for non-parametrized tests.
  684. class MacroNamingTestNonParametrized : public ::testing::Test {};
  685. TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized),
  686. PREFIX_WITH_FOO(SomeTestName)) {
  687. const ::testing::TestInfo* const test_info =
  688. ::testing::UnitTest::GetInstance()->current_test_info();
  689. EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_suite_name());
  690. EXPECT_STREQ("FooSomeTestName", test_info->name());
  691. }
  692. // Tests that user supplied custom parameter names are working correctly.
  693. // Runs the test with a builtin helper method which uses PrintToString,
  694. // as well as a custom function and custom functor to ensure all possible
  695. // uses work correctly.
  696. class CustomFunctorNamingTest : public TestWithParam<std::string> {};
  697. TEST_P(CustomFunctorNamingTest, CustomTestNames) {}
  698. struct CustomParamNameFunctor {
  699. std::string operator()(const ::testing::TestParamInfo<std::string>& inf) {
  700. return inf.param;
  701. }
  702. };
  703. INSTANTIATE_TEST_SUITE_P(CustomParamNameFunctor, CustomFunctorNamingTest,
  704. Values(std::string("FunctorName")),
  705. CustomParamNameFunctor());
  706. INSTANTIATE_TEST_SUITE_P(AllAllowedCharacters, CustomFunctorNamingTest,
  707. Values("abcdefghijklmnopqrstuvwxyz",
  708. "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "01234567890_"),
  709. CustomParamNameFunctor());
  710. inline std::string CustomParamNameFunction(
  711. const ::testing::TestParamInfo<std::string>& inf) {
  712. return inf.param;
  713. }
  714. class CustomFunctionNamingTest : public TestWithParam<std::string> {};
  715. TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
  716. INSTANTIATE_TEST_SUITE_P(CustomParamNameFunction, CustomFunctionNamingTest,
  717. Values(std::string("FunctionName")),
  718. CustomParamNameFunction);
  719. INSTANTIATE_TEST_SUITE_P(CustomParamNameFunctionP, CustomFunctionNamingTest,
  720. Values(std::string("FunctionNameP")),
  721. &CustomParamNameFunction);
  722. // Test custom naming with a lambda
  723. class CustomLambdaNamingTest : public TestWithParam<std::string> {};
  724. TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
  725. INSTANTIATE_TEST_SUITE_P(CustomParamNameLambda, CustomLambdaNamingTest,
  726. Values(std::string("LambdaName")),
  727. [](const ::testing::TestParamInfo<std::string>& inf) {
  728. return inf.param;
  729. });
  730. TEST(CustomNamingTest, CheckNameRegistry) {
  731. ::testing::UnitTest* unit_test = ::testing::UnitTest::GetInstance();
  732. std::set<std::string> test_names;
  733. for (int suite_num = 0; suite_num < unit_test->total_test_suite_count();
  734. ++suite_num) {
  735. const ::testing::TestSuite* test_suite = unit_test->GetTestSuite(suite_num);
  736. for (int test_num = 0; test_num < test_suite->total_test_count();
  737. ++test_num) {
  738. const ::testing::TestInfo* test_info = test_suite->GetTestInfo(test_num);
  739. test_names.insert(std::string(test_info->name()));
  740. }
  741. }
  742. EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctorName"));
  743. EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionName"));
  744. EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionNameP"));
  745. EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName"));
  746. }
  747. // Test a numeric name to ensure PrintToStringParamName works correctly.
  748. class CustomIntegerNamingTest : public TestWithParam<int> {};
  749. TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) {
  750. const ::testing::TestInfo* const test_info =
  751. ::testing::UnitTest::GetInstance()->current_test_info();
  752. Message test_name_stream;
  753. test_name_stream << "TestsReportCorrectNames/" << GetParam();
  754. EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
  755. }
  756. INSTANTIATE_TEST_SUITE_P(PrintToString, CustomIntegerNamingTest, Range(0, 5),
  757. ::testing::PrintToStringParamName());
  758. // Test a custom struct with PrintToString.
  759. struct CustomStruct {
  760. explicit CustomStruct(int value) : x(value) {}
  761. int x;
  762. };
  763. std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) {
  764. stream << val.x;
  765. return stream;
  766. }
  767. class CustomStructNamingTest : public TestWithParam<CustomStruct> {};
  768. TEST_P(CustomStructNamingTest, TestsReportCorrectNames) {
  769. const ::testing::TestInfo* const test_info =
  770. ::testing::UnitTest::GetInstance()->current_test_info();
  771. Message test_name_stream;
  772. test_name_stream << "TestsReportCorrectNames/" << GetParam();
  773. EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
  774. }
  775. INSTANTIATE_TEST_SUITE_P(PrintToString, CustomStructNamingTest,
  776. Values(CustomStruct(0), CustomStruct(1)),
  777. ::testing::PrintToStringParamName());
  778. // Test that using a stateful parameter naming function works as expected.
  779. struct StatefulNamingFunctor {
  780. StatefulNamingFunctor() : sum(0) {}
  781. std::string operator()(const ::testing::TestParamInfo<int>& info) {
  782. int value = info.param + sum;
  783. sum += info.param;
  784. return ::testing::PrintToString(value);
  785. }
  786. int sum;
  787. };
  788. class StatefulNamingTest : public ::testing::TestWithParam<int> {
  789. protected:
  790. StatefulNamingTest() : sum_(0) {}
  791. int sum_;
  792. };
  793. TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
  794. const ::testing::TestInfo* const test_info =
  795. ::testing::UnitTest::GetInstance()->current_test_info();
  796. sum_ += GetParam();
  797. Message test_name_stream;
  798. test_name_stream << "TestsReportCorrectNames/" << sum_;
  799. EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
  800. }
  801. INSTANTIATE_TEST_SUITE_P(StatefulNamingFunctor, StatefulNamingTest, Range(0, 5),
  802. StatefulNamingFunctor());
  803. // Class that cannot be streamed into an ostream. It needs to be copyable
  804. // (and, in case of MSVC, also assignable) in order to be a test parameter
  805. // type. Its default copy constructor and assignment operator do exactly
  806. // what we need.
  807. class Unstreamable {
  808. public:
  809. explicit Unstreamable(int value) : value_(value) {}
  810. // -Wunused-private-field: dummy accessor for `value_`.
  811. const int& dummy_value() const { return value_; }
  812. private:
  813. int value_;
  814. };
  815. class CommentTest : public TestWithParam<Unstreamable> {};
  816. TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
  817. const ::testing::TestInfo* const test_info =
  818. ::testing::UnitTest::GetInstance()->current_test_info();
  819. EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
  820. }
  821. INSTANTIATE_TEST_SUITE_P(InstantiationWithComments, CommentTest,
  822. Values(Unstreamable(1)));
  823. // Verify that we can create a hierarchy of test fixtures, where the base
  824. // class fixture is not parameterized and the derived class is. In this case
  825. // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We
  826. // perform simple tests on both.
  827. class NonParameterizedBaseTest : public ::testing::Test {
  828. public:
  829. NonParameterizedBaseTest() : n_(17) { }
  830. protected:
  831. int n_;
  832. };
  833. class ParameterizedDerivedTest : public NonParameterizedBaseTest,
  834. public ::testing::WithParamInterface<int> {
  835. protected:
  836. ParameterizedDerivedTest() : count_(0) { }
  837. int count_;
  838. static int global_count_;
  839. };
  840. int ParameterizedDerivedTest::global_count_ = 0;
  841. TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
  842. EXPECT_EQ(17, n_);
  843. }
  844. TEST_P(ParameterizedDerivedTest, SeesSequence) {
  845. EXPECT_EQ(17, n_);
  846. EXPECT_EQ(0, count_++);
  847. EXPECT_EQ(GetParam(), global_count_++);
  848. }
  849. class ParameterizedDeathTest : public ::testing::TestWithParam<int> { };
  850. TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
  851. EXPECT_DEATH_IF_SUPPORTED(GetParam(),
  852. ".* value-parameterized test .*");
  853. }
  854. INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest,
  855. Range(0, 5));
  856. // Tests param generator working with Enums
  857. enum MyEnums {
  858. ENUM1 = 1,
  859. ENUM2 = 3,
  860. ENUM3 = 8,
  861. };
  862. class MyEnumTest : public testing::TestWithParam<MyEnums> {};
  863. TEST_P(MyEnumTest, ChecksParamMoreThanZero) { EXPECT_GE(10, GetParam()); }
  864. INSTANTIATE_TEST_SUITE_P(MyEnumTests, MyEnumTest,
  865. ::testing::Values(ENUM1, ENUM2, 0));
  866. int main(int argc, char **argv) {
  867. // Used in TestGenerationTest test suite.
  868. AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
  869. // Used in GeneratorEvaluationTest test suite. Tests that the updated value
  870. // will be picked up for instantiating tests in GeneratorEvaluationTest.
  871. GeneratorEvaluationTest::set_param_value(1);
  872. ::testing::InitGoogleTest(&argc, argv);
  873. // Used in GeneratorEvaluationTest test suite. Tests that value updated
  874. // here will NOT be used for instantiating tests in
  875. // GeneratorEvaluationTest.
  876. GeneratorEvaluationTest::set_param_value(2);
  877. return RUN_ALL_TESTS();
  878. }