Commit 663ef863 authored by misterg's avatar misterg Committed by Gennadiy Civil
Browse files

Googletest export

New variadic implementation for gtest-param-test
Removed non-variadic implementation and added variadic for ValueArray and Values

PiperOrigin-RevId: 217703627
parent 3a7f0934
This diff is collapsed.
...@@ -335,19 +335,12 @@ internal::ParamGenerator<typename Container::value_type> ValuesIn( ...@@ -335,19 +335,12 @@ internal::ParamGenerator<typename Container::value_type> ValuesIn(
// //
// INSTANTIATE_TEST_CASE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5)); // INSTANTIATE_TEST_CASE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));
// //
// Currently, Values() supports from 1 to $n parameters.
// //
$range i 1..n template <typename... T>
$for i [[ internal::ValueArray<T...> Values(T... v) {
$range j 1..i return internal::ValueArray<T...>(std::move(v)...);
template <$for j, [[typename T$j]]>
internal::ValueArray$i<$for j, [[T$j]]> Values($for j, [[T$j v$j]]) {
return internal::ValueArray$i<$for j, [[T$j]]>($for j, [[v$j]]);
} }
]]
// Bool() allows generating tests with parameters in a set of (false, true). // Bool() allows generating tests with parameters in a set of (false, true).
// //
// Synopsis: // Synopsis:
......
...@@ -51,53 +51,7 @@ $var maxtuple = 10 $$ Maximum number of Combine arguments we want to support. ...@@ -51,53 +51,7 @@ $var maxtuple = 10 $$ Maximum number of Combine arguments we want to support.
namespace testing { namespace testing {
// Forward declarations of ValuesIn(), which is implemented in
// include/gtest/gtest-param-test.h.
template <typename ForwardIterator>
internal::ParamGenerator<
typename ::testing::internal::IteratorTraits<ForwardIterator>::value_type>
ValuesIn(ForwardIterator begin, ForwardIterator end);
template <typename T, size_t N>
internal::ParamGenerator<T> ValuesIn(const T (&array)[N]);
template <class Container>
internal::ParamGenerator<typename Container::value_type> ValuesIn(
const Container& container);
namespace internal { namespace internal {
// Used in the Values() function to provide polymorphic capabilities.
$range i 1..n
$for i [[
$range j 1..i
template <$for j, [[typename T$j]]>
class ValueArray$i {
public:
$if i==1 [[explicit ]]ValueArray$i($for j, [[T$j v$j]]) : $for j, [[v$(j)_(v$j)]] {}
template <typename T>
operator ParamGenerator<T>() const {
const T array[] = {$for j, [[static_cast<T>(v$(j)_)]]};
return ValuesIn(array);
}
ValueArray$i(const ValueArray$i& other) : $for j, [[v$(j)_(other.v$(j)_)]] {}
private:
// No implementation - assignment is unsupported.
void operator=(const ValueArray$i& other);
$for j [[
const T$j v$(j)_;
]]
};
]]
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
// //
// Generates values from the Cartesian product of values produced // Generates values from the Cartesian product of values produced
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <iterator> #include <iterator>
#include <set> #include <set>
#include <tuple>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -48,7 +49,6 @@ ...@@ -48,7 +49,6 @@
#include "gtest/gtest-printers.h" #include "gtest/gtest-printers.h"
namespace testing { namespace testing {
// Input to a parameterized test name generator, describing a test parameter. // Input to a parameterized test name generator, describing a test parameter.
// Consists of the parameter value and the integer parameter index. // Consists of the parameter value and the integer parameter index.
template <class ParamType> template <class ParamType>
...@@ -72,7 +72,29 @@ struct PrintToStringParamName { ...@@ -72,7 +72,29 @@ struct PrintToStringParamName {
namespace internal { namespace internal {
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
// // Utility Functions
// Block of code creating for_each_in_tuple
template <int... Is>
struct sequence {};
template <int N, int... Is>
struct generate_sequence : generate_sequence<N - 1, N - 1, Is...> {};
template <int... Is>
struct generate_sequence<0, Is...> : sequence<Is...> {};
template <typename T, typename F, int... Is>
void ForEachInTupleImpl(T&& t, F f_gtest, sequence<Is...>) {
int l[] = {(f_gtest(std::get<Is>(t)), 0)...};
(void)l; // silence "unused variable warning"
}
template <typename... T, typename F>
void ForEachInTuple(const std::tuple<T...>& t, F f_gtest) {
internal::ForEachInTupleImpl(t, f_gtest,
internal::generate_sequence<sizeof...(T)>());
}
// Outputs a message explaining invalid registration of different // Outputs a message explaining invalid registration of different
// fixture class for the same test case. This may happen when // fixture class for the same test case. This may happen when
// TEST_P macro is used to define two tests with the same name // TEST_P macro is used to define two tests with the same name
...@@ -714,6 +736,43 @@ class ParameterizedTestCaseRegistry { ...@@ -714,6 +736,43 @@ class ParameterizedTestCaseRegistry {
GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry); GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry);
}; };
} // namespace internal
// Forward declarations of ValuesIn(), which is implemented in
// include/gtest/gtest-param-test.h.
template <class Container>
internal::ParamGenerator<typename Container::value_type> ValuesIn(
const Container& container);
namespace internal {
// Used in the Values() function to provide polymorphic capabilities.
template <typename T>
struct PushBack {
template <typename U>
void operator()(const U& u) {
v_.push_back(static_cast<T>(u));
}
std::vector<T>& v_;
};
template <typename... Ts>
class ValueArray {
public:
ValueArray(Ts... v) : v_{std::move(v)...} {}
template <typename Tn>
operator ParamGenerator<Tn>() const {
std::vector<Tn> vc_accumulate;
PushBack<Tn> fnc{vc_accumulate};
ForEachInTuple(v_, fnc);
return ValuesIn(std::move(vc_accumulate));
}
private:
std::tuple<Ts...> v_;
};
} // namespace internal } // namespace internal
} // namespace testing } // namespace testing
......
...@@ -1031,6 +1031,18 @@ TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) { ...@@ -1031,6 +1031,18 @@ TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5)); INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5));
// Tests param generator working with Enums
enum MyEnums {
ENUM1 = 1,
ENUM2 = 3,
ENUM3 = 8,
};
class MyEnumTest : public testing::TestWithParam<MyEnums> {};
TEST_P(MyEnumTest, ChecksParamMoreThanZero) { EXPECT_GE(10, GetParam()); }
INSTANTIATE_TEST_CASE_P(MyEnumTests, MyEnumTest,
::testing::Values(ENUM1, ENUM2, 0));
int main(int argc, char **argv) { int main(int argc, char **argv) {
// Used in TestGenerationTest test case. // Used in TestGenerationTest test case.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment