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