11cb0ef41Sopenharmony_ci// Copyright 2008, Google Inc. 21cb0ef41Sopenharmony_ci// All rights reserved. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 51cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are 61cb0ef41Sopenharmony_ci// met: 71cb0ef41Sopenharmony_ci// 81cb0ef41Sopenharmony_ci// * Redistributions of source code must retain the above copyright 91cb0ef41Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 101cb0ef41Sopenharmony_ci// * Redistributions in binary form must reproduce the above 111cb0ef41Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 121cb0ef41Sopenharmony_ci// in the documentation and/or other materials provided with the 131cb0ef41Sopenharmony_ci// distribution. 141cb0ef41Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 151cb0ef41Sopenharmony_ci// contributors may be used to endorse or promote products derived from 161cb0ef41Sopenharmony_ci// this software without specific prior written permission. 171cb0ef41Sopenharmony_ci// 181cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191cb0ef41Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201cb0ef41Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211cb0ef41Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221cb0ef41Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231cb0ef41Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241cb0ef41Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251cb0ef41Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261cb0ef41Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271cb0ef41Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281cb0ef41Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci// Macros and functions for implementing parameterized tests 311cb0ef41Sopenharmony_ci// in Google C++ Testing and Mocking Framework (Google Test) 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci// IWYU pragma: private, include "gtest/gtest.h" 341cb0ef41Sopenharmony_ci// IWYU pragma: friend gtest/.* 351cb0ef41Sopenharmony_ci// IWYU pragma: friend gmock/.* 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ 381cb0ef41Sopenharmony_ci#define GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci// Value-parameterized tests allow you to test your code with different 411cb0ef41Sopenharmony_ci// parameters without writing multiple copies of the same test. 421cb0ef41Sopenharmony_ci// 431cb0ef41Sopenharmony_ci// Here is how you use value-parameterized tests: 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci#if 0 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci// To write value-parameterized tests, first you should define a fixture 481cb0ef41Sopenharmony_ci// class. It is usually derived from testing::TestWithParam<T> (see below for 491cb0ef41Sopenharmony_ci// another inheritance scheme that's sometimes useful in more complicated 501cb0ef41Sopenharmony_ci// class hierarchies), where the type of your parameter values. 511cb0ef41Sopenharmony_ci// TestWithParam<T> is itself derived from testing::Test. T can be any 521cb0ef41Sopenharmony_ci// copyable type. If it's a raw pointer, you are responsible for managing the 531cb0ef41Sopenharmony_ci// lifespan of the pointed values. 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciclass FooTest : public ::testing::TestWithParam<const char*> { 561cb0ef41Sopenharmony_ci // You can implement all the usual class fixture members here. 571cb0ef41Sopenharmony_ci}; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci// Then, use the TEST_P macro to define as many parameterized tests 601cb0ef41Sopenharmony_ci// for this fixture as you want. The _P suffix is for "parameterized" 611cb0ef41Sopenharmony_ci// or "pattern", whichever you prefer to think. 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciTEST_P(FooTest, DoesBlah) { 641cb0ef41Sopenharmony_ci // Inside a test, access the test parameter with the GetParam() method 651cb0ef41Sopenharmony_ci // of the TestWithParam<T> class: 661cb0ef41Sopenharmony_ci EXPECT_TRUE(foo.Blah(GetParam())); 671cb0ef41Sopenharmony_ci ... 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ciTEST_P(FooTest, HasBlahBlah) { 711cb0ef41Sopenharmony_ci ... 721cb0ef41Sopenharmony_ci} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci// Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test 751cb0ef41Sopenharmony_ci// case with any set of parameters you want. Google Test defines a number 761cb0ef41Sopenharmony_ci// of functions for generating test parameters. They return what we call 771cb0ef41Sopenharmony_ci// (surprise!) parameter generators. Here is a summary of them, which 781cb0ef41Sopenharmony_ci// are all in the testing namespace: 791cb0ef41Sopenharmony_ci// 801cb0ef41Sopenharmony_ci// 811cb0ef41Sopenharmony_ci// Range(begin, end [, step]) - Yields values {begin, begin+step, 821cb0ef41Sopenharmony_ci// begin+step+step, ...}. The values do not 831cb0ef41Sopenharmony_ci// include end. step defaults to 1. 841cb0ef41Sopenharmony_ci// Values(v1, v2, ..., vN) - Yields values {v1, v2, ..., vN}. 851cb0ef41Sopenharmony_ci// ValuesIn(container) - Yields values from a C-style array, an STL 861cb0ef41Sopenharmony_ci// ValuesIn(begin,end) container, or an iterator range [begin, end). 871cb0ef41Sopenharmony_ci// Bool() - Yields sequence {false, true}. 881cb0ef41Sopenharmony_ci// Combine(g1, g2, ..., gN) - Yields all combinations (the Cartesian product 891cb0ef41Sopenharmony_ci// for the math savvy) of the values generated 901cb0ef41Sopenharmony_ci// by the N generators. 911cb0ef41Sopenharmony_ci// 921cb0ef41Sopenharmony_ci// For more details, see comments at the definitions of these functions below 931cb0ef41Sopenharmony_ci// in this file. 941cb0ef41Sopenharmony_ci// 951cb0ef41Sopenharmony_ci// The following statement will instantiate tests from the FooTest test suite 961cb0ef41Sopenharmony_ci// each with parameter values "meeny", "miny", and "moe". 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciINSTANTIATE_TEST_SUITE_P(InstantiationName, 991cb0ef41Sopenharmony_ci FooTest, 1001cb0ef41Sopenharmony_ci Values("meeny", "miny", "moe")); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci// To distinguish different instances of the pattern, (yes, you 1031cb0ef41Sopenharmony_ci// can instantiate it more than once) the first argument to the 1041cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the 1051cb0ef41Sopenharmony_ci// actual test suite name. Remember to pick unique prefixes for different 1061cb0ef41Sopenharmony_ci// instantiations. The tests from the instantiation above will have 1071cb0ef41Sopenharmony_ci// these names: 1081cb0ef41Sopenharmony_ci// 1091cb0ef41Sopenharmony_ci// * InstantiationName/FooTest.DoesBlah/0 for "meeny" 1101cb0ef41Sopenharmony_ci// * InstantiationName/FooTest.DoesBlah/1 for "miny" 1111cb0ef41Sopenharmony_ci// * InstantiationName/FooTest.DoesBlah/2 for "moe" 1121cb0ef41Sopenharmony_ci// * InstantiationName/FooTest.HasBlahBlah/0 for "meeny" 1131cb0ef41Sopenharmony_ci// * InstantiationName/FooTest.HasBlahBlah/1 for "miny" 1141cb0ef41Sopenharmony_ci// * InstantiationName/FooTest.HasBlahBlah/2 for "moe" 1151cb0ef41Sopenharmony_ci// 1161cb0ef41Sopenharmony_ci// You can use these names in --gtest_filter. 1171cb0ef41Sopenharmony_ci// 1181cb0ef41Sopenharmony_ci// This statement will instantiate all tests from FooTest again, each 1191cb0ef41Sopenharmony_ci// with parameter values "cat" and "dog": 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ciconst char* pets[] = {"cat", "dog"}; 1221cb0ef41Sopenharmony_ciINSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets)); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci// The tests from the instantiation above will have these names: 1251cb0ef41Sopenharmony_ci// 1261cb0ef41Sopenharmony_ci// * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat" 1271cb0ef41Sopenharmony_ci// * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog" 1281cb0ef41Sopenharmony_ci// * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat" 1291cb0ef41Sopenharmony_ci// * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog" 1301cb0ef41Sopenharmony_ci// 1311cb0ef41Sopenharmony_ci// Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests 1321cb0ef41Sopenharmony_ci// in the given test suite, whether their definitions come before or 1331cb0ef41Sopenharmony_ci// AFTER the INSTANTIATE_TEST_SUITE_P statement. 1341cb0ef41Sopenharmony_ci// 1351cb0ef41Sopenharmony_ci// Please also note that generator expressions (including parameters to the 1361cb0ef41Sopenharmony_ci// generators) are evaluated in InitGoogleTest(), after main() has started. 1371cb0ef41Sopenharmony_ci// This allows the user on one hand, to adjust generator parameters in order 1381cb0ef41Sopenharmony_ci// to dynamically determine a set of tests to run and on the other hand, 1391cb0ef41Sopenharmony_ci// give the user a chance to inspect the generated tests with Google Test 1401cb0ef41Sopenharmony_ci// reflection API before RUN_ALL_TESTS() is executed. 1411cb0ef41Sopenharmony_ci// 1421cb0ef41Sopenharmony_ci// You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc 1431cb0ef41Sopenharmony_ci// for more examples. 1441cb0ef41Sopenharmony_ci// 1451cb0ef41Sopenharmony_ci// In the future, we plan to publish the API for defining new parameter 1461cb0ef41Sopenharmony_ci// generators. But for now this interface remains part of the internal 1471cb0ef41Sopenharmony_ci// implementation and is subject to change. 1481cb0ef41Sopenharmony_ci// 1491cb0ef41Sopenharmony_ci// 1501cb0ef41Sopenharmony_ci// A parameterized test fixture must be derived from testing::Test and from 1511cb0ef41Sopenharmony_ci// testing::WithParamInterface<T>, where T is the type of the parameter 1521cb0ef41Sopenharmony_ci// values. Inheriting from TestWithParam<T> satisfies that requirement because 1531cb0ef41Sopenharmony_ci// TestWithParam<T> inherits from both Test and WithParamInterface. In more 1541cb0ef41Sopenharmony_ci// complicated hierarchies, however, it is occasionally useful to inherit 1551cb0ef41Sopenharmony_ci// separately from Test and WithParamInterface. For example: 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ciclass BaseTest : public ::testing::Test { 1581cb0ef41Sopenharmony_ci // You can inherit all the usual members for a non-parameterized test 1591cb0ef41Sopenharmony_ci // fixture here. 1601cb0ef41Sopenharmony_ci}; 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ciclass DerivedTest : public BaseTest, public ::testing::WithParamInterface<int> { 1631cb0ef41Sopenharmony_ci // The usual test fixture members go here too. 1641cb0ef41Sopenharmony_ci}; 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciTEST_F(BaseTest, HasFoo) { 1671cb0ef41Sopenharmony_ci // This is an ordinary non-parameterized test. 1681cb0ef41Sopenharmony_ci} 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ciTEST_P(DerivedTest, DoesBlah) { 1711cb0ef41Sopenharmony_ci // GetParam works just the same here as if you inherit from TestWithParam. 1721cb0ef41Sopenharmony_ci EXPECT_TRUE(foo.Blah(GetParam())); 1731cb0ef41Sopenharmony_ci} 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci#endif // 0 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci#include <iterator> 1781cb0ef41Sopenharmony_ci#include <utility> 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-internal.h" 1811cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-param-util.h" 1821cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-port.h" 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_cinamespace testing { 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci// Functions producing parameter generators. 1871cb0ef41Sopenharmony_ci// 1881cb0ef41Sopenharmony_ci// Google Test uses these generators to produce parameters for value- 1891cb0ef41Sopenharmony_ci// parameterized tests. When a parameterized test suite is instantiated 1901cb0ef41Sopenharmony_ci// with a particular generator, Google Test creates and runs tests 1911cb0ef41Sopenharmony_ci// for each element in the sequence produced by the generator. 1921cb0ef41Sopenharmony_ci// 1931cb0ef41Sopenharmony_ci// In the following sample, tests from test suite FooTest are instantiated 1941cb0ef41Sopenharmony_ci// each three times with parameter values 3, 5, and 8: 1951cb0ef41Sopenharmony_ci// 1961cb0ef41Sopenharmony_ci// class FooTest : public TestWithParam<int> { ... }; 1971cb0ef41Sopenharmony_ci// 1981cb0ef41Sopenharmony_ci// TEST_P(FooTest, TestThis) { 1991cb0ef41Sopenharmony_ci// } 2001cb0ef41Sopenharmony_ci// TEST_P(FooTest, TestThat) { 2011cb0ef41Sopenharmony_ci// } 2021cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8)); 2031cb0ef41Sopenharmony_ci// 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci// Range() returns generators providing sequences of values in a range. 2061cb0ef41Sopenharmony_ci// 2071cb0ef41Sopenharmony_ci// Synopsis: 2081cb0ef41Sopenharmony_ci// Range(start, end) 2091cb0ef41Sopenharmony_ci// - returns a generator producing a sequence of values {start, start+1, 2101cb0ef41Sopenharmony_ci// start+2, ..., }. 2111cb0ef41Sopenharmony_ci// Range(start, end, step) 2121cb0ef41Sopenharmony_ci// - returns a generator producing a sequence of values {start, start+step, 2131cb0ef41Sopenharmony_ci// start+step+step, ..., }. 2141cb0ef41Sopenharmony_ci// Notes: 2151cb0ef41Sopenharmony_ci// * The generated sequences never include end. For example, Range(1, 5) 2161cb0ef41Sopenharmony_ci// returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2) 2171cb0ef41Sopenharmony_ci// returns a generator producing {1, 3, 5, 7}. 2181cb0ef41Sopenharmony_ci// * start and end must have the same type. That type may be any integral or 2191cb0ef41Sopenharmony_ci// floating-point type or a user defined type satisfying these conditions: 2201cb0ef41Sopenharmony_ci// * It must be assignable (have operator=() defined). 2211cb0ef41Sopenharmony_ci// * It must have operator+() (operator+(int-compatible type) for 2221cb0ef41Sopenharmony_ci// two-operand version). 2231cb0ef41Sopenharmony_ci// * It must have operator<() defined. 2241cb0ef41Sopenharmony_ci// Elements in the resulting sequences will also have that type. 2251cb0ef41Sopenharmony_ci// * Condition start < end must be satisfied in order for resulting sequences 2261cb0ef41Sopenharmony_ci// to contain any elements. 2271cb0ef41Sopenharmony_ci// 2281cb0ef41Sopenharmony_citemplate <typename T, typename IncrementT> 2291cb0ef41Sopenharmony_ciinternal::ParamGenerator<T> Range(T start, T end, IncrementT step) { 2301cb0ef41Sopenharmony_ci return internal::ParamGenerator<T>( 2311cb0ef41Sopenharmony_ci new internal::RangeGenerator<T, IncrementT>(start, end, step)); 2321cb0ef41Sopenharmony_ci} 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_citemplate <typename T> 2351cb0ef41Sopenharmony_ciinternal::ParamGenerator<T> Range(T start, T end) { 2361cb0ef41Sopenharmony_ci return Range(start, end, 1); 2371cb0ef41Sopenharmony_ci} 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci// ValuesIn() function allows generation of tests with parameters coming from 2401cb0ef41Sopenharmony_ci// a container. 2411cb0ef41Sopenharmony_ci// 2421cb0ef41Sopenharmony_ci// Synopsis: 2431cb0ef41Sopenharmony_ci// ValuesIn(const T (&array)[N]) 2441cb0ef41Sopenharmony_ci// - returns a generator producing sequences with elements from 2451cb0ef41Sopenharmony_ci// a C-style array. 2461cb0ef41Sopenharmony_ci// ValuesIn(const Container& container) 2471cb0ef41Sopenharmony_ci// - returns a generator producing sequences with elements from 2481cb0ef41Sopenharmony_ci// an STL-style container. 2491cb0ef41Sopenharmony_ci// ValuesIn(Iterator begin, Iterator end) 2501cb0ef41Sopenharmony_ci// - returns a generator producing sequences with elements from 2511cb0ef41Sopenharmony_ci// a range [begin, end) defined by a pair of STL-style iterators. These 2521cb0ef41Sopenharmony_ci// iterators can also be plain C pointers. 2531cb0ef41Sopenharmony_ci// 2541cb0ef41Sopenharmony_ci// Please note that ValuesIn copies the values from the containers 2551cb0ef41Sopenharmony_ci// passed in and keeps them to generate tests in RUN_ALL_TESTS(). 2561cb0ef41Sopenharmony_ci// 2571cb0ef41Sopenharmony_ci// Examples: 2581cb0ef41Sopenharmony_ci// 2591cb0ef41Sopenharmony_ci// This instantiates tests from test suite StringTest 2601cb0ef41Sopenharmony_ci// each with C-string values of "foo", "bar", and "baz": 2611cb0ef41Sopenharmony_ci// 2621cb0ef41Sopenharmony_ci// const char* strings[] = {"foo", "bar", "baz"}; 2631cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings)); 2641cb0ef41Sopenharmony_ci// 2651cb0ef41Sopenharmony_ci// This instantiates tests from test suite StlStringTest 2661cb0ef41Sopenharmony_ci// each with STL strings with values "a" and "b": 2671cb0ef41Sopenharmony_ci// 2681cb0ef41Sopenharmony_ci// ::std::vector< ::std::string> GetParameterStrings() { 2691cb0ef41Sopenharmony_ci// ::std::vector< ::std::string> v; 2701cb0ef41Sopenharmony_ci// v.push_back("a"); 2711cb0ef41Sopenharmony_ci// v.push_back("b"); 2721cb0ef41Sopenharmony_ci// return v; 2731cb0ef41Sopenharmony_ci// } 2741cb0ef41Sopenharmony_ci// 2751cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(CharSequence, 2761cb0ef41Sopenharmony_ci// StlStringTest, 2771cb0ef41Sopenharmony_ci// ValuesIn(GetParameterStrings())); 2781cb0ef41Sopenharmony_ci// 2791cb0ef41Sopenharmony_ci// 2801cb0ef41Sopenharmony_ci// This will also instantiate tests from CharTest 2811cb0ef41Sopenharmony_ci// each with parameter values 'a' and 'b': 2821cb0ef41Sopenharmony_ci// 2831cb0ef41Sopenharmony_ci// ::std::list<char> GetParameterChars() { 2841cb0ef41Sopenharmony_ci// ::std::list<char> list; 2851cb0ef41Sopenharmony_ci// list.push_back('a'); 2861cb0ef41Sopenharmony_ci// list.push_back('b'); 2871cb0ef41Sopenharmony_ci// return list; 2881cb0ef41Sopenharmony_ci// } 2891cb0ef41Sopenharmony_ci// ::std::list<char> l = GetParameterChars(); 2901cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(CharSequence2, 2911cb0ef41Sopenharmony_ci// CharTest, 2921cb0ef41Sopenharmony_ci// ValuesIn(l.begin(), l.end())); 2931cb0ef41Sopenharmony_ci// 2941cb0ef41Sopenharmony_citemplate <typename ForwardIterator> 2951cb0ef41Sopenharmony_ciinternal::ParamGenerator< 2961cb0ef41Sopenharmony_ci typename std::iterator_traits<ForwardIterator>::value_type> 2971cb0ef41Sopenharmony_ciValuesIn(ForwardIterator begin, ForwardIterator end) { 2981cb0ef41Sopenharmony_ci typedef typename std::iterator_traits<ForwardIterator>::value_type ParamType; 2991cb0ef41Sopenharmony_ci return internal::ParamGenerator<ParamType>( 3001cb0ef41Sopenharmony_ci new internal::ValuesInIteratorRangeGenerator<ParamType>(begin, end)); 3011cb0ef41Sopenharmony_ci} 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_citemplate <typename T, size_t N> 3041cb0ef41Sopenharmony_ciinternal::ParamGenerator<T> ValuesIn(const T (&array)[N]) { 3051cb0ef41Sopenharmony_ci return ValuesIn(array, array + N); 3061cb0ef41Sopenharmony_ci} 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_citemplate <class Container> 3091cb0ef41Sopenharmony_ciinternal::ParamGenerator<typename Container::value_type> ValuesIn( 3101cb0ef41Sopenharmony_ci const Container& container) { 3111cb0ef41Sopenharmony_ci return ValuesIn(container.begin(), container.end()); 3121cb0ef41Sopenharmony_ci} 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci// Values() allows generating tests from explicitly specified list of 3151cb0ef41Sopenharmony_ci// parameters. 3161cb0ef41Sopenharmony_ci// 3171cb0ef41Sopenharmony_ci// Synopsis: 3181cb0ef41Sopenharmony_ci// Values(T v1, T v2, ..., T vN) 3191cb0ef41Sopenharmony_ci// - returns a generator producing sequences with elements v1, v2, ..., vN. 3201cb0ef41Sopenharmony_ci// 3211cb0ef41Sopenharmony_ci// For example, this instantiates tests from test suite BarTest each 3221cb0ef41Sopenharmony_ci// with values "one", "two", and "three": 3231cb0ef41Sopenharmony_ci// 3241cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(NumSequence, 3251cb0ef41Sopenharmony_ci// BarTest, 3261cb0ef41Sopenharmony_ci// Values("one", "two", "three")); 3271cb0ef41Sopenharmony_ci// 3281cb0ef41Sopenharmony_ci// This instantiates tests from test suite BazTest each with values 1, 2, 3.5. 3291cb0ef41Sopenharmony_ci// The exact type of values will depend on the type of parameter in BazTest. 3301cb0ef41Sopenharmony_ci// 3311cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5)); 3321cb0ef41Sopenharmony_ci// 3331cb0ef41Sopenharmony_ci// 3341cb0ef41Sopenharmony_citemplate <typename... T> 3351cb0ef41Sopenharmony_ciinternal::ValueArray<T...> Values(T... v) { 3361cb0ef41Sopenharmony_ci return internal::ValueArray<T...>(std::move(v)...); 3371cb0ef41Sopenharmony_ci} 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ci// Bool() allows generating tests with parameters in a set of (false, true). 3401cb0ef41Sopenharmony_ci// 3411cb0ef41Sopenharmony_ci// Synopsis: 3421cb0ef41Sopenharmony_ci// Bool() 3431cb0ef41Sopenharmony_ci// - returns a generator producing sequences with elements {false, true}. 3441cb0ef41Sopenharmony_ci// 3451cb0ef41Sopenharmony_ci// It is useful when testing code that depends on Boolean flags. Combinations 3461cb0ef41Sopenharmony_ci// of multiple flags can be tested when several Bool()'s are combined using 3471cb0ef41Sopenharmony_ci// Combine() function. 3481cb0ef41Sopenharmony_ci// 3491cb0ef41Sopenharmony_ci// In the following example all tests in the test suite FlagDependentTest 3501cb0ef41Sopenharmony_ci// will be instantiated twice with parameters false and true. 3511cb0ef41Sopenharmony_ci// 3521cb0ef41Sopenharmony_ci// class FlagDependentTest : public testing::TestWithParam<bool> { 3531cb0ef41Sopenharmony_ci// virtual void SetUp() { 3541cb0ef41Sopenharmony_ci// external_flag = GetParam(); 3551cb0ef41Sopenharmony_ci// } 3561cb0ef41Sopenharmony_ci// } 3571cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool()); 3581cb0ef41Sopenharmony_ci// 3591cb0ef41Sopenharmony_ciinline internal::ParamGenerator<bool> Bool() { return Values(false, true); } 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci// Combine() allows the user to combine two or more sequences to produce 3621cb0ef41Sopenharmony_ci// values of a Cartesian product of those sequences' elements. 3631cb0ef41Sopenharmony_ci// 3641cb0ef41Sopenharmony_ci// Synopsis: 3651cb0ef41Sopenharmony_ci// Combine(gen1, gen2, ..., genN) 3661cb0ef41Sopenharmony_ci// - returns a generator producing sequences with elements coming from 3671cb0ef41Sopenharmony_ci// the Cartesian product of elements from the sequences generated by 3681cb0ef41Sopenharmony_ci// gen1, gen2, ..., genN. The sequence elements will have a type of 3691cb0ef41Sopenharmony_ci// std::tuple<T1, T2, ..., TN> where T1, T2, ..., TN are the types 3701cb0ef41Sopenharmony_ci// of elements from sequences produces by gen1, gen2, ..., genN. 3711cb0ef41Sopenharmony_ci// 3721cb0ef41Sopenharmony_ci// Example: 3731cb0ef41Sopenharmony_ci// 3741cb0ef41Sopenharmony_ci// This will instantiate tests in test suite AnimalTest each one with 3751cb0ef41Sopenharmony_ci// the parameter values tuple("cat", BLACK), tuple("cat", WHITE), 3761cb0ef41Sopenharmony_ci// tuple("dog", BLACK), and tuple("dog", WHITE): 3771cb0ef41Sopenharmony_ci// 3781cb0ef41Sopenharmony_ci// enum Color { BLACK, GRAY, WHITE }; 3791cb0ef41Sopenharmony_ci// class AnimalTest 3801cb0ef41Sopenharmony_ci// : public testing::TestWithParam<std::tuple<const char*, Color> > {...}; 3811cb0ef41Sopenharmony_ci// 3821cb0ef41Sopenharmony_ci// TEST_P(AnimalTest, AnimalLooksNice) {...} 3831cb0ef41Sopenharmony_ci// 3841cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest, 3851cb0ef41Sopenharmony_ci// Combine(Values("cat", "dog"), 3861cb0ef41Sopenharmony_ci// Values(BLACK, WHITE))); 3871cb0ef41Sopenharmony_ci// 3881cb0ef41Sopenharmony_ci// This will instantiate tests in FlagDependentTest with all variations of two 3891cb0ef41Sopenharmony_ci// Boolean flags: 3901cb0ef41Sopenharmony_ci// 3911cb0ef41Sopenharmony_ci// class FlagDependentTest 3921cb0ef41Sopenharmony_ci// : public testing::TestWithParam<std::tuple<bool, bool> > { 3931cb0ef41Sopenharmony_ci// virtual void SetUp() { 3941cb0ef41Sopenharmony_ci// // Assigns external_flag_1 and external_flag_2 values from the tuple. 3951cb0ef41Sopenharmony_ci// std::tie(external_flag_1, external_flag_2) = GetParam(); 3961cb0ef41Sopenharmony_ci// } 3971cb0ef41Sopenharmony_ci// }; 3981cb0ef41Sopenharmony_ci// 3991cb0ef41Sopenharmony_ci// TEST_P(FlagDependentTest, TestFeature1) { 4001cb0ef41Sopenharmony_ci// // Test your code using external_flag_1 and external_flag_2 here. 4011cb0ef41Sopenharmony_ci// } 4021cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest, 4031cb0ef41Sopenharmony_ci// Combine(Bool(), Bool())); 4041cb0ef41Sopenharmony_ci// 4051cb0ef41Sopenharmony_citemplate <typename... Generator> 4061cb0ef41Sopenharmony_ciinternal::CartesianProductHolder<Generator...> Combine(const Generator&... g) { 4071cb0ef41Sopenharmony_ci return internal::CartesianProductHolder<Generator...>(g...); 4081cb0ef41Sopenharmony_ci} 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ci// ConvertGenerator() wraps a parameter generator in order to cast each produced 4111cb0ef41Sopenharmony_ci// value through a known type before supplying it to the test suite 4121cb0ef41Sopenharmony_ci// 4131cb0ef41Sopenharmony_ci// Synopsis: 4141cb0ef41Sopenharmony_ci// ConvertGenerator<T>(gen) 4151cb0ef41Sopenharmony_ci// - returns a generator producing the same elements as generated by gen, but 4161cb0ef41Sopenharmony_ci// each element is static_cast to type T before being returned 4171cb0ef41Sopenharmony_ci// 4181cb0ef41Sopenharmony_ci// It is useful when using the Combine() function to get the generated 4191cb0ef41Sopenharmony_ci// parameters in a custom type instead of std::tuple 4201cb0ef41Sopenharmony_ci// 4211cb0ef41Sopenharmony_ci// Example: 4221cb0ef41Sopenharmony_ci// 4231cb0ef41Sopenharmony_ci// This will instantiate tests in test suite AnimalTest each one with 4241cb0ef41Sopenharmony_ci// the parameter values tuple("cat", BLACK), tuple("cat", WHITE), 4251cb0ef41Sopenharmony_ci// tuple("dog", BLACK), and tuple("dog", WHITE): 4261cb0ef41Sopenharmony_ci// 4271cb0ef41Sopenharmony_ci// enum Color { BLACK, GRAY, WHITE }; 4281cb0ef41Sopenharmony_ci// struct ParamType { 4291cb0ef41Sopenharmony_ci// using TupleT = std::tuple<const char*, Color>; 4301cb0ef41Sopenharmony_ci// std::string animal; 4311cb0ef41Sopenharmony_ci// Color color; 4321cb0ef41Sopenharmony_ci// ParamType(TupleT t) : animal(std::get<0>(t)), color(std::get<1>(t)) {} 4331cb0ef41Sopenharmony_ci// }; 4341cb0ef41Sopenharmony_ci// class AnimalTest 4351cb0ef41Sopenharmony_ci// : public testing::TestWithParam<ParamType> {...}; 4361cb0ef41Sopenharmony_ci// 4371cb0ef41Sopenharmony_ci// TEST_P(AnimalTest, AnimalLooksNice) {...} 4381cb0ef41Sopenharmony_ci// 4391cb0ef41Sopenharmony_ci// INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest, 4401cb0ef41Sopenharmony_ci// ConvertGenerator<ParamType::TupleT>( 4411cb0ef41Sopenharmony_ci// Combine(Values("cat", "dog"), 4421cb0ef41Sopenharmony_ci// Values(BLACK, WHITE)))); 4431cb0ef41Sopenharmony_ci// 4441cb0ef41Sopenharmony_citemplate <typename T> 4451cb0ef41Sopenharmony_ciinternal::ParamConverterGenerator<T> ConvertGenerator( 4461cb0ef41Sopenharmony_ci internal::ParamGenerator<T> gen) { 4471cb0ef41Sopenharmony_ci return internal::ParamConverterGenerator<T>(gen); 4481cb0ef41Sopenharmony_ci} 4491cb0ef41Sopenharmony_ci 4501cb0ef41Sopenharmony_ci#define TEST_P(test_suite_name, test_name) \ 4511cb0ef41Sopenharmony_ci class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ 4521cb0ef41Sopenharmony_ci : public test_suite_name, \ 4531cb0ef41Sopenharmony_ci private ::testing::internal::GTestNonCopyable { \ 4541cb0ef41Sopenharmony_ci public: \ 4551cb0ef41Sopenharmony_ci GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \ 4561cb0ef41Sopenharmony_ci void TestBody() override; \ 4571cb0ef41Sopenharmony_ci \ 4581cb0ef41Sopenharmony_ci private: \ 4591cb0ef41Sopenharmony_ci static int AddToRegistry() { \ 4601cb0ef41Sopenharmony_ci ::testing::UnitTest::GetInstance() \ 4611cb0ef41Sopenharmony_ci ->parameterized_test_registry() \ 4621cb0ef41Sopenharmony_ci .GetTestSuitePatternHolder<test_suite_name>( \ 4631cb0ef41Sopenharmony_ci GTEST_STRINGIFY_(test_suite_name), \ 4641cb0ef41Sopenharmony_ci ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ 4651cb0ef41Sopenharmony_ci ->AddTestPattern( \ 4661cb0ef41Sopenharmony_ci GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name), \ 4671cb0ef41Sopenharmony_ci new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \ 4681cb0ef41Sopenharmony_ci test_suite_name, test_name)>(), \ 4691cb0ef41Sopenharmony_ci ::testing::internal::CodeLocation(__FILE__, __LINE__)); \ 4701cb0ef41Sopenharmony_ci return 0; \ 4711cb0ef41Sopenharmony_ci } \ 4721cb0ef41Sopenharmony_ci static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \ 4731cb0ef41Sopenharmony_ci }; \ 4741cb0ef41Sopenharmony_ci int GTEST_TEST_CLASS_NAME_(test_suite_name, \ 4751cb0ef41Sopenharmony_ci test_name)::gtest_registering_dummy_ = \ 4761cb0ef41Sopenharmony_ci GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry(); \ 4771cb0ef41Sopenharmony_ci void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody() 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci// The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify 4801cb0ef41Sopenharmony_ci// generator and an optional function or functor that generates custom test name 4811cb0ef41Sopenharmony_ci// suffixes based on the test parameters. Such a function or functor should 4821cb0ef41Sopenharmony_ci// accept one argument of type testing::TestParamInfo<class ParamType>, and 4831cb0ef41Sopenharmony_ci// return std::string. 4841cb0ef41Sopenharmony_ci// 4851cb0ef41Sopenharmony_ci// testing::PrintToStringParamName is a builtin test suffix generator that 4861cb0ef41Sopenharmony_ci// returns the value of testing::PrintToString(GetParam()). 4871cb0ef41Sopenharmony_ci// 4881cb0ef41Sopenharmony_ci// Note: test names must be non-empty, unique, and may only contain ASCII 4891cb0ef41Sopenharmony_ci// alphanumeric characters or underscore. Because PrintToString adds quotes 4901cb0ef41Sopenharmony_ci// to std::string and C strings, it won't work for these types. 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci#define GTEST_EXPAND_(arg) arg 4931cb0ef41Sopenharmony_ci#define GTEST_GET_FIRST_(first, ...) first 4941cb0ef41Sopenharmony_ci#define GTEST_GET_SECOND_(first, second, ...) second 4951cb0ef41Sopenharmony_ci 4961cb0ef41Sopenharmony_ci#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \ 4971cb0ef41Sopenharmony_ci static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \ 4981cb0ef41Sopenharmony_ci gtest_##prefix##test_suite_name##_EvalGenerator_() { \ 4991cb0ef41Sopenharmony_ci return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \ 5001cb0ef41Sopenharmony_ci } \ 5011cb0ef41Sopenharmony_ci static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \ 5021cb0ef41Sopenharmony_ci const ::testing::TestParamInfo<test_suite_name::ParamType>& info) { \ 5031cb0ef41Sopenharmony_ci if (::testing::internal::AlwaysFalse()) { \ 5041cb0ef41Sopenharmony_ci ::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \ 5051cb0ef41Sopenharmony_ci __VA_ARGS__, \ 5061cb0ef41Sopenharmony_ci ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \ 5071cb0ef41Sopenharmony_ci DUMMY_PARAM_))); \ 5081cb0ef41Sopenharmony_ci auto t = std::make_tuple(__VA_ARGS__); \ 5091cb0ef41Sopenharmony_ci static_assert(std::tuple_size<decltype(t)>::value <= 2, \ 5101cb0ef41Sopenharmony_ci "Too Many Args!"); \ 5111cb0ef41Sopenharmony_ci } \ 5121cb0ef41Sopenharmony_ci return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \ 5131cb0ef41Sopenharmony_ci __VA_ARGS__, \ 5141cb0ef41Sopenharmony_ci ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \ 5151cb0ef41Sopenharmony_ci DUMMY_PARAM_))))(info); \ 5161cb0ef41Sopenharmony_ci } \ 5171cb0ef41Sopenharmony_ci static int gtest_##prefix##test_suite_name##_dummy_ \ 5181cb0ef41Sopenharmony_ci GTEST_ATTRIBUTE_UNUSED_ = \ 5191cb0ef41Sopenharmony_ci ::testing::UnitTest::GetInstance() \ 5201cb0ef41Sopenharmony_ci ->parameterized_test_registry() \ 5211cb0ef41Sopenharmony_ci .GetTestSuitePatternHolder<test_suite_name>( \ 5221cb0ef41Sopenharmony_ci GTEST_STRINGIFY_(test_suite_name), \ 5231cb0ef41Sopenharmony_ci ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ 5241cb0ef41Sopenharmony_ci ->AddTestSuiteInstantiation( \ 5251cb0ef41Sopenharmony_ci GTEST_STRINGIFY_(prefix), \ 5261cb0ef41Sopenharmony_ci >est_##prefix##test_suite_name##_EvalGenerator_, \ 5271cb0ef41Sopenharmony_ci >est_##prefix##test_suite_name##_EvalGenerateName_, \ 5281cb0ef41Sopenharmony_ci __FILE__, __LINE__) 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ci// Allow Marking a Parameterized test class as not needing to be instantiated. 5311cb0ef41Sopenharmony_ci#define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \ 5321cb0ef41Sopenharmony_ci namespace gtest_do_not_use_outside_namespace_scope {} \ 5331cb0ef41Sopenharmony_ci static const ::testing::internal::MarkAsIgnored gtest_allow_ignore_##T( \ 5341cb0ef41Sopenharmony_ci GTEST_STRINGIFY_(T)) 5351cb0ef41Sopenharmony_ci 5361cb0ef41Sopenharmony_ci// Legacy API is deprecated but still available 5371cb0ef41Sopenharmony_ci#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ 5381cb0ef41Sopenharmony_ci#define INSTANTIATE_TEST_CASE_P \ 5391cb0ef41Sopenharmony_ci static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \ 5401cb0ef41Sopenharmony_ci ""); \ 5411cb0ef41Sopenharmony_ci INSTANTIATE_TEST_SUITE_P 5421cb0ef41Sopenharmony_ci#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ 5431cb0ef41Sopenharmony_ci 5441cb0ef41Sopenharmony_ci} // namespace testing 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ 547