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